audio - How to generate complex sound on Android? -
i need generate complex sound few frequences (superposition) on android in real-time. how can using android sdk (preferred) or ndk?
it seems using tonegenerator not flexible enough. recording audiotrack generated sound data , playing seems right direction, need in real-time , recording , playing seems not real-time.
an example book "pro android media" shawn van every:
import android.app.activity; import android.media.audioformat; import android.media.audiomanager; import android.media.audiotrack; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class audiosynthesis extends activity implements onclicklistener { button startsound; button endsound; audiosynthesistask audiosynth; boolean keepgoing = false; float synth_frequency = 440; // 440 hz, middle @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); startsound = (button) this.findviewbyid(r.id.startsound); startsound.setonclicklistener(this); endsound = (button) this.findviewbyid(r.id.endsound); endsound.setonclicklistener(this); endsound.setenabled(false); } @override public void onpause() { super.onpause(); keepgoing = false; endsound.setenabled(false); startsound.setenabled(true); } public void onclick(view v) { if (v == startsound) { keepgoing = true; audiosynth = new audiosynthesistask(); audiosynth.execute(); endsound.setenabled(true); startsound.setenabled(false); } else if (v == endsound) { keepgoing = false; endsound.setenabled(false); startsound.setenabled(true); } } private class audiosynthesistask extends asynctask<void, void, void> { @override protected void doinbackground(void... params) { final int sample_rate= 11025; int minsize = audiotrack.getminbuffersize(sample_rate, audioformat.channel_configuration_mono, audioformat.encoding_pcm_16bit); audiotrack audiotrack = new audiotrack(audiomanager.stream_music, sample_rate, audioformat.channel_configuration_mono, audioformat.encoding_pcm_16bit, minsize, audiotrack.mode_stream); audiotrack.play(); short[] buffer = new short[minsize]; float angular_frequency = (float)(2*math.pi) * synth_frequency / sample_rate; float angle = 0; while (keepgoing) { (int = 0; < buffer.length; i++) { buffer[i] = (short)(short.max_value * ((float) math.sin(angle))); angle += angular_frequency; } audiotrack.write(buffer, 0, buffer.length); } return null; } } }
Comments
Post a Comment