Click here to Skip to main content
15,900,403 members
Articles / Desktop Programming / MFC
Article

Concatenating Wave Files

Rate me:
Please Sign up or sign in to vote.
4.56/5 (12 votes)
16 Jun 2003 161.7K   2.5K   65   30
An article about concatenating .WAV files "on the fly"

Introduction

Those who deal with voiceprints and prompts often should have prerecorded waves for testing/development, and sometimes you want to output a "joined wave", and even more better - get it dynamically. This article tries to solve this problem and create merged wave output "on the fly".

Background

It would be good for you to have a brief overview about wave format.

Using the code

The main points of CWave class are it's constructor :

CWave(string fileName) throw(LPCSTR);

and operator+

CWave operator+(const CWave& w) const throw(LPCSTR);

The concatenation is implemented as follows:

CWave CWave::operator+ (const CWave &w) const{
    if (fmt.wFormatTag!=w.fmt.wFormatTag)
        throw "Can't concatenate waves with different format tags";
    CWave ret_val;
    ret_val.fmt = w.fmt;
    ret_val.riff = w.riff;
    ret_val.data = w.data;
    ret_val.data.dataSIZE= data.dataSIZE+w.data.dataSIZE;

     
    ret_val.extraParamLength = w.extraParamLength;
    ret_val.extraParam = w.extraParam;
    ret_val.wave = new BYTE[ret_val.data.dataSIZE];
    memcpy(ret_val.wave,wave,data.dataSIZE);
    memcpy(ret_val.wave+data.dataSIZE,w.wave,w.data.dataSIZE);

    string folder = getFileFolder(fileName);
    string title1 = getFileTitle(fileName);
    string title2 = getFileTitle(w.fileName);
    
    
    ret_val.fileName = folder;
    ret_val.fileName.append(title1);
    ret_val.fileName.append(title2);
    ret_val.fileName.append(".wav");

    return ret_val;
}

The usage is very simple:

CWave wave1("a1.wav");
CWave wave("b7.wav");
CWave wave3(wave1+wave2);
wave3.saveToFile();
//by default fileName of wave3 instance is concatenation of wave1 and wave2 fileNames
//wave3.getFileName()=="a1b7.wav"
//you can specify another fileName by:
//wave3.setFileName("anotherName.wav");

Limitations

For the time being the CWave class can deal with PCM waves, extraParams and "fact" chunks in wave header. You can parse another chunks if your waves contain ones. Also, the two wave files must have the same encoding format.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralI like it. Pin
Adrian Edmonds18-Jun-03 20:28
Adrian Edmonds18-Jun-03 20:28 
GeneralGood Article! Pin
C. Augusto Proiete18-Jun-03 7:07
C. Augusto Proiete18-Jun-03 7:07 
GeneralTwo Things Pin
Gregor Brandt17-Jun-03 13:35
Gregor Brandt17-Jun-03 13:35 
GeneralRe: Two Things Pin
Ed Koolen26-Aug-05 0:00
sussEd Koolen26-Aug-05 0:00 
GeneralPretty Good!! Pin
WREY17-Jun-03 8:36
WREY17-Jun-03 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.