Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with short[] #25

Open
cyorobert opened this issue Jan 6, 2018 · 1 comment
Open

Usage with short[] #25

cyorobert opened this issue Jan 6, 2018 · 1 comment

Comments

@cyorobert
Copy link

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?

@HitRoxxx
Copy link

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;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants