pdeljanov / Symphonia

Pure Rust multimedia format demuxing, tag reading, and audio decoding library
Mozilla Public License 2.0
2.42k stars 144 forks source link

Question about Converting Audio Samples to WAV Format and Retrieving Bits-Per-Sample #282

Closed jBernavaPrah closed 6 months ago

jBernavaPrah commented 6 months ago

Hi there!

Thank you for providing this crate. I'm currently working on reading from a remote audio/MPEG stream endpoint and pushing the audio data to an external service that accepts WAV audio data (specifically, Azure Speech).

Although I have some bugs to fix, I've been able to successfully read the stream and implement the MediaSource on my testing MyMediaSource struct.

Using the basic example provided and passing MyMediaSource, I was able to decode the samples correctly (hopefully)

Now, I have some high-level questions (and please forgive me if they sound silly):

  1. Can I assume that those samples are already compatible with WAV audio data? Or do I need to convert them? If yes, how do I do that?
  2. How can I get the bits-per-sample? I know the sample rate and the channels using the spec struct returned here, but can I calculate the bits-per-sample or is it already provided somewhere?

Thank you very much for any help you can provide!

ps:

jBernavaPrah commented 6 months ago

Hi again!

So after further research, I now can respond to myself. I hope that this can be useful to someone else :)

  1. Yes, the samples in the example linked are already compatible with WAV formats.
  2. As the samples are resampled into f32 (I'm not sure 100% if this is the correct term) and the bit-per-sample to use is 32.

I used a hound crate, providing the specs matching the track info and writing the buffered samples from the SampleBuffer into the file. Remember to finalize it at the end of the loop and that's it!