Created
May 24, 2012 21:12
-
-
Save MasterEx/2784265 to your computer and use it in GitHub Desktop.
Audio Synthesis with android AudioTrack - 1
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
This is includes both the naive and the most advanced implementation. | |
The AudioGenerator class can be found here: https://github.com/MasterEx/BeatKeeper/blob/master/src/pntanasis/android/metronome/AudioGenerator.java | |
Code was crafted by Periklis Master_ex Ntanasis <pntanasis@gmail.com> 2012 | |
Frequencies were found here: http://www.phy.mtu.edu/~suits/notefreqs.html | |
Partitura was found here: |
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
package pntanasis.master_ex.android; | |
import pntanasis.master_ex.android.Synthesizer.Note0; | |
import android.app.Activity; | |
import android.os.Bundle; | |
public class AudioSynthesisDemoActivity extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
Synthesizer synthesizer = new Synthesizer(); | |
synthesizer.play(Note0.C, 5, 1.0/8); | |
synthesizer.play(Note0.E, 5, 1.0/8); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.A, 5, 1.0/4); | |
synthesizer.play(Note0.A, 5, 3.0/8); | |
synthesizer.play(Note0.B, 5, 1.0/8); | |
synthesizer.play(Note0.A, 5, 1.0/4); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.C, 5, 3.0/8); | |
synthesizer.play(Note0.E, 5, 1.0/8); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.E, 5, 1.0/4); | |
synthesizer.play(Note0.C, 5, 1.0/4); | |
synthesizer.play(Note0.E, 5, 3.0/4); | |
synthesizer.play(Note0.C, 5, 1.0/8); | |
synthesizer.play(Note0.E, 5, 1.0/8); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.A, 5, 1.0/4); | |
synthesizer.play(Note0.A, 5, 3.0/8); | |
synthesizer.play(Note0.B, 5, 1.0/8); | |
synthesizer.play(Note0.A, 5, 1.0/4); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.C, 5, 3.0/8); | |
synthesizer.play(Note0.E, 5, 1.0/8); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.F, 5, 1.0/4); | |
synthesizer.play(Note0.E, 5, 1.0/4); | |
synthesizer.play(Note0.C, 5, 1.0/4); | |
synthesizer.play(Note0.C, 5, 1); | |
synthesizer.stop(); | |
} | |
} |
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
package pntanasis.master_ex.android; | |
import android.app.Activity; | |
import android.os.Bundle; | |
public class AudioSynthesisDemoActivity extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
AudioGenerator audio = new AudioGenerator(8000); | |
double[] silence = audio.getSineWave(200, 8000, 0); | |
int noteDuration = 2400; | |
double[] doNote = audio.getSineWave(noteDuration/2, 8000, 523.25); | |
double[] reNote = audio.getSineWave(noteDuration/2, 8000, 587.33); | |
double[] faNote = audio.getSineWave(noteDuration, 8000, 698.46); | |
double[] laNote = audio.getSineWave(noteDuration, 8000, 880.00); | |
double[] laNote2 = audio.getSineWave((int) (noteDuration*1.25), 8000, 880.00); | |
double[] siNote = audio.getSineWave(noteDuration/2, 8000, 987.77); | |
double[] doNote2 = audio.getSineWave((int) (noteDuration*1.25), 8000, 523.25); | |
double[] miNote = audio.getSineWave(noteDuration/2, 8000, 659.26); | |
double[] miNote2 = audio.getSineWave(noteDuration, 8000, 659.26); | |
double[] doNote3 = audio.getSineWave(noteDuration, 8000, 523.25); | |
double[] miNote3 = audio.getSineWave(noteDuration*3, 8000, 659.26); | |
double[] reNote2 = audio.getSineWave(noteDuration*4, 8000, 587.33); | |
audio.createPlayer(); | |
audio.writeSound(doNote); | |
audio.writeSound(silence); | |
audio.writeSound(reNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(laNote); | |
audio.writeSound(silence); | |
audio.writeSound(laNote2); | |
audio.writeSound(silence); | |
audio.writeSound(siNote); | |
audio.writeSound(silence); | |
audio.writeSound(laNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(doNote2); | |
audio.writeSound(silence); | |
audio.writeSound(miNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(miNote2); | |
audio.writeSound(silence); | |
audio.writeSound(doNote3); | |
audio.writeSound(silence); | |
audio.writeSound(miNote3); | |
audio.writeSound(silence); | |
audio.writeSound(doNote); | |
audio.writeSound(silence); | |
audio.writeSound(reNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(laNote); | |
audio.writeSound(silence); | |
audio.writeSound(laNote2); | |
audio.writeSound(silence); | |
audio.writeSound(siNote); | |
audio.writeSound(silence); | |
audio.writeSound(laNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(doNote2); | |
audio.writeSound(silence); | |
audio.writeSound(miNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(faNote); | |
audio.writeSound(silence); | |
audio.writeSound(miNote2); | |
audio.writeSound(silence); | |
audio.writeSound(miNote2); | |
audio.writeSound(silence); | |
audio.writeSound(reNote2); | |
audio.destroyAudioTrack(); | |
} | |
} |
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
package pntanasis.master_ex.android; | |
public class Synthesizer { | |
private final short silenceDuration = 200; | |
private final short noteDuration = 2400; | |
private AudioGenerator audio = new AudioGenerator(8000); | |
public enum Note0 { | |
C(16.35), | |
Csharp(17.32), | |
D(18.35), | |
Dsharp(19.45), | |
E(20.60), | |
F(21.83), | |
Fsharp(23.12), | |
G(24.50), | |
Gsharp(25.96), | |
A(27.50), | |
Asharp(29.14), | |
B(30.87); | |
private final double frequency; | |
Note0(double frequency) { | |
this.frequency = frequency; | |
} | |
public double getFrequency() { | |
return frequency; | |
} | |
} | |
Synthesizer() { | |
audio.createPlayer(); | |
} | |
public void play(Note0 note,int octave,double duration) { | |
audio.writeSound(audio.getSineWave((int) (noteDuration*duration*4), 8000, note.getFrequency()*Math.pow(2, octave))); | |
audio.writeSound(audio.getSineWave(silenceDuration, 8000, 0)); | |
} | |
public void stop() { | |
audio.destroyAudioTrack(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment