forked from processing/processing-sound
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We can't use the same example for both desktop and Android because we need to request the RECORD_AUDIO permission on Android and the API for doing this does not exist on the desktop.
- Loading branch information
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="28" /> | ||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
<application android:label="" | ||
android:icon="@drawable/icon"> | ||
<activity android:name=".MainActivity" | ||
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Grab audio from the microphone input and draw a circle whose size | ||
* is determined by how loud the audio input is. | ||
* | ||
* The only difference between this example and AudioInput is that | ||
* Android requires requesting the permission RECORD_AUDIO. | ||
*/ | ||
|
||
import processing.sound.*; | ||
|
||
AudioIn input; | ||
Amplitude loudness; | ||
|
||
void setup() { | ||
size(640, 360); | ||
background(255); | ||
|
||
if (hasPermission("android.permission.RECORD_AUDIO")) { | ||
initialize(true); | ||
} else { | ||
requestPermission("android.permission.RECORD_AUDIO", "initialize"); | ||
} | ||
} | ||
|
||
void initialize(boolean granted) { | ||
if (!granted) { | ||
return; | ||
} | ||
|
||
// Create an Audio input and grab the 1st channel | ||
input = new AudioIn(this, 0); | ||
|
||
// Begin capturing the audio input | ||
input.start(); | ||
// start() activates audio capture so that you can use it as | ||
// the input to live sound analysis, but it does NOT cause the | ||
// captured audio to be played back to you. if you also want the | ||
// microphone input to be played back to you, call | ||
// input.play(); | ||
// instead (be careful with your speaker volume, you might produce | ||
// painful audio feedback. best to first try it out wearing headphones!) | ||
|
||
// Create a new Amplitude analyzer | ||
loudness = new Amplitude(this); | ||
|
||
// Patch the input to the volume analyzer | ||
loudness.input(input); | ||
} | ||
|
||
void draw() { | ||
if (input == null) { | ||
// Wait for user to give permission | ||
return; | ||
} | ||
|
||
// Adjust the volume of the audio input based on mouse position | ||
float inputLevel = map(mouseY, 0, height, 1.0, 0.0); | ||
input.amp(inputLevel); | ||
|
||
// loudness.analyze() return a value between 0 and 1. To adjust | ||
// the scaling and mapping of an ellipse we scale from 0 to 0.5 | ||
float volume = loudness.analyze(); | ||
int size = int(map(volume, 0, 0.5, 1, 350)); | ||
|
||
background(125, 255, 125); | ||
noStroke(); | ||
fill(255, 0, 150); | ||
// We draw a circle whose size is coupled to the audio analysis | ||
ellipse(width/2, height/2, size, size); | ||
} |