|
I created the following class:
public class MyLoopingSampleProvider : ISampleProvider
{
AudioFileReader audiofilereader;
/// <summary>
/// Initializes a new instance of MyLoopingSampleProvider
/// </summary>
/// <param name="filename">the path to a sound file</param>
public MyLoopingSampleProvider(string filename)
{
audiofilereader = new AudioFileReader(filename);
}
/// <summary>
/// Reads samples from this sample provider
/// </summary>
/// <param name="buffer">Sample buffer</param>
/// <param name="offset">Offset into sample buffer</param>
/// <param name="sampleCount">Number of samples desired</param>
/// <returns>Number of samples read</returns>
public int Read(float[] buffer, int offset, int count)
{
int bytesread = 0;
bytesread = audiofilereader.Read(buffer, offset, count);
if (bytesread == 0)
{
audiofilereader.Position = 0;
bytesread = audiofilereader.Read(buffer, offset, count);
}
return bytesread;
}
/// <summary>
/// WaveFormat
/// </summary>
public WaveFormat WaveFormat
{
get { return audiofilereader.WaveFormat; }
}
}
Not the Read function: When no bytes were returned, it will set the audiofilereader to 0, and just read again.
Then I do like this: For each file in my input array, I'll create the MyLoopingSampleProvider. With the output, I create a meteringsampleprovider (For painting the input waves).
With the array of meteringsampleproviders from above, I then create the mixingmultiplexingsampleprovider.
The output from the mixingmultiplexingsampleprovider is then in turn used to create a meteringsampleprovicer (for painting the output waves).
Finally tha last meteringsampleprovider is used for playing as a waveout.
When I choose only one stereo wavefile as input, it plays twice, then stops. When I choose 2 stereo inputs, it plays 1 and a half times, then stops. When I choose 3 inputs, it plays once, but never repeats.
When I play the MyLoopingSampleProvider directly, (iow without creating the metering and/or MixingMultiplexingSampleProvider it will loop correctly, with no problems.
Am I at least on the right track with what I am doing?
Will I be able to use this method for multiple files with different durations? What it needs to do is to keep playing until the longest sound has finished, then loop all of them at the same time. In other words, it should not loop a shorter file while a
longer file is still playing.
Any help will be very much appreciated!
Thanks!
|