0

I'm doing the following in VS2010 manually. 1. Read in 2 wave files, say "1.wav" and "2.wav". 2. Insert 2.wav to the middle of 1.wav. 3. Write the result wave stream to an output file "out.wav".

I can now read in the wave files successfully using the following structure

typedef struct {
    char ChunkID[4];        /* RIFF Header      */ //Magic header
    unsigned long ChunkSize;      /* RIFF Chunk Size  */
    char Format[4];        /* WAVE Header      */
    char Subchunk1ID[4];         /* FMT header       */
    unsigned long Subchunk1Size;  /* Size of the fmt chunk                                */
    unsigned short AudioFormat;    /* Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM */
    unsigned short NumChannels;      /* Number of channels 1=Mono 2=Sterio                   */
    unsigned long SampleRate;  /* Sampling Frequency in Hz                             */
    unsigned long ByteRate;    /* bytes per second */
    unsigned short BlockAlign;     /* 2=16-bit mono, 4=16-bit stereo */
    unsigned short BitsPerSample;  /* Number of bits per sample      */
    char Subchunk2ID[4]; /* "data"  string   */
    unsigned long Subchunk2Size;  /* Sampled data length    */
    BYTE Data[18000];
} WavFile;

But some of the parameters of the 2 wave files are not the same. For example, if SampleRate of 1.wav is 8000, SampleRate of 2.wav is 44100, what will SampleRate of out.wav be?

goldfrapp04
  • 2,326
  • 7
  • 34
  • 46

1 Answers1

1

The sampling rate of out.wav is something that you will have to define explicitly. In your example, you could choose your out.wav to be 44100, the better of the two, but you will need to upsample 1.wav by interpolation or adding zeros in between. Alternatively, you could keep it at 8000 and downsample 2.wav by running it through a low-pass filter to filter out frequencies more than Nyquist (half of new sampling rate)and then dropping the samples in the middle. Eitherway, you will have to explicitly decide what you want to do.

More info - https://ccrma.stanford.edu/~jos/resample/

e7mac
  • 553
  • 7
  • 19
  • 1
    As an addition, in case of downsampling, it is advisable to filter out all the frequencies above half of the sampling rate (the nyquist frequency) with a low pass filter before downsampling, to avoid aliasing. – Thies Heidecke Feb 13 '12 at 20:50
  • I don't know as much about digital audio processing as I know about digital image processing but inserting zeros and discarding samples in between other samples does not sound resonable to me. Could you elaborate? I'm kind of interested in the differences to digital image processing. – znkr Feb 13 '12 at 20:51
  • @krynr, I think you're correct - inserting zeros doesn't sound reasonable. Discarding samples is OK as long as a low-pass filter is applied as Thies Hedecke says, otherwise you'll get nasty sounding aliasing. The same is true in image processing. – Mark Ransom Feb 13 '12 at 21:12
  • check it here. Julius Smith is the master! https://ccrma.stanford.edu/~jos/sasp/Upsampling_Stretch_Operator.html Yeah I forgot to mention the lowpass filter - very important – e7mac Feb 13 '12 at 21:47