我正在使用Wave文件(立体声文件)处理,在这里我想将系统音量设置为最大并使用滚动条更改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,右声道设置为70dB,然后开始播放。
mWasAPIOutObj.Play();
mWasAPIOutObj.Volume
属性仅采用0.0到1.0之间的单个值。这里不谈论任何 channel 。我需要将音量设置为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)
谁能帮助我解决这个问题。
提前致谢。
最佳答案
您可以简单地创建一个新源:
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%
关于c# - 在C#中分别更改wave文件的左右音量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30365365/