2
* PortAudio Portable Real-Time Audio Library
3
* Windows WAVEFORMAT* data structure utilities
4
* portaudio.h should be included before this file.
6
* Copyright (c) 2007 Ross Bencina
8
* Permission is hereby granted, free of charge, to any person obtaining
9
* a copy of this software and associated documentation files
10
* (the "Software"), to deal in the Software without restriction,
11
* including without limitation the rights to use, copy, modify, merge,
12
* publish, distribute, sublicense, and/or sell copies of the Software,
13
* and to permit persons to whom the Software is furnished to do so,
14
* subject to the following conditions:
16
* The above copyright notice and this permission notice shall be
17
* included in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
23
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
* The text above constitutes the entire PortAudio license; however,
30
* the PortAudio community also makes the following non-binding requests:
32
* Any person wishing to distribute modifications to the Software is
33
* requested to send the modifications to the original developer so that
34
* they can be incorporated into the canonical version. It is also
35
* requested that these non-binding requests be included along with the
42
#include "portaudio.h"
43
#include "pa_win_waveformat.h"
46
#if !defined(WAVE_FORMAT_EXTENSIBLE)
47
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE
50
static GUID pawin_ksDataFormatSubtypeGuidBase =
51
{ (USHORT)(WAVE_FORMAT_PCM), 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 };
54
int PaWin_SampleFormatToLinearWaveFormatTag( PaSampleFormat sampleFormat )
56
if( sampleFormat == paFloat32 )
57
return PAWIN_WAVE_FORMAT_IEEE_FLOAT;
59
return PAWIN_WAVE_FORMAT_PCM;
63
void PaWin_InitializeWaveFormatEx( PaWinWaveFormat *waveFormat,
64
int numChannels, PaSampleFormat sampleFormat, int waveFormatTag, double sampleRate )
66
WAVEFORMATEX *waveFormatEx = (WAVEFORMATEX*)waveFormat;
67
int bytesPerSample = Pa_GetSampleSize(sampleFormat);
68
unsigned long bytesPerFrame = numChannels * bytesPerSample;
70
waveFormatEx->wFormatTag = waveFormatTag;
71
waveFormatEx->nChannels = (WORD)numChannels;
72
waveFormatEx->nSamplesPerSec = (DWORD)sampleRate;
73
waveFormatEx->nAvgBytesPerSec = waveFormatEx->nSamplesPerSec * bytesPerFrame;
74
waveFormatEx->nBlockAlign = (WORD)bytesPerFrame;
75
waveFormatEx->wBitsPerSample = bytesPerSample * 8;
76
waveFormatEx->cbSize = 0;
80
void PaWin_InitializeWaveFormatExtensible( PaWinWaveFormat *waveFormat,
81
int numChannels, PaSampleFormat sampleFormat, int waveFormatTag, double sampleRate,
82
PaWinWaveFormatChannelMask channelMask )
84
WAVEFORMATEX *waveFormatEx = (WAVEFORMATEX*)waveFormat;
85
int bytesPerSample = Pa_GetSampleSize(sampleFormat);
86
unsigned long bytesPerFrame = numChannels * bytesPerSample;
89
waveFormatEx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
90
waveFormatEx->nChannels = (WORD)numChannels;
91
waveFormatEx->nSamplesPerSec = (DWORD)sampleRate;
92
waveFormatEx->nAvgBytesPerSec = waveFormatEx->nSamplesPerSec * bytesPerFrame;
93
waveFormatEx->nBlockAlign = (WORD)bytesPerFrame;
94
waveFormatEx->wBitsPerSample = bytesPerSample * 8;
95
waveFormatEx->cbSize = 22;
97
*((WORD*)&waveFormat->fields[PAWIN_INDEXOF_WVALIDBITSPERSAMPLE]) =
98
waveFormatEx->wBitsPerSample;
100
*((DWORD*)&waveFormat->fields[PAWIN_INDEXOF_DWCHANNELMASK]) = channelMask;
102
guid = pawin_ksDataFormatSubtypeGuidBase;
103
guid.Data1 = (USHORT)waveFormatTag;
104
*((GUID*)&waveFormat->fields[PAWIN_INDEXOF_SUBFORMAT]) = guid;
108
PaWinWaveFormatChannelMask PaWin_DefaultChannelMask( int numChannels )
110
switch( numChannels ){
112
return PAWIN_SPEAKER_MONO;
114
return PAWIN_SPEAKER_STEREO;
116
return PAWIN_SPEAKER_FRONT_LEFT | PAWIN_SPEAKER_FRONT_CENTER | PAWIN_SPEAKER_FRONT_RIGHT;
118
return PAWIN_SPEAKER_QUAD;
120
return PAWIN_SPEAKER_QUAD | PAWIN_SPEAKER_FRONT_CENTER;
122
/* The meaning of the PAWIN_SPEAKER_5POINT1 flag has changed over time:
123
http://msdn2.microsoft.com/en-us/library/aa474707.aspx
124
We use PAWIN_SPEAKER_5POINT1 (not PAWIN_SPEAKER_5POINT1_SURROUND)
125
because on some cards (eg Audigy) PAWIN_SPEAKER_5POINT1_SURROUND
126
results in a virtual mixdown placing the rear output in the
127
front _and_ rear speakers.
129
return PAWIN_SPEAKER_5POINT1;
132
return PAWIN_SPEAKER_7POINT1;
135
/* Apparently some Audigy drivers will output silence
136
if the direct-out constant (0) is used. So this is not ideal.
138
return PAWIN_SPEAKER_DIRECTOUT;
140
/* Note that Alec Rogers proposed the following as an alternate method to
141
generate the default channel mask, however it doesn't seem to be an improvement
142
over the above, since some drivers will matrix outputs mapping to non-present
143
speakers accross multiple physical speakers.
146
pwfFormat->dwChannelMask = SPEAKER_FRONT_CENTER;
149
pwfFormat->dwChannelMask = 0;
150
for(i=0; i<nChannels; i++)
151
pwfFormat->dwChannelMask = (pwfFormat->dwChannelMask << 1) | 0x1;