nAudio FadeInOutSampleProvider not working

372 views Asked by At

I'm writing a program which will take a series of .wav files from a folder created by another application, trims a specified amount of time from the start and end of each sample and copies the result of that action to a temporary folder. From there, I want to take each of the files and apply a small amount of fading to the end of the files as to make the transitions between files smoother (each file will be merged together afterwards), and output the results of that operation to another temporary folder, where the program will merge the files in that folder to create the final output. Unfortunately, I can't get FadeInOutSampleProvider to work.

foreach (string file in files) {
    try {
        byte[] buffer = new byte[1024];
        AudioFileReader afr = new AudioFileReader(tempfile2);
        FadeInOutSampleProvider fade = new FadeInOutSampleProvider(afr);

        fade.BeginFadeOut(notes[run2].Length - 100);

        var stwp = new NAudio.Wave.SampleProviders.SampleToWaveProvider(fade);
        WaveFileWriter.CreateWaveFile(tempfile, stwp);

        run2++;
    }
    catch (Exception) { }
}

EDIT:

Here's more code:

public void PlaybackTemp(string tempDir, Sheet playbackSheet) {
    string[] files = Directory.GetFiles(tempDir);
    string tempdir = "";

    // Generate trimmed files ready for splicing 
    tempdir = GenEditedFiles(files, playbackSheet.notes);

    // Show the output in explorer if debug mode is on
    if (debug) {
        Process p = new Process();
        p.StartInfo.FileName = tempdir;
        p.Start();
    }

    // Splice the files
    ConcatenateWav(tempdir + "\\render.wav", Directory.GetFiles(tempdir));

    // Play back resulting file
    new System.Media.SoundPlayer(tempdir + "\\render.wav").Play();
}

private string GenEditedFiles(string[] files, List<Note> notes) {
    string tempdir = FluidSys.FluidSys.CreateTempDir();
    string tempdir2 = FluidSys.FluidSys.CreateTempDir();
    string tempfile = "";
    string tempfile2 = "";

    int run = 0;
    int run2 = 0;

    // Trim each note
    foreach (string file in files) {
        tempfile = tempdir + "\\" + run.ToString() + ".wav";
        tempfile2 = tempdir + "\\" + run.ToString() + "0.wav";

        WavFileUtils.TrimWavFile(file, tempfile2, TimeSpan.FromMilliseconds(notes[run].VoiceProperties.Start),
            TimeSpan.FromMilliseconds(notes[run].VoiceProperties.End));

        run++;
    }

    foreach (string file in files) {
        try {
            byte[] buffer = new byte[1024];
            AudioFileReader afr = new AudioFileReader(tempfile2);
            FadeInOutSampleProvider fade = new FadeInOutSampleProvider(afr);

            fade.BeginFadeOut(notes[run2].Length - 10);

            var stwp = new NAudio.Wave.SampleProviders.SampleToWaveProvider(fade);
            WaveFileWriter.CreateWaveFile(tempfile, stwp);

            run2++;
        }
        catch (Exception) { }
    }

    return tempdir;
}
0

There are 0 answers