Search code examples
webaudio-streamingpcmaudio-converterinterleave

How to convert PCM audio stream for online play


I have access to an audio stream of PCM audio buffers. I should be clear I do not have access to the audio file. I only have access to a stream of 4096 byte chunks of the audio data.

The PCM buffers come in with the following format:

  • PCM Int 16
  • Little Endian
  • Two Channels
  • Interleaved

To support audio playback on a standard browser I need to convert the audio to the following format:

  • PCM Float 32
  • Big Endian
  • Two channels (at most)
  • Deinterleaved

This audio is coming from an iOS app so I have access to Swift and Objective C (although I am not very comfortable with Objective C...which makes Apple's Audio Converter Services almost impossible to use because Swift really doesn't like pointers).

Additionally the playback will occur on a browser so I could handle the conversion in client side Javascript or server sider. I am proficient enough in the following server side languages to do a conversion:

  • Java (preferred)
  • PHP
  • Node.js
  • Python

If anyone knows a way to do this in any of these languages please let me know. I have worked on this for long enough that I will probably understand even a very technical description of how to do this.

My current plan is to use bitwise operations to deinterleave the left and right channels, then cast the Int 16 Buffer to a Float 32 Buffer with the Web Audio API. Does this seem like a good plan?

Any help is appreciated, thank you.


Solution

  • My current plan is to use bitwise operations to deinterleave the left and right channels, then cast the Int 16 Buffer to a Float 32 Buffer with the Web Audio API. Does this seem like a good plan?

    Yes, that is exactly what you need to do. I do the exact same thing in my applications, and this method works well and is really the only way that makes sense to do it. You don't want to send 32-bit float samples to the client from the server due to the amount of bandwidth. Do the conversion client-side.