Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions NAudio.Core/Wave/WaveFormats/WaveFormatExtensible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ public class WaveFormatExtensible : WaveFormat
/// <summary>
/// Creates a new WaveFormatExtensible for PCM or IEEE
/// </summary>
public WaveFormatExtensible(int rate, int bits, int channels)
public WaveFormatExtensible(int rate, int bits, int channels, int channelMask = 0)
: base(rate, bits, channels)
{
waveFormatTag = WaveFormatEncoding.Extensible;
extraSize = 22;
wValidBitsPerSample = (short) bits;
for (int n = 0; n < channels; n++)
if (channelMask != 0)
{
dwChannelMask |= (1 << n);
dwChannelMask = channelMask;
}
else
{
for (int n = 0; n < channels; n++)
{
dwChannelMask |= (1 << n);
}
}
if (bits == 32)
{
Expand Down
31 changes: 28 additions & 3 deletions NAudio.Wasapi/WasapiOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,40 @@ private WaveFormat GetFallbackFormat()
if (!bitDepthsToTry.Contains(24)) bitDepthsToTry.Add(24);
if (!bitDepthsToTry.Contains(16)) bitDepthsToTry.Add(16);

var channelMasksToTry = new List<int>() { 0 };
// The WaveFormatExtensible constructor covers the following channel masks by default.
// 0x0003 2.0: FL|FR (KSAUDIO_SPEAKER_STEREO)
// 0x0007 3.0: FL|FR|FC (KSAUDIO_SPEAKER_3POINT0)
// 0x000F 3.1: FL|FR|FC|LFE (KSAUDIO_SPEAKER_3POINT1)
// 0x003F 5.1: FL|FR|FC|LFE|BL|BR (KSAUDIO_SPEAKER_5POINT1_BACK; obsolete)
// 0x00FF 7.1: FL|FR|FC|LFE|BL|BR|FLC|FRC (KSAUDIO_SPEAKER_7POINT1_WIDE; obsolete)
// Add masks for other configurations.
// Candidates are taken from ksmedia.h in the Windows Driver Kit.
if (channelCountsToTry.Contains(1)) channelMasksToTry.Add(0x0004); // 1.0: FC (KSAUDIO_SPEAKER_MONO)
if (channelCountsToTry.Contains(2)) channelMasksToTry.Add(0x000C); // 1.1: FC|LFE (KSAUDIO_SPEAKER_1POINT1)
if (channelCountsToTry.Contains(3)) channelMasksToTry.Add(0x000B); // 2.1: FL|FR|LFE (KSAUDIO_SPEAKER_2POINT1)
if (channelCountsToTry.Contains(4))
{
channelMasksToTry.Add(0x0033); // 4.0: FL|FR|BL|BR (KSAUDIO_SPEAKER_QUAD)
channelMasksToTry.Add(0x0107); // 4.0: FL|FR|FC|BC (KSAUDIO_SPEAKER_SURROUND)
}
if (channelCountsToTry.Contains(5)) channelMasksToTry.Add(0x0607); // 5.0: FL|FR|FC|SL|SR (KSAUDIO_SPEAKER_5POINT0)
if (channelCountsToTry.Contains(6)) channelMasksToTry.Add(0x060F); // 5.1: FL|FR|FC|LFE|SL|SR (KSAUDIO_SPEAKER_5POINT1_SURROUND)
if (channelCountsToTry.Contains(7)) channelMasksToTry.Add(0x0637); // 7.0: FL|FR|FC|BL|BR|SL|SR (KSAUDIO_SPEAKER_7POINT0)
if (channelCountsToTry.Contains(8)) channelMasksToTry.Add(0x063F); // 7.1: FL|FR|FC|LFE|BL|BR|SL|SR (KSAUDIO_SPEAKER_7POINT1_SURROUND)

foreach (var sampleRate in sampleRatesToTry)
{
foreach (var channelCount in channelCountsToTry)
{
foreach (var bitDepth in bitDepthsToTry)
{
var format = new WaveFormatExtensible(sampleRate, bitDepth, channelCount);
if (audioClient.IsFormatSupported(shareMode, format))
return format;
foreach (var channelMask in channelMasksToTry)
{
var format = new WaveFormatExtensible(sampleRate, bitDepth, channelCount, channelMask);
if (audioClient.IsFormatSupported(shareMode, format))
return format;
}
}
}
}
Expand Down