Rtyper
Rtyper

Reputation: 44

CSCore crashes when changing sample rate and writing to file

I'm trying to use the CSCore .net library to convert sound files to a specific format for another process. I'm using it in Unity, but I don't think that's specific to the problem as it otherwise works perfectly.

I've got this code right now:

using (IWaveSource source = CodecFactory.Instance.GetCodec(audioPath)) {
    using (IWaveSource destination = source.ToSampleSource()
        .ChangeSampleRate(16000)
        .ToMono()
        .ToWaveSource(16)) {

        audioPath = Application.dataPath + "/" + Path.GetFileNameWithoutExtension(audioPath) + "_temp_converted" + Path.GetExtension(audioPath);

        destination.WriteToFile(audioPath);
    }
}

It seems to be the combination of changing the sample rate and writing the file that causes the crash. If I remove the .ChangeSampleRate line (or replace 16000 with the current sample rate of the file) then it saves a mono 16-bit .wav file fine, and if I keep that line but don't try to write it to a file, Unity doesn't crash.

Has anyone else experienced this, or have any insight into what might be causing it? I'm starting to tear my hair out a bit with this!

Thanks.

Upvotes: 2

Views: 581

Answers (0)

Related Questions