Mixing algorithm for large wave files

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
            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;
      //Reads the Selected Wave Files
      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)
*/            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];
                  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++ )
      // Setting that the current selection need not to be mixed again
      newSelection = false;
      return true;
Who is Participating?
chip3dConnect With a Mentor Commented:
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...
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...
HanuSoftwareAuthor Commented:
It fails if user mix a single wave file of very large length (say 1100MB). In that  case what we will do.
Have you seen http://www.codeproject.com/audio/admixer.asp ("Audio Mixer Functions Demo")?
HanuSoftwareAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.