0

I receive an audio wave stream, store it in byte[] and want to play it. I found this online & tested it:

//byte[] data

float[] arr = new float[data.Length / 2];

for(int i= 0; i < arr.Length; i++) {

arr[i] = (float)BitConverter.ToInt16(data, i*2) / 32767.0f;
}

I also tested this https://stackoverflow.com/a/76858809/15107271.

Voices are recognizable, but are very noisy. It sounds like a bad walkie talkie. Does anyone know how to interpret the data correctly?

Edit: I use this launch file with wave format to send the data. https://github.com/ros-drivers/audio_common/blob/master/audio_capture/launch/capture_wave.launch
In Unity the received data of the message is then stored in byte[] data

luzylulilu
  • 19
  • 5
  • "_audio stream in byte[] format_" `byte[]` is not an audio format. That's not telling us about the audio format of your audio data. Because, Everything is a bunch of bytes that can be stuffed in a byte array. Images, sound, text, code, sensor data, whatever, everything can be a `byte[]`. Unless you know and tell us how the audio is encoded by these bytes (i.e., the real **audio** format of the data in the byte array), nobody here really knows, with some users here perhaps be willing enough to do some daring blind-guessing about what the format of your audio data perhaps might be. – EyesShriveledToRaisins Aug 23 '23 at 15:26
  • Sorry! And thank you for being willing to help. I have added new information and hope that is enough and correct. – luzylulilu Aug 23 '23 at 16:23
  • Note that the 16-bit audio data in the byte array seems to be **signed** 16-bit samples (S16LE per your launch file), so values for those samples can be negative. What is the value range for your float-samples? Is it the interval [0.0 ... 1.0]? Double check the allowed value range for the float-based audio samples you are producing. – EyesShriveledToRaisins Aug 23 '23 at 18:22
  • Also, your divisor `/ 32758.0f` does not look right to me with regard to S16LE samples. The value range of signed 16-bit values is [-32768 ... 32767], so i have no clue where the 32758.0f in your code is coming from. Typo, perhaps...? – EyesShriveledToRaisins Aug 23 '23 at 18:31
  • If I understand it correctly, the range for Unity must be from -1.0 to 1.0? How do I check this? It was a typo in the question, using 32767 – luzylulilu Aug 24 '23 at 10:07
  • "_How do I check this?_" Dive into the documentation of whatever playback API / component you are using. It should tell you somewhere somehow what audio formats are supported by that API/component, and how to configure it for a particular audio format if required. I am not big with Unity stuff, so i unfortunately can't give much of an advice or insight here about what typical approaches to audio playback in Unity would be... :-( – EyesShriveledToRaisins Aug 24 '23 at 12:01

1 Answers1

0

You could take a look here. I know its kind of a pain but it could be helpful to provide an audio sample of what it sounds like. My bet is that the audio values are going out of range.

I've seen people mention that unity audio clips use a range of -1, 1. Do you know if the byte array contains a header? You might be able to use a pre-existing class to read the audio data intelligently with help from the header.