| 1 | #ifdef _WIN32 |
| 2 | /* |
| 3 | * PortAudio Portable Real-Time Audio Library |
| 4 | * Windows WAVEFORMAT* data structure utilities |
| 5 | * portaudio.h should be included before this file. |
| 6 | * |
| 7 | * Copyright (c) 2007 Ross Bencina |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining |
| 10 | * a copy of this software and associated documentation files |
| 11 | * (the "Software"), to deal in the Software without restriction, |
| 12 | * including without limitation the rights to use, copy, modify, merge, |
| 13 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 14 | * and to permit persons to whom the Software is furnished to do so, |
| 15 | * subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be |
| 18 | * included in all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 23 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 24 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 25 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 26 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * The text above constitutes the entire PortAudio license; however, |
| 31 | * the PortAudio community also makes the following non-binding requests: |
| 32 | * |
| 33 | * Any person wishing to distribute modifications to the Software is |
| 34 | * requested to send the modifications to the original developer so that |
| 35 | * they can be incorporated into the canonical version. It is also |
| 36 | * requested that these non-binding requests be included along with the |
| 37 | * license above. |
| 38 | */ |
| 39 | |
| 40 | #include <windows.h> |
| 41 | #include <mmsystem.h> |
| 42 | #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) |
| 43 | #include <mmreg.h> /* for WAVEFORMATEX */ |
| 44 | #endif |
| 45 | |
| 46 | #include "portaudio.h" |
| 47 | #include "pa_win_waveformat.h" |
| 48 | |
| 49 | |
| 50 | #if !defined(WAVE_FORMAT_EXTENSIBLE) |
| 51 | #define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
| 52 | #endif |
| 53 | |
| 54 | |
| 55 | static GUID pawin_ksDataFormatSubtypeGuidBase = |
| 56 | { (USHORT)(WAVE_FORMAT_PCM), 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 }; |
| 57 | |
| 58 | |
| 59 | int PaWin_SampleFormatToLinearWaveFormatTag( PaSampleFormat sampleFormat ) |
| 60 | { |
| 61 | if( sampleFormat == paFloat32 ) |
| 62 | return PAWIN_WAVE_FORMAT_IEEE_FLOAT; |
| 63 | |
| 64 | return PAWIN_WAVE_FORMAT_PCM; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | void PaWin_InitializeWaveFormatEx( PaWinWaveFormat *waveFormat, |
| 69 | int numChannels, PaSampleFormat sampleFormat, int waveFormatTag, double sampleRate ) |
| 70 | { |
| 71 | WAVEFORMATEX *waveFormatEx = (WAVEFORMATEX*)waveFormat; |
| 72 | int bytesPerSample = Pa_GetSampleSize(sampleFormat); |
| 73 | unsigned long bytesPerFrame = numChannels * bytesPerSample; |
| 74 | |
| 75 | waveFormatEx->wFormatTag = waveFormatTag; |
| 76 | waveFormatEx->nChannels = (WORD)numChannels; |
| 77 | waveFormatEx->nSamplesPerSec = (DWORD)sampleRate; |
| 78 | waveFormatEx->nAvgBytesPerSec = waveFormatEx->nSamplesPerSec * bytesPerFrame; |
| 79 | waveFormatEx->nBlockAlign = (WORD)bytesPerFrame; |
| 80 | waveFormatEx->wBitsPerSample = bytesPerSample * 8; |
| 81 | waveFormatEx->cbSize = 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void PaWin_InitializeWaveFormatExtensible( PaWinWaveFormat *waveFormat, |
| 86 | int numChannels, PaSampleFormat sampleFormat, int waveFormatTag, double sampleRate, |
| 87 | PaWinWaveFormatChannelMask channelMask ) |
| 88 | { |
| 89 | WAVEFORMATEX *waveFormatEx = (WAVEFORMATEX*)waveFormat; |
| 90 | int bytesPerSample = Pa_GetSampleSize(sampleFormat); |
| 91 | unsigned long bytesPerFrame = numChannels * bytesPerSample; |
| 92 | GUID guid; |
| 93 | |
| 94 | waveFormatEx->wFormatTag = WAVE_FORMAT_EXTENSIBLE; |
| 95 | waveFormatEx->nChannels = (WORD)numChannels; |
| 96 | waveFormatEx->nSamplesPerSec = (DWORD)sampleRate; |
| 97 | waveFormatEx->nAvgBytesPerSec = waveFormatEx->nSamplesPerSec * bytesPerFrame; |
| 98 | waveFormatEx->nBlockAlign = (WORD)bytesPerFrame; |
| 99 | waveFormatEx->wBitsPerSample = bytesPerSample * 8; |
| 100 | waveFormatEx->cbSize = 22; |
| 101 | |
| 102 | memcpy(&waveFormat->fields[PAWIN_INDEXOF_WVALIDBITSPERSAMPLE], |
| 103 | &waveFormatEx->wBitsPerSample, sizeof(WORD)); |
| 104 | |
| 105 | memcpy(&waveFormat->fields[PAWIN_INDEXOF_DWCHANNELMASK], |
| 106 | &channelMask, sizeof(DWORD)); |
| 107 | |
| 108 | guid = pawin_ksDataFormatSubtypeGuidBase; |
| 109 | guid.Data1 = (USHORT)waveFormatTag; |
| 110 | memcpy(&waveFormat->fields[PAWIN_INDEXOF_SUBFORMAT], &guid, sizeof(GUID)); |
| 111 | } |
| 112 | |
| 113 | PaWinWaveFormatChannelMask PaWin_DefaultChannelMask( int numChannels ) |
| 114 | { |
| 115 | switch( numChannels ){ |
| 116 | case 1: |
| 117 | return PAWIN_SPEAKER_MONO; |
| 118 | case 2: |
| 119 | return PAWIN_SPEAKER_STEREO; |
| 120 | case 3: |
| 121 | return PAWIN_SPEAKER_FRONT_LEFT | PAWIN_SPEAKER_FRONT_CENTER | PAWIN_SPEAKER_FRONT_RIGHT; |
| 122 | case 4: |
| 123 | return PAWIN_SPEAKER_QUAD; |
| 124 | case 5: |
| 125 | return PAWIN_SPEAKER_QUAD | PAWIN_SPEAKER_FRONT_CENTER; |
| 126 | case 6: |
| 127 | /* The meaning of the PAWIN_SPEAKER_5POINT1 flag has changed over time: |
| 128 | http://msdn2.microsoft.com/en-us/library/aa474707.aspx |
| 129 | We use PAWIN_SPEAKER_5POINT1 (not PAWIN_SPEAKER_5POINT1_SURROUND) |
| 130 | because on some cards (eg Audigy) PAWIN_SPEAKER_5POINT1_SURROUND |
| 131 | results in a virtual mixdown placing the rear output in the |
| 132 | front _and_ rear speakers. |
| 133 | */ |
| 134 | return PAWIN_SPEAKER_5POINT1; |
| 135 | /* case 7: */ |
| 136 | case 8: |
| 137 | /* RoBi: PAWIN_SPEAKER_7POINT1_SURROUND fits normal surround sound setups better than PAWIN_SPEAKER_7POINT1, f.i. NVidia HDMI Audio |
| 138 | output is silent on channels 5&6 with NVidia drivers, and channel 7&8 with Microsoft HD Audio driver using PAWIN_SPEAKER_7POINT1. |
| 139 | With PAWIN_SPEAKER_7POINT1_SURROUND both setups work OK. */ |
| 140 | return PAWIN_SPEAKER_7POINT1_SURROUND; |
| 141 | } |
| 142 | |
| 143 | /* Apparently some Audigy drivers will output silence |
| 144 | if the direct-out constant (0) is used. So this is not ideal. |
| 145 | |
| 146 | RoBi 2012-12-19: Also, NVidia driver seem to output garbage instead. Again not very ideal. |
| 147 | */ |
| 148 | return PAWIN_SPEAKER_DIRECTOUT; |
| 149 | |
| 150 | /* Note that Alec Rogers proposed the following as an alternate method to |
| 151 | generate the default channel mask, however it doesn't seem to be an improvement |
| 152 | over the above, since some drivers will matrix outputs mapping to non-present |
| 153 | speakers across multiple physical speakers. |
| 154 | |
| 155 | if(nChannels==1) { |
| 156 | pwfFormat->dwChannelMask = SPEAKER_FRONT_CENTER; |
| 157 | } |
| 158 | else { |
| 159 | pwfFormat->dwChannelMask = 0; |
| 160 | for(i=0; i<nChannels; i++) |
| 161 | pwfFormat->dwChannelMask = (pwfFormat->dwChannelMask << 1) | 0x1; |
| 162 | } |
| 163 | */ |
| 164 | } |
| 165 | #endif |