Skip to content

Commit 8431d7d

Browse files
committed
mme: don't restrict the host buffer to 16-bit
Currently, the MME Host API code only creates 16-bit integer MME buffers. All audio data provided by the user is therefore converted by PortAudio to and from 16-bit, regardless of the user buffer format. This basically makes it impossible to pass 24-bit audio through the MME Host API. If the user buffer format is not 16-bit, this also causes pointless conversions to take place, *even if the hardware is running at 16-bit*, because modern Windows versions (Vista+) convert the data to floating point behind the scenes before it is handed off to the hardware. This can lead to silly situations where 32-bit float samples from the user are (lossily) converted to 16-bit by PortAudio, then ended off to Windows via MME, only to be converted back to 32-bit float again, before finally being converted to the format the hardware device is configured to use. This can easily lead to two layers of 16-bit dithering (one from PortAudio, and one from Windows) being piled on top of each other, resulting in an elevated noise floor. This commit fixes this problem by configuring the MME buffers to use the same format as the user buffer. This should stop PortAudio from converting samples in all cases except paInt8, which is not supported by WAVEFORMATEX (only paUInt8 is). This is pretty much the same idea as #774. The new code assumes that MME will accept whatever format we throw at it. I feel confident that this is always true on Vista+ regardless of hardware, because starting from Vista MME does not access hardware directly - it always goes through the Windows Audio Engine which supports all formats in both directions (I verified this using paloopback). On pre-Vista Windows, this should still work all the way back to Windows 98 SE, because MME goes through KMixer which supports all formats [1]. [1]: https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/background-of-non-pcm-support#waveout-api Fixes #139
1 parent 68e963a commit 8431d7d

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

src/hostapi/wmme/pa_win_wmme.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,7 +1771,7 @@ static void InitializeSingleDirectionHandlesAndBuffers( PaWinMmeSingleDirectionH
17711771
static PaError InitializeWaveHandles( PaWinMmeHostApiRepresentation *winMmeHostApi,
17721772
PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers,
17731773
unsigned long winMmeSpecificFlags,
1774-
unsigned long bytesPerHostSample,
1774+
PaSampleFormat sampleFormat,
17751775
double sampleRate, PaWinMmeDeviceAndChannelCount *devices,
17761776
unsigned int deviceCount, PaWinWaveFormatChannelMask channelMask, int isInput );
17771777
static PaError TerminateWaveHandles( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, int isInput, int currentlyProcessingAnError );
@@ -1796,14 +1796,13 @@ static void InitializeSingleDirectionHandlesAndBuffers( PaWinMmeSingleDirectionH
17961796
static PaError InitializeWaveHandles( PaWinMmeHostApiRepresentation *winMmeHostApi,
17971797
PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers,
17981798
unsigned long winMmeSpecificFlags,
1799-
unsigned long bytesPerHostSample,
1799+
PaSampleFormat sampleFormat,
18001800
double sampleRate, PaWinMmeDeviceAndChannelCount *devices,
18011801
unsigned int deviceCount, PaWinWaveFormatChannelMask channelMask, int isInput )
18021802
{
18031803
PaError result;
18041804
MMRESULT mmresult;
18051805
signed int i, j;
1806-
PaSampleFormat sampleFormat;
18071806
int waveFormatTag;
18081807

18091808
/* for error cleanup we expect that InitializeSingleDirectionHandlesAndBuffers()
@@ -1832,8 +1831,6 @@ static PaError InitializeWaveHandles( PaWinMmeHostApiRepresentation *winMmeHostA
18321831
((HWAVEOUT*)handlesAndBuffers->waveHandles)[i] = 0;
18331832
}
18341833

1835-
/* @todo at the moment we only use 16 bit sample format */
1836-
sampleFormat = paInt16;
18371834
waveFormatTag = SampleFormatAndWinWmmeSpecificFlagsToLinearWaveFormatTag( sampleFormat, winMmeSpecificFlags );
18381835

18391836
for( i = 0; i < (signed int)deviceCount; ++i )
@@ -2329,6 +2326,8 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
23292326
unsigned long outputDeviceCount = 0; /* contains all devices and channel counts as local host api ids, even when PaWinMmeUseMultipleDevices is not used */
23302327
char throttleProcessingThreadOnOverload = 1;
23312328

2329+
/* These are all the formats that can be represented in WAVEFORMATEX */
2330+
const PaSampleFormat kNativeFormats = paUInt8 | paInt16 | paInt24 | paInt32 | paFloat32;
23322331

23332332
if( inputParameters )
23342333
{
@@ -2356,7 +2355,7 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
23562355
if( result != paNoError ) return result;
23572356

23582357
hostInputSampleFormat =
2359-
PaUtil_SelectClosestAvailableFormat( paInt16 /* native formats */, inputSampleFormat );
2358+
PaUtil_SelectClosestAvailableFormat( kNativeFormats, inputSampleFormat );
23602359

23612360
if( inputDeviceCount != 1 ){
23622361
/* always use direct speakers when using multi-device multichannel mode */
@@ -2406,7 +2405,7 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
24062405
if( result != paNoError ) return result;
24072406

24082407
hostOutputSampleFormat =
2409-
PaUtil_SelectClosestAvailableFormat( paInt16 /* native formats */, outputSampleFormat );
2408+
PaUtil_SelectClosestAvailableFormat( kNativeFormats, outputSampleFormat );
24102409

24112410
if( outputDeviceCount != 1 ){
24122411
/* always use direct speakers when using multi-device multichannel mode */
@@ -2549,7 +2548,7 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
25492548
{
25502549
result = InitializeWaveHandles( winMmeHostApi, &stream->input,
25512550
winMmeSpecificInputFlags,
2552-
stream->bufferProcessor.bytesPerHostInputSample, sampleRate,
2551+
hostInputSampleFormat, sampleRate,
25532552
inputDevices, inputDeviceCount, inputChannelMask, 1 /* isInput */ );
25542553
if( result != paNoError ) goto error;
25552554
}
@@ -2558,7 +2557,7 @@ static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
25582557
{
25592558
result = InitializeWaveHandles( winMmeHostApi, &stream->output,
25602559
winMmeSpecificOutputFlags,
2561-
stream->bufferProcessor.bytesPerHostOutputSample, sampleRate,
2560+
hostOutputSampleFormat, sampleRate,
25622561
outputDevices, outputDeviceCount, outputChannelMask, 0 /* isInput */ );
25632562
if( result != paNoError ) goto error;
25642563
}

0 commit comments

Comments
 (0)