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
      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;
      
      //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++ )
      {
            if(!in.OpenRead(inFileNames[n]))
                  return false;
            inSize[n] = in.GetNumSamples() * 2;
            samples[n] = new float[inSize[n]];
            for (i = 0; i < inSize[n]; i++)
            {
                  float sample;
                  in.ReadSampleBytes(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;
}
HanuSoftwareAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chip3dCommented:
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
HanuSoftwareAuthor Commented:
It fails if user mix a single wave file of very large length (say 1100MB). In that  case what we will do.
0
chip3dCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Have you seen http://www.codeproject.com/audio/admixer.asp ("Audio Mixer Functions Demo")?
0
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.