我正在使用Wave文件(立体声文件)处理,我想将系统音量设置为最大值并使用滚动条改变波形文件的声音。 即系统音量应该是100%,我想在60dB到100dB的范围内播放我的波形文件,并且波形文件左声道和右声道音量总是不一样。 (左声道可以播放60dB,右声道可以播放70dB)。
为了达到这个目的,我正在使用CSCore音频库,因为我的项目是在C#上开发的。
// Creating the Wave source from the source file
IWaveSource waveSource = CodecFactory.Instance.GetCodec("C:\\SampleWave.wav");
WasAPIOut mWasAPIOutObj = new WasapiOut();
AudioEndOutputVolume aeovObj = AudioEndpointVolume.FromDevice(_soundOut.Device);
// Getting the maximum and minimum volume range of Wave source.
aeovObj.GetVolumeRange(out vMinDB, out vMaxDB, out vIncrDB);
// Setting the System Master Volume to maximum (100%)
aepv.SetMasterVolumeLevel(vMaxDB, Guid.Empty);
// I want to play the wave file in a loop, so pushing the wave file to loop stream
LoopStream stream = new LoopStream(waveSource);
mWasAPIOutObj.Initialize(stream);
在播放波形文件之前,我想将我的波形文件左声道设置为60 dB,右声道设置为70 dB并开始播放。
mWasAPIOutObj.Play();
mWasAPIOutObj.Volume
属性仅采用0.0到1.0的单个值。这里没有谈论任何渠道。
我是否需要在WaveSource级别设置音量?
我也经历过NAudio音频库,但无法解决这个问题。
ArgumentOutOfRange异常的Stacktrace:
at CSCore.SampleSourceBase.Read(Single[] buffer, Int32 offset, Int32 count)
at SampleTool.VPWaveSource.Read(Single[] buffer, Int32 offset, Int32 count) in c:\****\*****\Projects\Sample Tool\Try\SampleTool\SampleTool\SoundManager.cs:line 308
at CSCore.Streams.SampleConverter.SampleToIeeeFloat32.Read(Byte[] buffer, Int32 offset, Int32 count)
at CSCore.WaveAggregatorBase.Read(Byte[] buffer, Int32 offset, Int32 count)
at CSCore.Streams.LoopStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at CSCore.SoundOut.WasapiOut.FeedBuffer(AudioRenderClient renderClient, Byte[] buffer, Int32 numFramesCount, Int32 frameSize)
at CSCore.SoundOut.WasapiOut.PlaybackProc(Object playbackStartedEventWaithandle)
任何人都可以帮我解决这个问题。
提前致谢。
答案 0 :(得分:2)
您只需创建一个新来源:
class ChannelVolumeControlSource : SampleSourceBase
{
public ChannelVolumeControlSource(ISampleSource source) : base(source)
{
if(source.WaveFormat.Channels != 2)
throw new ArgumentException("Source must have two channels.", "source");
}
//todo: add some validation -> make sure only values between 0 and 1 are allowed.
public float VolumeLeft { get; set; }
public float VolumeRight { get; set; }
public override int Read(float[] buffer, int offset, int count)
{
int read = base.Read(buffer, offset, count);
for (int i = 0; i < read; i+=2)
{
buffer[i] *= VolumeLeft;
buffer[i + 1] *= VolumeRight;
}
return read;
}
}
用法:
var channelVolumeSourceControl = new ChannelVolumeControlSource(stream.ToSampleSource());
mWasAPIOutObj.Initialize(channelVolumeSourceControl.ToWaveSource());
//control the volume during playback by setting the volume properties
channelVolumeSourceControl.VolumeLeft = 0.5f; //left channel 50%
channelVolumeSourceControl.VolumeRight = 1.0f; //right channel 100%
答案 1 :(得分:0)
我不确定这段代码会编译,但我想你的意图。 SetChannelVolumeLevelNative API允许您为每个通道而不是SetMasterVolume设置dB设置。