[软件设计/软件工程] 使用 NAudio 改变左右声道的声音平衡

[复制链接]
发表于 2022-5-5 09:25:01
问题
播放声音时,需要分别调节左右声道的音量。

我让全班播放声音:
  1. public class SoundPlayer
  2. {
  3.     private WaveOutEvent _outputDevice;
  4.     private AudioFileReader _audioFile;
  5.     private float _volume = 1f;

  6.     public float Volume
  7.     {
  8.         get => _volume;
  9.         set
  10.         {
  11.             _volume = value;

  12.             if (_audioFile != null)
  13.                 _audioFile.Volume = value;
  14.         }
  15.     }

  16.     public void Play(string fileName)
  17.     {
  18.         if (_outputDevice == null)
  19.         {
  20.             _outputDevice = new WaveOutEvent();
  21.             _outputDevice.PlaybackStopped += (sender, args) =>
  22.             {
  23.                 _outputDevice.Dispose();
  24.                 _outputDevice = null;
  25.                 _audioFile.Dispose();
  26.                 _audioFile = null;
  27.             };
  28.         }
  29.         if (_audioFile == null)
  30.         {
  31.             _audioFile = new AudioFileReader(fileName) { Volume = _volume };
  32.             _outputDevice.Init(_audioFile);

  33.         }
  34.         else
  35.         {
  36.             if (string.IsNullOrWhiteSpace(fileName))
  37.                 _outputDevice = null;
  38.             else
  39.             {
  40.                 if (_audioFile.FileName != fileName)
  41.                 {
  42.                     _audioFile = new AudioFileReader(fileName) { Volume = _volume };
  43.                     _outputDevice.Init(_audioFile);
  44.                 }
  45.             }
  46.         }

  47.         _outputDevice?.Play();
  48.     }

  49.     public void Stop()
  50.     {
  51.         _outputDevice?.Stop();
  52.     }
  53. }
复制代码

但在本课程中,您只能调整整体音量。如何制作这样的财产:

soundPlayer.LeftChannelVolume=1.0f

soundPlayer.RightChannelVolume=0.5 华氏度

回答
我做了我的提供者,它奏效了! )

如果有人需要,这里是:
  1. /// <summary>
  2. /// Very simple sample provider supporting adjustable gain
  3. /// </summary>
  4. public class VolumeStereoSampleProvider : ISampleProvider
  5. {
  6.     private readonly ISampleProvider source;

  7.     /// <summary>
  8.     /// Allows adjusting the volume left channel, 1.0f = full volume
  9.     /// </summary>
  10.     public float VolumeLeft { get; set; }

  11.     /// <summary>
  12.     /// Allows adjusting the volume right channel, 1.0f = full volume
  13.     /// </summary>
  14.     public float VolumeRight { get; set; }

  15.     /// <summary>
  16.     /// Initializes a new instance of VolumeStereoSampleProvider
  17.     /// </summary>
  18.     /// <param name="source">Source sample provider, must be stereo</param>
  19.     public VolumeStereoSampleProvider(ISampleProvider source)
  20.     {
  21.         if (source.WaveFormat.Channels != 2)
  22.             throw new ArgumentException("Source sample provider must be stereo");

  23.         this.source = source;
  24.         VolumeLeft = 1.0f;
  25.         VolumeRight = 1.0f;
  26.     }

  27.     /// <summary>
  28.     /// WaveFormat
  29.     /// </summary>
  30.     public WaveFormat WaveFormat => source.WaveFormat;

  31.     /// <summary>
  32.     /// Reads samples from this sample provider
  33.     /// </summary>
  34.     /// <param name="buffer">Sample buffer</param>
  35.     /// <param name="offset">Offset into sample buffer</param>
  36.     /// <param name="sampleCount">Number of samples desired</param>
  37.     /// <returns>Number of samples read</returns>
  38.     public int Read(float[] buffer, int offset, int sampleCount)
  39.     {
  40.         int samplesRead = source.Read(buffer, offset, sampleCount);

  41.         for (int n = 0; n < sampleCount; n += 2)
  42.         {
  43.             buffer[offset + n] *= VolumeLeft;
  44.             buffer[offset + n + 1] *= VolumeRight;
  45.         }

  46.         return samplesRead;
  47.     }
  48. }
复制代码






上一篇:mat form字段必须包含MatFormFieldControl
下一篇:C++与gdB调试/打印自定义类型:以NLHMANN JSON库为例

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表