Merging 2 binary data files together in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kmak
    New Member
    • Sep 2011
    • 3

    Merging 2 binary data files together in C

    Hi,
    I want to be able to merge audio file A and audio file B into a new file C.
    All of the files are binary, however I am stooped of how to do it. I'm not sure if different lengths of the audio files will affect the merging to create file C.
    Could someone help me out?

    *I had a look at the python thread on merging 2 binary files together, however I don't know how to implement it in C

    Thank you.
    Last edited by kmak; Sep 18 '11, 01:14 PM. Reason: extra information
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There may be more to this than you think. Audio files have a precise format so your merged file C probably has to be created by processing file A and B rather than just appending them together.

    You don't say what kind of audio files you have but I would research the file format before proceeding.

    Comment

    • kmak
      New Member
      • Sep 2011
      • 3

      #3
      The audio files I have are WAV files.

      typedef struct wav_s {
      u32 chunk_ID; /* 'RIFF' - 0x52494646 */
      u32 chunk_size; /* size of remainder of file, in bytes */
      u32 format_ID; /* 'WAVE' - 0x57415645 */
      u32 subchunk1_ID; /* 'fmt ' - 0x666d7420 */
      u32 subchunk1_size; /* 16 (size in bytes of rest of this header) */
      u16 audio_format; /* 1 (PCM) */
      u16 num_channels; /* 2 (left and right stereo) */
      u32 sample_rate; /* 48000 */
      u32 byte_rate; /* sample rate * channels * bits per sample/8 */
      u16 block_align; /* bytes for one sample (all channels) */
      u16 bits_per_sample ; /* 16 */
      u32 subchunk2_ID; /* 'data' - 0x64617461 */
      u32 subchunk2_size; /* size of sample data, in bytes */
      } wav_t;

      At the moment for my program, I can play a WAV file by storing the data into two ping-pong buffers which are then passed to an audio codec.

      My aim is to mix two audio wav files (of the same format) together so that the mixed audio data can be passed to the codec to be played at the same time. I know that the raw audio data begins 44 bytes into the file.

      Also, I am not sure if it is easier to create a sine function to create a beep (440hz -> 'A') and then mix it to the raw audio data (sampled at 48000hz). However, I don't know how to implement the values to be at 16bit.

      Once again, any help on merging two wav files would be greatly appreciated.
      Thank you

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Can you not play file A and file B concurrently on separate threads?

        Comment

        • kmak
          New Member
          • Sep 2011
          • 3

          #5
          I can't play both files at the same time, as the data stored in the buffers are passed to the same codec driver one block at a time.0 If I try to run both separately, only one file is played then other the sequentially.

          So my idea is that since both files are WAV files and that the raw audio data starts 44 bytes into each file, if the audio data can be somehow merged then the buffer should be able to play the contents of both. I am still stomped on how to do this unfortunately.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            WAV file format is fairly well known and easily findable on the internet.

            Concatinating 2 files that were already using the same sampling rate, etc should be a fairly easy task of just copying the samples on one file and then the samples of the other file and then writing the file header appropriately.

            Merging 2 files may be a little more complex but again, assuming the data has the sample sampling rate, not an insurmountable problem. Something as simple as adding the samples together and then normalising them back to the original amplitude would probably work.

            Comment

            Working...