Using NAudio to achieve fade out and fade in for a series of 44 kHz 16-bit two-channel wave files

不想你离开。 提交于 2019-12-12 02:28:11

问题


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);


来源:https://stackoverflow.com/questions/17322052/using-naudio-to-achieve-fade-out-and-fade-in-for-a-series-of-44-khz-16-bit-two-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!