Verständnis für die vorangehenden Beispiele durch ihre Zerlegung in kleine Einzelbeispiele gewinnen
(EN google-translate)
(PL google-translate)
Verwendung des 3-Achsigen Beschleunigungsaufnehmers
import ketai.sensors.*;
KetaiSensor sensor;
float XX, YY, ZZ;
float LAENGE=1.0f;
void setup()
{
fullScreen();
orientation(LANDSCAPE);
textSize(120);
sensor = new KetaiSensor(this);
sensor.start();
}
public void draw()
{
background(255);
fill(0);
text("x="+XX,50,150);
text("y="+YY,50,350);
text("z="+ZZ,50,550);
}
void onAccelerometerEvent(float x, float y, float z)
{
XX += 0.01f*(x - XX);
YY += 0.01f*(y - YY);
ZZ += 0.01f*(z - ZZ);
LAENGE = sqrt(XX*XX+YY*YY+ZZ*ZZ);
if(LAENGE<0.000000001f)
LAENGE = 9.81f;
XX/=LAENGE;
YY/=LAENGE;
ZZ/=LAENGE;
}
Code 0-1: Minimales Testprogramm
KFBASIS501_analyse_beschleunigung.zip
Minimales Programm zum Abspeielen eines Sinustons
Sound sound;
void setup()
{
fullScreen();
orientation(LANDSCAPE);
sound = new Sound();
}
public void draw()
{
background(255,0,0);
}
//Zweiter Tab: "sound"
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import android.media.AudioTrack;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaRecorder;
public class Sound implements Runnable
{
private AudioTrack audioTrack; //Connection to sound card
private int sr=44100; //Sample rate
private int buffsize=512; //buffer size
private int buffsize2=buffsize*2;
private ScheduledExecutorService schedExecService; //realtime process
private short[] shortbuffer = new short[buffsize*2]; //stereo buffer l0 r0 l1 r1 l2 r2 ... frequently sent to sound card
private float t=0.0; //realtime
private float dt = 1.0/(float)sr; //time step according to sampling rate.
private float MAX_SHORT = 32767.0;
public Sound()
{
try
{
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sr,
AudioFormat.CHANNEL_OUT_STEREO,
AudioFormat.ENCODING_PCM_16BIT,
buffsize*10,
AudioTrack.MODE_STREAM);
audioTrack.setStereoVolume(1.0f, 1.0f);
audioTrack.play();
}
catch(Exception eee)
{
System.out.println("FEHLER: "+eee);
}
schedExecService = Executors.newSingleThreadScheduledExecutor();
long period = (buffsize*1000)/sr; //Seconds per Beat==60/BPM, die Hälfte weil 8tel, mal 1000 weil Millisekunden.
schedExecService.scheduleAtFixedRate(this, 0, period, TimeUnit.MILLISECONDS);
}
float dtt = 1.0f/(float)sr;
public void run()
{
for(int i=0;i<buffsize2;i+=2)
{
//This may be the part of the programm where you might want to modify something like
//introducing other sound sources / directional sound and so on:
float left = MAX_SHORT*sin(TWO_PI*440.0f*t);
float right = MAX_SHORT*sin(TWO_PI*770.0f*t);
shortbuffer[i]=(short)left;
shortbuffer[i+1]=(short)right;
t+=dt; //increment of realtime for each sample
}
audioTrack.write(shortbuffer, 0,buffsize2);
}
}
Code 0-2: Minimales Testprogramm
KFBASIS502_analyse_sinuston.zip