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

Corrupted audio after ByteBuffer change #82

Open
DurandA opened this issue Oct 8, 2023 · 7 comments
Open

Corrupted audio after ByteBuffer change #82

DurandA opened this issue Oct 8, 2023 · 7 comments

Comments

@DurandA
Copy link

DurandA commented Oct 8, 2023

The commit a3c1ddb results in corrupted audio recording on my Android device with PCM-16. From playback, I can recognize the volume but the audio seems fuzzed as if buffer writes started at random places.

I will continue investigating on this issue and document why this change cause issue in the next few days. More context is given in #81 where I misidentified the issue to be with endianness.

@DurandA DurandA changed the title Corrupted audio after a3c1ddb42db94ba17aef5ae32f7fb6addec1b12e Corrupted audio after ByteBuffer change Oct 8, 2023
@DurandA
Copy link
Author

DurandA commented Oct 8, 2023

Note to self: try resetting buffer position with data.clear(); before

// Read audio data into buffer
recorder.read(data, BUFFER_SIZE, AudioRecord.READ_BLOCKING);

@anarchuser
Copy link
Owner

Thank you for your investigations. I will give it a look ASAP.

@DurandA
Copy link
Author

DurandA commented Oct 9, 2023

I gave it another shot. Using data.clear() did nothing which is not surprising:

Note that the value returned by Buffer.position() on this buffer is unchanged after a call to this method. The representation of the data in the buffer will depend on the format specified in the AudioRecord constructor, and will be native endian.

I tried swapping the bytes of the resulting array without success so far. I am not sure what the layout of this direct buffer is. Are you able to playback the audio when it is recorded that way?

@anarchuser
Copy link
Owner

anarchuser commented Oct 10, 2023

I cannot, currently, confirm that the audio is actually correct. I need to fix my test setup but am really short on time currently. I do get a waveform with general correspondence to what the mic is recording.

As far as I understood, the ByteBuffer should be the way to go, and I'm getting lost at what the appropriate implementation really is.

@anarchuser
Copy link
Owner

  1. recorder.read(...) should not modify the position like you cited it. Thus, the current repeat calls to it should be safe. Furthermore, the whole buffer should be completely rewritten every read anyway.
  2. The data should get written as native endian => the simple data.array() should suffice in getting the data in the appropriate byte order.

One way to check for endianness issues is printing some (e.g., first 20) samples straight after recording and then again from the received stream. I'm pretty sure I verified the correctness here at least (I'm still looking for a simple way to automate this testing).

Another potential pitfall may be incorrect application of AudioRecord parameters. I'll need to check that, too.

@DurandA
Copy link
Author

DurandA commented Oct 11, 2023

Hi @anarchuser and thank you for your time. I didn't have much time to do more research on this yet, for now I am using a fork which reverts a3c1ddb.

One way to check for endianness issues is printing some (e.g., first 20) samples straight after recording and then again from the received stream.

This is something I will do for sure.

@anarchuser
Copy link
Owner

yeah no worries. I'm just maintaining this as a side hobby, I'm happy as long as people find some use in it

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