Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > irrKlang
join wav files

Registered User
2013-09-15 17:05:48

Hello, I am trying to use irrklang to join two wav files, but I am kind of stuck right now. For the beginning I would like to just simply attach the data segment of one wav file to itself *to practice*.

For example: the input wav file might be 3 seconds long and someone says "ok", -> I want the output wavfile to be six second long with the person saying ok, twice - i hope this makes sense^^

I doubled the length and size, but something seems to be wrong with the way i duplicate the data buffer...

ISoundSource* firstwav = engine->addSoundSourceFromFile("somewav.wav");

SAudioStreamFormat format1 = firstwav->getAudioFormat();
SAudioStreamFormat format2 = secondwav->getAudioFormat();

// write wave header
unsigned short formatType = 1;
unsigned short numChannels = format1.ChannelCount;
unsigned long sampleRate = format1.SampleRate;
unsigned short bitsPerChannel = format1.getSampleSize() * 8;
unsigned short bytesPerSample = format1.getFrameSize() ;
unsigned long bytesPerSecond = format1.getBytesPerSecond();
unsigned long dataLen = format1.getSampleDataSize();

const int fmtChunkLen = 16;
const int waveHeaderLen = 4 + 8 + fmtChunkLen + 8;

unsigned long totalLen = waveHeaderLen + dataLen;

void* data = firstwav->getSampleData();

char* dopple = new char[dataLen*2];
char* single = (char*)data;

int counter1=0; int counter2 =0;
while(counter1 < dataLen )
dopple[counter1] = single[counter1];

//the error should be here
while(counter1 < dataLen)
dopple[counter1] = single[counter2];

dataLen = dataLen *2;
totalLen = waveHeaderLen + dataLen;

wxFile file1;
if (!file1.IsOpened())
return false;
file1.Write(&totalLen, 4);
file1.Write("WAVE", 4);
file1.Write("fmt ", 4);
file1.Write(&fmtChunkLen, 4);
file1.Write(&formatType, 2);
file1.Write(&numChannels, 2);
file1.Write(&sampleRate, 4);
file1.Write(&bytesPerSecond, 4);
file1.Write(&bytesPerSample, 2);
file1.Write(&bitsPerChannel, 2);

// write data

file1.Write("data", 4);
file1.Write(&dataLen, 4);
file1.Write(dopple, dataLen);


Registered User
2013-09-15 17:16:32

sorry about this post I figured it out myself. The second counter obviously has to check against dataLen * 2

while(counter1 < dataLen*2)
dopple[counter1] = single[counter2];

2013-09-16 21:41:48

Nice :)

Registered User
2013-09-17 11:24:39

thx :)

on a similar note, I have change the method now so it can now
eiterh join two wav files, or join two wave files with a user-specified intervall in the middle - however... there are sound artifacts at the end of my first wav / at the beginning of the intervall wav.

Apparently this is quite normal and the way around it would be to fade out at the end of wav 1 and fade back in at the beginning of wav 2. / the dummy intervall wav.

Also see:

The only problem now is that my wav files are already recorded so I basically need to change the volume in the data segment in the recorded wav file... is there any way to access this data with irrklang ?
and then change the volume of say the last 0.5 seconds ?

Registered User
2013-09-17 18:02:36


In case someone needs it in the future - the trick with the audio artifacts was to use memset on the allocated buffers before filling them with other stuff .

This part of the function below should get anyone on the right track that wants to add a buffer-wav of x-length to their preexisting wav.

Great API ! by the way

// that here is the formular to compute the length of the data segment in the buffer wav
unsigned long dataLen2 = format1.SampleRate * format1.ChannelCount * format1.getSampleSize() * seconds;
char* emptybuf = new char[dataLen2];

// lets create a bigbuffer, so we can attach the seconddatabuffer to the first.

char* bigbuffer = new char[dataLen1+dataLen2]; //sizeof(emptybuf)];
// prevents audio artifacts !! - very important

char* firstbuffer = (char*)data1;
char* secondbuffer = emptybuf;

int counter1=0; int counter2 =0;

while(counter1 < dataLen1 )
bigbuffer[counter1] = firstbuffer[counter1]; // * x increases volume

while(counter1 < dataLen1+dataLen2)
bigbuffer[counter1] = secondbuffer[counter2];

dataLen2 = dataLen1+dataLen2;
totalLen1 = waveHeaderLen1 + dataLen2;

Create reply:

Posted by: (you are not logged in)

Enter the missing letter in: "Intern?tional" (you are not logged in)




Possible Codes

Feature Code
Link [url] [/url]
Bold [b]bold text[/b]
Image [img][/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]



Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact