Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading in 16 and 24-bit audio data into (32-bit) integer buffers

Reading in 16 and 24-bit audio data into (32-bit) integer buffers

Scheduled Pinned Locked Moved C / C++ / MFC
c++dockerquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheBlindWatchmaker
    wrote on last edited by
    #1

    Hello! Here is what I am trying to do. I am writing an audio processing program that can read 16 and 24 bit WAV files into int (32-bit) buffers. The 16-bit condition is straight-forward: I would read in the required number of bits into short buffers. However, since C++ does not have a native 24-bit datatype, and I would like to use the same buffer for 16 or 24 bit data, I would have to read the WAV file data into int buffers. The further processing that the program does is on the ENTIRE contents of the int buffer and NOT on a byte-by-byte basis. I have written the WAV read/write part of the program. Where I am running into a bit of difficulty is unpacking three bytes of data (24-bits) into a 32-bit container. Should I copy the contents of the audio file into a temporary buffer as contiguous data and then fill my main buffer with three bytes of this data, and then skip a byte? Is there any other way that you would recommend reading 24-bit data into a 32-bit buffer? Thank you for your pointers, as it were. I do not expect code to be written for me, but pseudocode would be immensely helpful. Thanks!

    enhzflepE 1 Reply Last reply
    0
    • T TheBlindWatchmaker

      Hello! Here is what I am trying to do. I am writing an audio processing program that can read 16 and 24 bit WAV files into int (32-bit) buffers. The 16-bit condition is straight-forward: I would read in the required number of bits into short buffers. However, since C++ does not have a native 24-bit datatype, and I would like to use the same buffer for 16 or 24 bit data, I would have to read the WAV file data into int buffers. The further processing that the program does is on the ENTIRE contents of the int buffer and NOT on a byte-by-byte basis. I have written the WAV read/write part of the program. Where I am running into a bit of difficulty is unpacking three bytes of data (24-bits) into a 32-bit container. Should I copy the contents of the audio file into a temporary buffer as contiguous data and then fill my main buffer with three bytes of this data, and then skip a byte? Is there any other way that you would recommend reading 24-bit data into a 32-bit buffer? Thank you for your pointers, as it were. I do not expect code to be written for me, but pseudocode would be immensely helpful. Thanks!

      enhzflepE Offline
      enhzflepE Offline
      enhzflep
      wrote on last edited by
      #2

      G'day, You know sometimes pseudo code just seems too hard, hope code's okay Something else you could consider is logical-ORing each byte as it's read into a variable of suitable length. You then bit-shift left by 8 bits and then OR in the next byte, and so on and so forth. I can't remember the format of WAV files off-hand, I forget if they're little-endian or big-endian, but heres just one of many ways you could implement it. Consider the code that follows. When executed, it produces the output:

      Putting buffer into array of 24 bit nums
      0x00010203
      0x00040506
      Putting buffer into array of 16 bit nums
      0x00000102
      0x00000304
      0x00000506

      Putting buffer into array of 24 bit nums - ROUND 2
      0x03020100
      0x06050400
      Putting buffer into array of 16 bit nums - ROUND 2
      0x02010000
      0x04030000
      0x06050000

      ** PORTABILITY WARNING ** I have not actually checked at runtime the sizeof different datatypes. (const int bitsInLong = 32) As well as the use of longs to store the elements of your converted and stored buffer. Long's are 64 bit in some systems. This really must be done to avoid the code becoming broken. (it's late & I'm tired :-O )

      #include < stdio.h >

      int main()
      {
      char soundBuffer[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
      long buf24[2];
      long buf16[3];

      const int bufLen = 6;
      const int bytesIn24Bits = 3;
      const int bytesIn16Bits = 2;
      const int bitsInByte = 8;
      const int bitsInLong = 32;
      
      long iam16Bits, iam24Bits;
      
      long i, j, k;
      char \*ptr;
      
      printf("Putting buffer into array of 24 bit nums\\n");
      ptr = soundBuffer;
      k = 0;
      for (i=0; i < bufLen/bytesIn24Bits; i++)
      {
          iam24Bits = 0;
          for (j=0; j < bytesIn24Bits; j++)
          {
              iam24Bits <<= bitsInByte;
              iam24Bits |= \*ptr++;
          }
          buf24\[k++\] = iam24Bits;
          printf("0x%08x\\n", iam24Bits);
      }
      
      printf("Putting buffer into array of 16 bit nums\\n");
      ptr = soundBuffer;
      k = 0;
      for (i=0; i < bufLen/bytesIn16Bits; i++)
      {
          iam16Bits = 0;
          for (j=0; j < bytesIn16Bits; j++)
          {
              iam16Bits <<= bitsInByte;
              iam16Bits |= \*ptr++;
          }
          buf16\[k++\] = iam16Bits;
          printf("0x%08x\\n", iam16Bits);
      }
      printf("\\n\\n");
      
      printf("Putting buffer into array of 24 bit nums - ROUND 2\\n");
      ptr = soundBuffer;
      k = 0;
      for (i=0; i < bufLen/byt
      
      T 1 Reply Last reply
      0
      • enhzflepE enhzflep

        G'day, You know sometimes pseudo code just seems too hard, hope code's okay Something else you could consider is logical-ORing each byte as it's read into a variable of suitable length. You then bit-shift left by 8 bits and then OR in the next byte, and so on and so forth. I can't remember the format of WAV files off-hand, I forget if they're little-endian or big-endian, but heres just one of many ways you could implement it. Consider the code that follows. When executed, it produces the output:

        Putting buffer into array of 24 bit nums
        0x00010203
        0x00040506
        Putting buffer into array of 16 bit nums
        0x00000102
        0x00000304
        0x00000506

        Putting buffer into array of 24 bit nums - ROUND 2
        0x03020100
        0x06050400
        Putting buffer into array of 16 bit nums - ROUND 2
        0x02010000
        0x04030000
        0x06050000

        ** PORTABILITY WARNING ** I have not actually checked at runtime the sizeof different datatypes. (const int bitsInLong = 32) As well as the use of longs to store the elements of your converted and stored buffer. Long's are 64 bit in some systems. This really must be done to avoid the code becoming broken. (it's late & I'm tired :-O )

        #include < stdio.h >

        int main()
        {
        char soundBuffer[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
        long buf24[2];
        long buf16[3];

        const int bufLen = 6;
        const int bytesIn24Bits = 3;
        const int bytesIn16Bits = 2;
        const int bitsInByte = 8;
        const int bitsInLong = 32;
        
        long iam16Bits, iam24Bits;
        
        long i, j, k;
        char \*ptr;
        
        printf("Putting buffer into array of 24 bit nums\\n");
        ptr = soundBuffer;
        k = 0;
        for (i=0; i < bufLen/bytesIn24Bits; i++)
        {
            iam24Bits = 0;
            for (j=0; j < bytesIn24Bits; j++)
            {
                iam24Bits <<= bitsInByte;
                iam24Bits |= \*ptr++;
            }
            buf24\[k++\] = iam24Bits;
            printf("0x%08x\\n", iam24Bits);
        }
        
        printf("Putting buffer into array of 16 bit nums\\n");
        ptr = soundBuffer;
        k = 0;
        for (i=0; i < bufLen/bytesIn16Bits; i++)
        {
            iam16Bits = 0;
            for (j=0; j < bytesIn16Bits; j++)
            {
                iam16Bits <<= bitsInByte;
                iam16Bits |= \*ptr++;
            }
            buf16\[k++\] = iam16Bits;
            printf("0x%08x\\n", iam16Bits);
        }
        printf("\\n\\n");
        
        printf("Putting buffer into array of 24 bit nums - ROUND 2\\n");
        ptr = soundBuffer;
        k = 0;
        for (i=0; i < bufLen/byt
        
        T Offline
        T Offline
        TheBlindWatchmaker
        wrote on last edited by
        #3

        Thank you very much for your clear explanation and the code! It has been of great help and I modified it to exactly what I wanted. Muchas Gracias! N.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups