Pergunta

I'm playing around with the Android AudioRecord library in an attempt to make an application read data from the audioBuffer continuously. I think to get my application to work, I need a better of understanding how the buffer that is initialized with the constructor.

For instance, if I initialize a new AudioRecord object:

recorder = new AudioRecord(AudioSource.MIC, 44100, AudioFormat.CHANNEL_IN_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, 2205000); //50 seconds worth

In the application, I want to hold the last 50 seconds of data in the buffer initialized here, but I want to periodically pull out the last 10 seconds of data (and the whole 50 if a condition is met).

First of all, from my understanding, the recorder.read(short readData[...) function copies part of the internal buffer into the array I pass in (?).

I'm guessing that the internal buffer is some sort of ring buffer, so how I do know what the last sample was? Or is this handled internally when i call recorder.read(short readData[1000],0,1000)? Does this give me the last 1000 samples, the first 1000 samples? The second parameter (offset) offsets where it writes in the readData parameter as far as I can tell.

Thanks!

Foi útil?

Solução

When you call the recorder.read(...) function, the sizeInBytes parameter specifies how many bytes you want to read from the internal buffer starting from the oldest data not yet read in the buffer. The offsetInBytes parameter refers to the array you pass in, not the internal buffer.

If you want to read out the entire buffer (50ms) then you need to pass in an array (of length >= 2205000) and a sizeInBytes of 2205000. If you only want the most recent 10 seconds, then you still have to read out the entire buffer, and the most recent 10 seconds will be the last 441000 of the bytes that are returned. Note that you will not necessarily always get 2205000 bytes returned; if you try and read this amount of data at intervals < 50ms then you will just get whatever data is available at each read. But the most recent data will always be at the end of the data that is returned.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top