Skip to content

Instantly share code, notes, and snippets.

@zicohasan
zicohasan / gist:821046a3a2772fd75ff9
Created March 20, 2016 23:25 — forked from slightfoot/gist:6330866
Android Tone Generator
// Usage:
// AudioTrack tone = generateTone(440, 250);
// tone.play();
//
private AudioTrack generateTone(double freqHz, int durationMs)
{
int count = (int)(44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
short[] samples = new short[count];
for(int i = 0; i < count; i += 2){
short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);