|
| 1 | +# Convert Between Stereo and Mono |
| 2 | + |
| 3 | +NAudio includes a number of utility classes that can help you to convert between mono and stereo audio. You can use these whether you are playing audio live, or whether you are simply converting from one file format to another. |
| 4 | + |
| 5 | +# Mono to Stereo |
| 6 | + |
| 7 | +If you have a mono input file, and want to convert to stereo, the `MonoToStereoSampleProvider` allows you to do this. It takes a `SampleProvider` as input, and has two floating point `LeftVolume` and `RightVolume` properties, which default to `1.0f`. This means that the mono input will be copied at 100% volume into both left and right channels. |
| 8 | + |
| 9 | +If you wanted to route it just to the left channel, you could set `LeftVolume` to `1.0f` and `RightVolume` to `0.0f`. And if you wanted it more to the right than the left you might set `LeftVolume` to `0.25f` and `RightVolume` to `1.0f`. |
| 10 | + |
| 11 | +```c# |
| 12 | +using(var inputReader = new AudioFileReader(monoFilePath)) |
| 13 | +{ |
| 14 | + // convert our mono ISampleProvider to stereo |
| 15 | + var stereo = new MonoToStereoSampleProvider(inputReader); |
| 16 | + stereo.LeftVolume = 0.0f; // silence in left channel |
| 17 | + stereo.RightVolume = 1.0f; // full volume in right channel |
| 18 | +
|
| 19 | + // can either use this for playback: |
| 20 | + myOutputDevice.Init(stereo); |
| 21 | + myOutputDevice.Play(); |
| 22 | + // ... |
| 23 | +
|
| 24 | + // ... OR ... could write the stereo audio out to a WAV file |
| 25 | + WaveFileWriter.CreateWaveFile16(outputFilePath, stereo); |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +There's also a `MonoToStereoProvider16` that works with 16 bit PCM `IWaveProvider` inputs and outputs 16 bit PCM. It works very similarly to `MonoToStereoSampleProvider` otherwise. |
| 30 | + |
| 31 | +# Stereo to Mono |
| 32 | + |
| 33 | +If you have a stereo input file and want to collapse to mono, then the `StereoToMonoSampleProvider` is what you want. It takes a stereo `ISampleProvider` as input, and also has a `LeftVolume` and `RightVolume` property, although the defaults are `0.5f` for each. This means the left sample will be multiplied by `0.5f` and the right by `0.5f` and the two are then summed together. |
| 34 | + |
| 35 | +If you want to just keep the left channel and throw away the right, you'd set `LeftVolume` to 1.0f and `RightVolume` to 0.0f. You could even sort out an out of phase issue by setting `LeftVolume` to `0.5f` and `RightVolume` to -0.5f. |
| 36 | + |
| 37 | +Usage is almost exactly the same. Note that some output devices won't let you play a mono file directly, so this would be more common if you were creating a mono output file, or if the mono audio was going to be passed on as a mixer input to `MixingSampleProvider`. |
| 38 | + |
| 39 | +```c# |
| 40 | +using(var inputReader = new AudioFileReader(stereoFilePath)) |
| 41 | +{ |
| 42 | + // convert our stereo ISampleProvider to mono |
| 43 | + var mono = new StereoToMonoSampleProvider(inputReader); |
| 44 | + stereo.LeftVolume = 0.0f; // discard the left channel |
| 45 | + stereo.RightVolume = 1.0f; // keep the right channel |
| 46 | +
|
| 47 | + // can either use this for playback: |
| 48 | + myOutputDevice.Init(mono); |
| 49 | + myOutputDevice.Play(); |
| 50 | + // ... |
| 51 | +
|
| 52 | + // ... OR ... could write the mono audio out to a WAV file |
| 53 | + WaveFileWriter.CreateWaveFile16(outputFilePath, mono); |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +There is also a `StereoToMonoProvider16` that works with 16 bit PCM stereo `IWaveProvider` inputs and emits 16 bit PCM. |
| 58 | + |
| 59 | +# Panning Mono to Stereo |
| 60 | + |
| 61 | +Finally, NAudio offers a `PanningSampleProvider` which allows you to use customisable panning laws to govern how a mono input signal is placed into a stereo output signal. |
| 62 | + |
| 63 | +It has a `Pan` property which can be configured between `-1.0f` (fully left) and `1.0f` (fully right), with `0.0f` being central. |
| 64 | + |
| 65 | +The `PanningStrategy` can be overridden. By default is uses the `SinPanStrategy`. There is also `SquareRootPanStrategy`, `LinearPanStrategy` and `StereoBalanceStrategy`, each one operating slightly differently with regards to how loud central panning is, and how the sound tapers off as it is panned to each side. You can experiment to discover which one fits your needs the best. |
| 66 | + |
| 67 | +Usage is very similar to the `MonoToStereoSampleProvider` |
| 68 | + |
| 69 | +```c# |
| 70 | +using(var inputReader = new AudioFileReader(monoFilePath)) |
| 71 | +{ |
| 72 | + // convert our mono ISampleProvider to stereo |
| 73 | + var panner = new PanningSampleProvider(inputReader); |
| 74 | + // override the default pan strategy |
| 75 | + panner.PanStrategy = new SquareRootPanStrategy(); |
| 76 | + panner.Pan = -0.5f; // pan 50% left |
| 77 | +
|
| 78 | + // can either use this for playback: |
| 79 | + myOutputDevice.Init(panner); |
| 80 | + myOutputDevice.Play(); |
| 81 | + // ... |
| 82 | +
|
| 83 | + // ... OR ... could write the stereo audio out to a WAV file |
| 84 | + WaveFileWriter.CreateWaveFile16(outputFilePath, panner); |
| 85 | +} |
| 86 | +``` |
0 commit comments