Home Generate audiotrack as separate thread and make manipulations during playing
Reply: 0

Generate audiotrack as separate thread and make manipulations during playing

Tessmus
1#
Tessmus Published in 2018-02-10 22:47:46Z

I am trying to build an app in java with android studio, and what I want to do is to generate a AudioTrack (right now basically a sine wave), but I want this to run as a separate thread (so multiple tracks can be started simultaneous, and also the rest of the app is not locked while playing), and I want to make it stop on demand (for example pushing a stop button). However I also want to be able to make changes in parameters such as frequency while the track is playing and get these changes in more or less realtime (a small delay is definitely acceptable)

This must be pretty standard, but I can't get my head around how to do it, because if the class generating the audio is a separate class I seem to loose access to the built in functions (if they are not native to Thread class), and also I can't make changes to internal variables (like frequency), while the audio is being generated.

Below is the code right now producing the sound, basically building a buffer in a loop and then writing to the audio track. Any ideas how I can do this?

for (int i = 0; i < mSound.length; i++)  {
            double beatFreq = Math.sin((2.0*Math.PI * i/(44100/this.beat)));
            mSound[i] = beatFreq*Math.sin((2.0*Math.PI * i/(44100/this.pitch)));
            mBuffer[i] = (short) (mSound[i]*Short.MAX_VALUE);

        }

        mAudioTrack.setStereoVolume(AudioTrack.getMaxVolume(), AudioTrack.getMaxVolume());
        mAudioTrack.play();

        mAudioTrack.write(mBuffer, 0, mSound.length);
        mAudioTrack.stop();
        mAudioTrack.release();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.29765 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO