0

I'm trying to send AudioClips through UnityWebRequest (not important for now) and the way I've found to do that is converting AudioClips to byte[] and sending it as a raw audio file. But this audio file doesn't seem to be working correctly. When I try saving the raw audio directly to my computer and play it in Audacity, it sounds very noisy and corrupt. When I speak into the mic, my voice can barely be made out in the static when I play it in Audacity.

This is what I am currently doing to get my AudioClip to a file:

float[] samples = new float[audioClip.samples * audioClip.channels];
audioClip.GetData(samples, 0);
byte[] byteArray = new byte[samples.Length * 4];
Buffer.BlockCopy(samples, 0, byteArray, 0, byteArray.Length);
File.WriteAllBytes("myAudio/audio.raw", byteArray);

Playing it within Unity works fine (whether playing the original AudioClip or converting it back from bytes). How can I get the file to play correctly. Is there a specific way to decode it in Audacity? Do the bytes not correspond to a raw audio file and must they somehow be converted?

derHugo
  • 83,094
  • 9
  • 75
  • 115
Jahill
  • 78
  • 1
  • 7
  • When you play this in Audacity, are you configuring the sample rate, number of channels, and encoding? – Ron Beyer Jan 31 '23 at 22:30
  • First of all why is your `samples` for the length of all channels .. but in `GetData` you only use the channel 0 ...? And then well the loaded `float[]` is in a Unity internal format .. doesn't automatically mean you can just export it as an audio file. You need to convert it to a specific format like e.g. [WAV](https://gist.github.com/darktable/2317063) – derHugo Feb 01 '23 at 06:48
  • Does this answer your question? [create AudioClip from byte\[\]](https://stackoverflow.com/questions/16078254/create-audioclip-from-byte) – IndieGameDev Feb 01 '23 at 07:28

1 Answers1

0

The configuration within Audacity actually was not trivial. It took me some trial and error but it ended up playing correctly when I imported it as 32-bit float, Little-endian, 1 channel, and 44100Hz.

Thanks to Ron Beyer who also suggested a custom Audacity import configuration.

Jahill
  • 78
  • 1
  • 7