Using NAudio to achieve fade out and fade in for a

2019-08-31 02:40发布

问题:

I have a series of 44 kHz 16-bit two-channel uncompressed wave files (read from resources) and want to apply the fade out and fade in effect to create a stream from the sequence of all the WAV files.

The resource reading, and getting the 16-bit wavestream happens correctly. The target format is also shown correct, but I keep getting acmnotpossible as the exception in the waveformat coversion step below. What am I doing wrong?

String ResToPlay2 = NameSpaceString + ".Resources." + inWave2 + ".wav";
Stream _audioStream2;
int wavdur2 = 0;
Double fadeDurDbl2 = 0;
int fadeDur2 = 0;
if (!resA.GetManifestResourceStream (ResToPlay2).Equals (Stream.Null))
{
    _audioStream2 = resA.GetManifestResourceStream (ResToPlay2);
    WaveStream wavePCMStream2 = WaveFormatConversionStream.CreatePcmStream (new WaveFileReader (_audioStream2));
    WaveFormat targetFmt2 = new WaveFormat (44100, 32, 2);
    WaveStream waveStream2 = new WaveFormatConversionStream(targetFmt2,wavePCMStream2);
    using (waveStream2)
    {
        wavdur2 = (int) waveStream2.TotalTime.Milliseconds;
        var fader2 = new FadeInOutSampleProvider(new WaveToSampleProvider(waveStream2));
        fadeDurDbl2 = (wavdur2*OverlapPCT) / 100;
        fadeDur2 = (int) Math.Round(fadeDurDbl2, 0);
        fader2.BeginFadeIn (fadeDur2);
        var stwp2 = new NAudio.Wave.SampleProviders.SampleToWaveProvider (fader2);
        WaveFileWriter.CreateWaveFile (Application.StartupPath + "\\" + "fadedIn_?.wav", stwp2);
    }
}

回答1:

I'm not sure why you are using WaveFormatConversionStream since you are starting in PCM. First get to a sample provider, then you can use FadeInOutSampleProvider

var reader = new WaveFileReader (_audioStream2)
var sampleProvider = SampleProviderConverters.ConvertWaveProviderIntoSampleProvider(reader);
var fader = new FadeInOutSampleProvider (sampleProvider);


标签: wav naudio