title: How to efficiently unpack float, int16, int32, etc. data from void* array in C++?
body: <p>I’ve got an array of PCM data; it can be 16-bit, 24-bit packed, 32-bit, etc. It can be signed, or unsigned, and it can be 32 or 64-bit floating point. It is currently stored as a <code>void**</code> matrix, indexed first by channel, then by frame. The goal is to allow my library to take in any PCM format and buffer it, without requiring manipulation of the data to fit a designated structure. If the A/D converter spits out 24-bit packed arrays of interleaved PCM, I need to accept it gracefully. I also need to support 16-bit non-interleaved, as well as any permutation of the above formats.</p>
  <p>I know the bit depth and other information at runtime, and I’m trying to code efficiently while not duplicating code. What I need is an effective way to cast the matrix, put PCM data into the matrix, and then pull it out later.</p>
  <p>I can cast the matrix to <code>int32_t</code>, or <code>int16_t</code> for the 32 and 16-bit signed PCM respectively; I’ll probably have to store the 24-bit PCM in an <code>int32_t</code> for 32-bit, 8-bit byte systems as well.</p>
  <p>Can anyone recommend a good way to put data into this array, and pull it out later? I’d like to avoid large sections of code which look like:</p>
  <pre><code>switch (mFormat) {
  case 1:  // unsigned 8 bit
    for (int i = 0; i &lt; mChannels; i++)
      framesArray = (uint8_t*)pcm[i];
    break;
  case 2:  // signed 8 bit
    for (int i = 0; i &lt; mChannels; i++)
      framesArray = (int8_t*)pcm[i];
    break;
  case 3:  // unsigned 16 bit
  ...
  </code></pre>
  <p>Limitations: I’m working in C/C++, no templates, no RTTI, no STL. Think embedded.  Things get trickier when I have to port this to a DSP with 16-bit bytes.</p>
  <p>Does anybody have any useful macros they might be willing to share?</p>