How to use Synthesizer addSound and removeSound?


#1

Hi I don't understand how I can get an index when I call Synthesizer::addSound() in order to pass it to Synthesizer::removeSound().

I tried something like soundNumber = processor.synth.addSound(sound)->getReferenceCount(); but it returns the same number all the time...

Thanks!


#2

Calling Synthesizer::addSound will just add the Sound to the end of an internal array, so the index of that sound will be Synthesizer::getNumSounds() - 1. Obviously, that index will only be valid as long as you do not remove any other sounds (as this will change the position of all following elements in the array). 

Another way of getting the index would be to iterate over all sounds and compare the pointers, for example:

for (int i = 0; i < synth.getNumSounds(); ++i)
{
   if (synth.getSound (i) == mySound)
   {
       synth.removeSound (i);
       break;
   }     
}

You need to be careful with the above solution and make sure that no other thread is add/removing the sounds of the synthesizer while this loop is executing.