CasperPas / flutter-sound-stream

stream audio data in & out
GNU General Public License v3.0
84 stars 111 forks source link

Is data lost when going from 16-bit shortArray (Kotlin) to a Uint8List (dart) #8

Closed chi-tea-72 closed 4 years ago

chi-tea-72 commented 4 years ago

Hello,

I'm not super familiar with audio so I wanted to ask two questions:

1) I have a function that expects 16-bit pcm data. In the conversion from shortArray (Kotlin) to Uint8List (dart) due to that limitation you mention, are you losing audio data (going from 16-bits to 8-bits), or is it spread across a pair of indices in the Uint8List, or something else?

2) I was wondering if making this line: https://github.com/CasperPas/flutter-sound-stream/blob/7948a871d0dd6e408f702b9f18bb146032e56266/lib/recorder_stream.dart#L46 an Int32List instead would still work?

I'm not super familiar with Kotlin, but this line: https://github.com/CasperPas/flutter-sound-stream/blob/7948a871d0dd6e408f702b9f18bb146032e56266/android/src/main/kotlin/vn/casperpas/sound_stream/SoundStreamPlugin.kt#L390 kind of looks like you may be allocating 32 bits here and then packing the lower bits with the audio data? If so, then maybe using a method like asInt16List with an offset of 2 would get the desired 16 bit number: https://api.dart.dev/stable/2.8.4/dart-typed_data/ByteBuffer/asInt16List.html?

Thanks

CasperPas commented 4 years ago

Hello,

  1. The conversion is just "map" the same amount of data, each 16-bit (2 bytes) item will be 2 items in 8-bit (still 2 bytes) => no data loss
  2. It won't work. That's how Dart work. You're trying to assigning with 2 different types.

shortOut is the number of 16-bit items => we'll need shortOut * 2 of 8-bit items to store the same amount of data.

chi-tea-72 commented 4 years ago

Ok thanks for the response. I think I understand now.

So here you're taking the Uint8List you passed over the method channel and converting into a short ByteBuffer: https://github.com/CasperPas/flutter-sound-stream/blob/7948a871d0dd6e408f702b9f18bb146032e56266/android/src/main/kotlin/vn/casperpas/sound_stream/SoundStreamPlugin.kt#L338 to be able to playback the 16 bit values correctly.

If my understanding is correct, I should be able to do something similar in Dart like Uint16List.view(theUint8ListChunk.buffer) to get the correct list of 16-bit integers to pass to my function that expects 16-bit pcm?

CasperPas commented 4 years ago

yes, theoretically, it should works