May 16, 2014 at 5:00 AM
Edited May 16, 2014 at 5:03 AM
I noticed that when I load multiple sound files in parallel (using threads), the sounds end up crackling when they are played.
I was able to examine the source code and make a change which fixed my problem. In the file Mdct.cs, I changed the Reverse method to look like this:
public static void Reverse(float samples, int sampleCount)
Mdct mdct = GetSetup(sampleCount);
I added the "lock(mdtc)". The reason I did this is because multiple threads may be processing using the same mdct object at the same time. The mdtc object has a member variable named "buf2Temp" that seems to be getting modified by the CalcReverse
method. So if two threads try to call that CalcReverse method at the same time on the same mdct object, then they will conflict.
Instead of my solution, it might be better to change the mdct implementation so that "buf2Temp" is not a member variable but is instead created when CalcReverse is called and passed down the chain as needed.
Anyhow, that change fixed my problem.
I also noticed that the GetSetup() method in the same class is not protecting _setupCache properly. One thread should not be attempting to read from _setupCache while another thread is writing to it (because adding to a dictionary can cause a re-hash of its
contents, which repositions everything, and potentially interferes with a read). I changed that to look like this:
static ReaderWriterLockSlim _cacheLock = new ReaderWriterLockSlim();
static Mdct GetSetup(int n)
_setupCache[n] = new Mdct(n);
Mdct mdct = _setupCache[n];
Anyhow, just wanted to let you know about this, and see if any of these fixes might be helpful. It's also possible that I made some incorrect assumption about how things are meant to be used. If so, that would be good to know too.