Question

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?

Was it helpful?

Solution

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top