Solved

# Mixing algorithm for large wave files

Posted on 2006-04-15
434 Views
Hi experts,

We have implemented a mixing algorithm for wave files. The  mixing algorithm mix the wave file which are small in size but it fails to mix the large wave file(in size 100mb) and displayed an error out of memory. Given below is the source code of mixing algorithm. I will be very greatful if anybody in the world will tell me what should I  change in my algorithm so that it provide the desired output.

//Definition of CWaveFile : MixWaveFile()
//Input                  :Selected Items actual locations, Volume value, WaveFile parameter
//Output            :Mixes the audio data and store the mixed data in a global buffer
//Return Value  :true for success, false for failure
//Usage                :Used for mixing two or more audio data
bool WaveMixer ::MixSamples(std::vector<CString> SelectedItems, float *fVol, WaveFile *outFile)
{
// If the selection is already mixed then return
if(!newSelection)
return true;

LPCSTR      inFileNames[11];
float **samples;
WaveFile in, out;
int n;                              // iterate through selected files
size_t i,
inSize[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int noOfFiles;

for(n = 0; n < (int)SelectedItems.size(); n++)
{
inFileNames[n] = SelectedItems.at(n);
}
noOfFiles = (int)SelectedItems.size();            //No of Selected Wave File

samples = new float*[noOfFiles];

//****************************************************************//
//            Volume Implementation - Start
//****************************************************************//
for( n = 0; n < noOfFiles; n++ )
{
return false;
inSize[n] = in.GetNumSamples() * 2;
samples[n] = new float[inSize[n]];
for (i = 0; i < inSize[n]; i++)
{
float sample;
samples[n][i] = sample * fVol[n];
}

/*            // if inside GenerateWaveFile then copy the file format
if( n == noOfFiles - 1 && outFile != NULL)
outFile->CopyFormatFrom(in);
*/            in.Close();
}
//****************************************************************//
//            Volume Implementation - End
//****************************************************************//

// Finding maximum of the file sizes
m_maxSize = inSize[0];
for( n = 1; n < noOfFiles; n++ )
{
if( m_maxSize < inSize[n] )
m_maxSize = inSize[n];
}

//****************************************************************//
//            Mixing Implementation - Start
//****************************************************************//
m_finalSamples = new short[m_maxSize];

for (i = 0; i < inSize[0] || i < inSize[1] || i < inSize[2] || i < inSize[3] || i < inSize[4] || i < inSize[5] || i < inSize[6] || i < inSize[7] || i < inSize[8] || i < inSize[9] || i < inSize[10]; i++)
{
float sample[11];

for( n =0; n < noOfFiles; n++ )
{
sample[n] = 0;
if (i < inSize[n])
{
sample[n] = samples[n][i];
}
}

float resSample = 0;

if( noOfFiles == 1 )
resSample = sample[0];
else
{
for( n =0; n < noOfFiles; n++ )
resSample = resSample + sample[n] / 2;
}

double dSample;
short sSample;

dSample = double(resSample);
sSample = short(dSample * ((1 << (16 - 1)) - 1));

m_finalSamples[i] = sSample;
}
//****************************************************************//
//            Mixing Implementation - End
//****************************************************************//

// Freeing memory buffers that are not required
for( n =0; n < noOfFiles; n++ )
delete(samples[n]);
delete(samples);
// Setting that the current selection need not to be mixed again
newSelection = false;
return true;
}
0
Question by:HanuSoftware

LVL 4

Expert Comment

for big wave files it's not a good idea to load all files into the heap at the same time and after this start the mixing.
If you really have 11 wavefiles to load -> 100*11 == 1100MB. That could cause a out of memory error
Think about mixing one file after another, or just load littel parts of the files and at the same time...
0

Author Comment

It fails if user mix a single wave file of very large length (say 1100MB). In that  case what we will do.
0

LVL 4

Accepted Solution

only load parts of the file. Use a filestream.

Are the files you are mixing of different sizes?

For example, open 5 input filestreams and one outfilestream, than only read one Sample of every instream, mix them and write to the outstream. If you first need information from the instream like the highest value for scaling, you must first scan all instreams. This surely will take more time to process but you will never get an out of memory error, except your hd is full...

If you need this for realtime, us a mixing method you don't need the information about the whole wavefiles our just process a timewindow thats not exceeding your memory.

You can also use the stk: http://ccrma.stanford.edu/software/stk/
Here you can find an openSource lib with all kind of tools. It also supports wavestreams...
0

LVL 86

Expert Comment

Have you seen http://www.codeproject.com/audio/admixer.asp ("Audio Mixer Functions Demo")?
0

Author Comment

Apart from playing the mixed file, we also need to allocate memory for the final mixed file so that it can be saved for playing it later directly.

but in case the size of mixed file is large like 50MB, we need to allocate memory of that size.
We have tried to do it with memset, new etc, but itm failed.

Kindly let me know how can it allocate  such huge size of memory.
0

## Featured Post

### Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.