Yalantis/Horizon

Usage with short[]

Opened this issue · 1 comments

Thank you for your hard work making this excellent library. According to the AudioRecord documentation, reading byte[]s from a buffer is only compatible with ENCODING_PCM_8BIT.

Reads audio data from the audio hardware for recording into a byte array. The format specified in the AudioRecord constructor should be ENCODING_PCM_8BIT to correspond to the data in the array.

https://developer.android.com/reference/android/media/AudioRecord.html#read(byte[], int, int)

I see that in your example you are indeed using reading a byte[] from the AudioRecord buffer, yet you're also specifying ENCODING_PCM_8BIT. Is there any way to use Horizon with a short[], or should we continue to use byte[] and specify ENCODING_PCM_8BIT?

I am using short [] and with some modification and it is working Hope its help:-

public void onAudioChunkPulled(AudioChunk audioChunk, short[] buffer) {

     if ( audioChunk.readCount() > 0) {
         byte bys[] = new byte[audioChunk.readCount() * 2];
         // Because arm byte order problem, so need high and low bit exchange
         for (int i = 0; i < audioChunk.readCount(); i++) {
             byte ss[] = getBytes(buffer[i]);
             bys[i * 2] = ss[0];
             bys[i * 2 + 1] = ss[1];
         }

         mHorizon.updateView(bys);
     }
 }


public byte[] getBytes(short s) {
     byte[] buf = new byte[2];
     for (int i = 0; i < buf.length; i++) {
         buf[i] = (byte) (s & 0x00ff);
         s >>= 8;
     }
     return buf;
 }