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

  1. #1
    Registered User
    Join Date
    Apr 2008

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


    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.


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Without knowing your processing: is your program going to expect the result to have the right "value" (for instance if the 24-bit value was 0x27 43 ba, will your int need to be 0x002743ba)? If so, then that's going to dictate how you fill your buffer.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    You could just use 32 bit integers, and ignore the top 8 bits. Or shift the audio data by 8 bits, and just convert the 24 bit data into 32 bit, do your processing, and then scale it back down to 24 bit.

    uint32_t samples[];
    for each sample in the file:
       samples[ x ] = read_in_24_bit_sample() << 8;
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a 16 bit Register
    By Bladactania in forum C Programming
    Replies: 7
    Last Post: 07-04-2009, 03:15 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 06:18 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM