kramann.info
© Guido Kramann

Login: Passwort:










3.11.5.1 Klangerzeugung mit Android
3.11.5.1 Sound generation with Android (EN google-translate)
3.11.5.1 Generowanie dźwięku za pomocą Androida (PL google-translate)
Was wird benötigt, um mit Android zwei unterschiedliche Sinustöne hörbar zu machen?
What is needed to make two different sinusoids audible with Android?
Co jest potrzebne, aby dwie dźwięki sinusoidy były słyszalne w systemie Android?

Zunächst wird gezeigt, wie überhaupt Sound auf einem Androidgerät erzeugt wird. Es wird noch kein Bezug auf VR gegeben.

First, it shows how sound at all generated on an Android device. There is no reference to VR.

Po pierwsze pokazuje, jak w ogóle brzmi wygenerowany na urządzeniu z systemem Android. Nie ma odniesienia do VR.

Grundsätzlich wird ein Echtzeitthread benutzt, der zyklisch einen Puffer füllt. Der Puffer wird dann an die Soundkarte übergeben.

Basically, a real-time thread is used, cyclically fills a buffer. The buffer is then passed to the sound card.

Zasadniczo używa się wątku czasu rzeczywistego, cyklicznie wypełnia bufor. Bufor jest następnie przekazywany do karty dźwiękowej.

//Shortest program to just play two sinus waves with Android

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;

Sound sound;

public void setup()
{
    sound = new Sound();
    fullScreen();
    orientation(LANDSCAPE);
}

public void draw()
{
    background(0,0,255);
}

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 double t=0.0; //realtime
    private double dt = 1.0/(double)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);                
        
        
    }
    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.0*(float)t);
             float right = MAX_SHORT*sin(TWO_PI*770.0*(float)t);
             shortbuffer[i]=(short)left;
             shortbuffer[i+1]=(short)right;
             t+=dt; //increment of realtime for each sample
        }
      
        audioTrack.write(shortbuffer, 0,buffsize2);      
    }
}

Code 3.11.5.1-1: Androidsound001

Androidsound001.zip -- Sketch folder. Please set to Android-mode and choose "Android->App"!