简体   繁体   中英

Metronome android app AudioTrack releaseBuffer() error

I am trying to create a sounding metronome app for Android. Right now I have an AudioTrack that I create and press play to initialize. Then I write PCM bytes to a buffer and use AudioTrack.writeSound() to write to the buffer. First I write to the buffer

        for(int i=0;i<this.tick;i++) {
            soundTickArray[i] = tick[i];
        }
        for(int i=0;i<silence;i++)
            silenceSoundArray[i] = 0;
    }

Then, I write to audioTrack after its been converted to PCM sequentially like this in a loop.

        do {
            audioGenerator.writeSound(soundTickArray);
            audioGenerator.writeSound(silenceSoundArray);
        } while(running);

in a while loop until it is terminated in another thread.

The problem is I keep getting this error.

releaseBuffer() track 0xb491b280 disabled due to previous underrun, restarting

And the metronome won't stop.

Does anyone have any suggestions as to why this is happening or how to solve it.

Or any other suggestions to run consistent metronome beats that doesn't lag and is precise? I tried to use SoundPool but ran into lag.

Thanks!

This refers to the accuracy of the metronome.

When you're doing this sort of thing, you generally have to make sure that your beats are being controlled by a consistent pulse that is matching your sampling rate. That is, for true accurate timing for rhythm, you need to control your metronome clicks with a control rate that matches your sampling rate.

A common solution to this problem is to create a square wave that runs at the sampling rate of your audio. The frequency of this square wave is then adjusted by a user to match the BPM of your metronome. Then, whenever this squarewave produces a 1 or -1, you use this to trigger your audio samples.

The best solution to this though, is get access to your audio clock on the computers sound card. Take a look at this webpage that explains it in more detail;

http://www.javaperformancetuning.com/articles/soundtimer.shtml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM