The following Code contains to examples on how to create a wave source:
public IWaveSource CreateAWaveSourceFromFile(string filename)
{
    try
    {
        return CodecFactory.Instance.GetCodec(filename);
    }
    catch(Exception)
    {
        //Any Exception occurred.
        //Maybe the fileformat is not supported or the file could not be found.
        //Handle the Exception here:
        //...

        throw;
    }
}

public IWaveSource CreateASineSource()
{
    double frequency = 1200;
    double amplitude = 0.6;
    double phase = 0.0;

    //Create a ISampleSource
    ISampleSource sampleSource = new SineGenerator(frequency, amplitude, phase);

    //Convert the ISampleSource into a IWaveSource
    return sampleSource.ToWaveSource();
}

The CreateAWaveSourceFromFile–method creates a wave source based on a file (using the CSCore.Codecs.CodecFactory class).

The CreateASineSource-method creates a infinite wave source which constantly generates a sine tone. If you take a closer look, at the CreateASineSource-method, you’ll recognise that the SineGenerator itself implements the ISampleSource interface which has to get converted to a IWaveSource.

ISampleSource vs IWaveSource

While the IWaveSource is based on raw bytes, the ISampleSource is based on 32 bit float samples with the range of –1 to 1.

Working with float samples provides an relatively easy way to work with audio data. Since a soundout can only work with raw bytes, it requires a wave source (IWaveSource). The ToWaveSource() extension provides an easy way to convert a sample source to a wave source. The ToSampleSource() extension converts the type IWaveSource to the type ISampleSource. For both extensions, the namespace CSCore has to be included.

Last edited Apr 19, 2014 at 12:06 PM by filoe, version 2

Comments

No comments yet.