| 1 |
#ifdef _WIN32 |
| 2 |
/* |
| 3 |
* $Id$ |
| 4 |
* pa_win_wmme.c |
| 5 |
* Implementation of PortAudio for Windows MultiMedia Extensions (WMME) |
| 6 |
* |
| 7 |
* PortAudio Portable Real-Time Audio Library |
| 8 |
* Latest Version at: http://www.portaudio.com |
| 9 |
* |
| 10 |
* Authors: Ross Bencina and Phil Burk |
| 11 |
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk |
| 12 |
* |
| 13 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 14 |
* a copy of this software and associated documentation files |
| 15 |
* (the "Software"), to deal in the Software without restriction, |
| 16 |
* including without limitation the rights to use, copy, modify, merge, |
| 17 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 18 |
* and to permit persons to whom the Software is furnished to do so, |
| 19 |
* subject to the following conditions: |
| 20 |
* |
| 21 |
* The above copyright notice and this permission notice shall be |
| 22 |
* included in all copies or substantial portions of the Software. |
| 23 |
* |
| 24 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 27 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 28 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 29 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 30 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 31 |
*/ |
| 32 |
|
| 33 |
/* |
| 34 |
* The text above constitutes the entire PortAudio license; however, |
| 35 |
* the PortAudio community also makes the following non-binding requests: |
| 36 |
* |
| 37 |
* Any person wishing to distribute modifications to the Software is |
| 38 |
* requested to send the modifications to the original developer so that |
| 39 |
* they can be incorporated into the canonical version. It is also |
| 40 |
* requested that these non-binding requests be included along with the |
| 41 |
* license above. |
| 42 |
*/ |
| 43 |
|
| 44 |
/* Modification History: |
| 45 |
PLB = Phil Burk |
| 46 |
JM = Julien Maillard |
| 47 |
RDB = Ross Bencina |
| 48 |
PLB20010402 - sDevicePtrs now allocates based on sizeof(pointer) |
| 49 |
PLB20010413 - check for excessive numbers of channels |
| 50 |
PLB20010422 - apply Mike Berry's changes for CodeWarrior on PC |
| 51 |
including conditional inclusion of memory.h, |
| 52 |
and explicit typecasting on memory allocation |
| 53 |
PLB20010802 - use GlobalAlloc for sDevicesPtr instead of PaHost_AllocFastMemory |
| 54 |
PLB20010816 - pass process instead of thread to SetPriorityClass() |
| 55 |
PLB20010927 - use number of frames instead of real-time for CPULoad calculation. |
| 56 |
JM20020118 - prevent hung thread when buffers underflow. |
| 57 |
PLB20020321 - detect Win XP versus NT, 9x; fix DBUG typo; removed init of CurrentCount |
| 58 |
RDB20020411 - various renaming cleanups, factored streamData alloc and cpu usage init |
| 59 |
RDB20020417 - stopped counting WAVE_MAPPER when there were no real devices |
| 60 |
refactoring, renaming and fixed a few edge case bugs |
| 61 |
RDB20020531 - converted to V19 framework |
| 62 |
** NOTE maintenance history is now stored in CVS ** |
| 63 |
*/ |
| 64 |
|
| 65 |
/** @file |
| 66 |
@ingroup hostapi_src |
| 67 |
|
| 68 |
@brief Win32 host API implementation for the Windows MultiMedia Extensions (WMME) audio API. |
| 69 |
*/ |
| 70 |
|
| 71 |
/* |
| 72 |
How it works: |
| 73 |
|
| 74 |
For both callback and blocking read/write streams we open the MME devices |
| 75 |
in CALLBACK_EVENT mode. In this mode, MME signals an Event object whenever |
| 76 |
it has finished with a buffer (either filled it for input, or played it |
| 77 |
for output). Where necessary, we block waiting for Event objects using |
| 78 |
WaitMultipleObjects(). |
| 79 |
|
| 80 |
When implementing a PA callback stream, we set up a high priority thread |
| 81 |
which waits on the MME buffer Events and drains/fills the buffers when |
| 82 |
they are ready. |
| 83 |
|
| 84 |
When implementing a PA blocking read/write stream, we simply wait on these |
| 85 |
Events (when necessary) inside the ReadStream() and WriteStream() functions. |
| 86 |
*/ |
| 87 |
|
| 88 |
#include <stdio.h> |
| 89 |
#include <stdlib.h> |
| 90 |
#include <limits.h> |
| 91 |
#include <math.h> |
| 92 |
#include <windows.h> |
| 93 |
#include <mmsystem.h> |
| 94 |
#ifndef UNDER_CE |
| 95 |
#include <process.h> |
| 96 |
#endif |
| 97 |
#include <assert.h> |
| 98 |
/* PLB20010422 - "memory.h" doesn't work on CodeWarrior for PC. Thanks Mike Berry for the mod. */ |
| 99 |
#ifndef __MWERKS__ |
| 100 |
#include <malloc.h> |
| 101 |
#include <memory.h> |
| 102 |
#endif /* __MWERKS__ */ |
| 103 |
|
| 104 |
#include "portaudio.h" |
| 105 |
#include "pa_trace.h" |
| 106 |
#include "pa_util.h" |
| 107 |
#include "pa_allocation.h" |
| 108 |
#include "pa_hostapi.h" |
| 109 |
#include "pa_stream.h" |
| 110 |
#include "pa_cpuload.h" |
| 111 |
#include "pa_process.h" |
| 112 |
#include "pa_debugprint.h" |
| 113 |
|
| 114 |
#include "pa_win_wmme.h" |
| 115 |
#include "pa_win_waveformat.h" |
| 116 |
|
| 117 |
#ifdef PAWIN_USE_WDMKS_DEVICE_INFO |
| 118 |
#include "pa_win_wdmks_utils.h" |
| 119 |
#ifndef DRV_QUERYDEVICEINTERFACE |
| 120 |
#define DRV_QUERYDEVICEINTERFACE (DRV_RESERVED + 12) |
| 121 |
#endif |
| 122 |
#ifndef DRV_QUERYDEVICEINTERFACESIZE |
| 123 |
#define DRV_QUERYDEVICEINTERFACESIZE (DRV_RESERVED + 13) |
| 124 |
#endif |
| 125 |
#endif /* PAWIN_USE_WDMKS_DEVICE_INFO */ |
| 126 |
|
| 127 |
/* use CreateThread for CYGWIN, _beginthreadex for all others */ |
| 128 |
#if !defined(__CYGWIN__) && !defined(_WIN32_WCE) |
| 129 |
#define CREATE_THREAD (HANDLE)_beginthreadex( 0, 0, ProcessingThreadProc, stream, 0, &stream->processingThreadId ) |
| 130 |
#define PA_THREAD_FUNC static unsigned WINAPI |
| 131 |
#define PA_THREAD_ID unsigned |
| 132 |
#else |
| 133 |
#define CREATE_THREAD CreateThread( 0, 0, ProcessingThreadProc, stream, 0, &stream->processingThreadId ) |
| 134 |
#define PA_THREAD_FUNC static DWORD WINAPI |
| 135 |
#define PA_THREAD_ID DWORD |
| 136 |
#endif |
| 137 |
#if (defined(_WIN32_WCE)) |
| 138 |
#pragma comment(lib, "Coredll.lib") |
| 139 |
#elif (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) /* MSC version 6 and above */ |
| 140 |
#pragma comment(lib, "winmm.lib") |
| 141 |
#endif |
| 142 |
|
| 143 |
/* |
| 144 |
provided in newer platform sdks |
| 145 |
*/ |
| 146 |
#ifndef DWORD_PTR |
| 147 |
#if defined(_WIN64) |
| 148 |
#define DWORD_PTR unsigned __int64 |
| 149 |
#else |
| 150 |
#define DWORD_PTR unsigned long |
| 151 |
#endif |
| 152 |
#endif |
| 153 |
|
| 154 |
/************************************************* Constants ********/ |
| 155 |
|
| 156 |
#define PA_MME_USE_HIGH_DEFAULT_LATENCY_ (0) /* For debugging glitches. */ |
| 157 |
|
| 158 |
#if PA_MME_USE_HIGH_DEFAULT_LATENCY_ |
| 159 |
#define PA_MME_WIN_9X_DEFAULT_LATENCY_ (0.4) |
| 160 |
#define PA_MME_MIN_HOST_OUTPUT_BUFFER_COUNT_ (4) |
| 161 |
#define PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_FULL_DUPLEX_ (4) |
| 162 |
#define PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_HALF_DUPLEX_ (4) |
| 163 |
#define PA_MME_HOST_BUFFER_GRANULARITY_FRAMES_WHEN_UNSPECIFIED_ (16) |
| 164 |
#define PA_MME_MAX_HOST_BUFFER_SECS_ (0.3) /* Do not exceed unless user buffer exceeds */ |
| 165 |
#define PA_MME_MAX_HOST_BUFFER_BYTES_ (32 * 1024) /* Has precedence over PA_MME_MAX_HOST_BUFFER_SECS_, some drivers are known to crash with buffer sizes > 32k */ |
| 166 |
#else |
| 167 |
#define PA_MME_WIN_9X_DEFAULT_LATENCY_ (0.2) |
| 168 |
#define PA_MME_MIN_HOST_OUTPUT_BUFFER_COUNT_ (2) |
| 169 |
#define PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_FULL_DUPLEX_ (3) /* always use at least 3 input buffers for full duplex */ |
| 170 |
#define PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_HALF_DUPLEX_ (2) |
| 171 |
#define PA_MME_HOST_BUFFER_GRANULARITY_FRAMES_WHEN_UNSPECIFIED_ (16) |
| 172 |
#define PA_MME_MAX_HOST_BUFFER_SECS_ (0.1) /* Do not exceed unless user buffer exceeds */ |
| 173 |
#define PA_MME_MAX_HOST_BUFFER_BYTES_ (32 * 1024) /* Has precedence over PA_MME_MAX_HOST_BUFFER_SECS_, some drivers are known to crash with buffer sizes > 32k */ |
| 174 |
#endif |
| 175 |
|
| 176 |
/* Use higher latency for NT because it is even worse at real-time |
| 177 |
operation than Win9x. |
| 178 |
*/ |
| 179 |
#define PA_MME_WIN_NT_DEFAULT_LATENCY_ (0.4) |
| 180 |
|
| 181 |
/* Default low latency for WDM based systems. This is based on a rough |
| 182 |
survey of workable latency settings using patest_wmme_find_best_latency_params.c. |
| 183 |
See pdf attached to ticket 185 for a graph of the survey results: |
| 184 |
http://www.portaudio.com/trac/ticket/185 |
| 185 |
|
| 186 |
Workable latencies varied between 40ms and ~80ms on different systems (different |
| 187 |
combinations of hardware, 32 and 64 bit, WinXP, Vista and Win7. We didn't |
| 188 |
get enough Vista results to know if Vista has systemically worse latency. |
| 189 |
For now we choose a safe value across all Windows versions here. |
| 190 |
*/ |
| 191 |
#define PA_MME_WIN_WDM_DEFAULT_LATENCY_ (0.090) |
| 192 |
|
| 193 |
|
| 194 |
/* When client suggestedLatency could result in many host buffers, we aim to have around 8, |
| 195 |
based off Windows documentation that suggests that the kmixer uses 8 buffers. This choice |
| 196 |
is somewhat arbitrary here, since we haven't observed significant stability degradation |
| 197 |
with using either more, or less buffers. |
| 198 |
*/ |
| 199 |
#define PA_MME_TARGET_HOST_BUFFER_COUNT_ 8 |
| 200 |
|
| 201 |
#define PA_MME_MIN_TIMEOUT_MSEC_ (1000) |
| 202 |
|
| 203 |
static const char constInputMapperSuffix_[] = " - Input"; |
| 204 |
static const char constOutputMapperSuffix_[] = " - Output"; |
| 205 |
|
| 206 |
/********************************************************************/ |
| 207 |
|
| 208 |
/* Copy null-terminated WCHAR string to explicit char string using UTF8 encoding */ |
| 209 |
static char *CopyWCharStringToUtf8CString(char *destination, size_t destLengthBytes, const WCHAR *source) |
| 210 |
{ |
| 211 |
/* The cbMultiByte parameter ["destLengthBytes" below] is: |
| 212 |
""" |
| 213 |
Size, in bytes, of the buffer indicated by lpMultiByteStr ["destination" below]. |
| 214 |
If this parameter is set to 0, the function returns the required buffer |
| 215 |
size for lpMultiByteStr and makes no use of the output parameter itself. |
| 216 |
""" |
| 217 |
Source: WideCharToMultiByte at MSDN: |
| 218 |
http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130(v=vs.85).aspx |
| 219 |
*/ |
| 220 |
int intDestLengthBytes; /* cbMultiByte */ |
| 221 |
/* intDestLengthBytes is an int, destLengthBytes is a size_t. Ensure that we don't overflow |
| 222 |
intDestLengthBytes by only using at most INT_MAX bytes of destination buffer. |
| 223 |
*/ |
| 224 |
if (destLengthBytes < INT_MAX) |
| 225 |
{ |
| 226 |
#pragma warning (disable : 4267) /* "conversion from 'size_t' to 'int', possible loss of data" */ |
| 227 |
intDestLengthBytes = (int)destLengthBytes; /* destLengthBytes is guaranteed < INT_MAX here */ |
| 228 |
#pragma warning (default : 4267) |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
intDestLengthBytes = INT_MAX; |
| 233 |
} |
| 234 |
|
| 235 |
if (WideCharToMultiByte(CP_UTF8, 0, source, -1, destination, /*cbMultiByte=*/intDestLengthBytes, NULL, NULL) == 0) |
| 236 |
return NULL; |
| 237 |
return destination; |
| 238 |
} |
| 239 |
|
| 240 |
/* returns required length (in bytes) of destination buffer when |
| 241 |
converting WCHAR string to UTF8 bytes, not including the terminating null. */ |
| 242 |
static size_t WCharStringLen(const WCHAR *str) |
| 243 |
{ |
| 244 |
return WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL); |
| 245 |
} |
| 246 |
|
| 247 |
/********************************************************************/ |
| 248 |
|
| 249 |
typedef struct PaWinMmeStream PaWinMmeStream; /* forward declaration */ |
| 250 |
|
| 251 |
/* prototypes for functions declared in this file */ |
| 252 |
|
| 253 |
#ifdef __cplusplus |
| 254 |
extern "C" |
| 255 |
{ |
| 256 |
#endif /* __cplusplus */ |
| 257 |
|
| 258 |
PaError PaWinMme_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index ); |
| 259 |
|
| 260 |
#ifdef __cplusplus |
| 261 |
} |
| 262 |
#endif /* __cplusplus */ |
| 263 |
|
| 264 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ); |
| 265 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 266 |
PaStream** stream, |
| 267 |
const PaStreamParameters *inputParameters, |
| 268 |
const PaStreamParameters *outputParameters, |
| 269 |
double sampleRate, |
| 270 |
unsigned long framesPerBuffer, |
| 271 |
PaStreamFlags streamFlags, |
| 272 |
PaStreamCallback *streamCallback, |
| 273 |
void *userData ); |
| 274 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 275 |
const PaStreamParameters *inputParameters, |
| 276 |
const PaStreamParameters *outputParameters, |
| 277 |
double sampleRate ); |
| 278 |
static PaError CloseStream( PaStream* stream ); |
| 279 |
static PaError StartStream( PaStream *stream ); |
| 280 |
static PaError StopStream( PaStream *stream ); |
| 281 |
static PaError AbortStream( PaStream *stream ); |
| 282 |
static PaError IsStreamStopped( PaStream *s ); |
| 283 |
static PaError IsStreamActive( PaStream *stream ); |
| 284 |
static PaTime GetStreamTime( PaStream *stream ); |
| 285 |
static double GetStreamCpuLoad( PaStream* stream ); |
| 286 |
static PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames ); |
| 287 |
static PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames ); |
| 288 |
static signed long GetStreamReadAvailable( PaStream* stream ); |
| 289 |
static signed long GetStreamWriteAvailable( PaStream* stream ); |
| 290 |
|
| 291 |
|
| 292 |
/* macros for setting last host error information */ |
| 293 |
|
| 294 |
#define PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ) \ |
| 295 |
{ \ |
| 296 |
wchar_t mmeErrorTextWide[ MAXERRORLENGTH ]; \ |
| 297 |
char mmeErrorText[ MAXERRORLENGTH ]; \ |
| 298 |
waveInGetErrorTextW( mmresult, mmeErrorTextWide, MAXERRORLENGTH ); \ |
| 299 |
WideCharToMultiByte( CP_UTF8, 0, mmeErrorTextWide, -1, \ |
| 300 |
mmeErrorText, MAXERRORLENGTH, NULL, NULL ); \ |
| 301 |
PaUtil_SetLastHostErrorInfo( paMME, mmresult, mmeErrorText ); \ |
| 302 |
} |
| 303 |
|
| 304 |
#define PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ) \ |
| 305 |
{ \ |
| 306 |
wchar_t mmeErrorTextWide[ MAXERRORLENGTH ]; \ |
| 307 |
char mmeErrorText[ MAXERRORLENGTH ]; \ |
| 308 |
waveOutGetErrorTextW( mmresult, mmeErrorTextWide, MAXERRORLENGTH ); \ |
| 309 |
WideCharToMultiByte( CP_UTF8, 0, mmeErrorTextWide, -1, \ |
| 310 |
mmeErrorText, MAXERRORLENGTH, NULL, NULL ); \ |
| 311 |
PaUtil_SetLastHostErrorInfo( paMME, mmresult, mmeErrorText ); \ |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
static void PaMme_SetLastSystemError( DWORD errorCode ) |
| 316 |
{ |
| 317 |
char *lpMsgBuf; |
| 318 |
FormatMessage( |
| 319 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 320 |
NULL, |
| 321 |
errorCode, |
| 322 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 323 |
(LPTSTR) &lpMsgBuf, |
| 324 |
0, |
| 325 |
NULL |
| 326 |
); |
| 327 |
PaUtil_SetLastHostErrorInfo( paMME, errorCode, lpMsgBuf ); |
| 328 |
LocalFree( lpMsgBuf ); |
| 329 |
} |
| 330 |
|
| 331 |
#define PA_MME_SET_LAST_SYSTEM_ERROR( errorCode ) \ |
| 332 |
PaMme_SetLastSystemError( errorCode ) |
| 333 |
|
| 334 |
|
| 335 |
/* PaError returning wrappers for some commonly used win32 functions |
| 336 |
note that we allow passing a null ptr to have no effect. |
| 337 |
*/ |
| 338 |
|
| 339 |
static PaError CreateEventWithPaError( HANDLE *handle, |
| 340 |
LPSECURITY_ATTRIBUTES lpEventAttributes, |
| 341 |
BOOL bManualReset, |
| 342 |
BOOL bInitialState, |
| 343 |
LPCTSTR lpName ) |
| 344 |
{ |
| 345 |
PaError result = paNoError; |
| 346 |
|
| 347 |
*handle = NULL; |
| 348 |
|
| 349 |
*handle = CreateEvent( lpEventAttributes, bManualReset, bInitialState, lpName ); |
| 350 |
if( *handle == NULL ) |
| 351 |
{ |
| 352 |
result = paUnanticipatedHostError; |
| 353 |
PA_MME_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 354 |
} |
| 355 |
|
| 356 |
return result; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
static PaError ResetEventWithPaError( HANDLE handle ) |
| 361 |
{ |
| 362 |
PaError result = paNoError; |
| 363 |
|
| 364 |
if( handle ) |
| 365 |
{ |
| 366 |
if( ResetEvent( handle ) == 0 ) |
| 367 |
{ |
| 368 |
result = paUnanticipatedHostError; |
| 369 |
PA_MME_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
return result; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
static PaError CloseHandleWithPaError( HANDLE handle ) |
| 378 |
{ |
| 379 |
PaError result = paNoError; |
| 380 |
|
| 381 |
if( handle ) |
| 382 |
{ |
| 383 |
if( CloseHandle( handle ) == 0 ) |
| 384 |
{ |
| 385 |
result = paUnanticipatedHostError; |
| 386 |
PA_MME_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return result; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/* PaWinMmeHostApiRepresentation - host api datastructure specific to this implementation */ |
| 395 |
|
| 396 |
typedef struct |
| 397 |
{ |
| 398 |
PaUtilHostApiRepresentation inheritedHostApiRep; |
| 399 |
PaUtilStreamInterface callbackStreamInterface; |
| 400 |
PaUtilStreamInterface blockingStreamInterface; |
| 401 |
|
| 402 |
PaUtilAllocationGroup *allocations; |
| 403 |
|
| 404 |
int inputDeviceCount, outputDeviceCount; |
| 405 |
|
| 406 |
/** winMmeDeviceIds is an array of WinMme device ids. |
| 407 |
fields in the range [0, inputDeviceCount) are input device ids, |
| 408 |
and [inputDeviceCount, inputDeviceCount + outputDeviceCount) are output |
| 409 |
device ids. |
| 410 |
*/ |
| 411 |
UINT *winMmeDeviceIds; |
| 412 |
} |
| 413 |
PaWinMmeHostApiRepresentation; |
| 414 |
|
| 415 |
|
| 416 |
typedef struct |
| 417 |
{ |
| 418 |
PaDeviceInfo inheritedDeviceInfo; |
| 419 |
DWORD dwFormats; /**<< standard formats bitmask from the WAVEINCAPS and WAVEOUTCAPS structures */ |
| 420 |
char deviceInputChannelCountIsKnown; /**<< if the system returns 0xFFFF then we don't really know the number of supported channels (1=>known, 0=>unknown)*/ |
| 421 |
char deviceOutputChannelCountIsKnown; /**<< if the system returns 0xFFFF then we don't really know the number of supported channels (1=>known, 0=>unknown)*/ |
| 422 |
} |
| 423 |
PaWinMmeDeviceInfo; |
| 424 |
|
| 425 |
|
| 426 |
/************************************************************************* |
| 427 |
* Returns recommended device ID. |
| 428 |
* On the PC, the recommended device can be specified by the user by |
| 429 |
* setting an environment variable. For example, to use device #1. |
| 430 |
* |
| 431 |
* set PA_RECOMMENDED_OUTPUT_DEVICE=1 |
| 432 |
* |
| 433 |
* The user should first determine the available device ID by using |
| 434 |
* the supplied application "pa_devs". |
| 435 |
*/ |
| 436 |
#define PA_ENV_BUF_SIZE_ (32) |
| 437 |
#define PA_REC_IN_DEV_ENV_NAME_ ("PA_RECOMMENDED_INPUT_DEVICE") |
| 438 |
#define PA_REC_OUT_DEV_ENV_NAME_ ("PA_RECOMMENDED_OUTPUT_DEVICE") |
| 439 |
static PaDeviceIndex GetEnvDefaultDeviceID( char *envName ) |
| 440 |
{ |
| 441 |
PaDeviceIndex recommendedIndex = paNoDevice; |
| 442 |
DWORD hresult; |
| 443 |
char envbuf[PA_ENV_BUF_SIZE_]; |
| 444 |
|
| 445 |
#ifndef WIN32_PLATFORM_PSPC /* no GetEnvironmentVariable on PocketPC */ |
| 446 |
|
| 447 |
/* Let user determine default device by setting environment variable. */ |
| 448 |
hresult = GetEnvironmentVariableA( envName, envbuf, PA_ENV_BUF_SIZE_ ); |
| 449 |
if( (hresult > 0) && (hresult < PA_ENV_BUF_SIZE_) ) |
| 450 |
{ |
| 451 |
recommendedIndex = atoi( envbuf ); |
| 452 |
} |
| 453 |
#endif |
| 454 |
|
| 455 |
return recommendedIndex; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
static void InitializeDefaultDeviceIdsFromEnv( PaWinMmeHostApiRepresentation *hostApi ) |
| 460 |
{ |
| 461 |
PaDeviceIndex device; |
| 462 |
|
| 463 |
/* input */ |
| 464 |
device = GetEnvDefaultDeviceID( PA_REC_IN_DEV_ENV_NAME_ ); |
| 465 |
if( device != paNoDevice && |
| 466 |
( device >= 0 && device < hostApi->inheritedHostApiRep.info.deviceCount ) && |
| 467 |
hostApi->inheritedHostApiRep.deviceInfos[ device ]->maxInputChannels > 0 ) |
| 468 |
{ |
| 469 |
hostApi->inheritedHostApiRep.info.defaultInputDevice = device; |
| 470 |
} |
| 471 |
|
| 472 |
/* output */ |
| 473 |
device = GetEnvDefaultDeviceID( PA_REC_OUT_DEV_ENV_NAME_ ); |
| 474 |
if( device != paNoDevice && |
| 475 |
( device >= 0 && device < hostApi->inheritedHostApiRep.info.deviceCount ) && |
| 476 |
hostApi->inheritedHostApiRep.deviceInfos[ device ]->maxOutputChannels > 0 ) |
| 477 |
{ |
| 478 |
hostApi->inheritedHostApiRep.info.defaultOutputDevice = device; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
/** Convert external PA ID to a windows multimedia device ID |
| 484 |
*/ |
| 485 |
static UINT LocalDeviceIndexToWinMmeDeviceId( PaWinMmeHostApiRepresentation *hostApi, PaDeviceIndex device ) |
| 486 |
{ |
| 487 |
assert( device >= 0 && device < hostApi->inputDeviceCount + hostApi->outputDeviceCount ); |
| 488 |
|
| 489 |
return hostApi->winMmeDeviceIds[ device ]; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
static int SampleFormatAndWinWmmeSpecificFlagsToLinearWaveFormatTag( PaSampleFormat sampleFormat, unsigned long winMmeSpecificFlags ) |
| 494 |
{ |
| 495 |
int waveFormatTag = 0; |
| 496 |
|
| 497 |
if( winMmeSpecificFlags & paWinMmeWaveFormatDolbyAc3Spdif ) |
| 498 |
waveFormatTag = PAWIN_WAVE_FORMAT_DOLBY_AC3_SPDIF; |
| 499 |
else if( winMmeSpecificFlags & paWinMmeWaveFormatWmaSpdif ) |
| 500 |
waveFormatTag = PAWIN_WAVE_FORMAT_WMA_SPDIF; |
| 501 |
else |
| 502 |
waveFormatTag = PaWin_SampleFormatToLinearWaveFormatTag( sampleFormat ); |
| 503 |
|
| 504 |
return waveFormatTag; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
static PaError QueryInputWaveFormatEx( int deviceId, WAVEFORMATEX *waveFormatEx ) |
| 509 |
{ |
| 510 |
MMRESULT mmresult; |
| 511 |
|
| 512 |
switch( mmresult = waveInOpen( NULL, deviceId, waveFormatEx, 0, 0, WAVE_FORMAT_QUERY ) ) |
| 513 |
{ |
| 514 |
case MMSYSERR_NOERROR: |
| 515 |
return paNoError; |
| 516 |
case MMSYSERR_ALLOCATED: /* Specified resource is already allocated. */ |
| 517 |
return paDeviceUnavailable; |
| 518 |
case MMSYSERR_NODRIVER: /* No device driver is present. */ |
| 519 |
return paDeviceUnavailable; |
| 520 |
case MMSYSERR_NOMEM: /* Unable to allocate or lock memory. */ |
| 521 |
return paInsufficientMemory; |
| 522 |
case WAVERR_BADFORMAT: /* Attempted to open with an unsupported waveform-audio format. */ |
| 523 |
return paSampleFormatNotSupported; |
| 524 |
|
| 525 |
case MMSYSERR_BADDEVICEID: /* Specified device identifier is out of range. */ |
| 526 |
/* falls through */ |
| 527 |
default: |
| 528 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 529 |
return paUnanticipatedHostError; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
static PaError QueryOutputWaveFormatEx( int deviceId, WAVEFORMATEX *waveFormatEx ) |
| 535 |
{ |
| 536 |
MMRESULT mmresult; |
| 537 |
|
| 538 |
switch( mmresult = waveOutOpen( NULL, deviceId, waveFormatEx, 0, 0, WAVE_FORMAT_QUERY ) ) |
| 539 |
{ |
| 540 |
case MMSYSERR_NOERROR: |
| 541 |
return paNoError; |
| 542 |
case MMSYSERR_ALLOCATED: /* Specified resource is already allocated. */ |
| 543 |
return paDeviceUnavailable; |
| 544 |
case MMSYSERR_NODRIVER: /* No device driver is present. */ |
| 545 |
return paDeviceUnavailable; |
| 546 |
case MMSYSERR_NOMEM: /* Unable to allocate or lock memory. */ |
| 547 |
return paInsufficientMemory; |
| 548 |
case WAVERR_BADFORMAT: /* Attempted to open with an unsupported waveform-audio format. */ |
| 549 |
return paSampleFormatNotSupported; |
| 550 |
|
| 551 |
case MMSYSERR_BADDEVICEID: /* Specified device identifier is out of range. */ |
| 552 |
/* falls through */ |
| 553 |
default: |
| 554 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 555 |
return paUnanticipatedHostError; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
static PaError QueryFormatSupported( PaDeviceInfo *deviceInfo, |
| 561 |
PaError (*waveFormatExQueryFunction)(int, WAVEFORMATEX*), |
| 562 |
int winMmeDeviceId, int channels, double sampleRate, unsigned long winMmeSpecificFlags ) |
| 563 |
{ |
| 564 |
PaWinMmeDeviceInfo *winMmeDeviceInfo = (PaWinMmeDeviceInfo*)deviceInfo; |
| 565 |
PaWinWaveFormat waveFormat; |
| 566 |
PaSampleFormat sampleFormat; |
| 567 |
int waveFormatTag; |
| 568 |
|
| 569 |
/* @todo at the moment we only query with 16 bit sample format and directout speaker config*/ |
| 570 |
|
| 571 |
sampleFormat = paInt16; |
| 572 |
waveFormatTag = SampleFormatAndWinWmmeSpecificFlagsToLinearWaveFormatTag( sampleFormat, winMmeSpecificFlags ); |
| 573 |
|
| 574 |
if( waveFormatTag == PaWin_SampleFormatToLinearWaveFormatTag( paInt16 ) ){ |
| 575 |
|
| 576 |
/* attempt bypass querying the device for linear formats */ |
| 577 |
|
| 578 |
if( sampleRate == 11025.0 |
| 579 |
&& ( (channels == 1 && (winMmeDeviceInfo->dwFormats & WAVE_FORMAT_1M16)) |
| 580 |
|| (channels == 2 && (winMmeDeviceInfo->dwFormats & WAVE_FORMAT_1S16)) ) ){ |
| 581 |
|
| 582 |
return paNoError; |
| 583 |
} |
| 584 |
|
| 585 |
if( sampleRate == 22050.0 |
| 586 |
&& ( (channels == 1 && (winMmeDeviceInfo->dwFormats & WAVE_FORMAT_2M16)) |
| 587 |
|| (channels == 2 && (winMmeDeviceInfo->dwFormats & WAVE_FORMAT_2S16)) ) ){ |
| 588 |
|
| 589 |
return paNoError; |
| 590 |
} |
| 591 |
|
| 592 |
if( sampleRate == 44100.0 |
| 593 |
&& ( (channels == 1 && (winMmeDeviceInfo->dwFormats & WAVE_FORMAT_4M16)) |
| 594 |
|| (channels == 2 && (winMmeDeviceInfo->dwFormats & WAVE_FORMAT_4S16)) ) ){ |
| 595 |
|
| 596 |
return paNoError; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
/* first, attempt to query the device using WAVEFORMATEXTENSIBLE, |
| 602 |
if this fails we fall back to WAVEFORMATEX */ |
| 603 |
|
| 604 |
PaWin_InitializeWaveFormatExtensible( &waveFormat, channels, sampleFormat, waveFormatTag, |
| 605 |
sampleRate, PAWIN_SPEAKER_DIRECTOUT ); |
| 606 |
|
| 607 |
if( waveFormatExQueryFunction( winMmeDeviceId, (WAVEFORMATEX*)&waveFormat ) == paNoError ) |
| 608 |
return paNoError; |
| 609 |
|
| 610 |
PaWin_InitializeWaveFormatEx( &waveFormat, channels, sampleFormat, waveFormatTag, sampleRate ); |
| 611 |
|
| 612 |
return waveFormatExQueryFunction( winMmeDeviceId, (WAVEFORMATEX*)&waveFormat ); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
#define PA_DEFAULTSAMPLERATESEARCHORDER_COUNT_ (13) /* must match array length below */ |
| 617 |
static double defaultSampleRateSearchOrder_[] = |
| 618 |
{ 44100.0, 48000.0, 32000.0, 24000.0, 22050.0, 88200.0, 96000.0, 192000.0, |
| 619 |
16000.0, 12000.0, 11025.0, 9600.0, 8000.0 }; |
| 620 |
|
| 621 |
static void DetectDefaultSampleRate( PaWinMmeDeviceInfo *winMmeDeviceInfo, int winMmeDeviceId, |
| 622 |
PaError (*waveFormatExQueryFunction)(int, WAVEFORMATEX*), int maxChannels ) |
| 623 |
{ |
| 624 |
PaDeviceInfo *deviceInfo = &winMmeDeviceInfo->inheritedDeviceInfo; |
| 625 |
int i; |
| 626 |
|
| 627 |
deviceInfo->defaultSampleRate = 0.; |
| 628 |
|
| 629 |
for( i=0; i < PA_DEFAULTSAMPLERATESEARCHORDER_COUNT_; ++i ) |
| 630 |
{ |
| 631 |
double sampleRate = defaultSampleRateSearchOrder_[ i ]; |
| 632 |
PaError paerror = QueryFormatSupported( deviceInfo, waveFormatExQueryFunction, winMmeDeviceId, maxChannels, sampleRate, 0 ); |
| 633 |
if( paerror == paNoError ) |
| 634 |
{ |
| 635 |
deviceInfo->defaultSampleRate = sampleRate; |
| 636 |
break; |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
#ifdef PAWIN_USE_WDMKS_DEVICE_INFO |
| 643 |
static int QueryWaveInKSFilterMaxChannels( UINT waveInDeviceId, int *maxChannels ) |
| 644 |
{ |
| 645 |
void *devicePath; |
| 646 |
DWORD devicePathSize; |
| 647 |
int result = 0; |
| 648 |
|
| 649 |
/* pass UINT ID via punned HWAVEIN, as per DRV_QUERYDEVICEINTERFACESIZE documentation */ |
| 650 |
HWAVEIN hDeviceId = (HWAVEIN)((UINT_PTR)waveInDeviceId); |
| 651 |
|
| 652 |
if( waveInMessage(hDeviceId, DRV_QUERYDEVICEINTERFACESIZE, |
| 653 |
(DWORD_PTR)&devicePathSize, 0 ) != MMSYSERR_NOERROR ) |
| 654 |
return 0; |
| 655 |
|
| 656 |
devicePath = PaUtil_AllocateMemory( devicePathSize ); |
| 657 |
if( !devicePath ) |
| 658 |
return 0; |
| 659 |
|
| 660 |
/* apparently DRV_QUERYDEVICEINTERFACE returns a unicode interface path, although this is undocumented */ |
| 661 |
if( waveInMessage(hDeviceId, DRV_QUERYDEVICEINTERFACE, |
| 662 |
(DWORD_PTR)devicePath, devicePathSize ) == MMSYSERR_NOERROR ) |
| 663 |
{ |
| 664 |
int count = PaWin_WDMKS_QueryFilterMaximumChannelCount( devicePath, /* isInput= */ 1 ); |
| 665 |
if( count > 0 ) |
| 666 |
{ |
| 667 |
*maxChannels = count; |
| 668 |
result = 1; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
PaUtil_FreeMemory( devicePath ); |
| 673 |
|
| 674 |
return result; |
| 675 |
} |
| 676 |
#endif /* PAWIN_USE_WDMKS_DEVICE_INFO */ |
| 677 |
|
| 678 |
|
| 679 |
static PaError InitializeInputDeviceInfo( PaWinMmeHostApiRepresentation *winMmeHostApi, |
| 680 |
PaWinMmeDeviceInfo *winMmeDeviceInfo, UINT winMmeInputDeviceId, int *success ) |
| 681 |
{ |
| 682 |
PaError result = paNoError; |
| 683 |
char *deviceName; /* non-const ptr */ |
| 684 |
MMRESULT mmresult; |
| 685 |
WAVEINCAPSW wic; |
| 686 |
PaDeviceInfo *deviceInfo = &winMmeDeviceInfo->inheritedDeviceInfo; |
| 687 |
size_t len; |
| 688 |
|
| 689 |
*success = 0; |
| 690 |
|
| 691 |
mmresult = waveInGetDevCapsW( winMmeInputDeviceId, &wic, sizeof( WAVEINCAPSW ) ); |
| 692 |
if( mmresult == MMSYSERR_NOMEM ) |
| 693 |
{ |
| 694 |
result = paInsufficientMemory; |
| 695 |
goto error; |
| 696 |
} |
| 697 |
else if( mmresult != MMSYSERR_NOERROR ) |
| 698 |
{ |
| 699 |
/* instead of returning paUnanticipatedHostError we return |
| 700 |
paNoError, but leave success set as 0. This allows |
| 701 |
Pa_Initialize to just ignore this device, without failing |
| 702 |
the entire initialisation process. |
| 703 |
*/ |
| 704 |
return paNoError; |
| 705 |
} |
| 706 |
|
| 707 |
/* NOTE: the WAVEOUTCAPS.szPname is a null-terminated array of 32 characters, |
| 708 |
so we are limited to displaying only the first 31 characters of the device name. */ |
| 709 |
if( winMmeInputDeviceId == WAVE_MAPPER ) |
| 710 |
{ |
| 711 |
len = WCharStringLen( wic.szPname ) + 1 + sizeof(constInputMapperSuffix_); |
| 712 |
/* Append I/O suffix to WAVE_MAPPER device. */ |
| 713 |
deviceName = (char*)PaUtil_GroupAllocateMemory( |
| 714 |
winMmeHostApi->allocations, |
| 715 |
(long)len ); |
| 716 |
if( !deviceName ) |
| 717 |
{ |
| 718 |
result = paInsufficientMemory; |
| 719 |
goto error; |
| 720 |
} |
| 721 |
CopyWCharStringToUtf8CString( deviceName, len, wic.szPname ); |
| 722 |
strcat( deviceName, constInputMapperSuffix_ ); |
| 723 |
} |
| 724 |
else |
| 725 |
{ |
| 726 |
len = WCharStringLen( wic.szPname ) + 1; |
| 727 |
deviceName = (char*)PaUtil_GroupAllocateMemory( |
| 728 |
winMmeHostApi->allocations, |
| 729 |
(long)len ); |
| 730 |
if( !deviceName ) |
| 731 |
{ |
| 732 |
result = paInsufficientMemory; |
| 733 |
goto error; |
| 734 |
} |
| 735 |
CopyWCharStringToUtf8CString( deviceName, len, wic.szPname ); |
| 736 |
} |
| 737 |
deviceInfo->name = deviceName; |
| 738 |
|
| 739 |
if( wic.wChannels == 0xFFFF || wic.wChannels < 1 || wic.wChannels > 255 ){ |
| 740 |
/* For Windows versions using WDM (possibly Windows 98 ME and later) |
| 741 |
* the kernel mixer sits between the application and the driver. As a result, |
| 742 |
* wave*GetDevCaps often kernel mixer channel counts, which are unlimited. |
| 743 |
* When this happens we assume the device is stereo and set a flag |
| 744 |
* so that other channel counts can be tried with OpenStream -- i.e. when |
| 745 |
* device*ChannelCountIsKnown is false, OpenStream will try whatever |
| 746 |
* channel count you supply. |
| 747 |
* see also InitializeOutputDeviceInfo() below. |
| 748 |
*/ |
| 749 |
|
| 750 |
PA_DEBUG(("Pa_GetDeviceInfo: Num input channels reported as %d! Changed to 2.\n", wic.wChannels )); |
| 751 |
deviceInfo->maxInputChannels = 2; |
| 752 |
winMmeDeviceInfo->deviceInputChannelCountIsKnown = 0; |
| 753 |
}else{ |
| 754 |
deviceInfo->maxInputChannels = wic.wChannels; |
| 755 |
winMmeDeviceInfo->deviceInputChannelCountIsKnown = 1; |
| 756 |
} |
| 757 |
|
| 758 |
#ifdef PAWIN_USE_WDMKS_DEVICE_INFO |
| 759 |
winMmeDeviceInfo->deviceInputChannelCountIsKnown = |
| 760 |
QueryWaveInKSFilterMaxChannels( winMmeInputDeviceId, &deviceInfo->maxInputChannels ); |
| 761 |
#endif /* PAWIN_USE_WDMKS_DEVICE_INFO */ |
| 762 |
|
| 763 |
winMmeDeviceInfo->dwFormats = wic.dwFormats; |
| 764 |
|
| 765 |
DetectDefaultSampleRate( winMmeDeviceInfo, winMmeInputDeviceId, |
| 766 |
QueryInputWaveFormatEx, deviceInfo->maxInputChannels ); |
| 767 |
|
| 768 |
*success = 1; |
| 769 |
|
| 770 |
error: |
| 771 |
return result; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
#ifdef PAWIN_USE_WDMKS_DEVICE_INFO |
| 776 |
static int QueryWaveOutKSFilterMaxChannels( UINT waveOutDeviceId, int *maxChannels ) |
| 777 |
{ |
| 778 |
void *devicePath; |
| 779 |
DWORD devicePathSize; |
| 780 |
int result = 0; |
| 781 |
|
| 782 |
/* pass UINT ID via punned HWAVEOUT, as per DRV_QUERYDEVICEINTERFACESIZE documentation */ |
| 783 |
HWAVEOUT hDeviceId = (HWAVEOUT)((UINT_PTR)waveOutDeviceId); |
| 784 |
|
| 785 |
if( waveOutMessage(hDeviceId, DRV_QUERYDEVICEINTERFACESIZE, |
| 786 |
(DWORD_PTR)&devicePathSize, 0 ) != MMSYSERR_NOERROR ) |
| 787 |
return 0; |
| 788 |
|
| 789 |
devicePath = PaUtil_AllocateMemory( devicePathSize ); |
| 790 |
if( !devicePath ) |
| 791 |
return 0; |
| 792 |
|
| 793 |
/* apparently DRV_QUERYDEVICEINTERFACE returns a unicode interface path, although this is undocumented */ |
| 794 |
if( waveOutMessage(hDeviceId, DRV_QUERYDEVICEINTERFACE, |
| 795 |
(DWORD_PTR)devicePath, devicePathSize ) == MMSYSERR_NOERROR ) |
| 796 |
{ |
| 797 |
int count = PaWin_WDMKS_QueryFilterMaximumChannelCount( devicePath, /* isInput= */ 0 ); |
| 798 |
if( count > 0 ) |
| 799 |
{ |
| 800 |
*maxChannels = count; |
| 801 |
result = 1; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
PaUtil_FreeMemory( devicePath ); |
| 806 |
|
| 807 |
return result; |
| 808 |
} |
| 809 |
#endif /* PAWIN_USE_WDMKS_DEVICE_INFO */ |
| 810 |
|
| 811 |
|
| 812 |
static PaError InitializeOutputDeviceInfo( PaWinMmeHostApiRepresentation *winMmeHostApi, |
| 813 |
PaWinMmeDeviceInfo *winMmeDeviceInfo, UINT winMmeOutputDeviceId, int *success ) |
| 814 |
{ |
| 815 |
PaError result = paNoError; |
| 816 |
char *deviceName; /* non-const ptr */ |
| 817 |
MMRESULT mmresult; |
| 818 |
WAVEOUTCAPSW woc; |
| 819 |
PaDeviceInfo *deviceInfo = &winMmeDeviceInfo->inheritedDeviceInfo; |
| 820 |
size_t len; |
| 821 |
#ifdef PAWIN_USE_WDMKS_DEVICE_INFO |
| 822 |
int wdmksDeviceOutputChannelCountIsKnown; |
| 823 |
#endif |
| 824 |
|
| 825 |
*success = 0; |
| 826 |
|
| 827 |
mmresult = waveOutGetDevCapsW( winMmeOutputDeviceId, &woc, sizeof( WAVEOUTCAPSW ) ); |
| 828 |
if( mmresult == MMSYSERR_NOMEM ) |
| 829 |
{ |
| 830 |
result = paInsufficientMemory; |
| 831 |
goto error; |
| 832 |
} |
| 833 |
else if( mmresult != MMSYSERR_NOERROR ) |
| 834 |
{ |
| 835 |
/* instead of returning paUnanticipatedHostError we return |
| 836 |
paNoError, but leave success set as 0. This allows |
| 837 |
Pa_Initialize to just ignore this device, without failing |
| 838 |
the entire initialisation process. |
| 839 |
*/ |
| 840 |
return paNoError; |
| 841 |
} |
| 842 |
|
| 843 |
/* NOTE: the WAVEOUTCAPS.szPname is a null-terminated array of 32 characters, |
| 844 |
so we are limited to displaying only the first 31 characters of the device name. */ |
| 845 |
if( winMmeOutputDeviceId == WAVE_MAPPER ) |
| 846 |
{ |
| 847 |
/* Append I/O suffix to WAVE_MAPPER device. */ |
| 848 |
len = WCharStringLen( woc.szPname ) + 1 + sizeof(constOutputMapperSuffix_); |
| 849 |
deviceName = (char*)PaUtil_GroupAllocateMemory( |
| 850 |
winMmeHostApi->allocations, |
| 851 |
(long)len ); |
| 852 |
if( !deviceName ) |
| 853 |
{ |
| 854 |
result = paInsufficientMemory; |
| 855 |
goto error; |
| 856 |
} |
| 857 |
CopyWCharStringToUtf8CString( deviceName, len, woc.szPname ); |
| 858 |
strcat( deviceName, constOutputMapperSuffix_ ); |
| 859 |
} |
| 860 |
else |
| 861 |
{ |
| 862 |
len = WCharStringLen( woc.szPname ) + 1; |
| 863 |
deviceName = (char*)PaUtil_GroupAllocateMemory( |
| 864 |
winMmeHostApi->allocations, |
| 865 |
(long)len ); |
| 866 |
if( !deviceName ) |
| 867 |
{ |
| 868 |
result = paInsufficientMemory; |
| 869 |
goto error; |
| 870 |
} |
| 871 |
CopyWCharStringToUtf8CString( deviceName, len, woc.szPname ); |
| 872 |
} |
| 873 |
deviceInfo->name = deviceName; |
| 874 |
|
| 875 |
if( woc.wChannels == 0xFFFF || woc.wChannels < 1 || woc.wChannels > 255 ){ |
| 876 |
/* For Windows versions using WDM (possibly Windows 98 ME and later) |
| 877 |
* the kernel mixer sits between the application and the driver. As a result, |
| 878 |
* wave*GetDevCaps often kernel mixer channel counts, which are unlimited. |
| 879 |
* When this happens we assume the device is stereo and set a flag |
| 880 |
* so that other channel counts can be tried with OpenStream -- i.e. when |
| 881 |
* device*ChannelCountIsKnown is false, OpenStream will try whatever |
| 882 |
* channel count you supply. |
| 883 |
* see also InitializeInputDeviceInfo() above. |
| 884 |
*/ |
| 885 |
|
| 886 |
PA_DEBUG(("Pa_GetDeviceInfo: Num output channels reported as %d! Changed to 2.\n", woc.wChannels )); |
| 887 |
deviceInfo->maxOutputChannels = 2; |
| 888 |
winMmeDeviceInfo->deviceOutputChannelCountIsKnown = 0; |
| 889 |
}else{ |
| 890 |
deviceInfo->maxOutputChannels = woc.wChannels; |
| 891 |
winMmeDeviceInfo->deviceOutputChannelCountIsKnown = 1; |
| 892 |
} |
| 893 |
|
| 894 |
#ifdef PAWIN_USE_WDMKS_DEVICE_INFO |
| 895 |
wdmksDeviceOutputChannelCountIsKnown = QueryWaveOutKSFilterMaxChannels( |
| 896 |
winMmeOutputDeviceId, &deviceInfo->maxOutputChannels ); |
| 897 |
if( wdmksDeviceOutputChannelCountIsKnown && !winMmeDeviceInfo->deviceOutputChannelCountIsKnown ) |
| 898 |
winMmeDeviceInfo->deviceOutputChannelCountIsKnown = 1; |
| 899 |
#endif /* PAWIN_USE_WDMKS_DEVICE_INFO */ |
| 900 |
|
| 901 |
winMmeDeviceInfo->dwFormats = woc.dwFormats; |
| 902 |
|
| 903 |
DetectDefaultSampleRate( winMmeDeviceInfo, winMmeOutputDeviceId, |
| 904 |
QueryOutputWaveFormatEx, deviceInfo->maxOutputChannels ); |
| 905 |
|
| 906 |
*success = 1; |
| 907 |
|
| 908 |
error: |
| 909 |
return result; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
static void GetDefaultLatencies( PaTime *defaultLowLatency, PaTime *defaultHighLatency ) |
| 914 |
{ |
| 915 |
/* |
| 916 |
NOTE: GetVersionEx() is deprecated as of Windows 8.1 and can not be used to reliably detect |
| 917 |
versions of Windows higher than Windows 8 (due to manifest requirements for reporting higher versions). |
| 918 |
Microsoft recommends switching to VerifyVersionInfo (available on Win 2k and later), however GetVersionEx |
| 919 |
is faster, for now we just disable the deprecation warning. |
| 920 |
See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).aspx |
| 921 |
See: http://www.codeproject.com/Articles/678606/Part-Overcoming-Windows-s-deprecation-of-GetVe |
| 922 |
*/ |
| 923 |
#pragma warning (disable : 4996) /* use of GetVersionEx */ |
| 924 |
|
| 925 |
OSVERSIONINFO osvi; |
| 926 |
osvi.dwOSVersionInfoSize = sizeof( osvi ); |
| 927 |
GetVersionEx( &osvi ); |
| 928 |
|
| 929 |
/* Check for NT */ |
| 930 |
if( (osvi.dwMajorVersion == 4) && (osvi.dwPlatformId == 2) ) |
| 931 |
{ |
| 932 |
*defaultLowLatency = PA_MME_WIN_NT_DEFAULT_LATENCY_; |
| 933 |
} |
| 934 |
else if(osvi.dwMajorVersion >= 5) |
| 935 |
{ |
| 936 |
*defaultLowLatency = PA_MME_WIN_WDM_DEFAULT_LATENCY_; |
| 937 |
} |
| 938 |
else |
| 939 |
{ |
| 940 |
*defaultLowLatency = PA_MME_WIN_9X_DEFAULT_LATENCY_; |
| 941 |
} |
| 942 |
|
| 943 |
*defaultHighLatency = *defaultLowLatency * 2; |
| 944 |
|
| 945 |
#pragma warning (default : 4996) |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
PaError PaWinMme_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex ) |
| 950 |
{ |
| 951 |
PaError result = paNoError; |
| 952 |
int i; |
| 953 |
PaWinMmeHostApiRepresentation *winMmeHostApi; |
| 954 |
int inputDeviceCount, outputDeviceCount, maximumPossibleDeviceCount; |
| 955 |
PaWinMmeDeviceInfo *deviceInfoArray; |
| 956 |
int deviceInfoInitializationSucceeded; |
| 957 |
PaTime defaultLowLatency, defaultHighLatency; |
| 958 |
DWORD waveInPreferredDevice, waveOutPreferredDevice; |
| 959 |
DWORD preferredDeviceStatusFlags; |
| 960 |
|
| 961 |
winMmeHostApi = (PaWinMmeHostApiRepresentation*)PaUtil_AllocateMemory( sizeof(PaWinMmeHostApiRepresentation) ); |
| 962 |
if( !winMmeHostApi ) |
| 963 |
{ |
| 964 |
result = paInsufficientMemory; |
| 965 |
goto error; |
| 966 |
} |
| 967 |
|
| 968 |
winMmeHostApi->allocations = PaUtil_CreateAllocationGroup(); |
| 969 |
if( !winMmeHostApi->allocations ) |
| 970 |
{ |
| 971 |
result = paInsufficientMemory; |
| 972 |
goto error; |
| 973 |
} |
| 974 |
|
| 975 |
*hostApi = &winMmeHostApi->inheritedHostApiRep; |
| 976 |
(*hostApi)->info.structVersion = 1; |
| 977 |
(*hostApi)->info.type = paMME; |
| 978 |
(*hostApi)->info.name = "MME"; |
| 979 |
|
| 980 |
|
| 981 |
/* initialise device counts and default devices under the assumption that |
| 982 |
there are no devices. These values are incremented below if and when |
| 983 |
devices are successfully initialized. |
| 984 |
*/ |
| 985 |
(*hostApi)->info.deviceCount = 0; |
| 986 |
(*hostApi)->info.defaultInputDevice = paNoDevice; |
| 987 |
(*hostApi)->info.defaultOutputDevice = paNoDevice; |
| 988 |
winMmeHostApi->inputDeviceCount = 0; |
| 989 |
winMmeHostApi->outputDeviceCount = 0; |
| 990 |
|
| 991 |
#if !defined(DRVM_MAPPER_PREFERRED_GET) |
| 992 |
/* DRVM_MAPPER_PREFERRED_GET is defined in mmddk.h but we avoid a dependency on the DDK by defining it here */ |
| 993 |
#define DRVM_MAPPER_PREFERRED_GET (0x2000+21) |
| 994 |
#endif |
| 995 |
|
| 996 |
/* the following calls assume that if wave*Message fails the preferred device parameter won't be modified */ |
| 997 |
preferredDeviceStatusFlags = 0; |
| 998 |
waveInPreferredDevice = -1; |
| 999 |
waveInMessage( (HWAVEIN)((UINT_PTR)WAVE_MAPPER), DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&waveInPreferredDevice, (DWORD_PTR)&preferredDeviceStatusFlags ); |
| 1000 |
|
| 1001 |
preferredDeviceStatusFlags = 0; |
| 1002 |
waveOutPreferredDevice = -1; |
| 1003 |
waveOutMessage( (HWAVEOUT)((UINT_PTR)WAVE_MAPPER), DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&waveOutPreferredDevice, (DWORD_PTR)&preferredDeviceStatusFlags ); |
| 1004 |
|
| 1005 |
maximumPossibleDeviceCount = 0; |
| 1006 |
|
| 1007 |
inputDeviceCount = waveInGetNumDevs(); |
| 1008 |
if( inputDeviceCount > 0 ) |
| 1009 |
maximumPossibleDeviceCount += inputDeviceCount + 1; /* assume there is a WAVE_MAPPER */ |
| 1010 |
|
| 1011 |
outputDeviceCount = waveOutGetNumDevs(); |
| 1012 |
if( outputDeviceCount > 0 ) |
| 1013 |
maximumPossibleDeviceCount += outputDeviceCount + 1; /* assume there is a WAVE_MAPPER */ |
| 1014 |
|
| 1015 |
|
| 1016 |
if( maximumPossibleDeviceCount > 0 ){ |
| 1017 |
|
| 1018 |
(*hostApi)->deviceInfos = (PaDeviceInfo**)PaUtil_GroupAllocateMemory( |
| 1019 |
winMmeHostApi->allocations, sizeof(PaDeviceInfo*) * maximumPossibleDeviceCount ); |
| 1020 |
if( !(*hostApi)->deviceInfos ) |
| 1021 |
{ |
| 1022 |
result = paInsufficientMemory; |
| 1023 |
goto error; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/* allocate all device info structs in a contiguous block */ |
| 1027 |
deviceInfoArray = (PaWinMmeDeviceInfo*)PaUtil_GroupAllocateMemory( |
| 1028 |
winMmeHostApi->allocations, sizeof(PaWinMmeDeviceInfo) * maximumPossibleDeviceCount ); |
| 1029 |
if( !deviceInfoArray ) |
| 1030 |
{ |
| 1031 |
result = paInsufficientMemory; |
| 1032 |
goto error; |
| 1033 |
} |
| 1034 |
|
| 1035 |
winMmeHostApi->winMmeDeviceIds = (UINT*)PaUtil_GroupAllocateMemory( |
| 1036 |
winMmeHostApi->allocations, sizeof(int) * maximumPossibleDeviceCount ); |
| 1037 |
if( !winMmeHostApi->winMmeDeviceIds ) |
| 1038 |
{ |
| 1039 |
result = paInsufficientMemory; |
| 1040 |
goto error; |
| 1041 |
} |
| 1042 |
|
| 1043 |
GetDefaultLatencies( &defaultLowLatency, &defaultHighLatency ); |
| 1044 |
|
| 1045 |
if( inputDeviceCount > 0 ){ |
| 1046 |
/* -1 is the WAVE_MAPPER */ |
| 1047 |
for( i = -1; i < inputDeviceCount; ++i ){ |
| 1048 |
UINT winMmeDeviceId = (UINT)((i==-1) ? WAVE_MAPPER : i); |
| 1049 |
PaWinMmeDeviceInfo *wmmeDeviceInfo = &deviceInfoArray[ (*hostApi)->info.deviceCount ]; |
| 1050 |
PaDeviceInfo *deviceInfo = &wmmeDeviceInfo->inheritedDeviceInfo; |
| 1051 |
deviceInfo->structVersion = 2; |
| 1052 |
deviceInfo->hostApi = hostApiIndex; |
| 1053 |
|
| 1054 |
deviceInfo->maxInputChannels = 0; |
| 1055 |
wmmeDeviceInfo->deviceInputChannelCountIsKnown = 1; |
| 1056 |
deviceInfo->maxOutputChannels = 0; |
| 1057 |
wmmeDeviceInfo->deviceOutputChannelCountIsKnown = 1; |
| 1058 |
|
| 1059 |
deviceInfo->defaultLowInputLatency = defaultLowLatency; |
| 1060 |
deviceInfo->defaultLowOutputLatency = defaultLowLatency; |
| 1061 |
deviceInfo->defaultHighInputLatency = defaultHighLatency; |
| 1062 |
deviceInfo->defaultHighOutputLatency = defaultHighLatency; |
| 1063 |
|
| 1064 |
result = InitializeInputDeviceInfo( winMmeHostApi, wmmeDeviceInfo, |
| 1065 |
winMmeDeviceId, &deviceInfoInitializationSucceeded ); |
| 1066 |
if( result != paNoError ) |
| 1067 |
goto error; |
| 1068 |
|
| 1069 |
if( deviceInfoInitializationSucceeded ){ |
| 1070 |
if( (*hostApi)->info.defaultInputDevice == paNoDevice ){ |
| 1071 |
/* if there is currently no default device, use the first one available */ |
| 1072 |
(*hostApi)->info.defaultInputDevice = (*hostApi)->info.deviceCount; |
| 1073 |
|
| 1074 |
}else if( winMmeDeviceId == waveInPreferredDevice ){ |
| 1075 |
/* set the default device to the system preferred device */ |
| 1076 |
(*hostApi)->info.defaultInputDevice = (*hostApi)->info.deviceCount; |
| 1077 |
} |
| 1078 |
|
| 1079 |
winMmeHostApi->winMmeDeviceIds[ (*hostApi)->info.deviceCount ] = winMmeDeviceId; |
| 1080 |
(*hostApi)->deviceInfos[ (*hostApi)->info.deviceCount ] = deviceInfo; |
| 1081 |
|
| 1082 |
winMmeHostApi->inputDeviceCount++; |
| 1083 |
(*hostApi)->info.deviceCount++; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
if( outputDeviceCount > 0 ){ |
| 1089 |
/* -1 is the WAVE_MAPPER */ |
| 1090 |
for( i = -1; i < outputDeviceCount; ++i ){ |
| 1091 |
UINT winMmeDeviceId = (UINT)((i==-1) ? WAVE_MAPPER : i); |
| 1092 |
PaWinMmeDeviceInfo *wmmeDeviceInfo = &deviceInfoArray[ (*hostApi)->info.deviceCount ]; |
| 1093 |
PaDeviceInfo *deviceInfo = &wmmeDeviceInfo->inheritedDeviceInfo; |
| 1094 |
deviceInfo->structVersion = 2; |
| 1095 |
deviceInfo->hostApi = hostApiIndex; |
| 1096 |
|
| 1097 |
deviceInfo->maxInputChannels = 0; |
| 1098 |
wmmeDeviceInfo->deviceInputChannelCountIsKnown = 1; |
| 1099 |
deviceInfo->maxOutputChannels = 0; |
| 1100 |
wmmeDeviceInfo->deviceOutputChannelCountIsKnown = 1; |
| 1101 |
|
| 1102 |
deviceInfo->defaultLowInputLatency = defaultLowLatency; |
| 1103 |
deviceInfo->defaultLowOutputLatency = defaultLowLatency; |
| 1104 |
deviceInfo->defaultHighInputLatency = defaultHighLatency; |
| 1105 |
deviceInfo->defaultHighOutputLatency = defaultHighLatency; |
| 1106 |
|
| 1107 |
result = InitializeOutputDeviceInfo( winMmeHostApi, wmmeDeviceInfo, |
| 1108 |
winMmeDeviceId, &deviceInfoInitializationSucceeded ); |
| 1109 |
if( result != paNoError ) |
| 1110 |
goto error; |
| 1111 |
|
| 1112 |
if( deviceInfoInitializationSucceeded ){ |
| 1113 |
if( (*hostApi)->info.defaultOutputDevice == paNoDevice ){ |
| 1114 |
/* if there is currently no default device, use the first one available */ |
| 1115 |
(*hostApi)->info.defaultOutputDevice = (*hostApi)->info.deviceCount; |
| 1116 |
|
| 1117 |
}else if( winMmeDeviceId == waveOutPreferredDevice ){ |
| 1118 |
/* set the default device to the system preferred device */ |
| 1119 |
(*hostApi)->info.defaultOutputDevice = (*hostApi)->info.deviceCount; |
| 1120 |
} |
| 1121 |
|
| 1122 |
winMmeHostApi->winMmeDeviceIds[ (*hostApi)->info.deviceCount ] = winMmeDeviceId; |
| 1123 |
(*hostApi)->deviceInfos[ (*hostApi)->info.deviceCount ] = deviceInfo; |
| 1124 |
|
| 1125 |
winMmeHostApi->outputDeviceCount++; |
| 1126 |
(*hostApi)->info.deviceCount++; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
InitializeDefaultDeviceIdsFromEnv( winMmeHostApi ); |
| 1133 |
|
| 1134 |
(*hostApi)->Terminate = Terminate; |
| 1135 |
(*hostApi)->OpenStream = OpenStream; |
| 1136 |
(*hostApi)->IsFormatSupported = IsFormatSupported; |
| 1137 |
|
| 1138 |
PaUtil_InitializeStreamInterface( &winMmeHostApi->callbackStreamInterface, CloseStream, StartStream, |
| 1139 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 1140 |
GetStreamTime, GetStreamCpuLoad, |
| 1141 |
PaUtil_DummyRead, PaUtil_DummyWrite, |
| 1142 |
PaUtil_DummyGetReadAvailable, PaUtil_DummyGetWriteAvailable ); |
| 1143 |
|
| 1144 |
PaUtil_InitializeStreamInterface( &winMmeHostApi->blockingStreamInterface, CloseStream, StartStream, |
| 1145 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 1146 |
GetStreamTime, PaUtil_DummyGetCpuLoad, |
| 1147 |
ReadStream, WriteStream, GetStreamReadAvailable, GetStreamWriteAvailable ); |
| 1148 |
|
| 1149 |
return result; |
| 1150 |
|
| 1151 |
error: |
| 1152 |
if( winMmeHostApi ) |
| 1153 |
{ |
| 1154 |
if( winMmeHostApi->allocations ) |
| 1155 |
{ |
| 1156 |
PaUtil_FreeAllAllocations( winMmeHostApi->allocations ); |
| 1157 |
PaUtil_DestroyAllocationGroup( winMmeHostApi->allocations ); |
| 1158 |
} |
| 1159 |
|
| 1160 |
PaUtil_FreeMemory( winMmeHostApi ); |
| 1161 |
} |
| 1162 |
|
| 1163 |
return result; |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ) |
| 1168 |
{ |
| 1169 |
PaWinMmeHostApiRepresentation *winMmeHostApi = (PaWinMmeHostApiRepresentation*)hostApi; |
| 1170 |
|
| 1171 |
if( winMmeHostApi->allocations ) |
| 1172 |
{ |
| 1173 |
PaUtil_FreeAllAllocations( winMmeHostApi->allocations ); |
| 1174 |
PaUtil_DestroyAllocationGroup( winMmeHostApi->allocations ); |
| 1175 |
} |
| 1176 |
|
| 1177 |
PaUtil_FreeMemory( winMmeHostApi ); |
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
static PaError IsInputChannelCountSupported( PaWinMmeDeviceInfo* deviceInfo, int channelCount ) |
| 1182 |
{ |
| 1183 |
PaError result = paNoError; |
| 1184 |
|
| 1185 |
if( channelCount > 0 |
| 1186 |
&& deviceInfo->deviceInputChannelCountIsKnown |
| 1187 |
&& channelCount > deviceInfo->inheritedDeviceInfo.maxInputChannels ){ |
| 1188 |
|
| 1189 |
result = paInvalidChannelCount; |
| 1190 |
} |
| 1191 |
|
| 1192 |
return result; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static PaError IsOutputChannelCountSupported( PaWinMmeDeviceInfo* deviceInfo, int channelCount ) |
| 1196 |
{ |
| 1197 |
PaError result = paNoError; |
| 1198 |
|
| 1199 |
if( channelCount > 0 |
| 1200 |
&& deviceInfo->deviceOutputChannelCountIsKnown |
| 1201 |
&& channelCount > deviceInfo->inheritedDeviceInfo.maxOutputChannels ){ |
| 1202 |
|
| 1203 |
result = paInvalidChannelCount; |
| 1204 |
} |
| 1205 |
|
| 1206 |
return result; |
| 1207 |
} |
| 1208 |
|
| 1209 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 1210 |
const PaStreamParameters *inputParameters, |
| 1211 |
const PaStreamParameters *outputParameters, |
| 1212 |
double sampleRate ) |
| 1213 |
{ |
| 1214 |
PaWinMmeHostApiRepresentation *winMmeHostApi = (PaWinMmeHostApiRepresentation*)hostApi; |
| 1215 |
PaDeviceInfo *inputDeviceInfo, *outputDeviceInfo; |
| 1216 |
int inputChannelCount, outputChannelCount; |
| 1217 |
int inputMultipleDeviceChannelCount, outputMultipleDeviceChannelCount; |
| 1218 |
PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 1219 |
PaWinMmeStreamInfo *inputStreamInfo, *outputStreamInfo; |
| 1220 |
UINT winMmeInputDeviceId, winMmeOutputDeviceId; |
| 1221 |
unsigned int i; |
| 1222 |
PaError paerror; |
| 1223 |
|
| 1224 |
/* The calls to QueryFormatSupported below are intended to detect invalid |
| 1225 |
sample rates. If we assume that the channel count and format are OK, |
| 1226 |
then the only thing that could fail is the sample rate. This isn't |
| 1227 |
strictly true, but I can't think of a better way to test that the |
| 1228 |
sample rate is valid. |
| 1229 |
*/ |
| 1230 |
|
| 1231 |
if( inputParameters ) |
| 1232 |
{ |
| 1233 |
inputChannelCount = inputParameters->channelCount; |
| 1234 |
inputSampleFormat = inputParameters->sampleFormat; |
| 1235 |
inputStreamInfo = inputParameters->hostApiSpecificStreamInfo; |
| 1236 |
|
| 1237 |
/* all standard sample formats are supported by the buffer adapter, |
| 1238 |
this implementation doesn't support any custom sample formats */ |
| 1239 |
if( inputSampleFormat & paCustomFormat ) |
| 1240 |
return paSampleFormatNotSupported; |
| 1241 |
|
| 1242 |
if( inputParameters->device == paUseHostApiSpecificDeviceSpecification |
| 1243 |
&& inputStreamInfo && (inputStreamInfo->flags & paWinMmeUseMultipleDevices) ) |
| 1244 |
{ |
| 1245 |
inputMultipleDeviceChannelCount = 0; |
| 1246 |
for( i=0; i< inputStreamInfo->deviceCount; ++i ) |
| 1247 |
{ |
| 1248 |
inputMultipleDeviceChannelCount += inputStreamInfo->devices[i].channelCount; |
| 1249 |
|
| 1250 |
inputDeviceInfo = hostApi->deviceInfos[ inputStreamInfo->devices[i].device ]; |
| 1251 |
|
| 1252 |
/* check that input device can support inputChannelCount */ |
| 1253 |
if( inputStreamInfo->devices[i].channelCount < 1 ) |
| 1254 |
return paInvalidChannelCount; |
| 1255 |
|
| 1256 |
paerror = IsInputChannelCountSupported( (PaWinMmeDeviceInfo*)inputDeviceInfo, |
| 1257 |
inputStreamInfo->devices[i].channelCount ); |
| 1258 |
if( paerror != paNoError ) |
| 1259 |
return paerror; |
| 1260 |
|
| 1261 |
/* test for valid sample rate, see comment above */ |
| 1262 |
winMmeInputDeviceId = LocalDeviceIndexToWinMmeDeviceId( winMmeHostApi, inputStreamInfo->devices[i].device ); |
| 1263 |
paerror = QueryFormatSupported( inputDeviceInfo, QueryInputWaveFormatEx, |
| 1264 |
winMmeInputDeviceId, inputStreamInfo->devices[i].channelCount, sampleRate, |
| 1265 |
((inputStreamInfo) ? inputStreamInfo->flags : 0) ); |
| 1266 |
if( paerror != paNoError ) |
| 1267 |
return paInvalidSampleRate; |
| 1268 |
} |
| 1269 |
|
| 1270 |
if( inputMultipleDeviceChannelCount != inputChannelCount ) |
| 1271 |
return paIncompatibleHostApiSpecificStreamInfo; |
| 1272 |
} |
| 1273 |
else |
| 1274 |
{ |
| 1275 |
if( inputStreamInfo && (inputStreamInfo->flags & paWinMmeUseMultipleDevices) ) |
| 1276 |
return paIncompatibleHostApiSpecificStreamInfo; /* paUseHostApiSpecificDeviceSpecification was not supplied as the input device */ |
| 1277 |
|
| 1278 |
inputDeviceInfo = hostApi->deviceInfos[ inputParameters->device ]; |
| 1279 |
|
| 1280 |
/* check that input device can support inputChannelCount */ |
| 1281 |
paerror = IsInputChannelCountSupported( (PaWinMmeDeviceInfo*)inputDeviceInfo, inputChannelCount ); |
| 1282 |
if( paerror != paNoError ) |
| 1283 |
return paerror; |
| 1284 |
|
| 1285 |
/* test for valid sample rate, see comment above */ |
| 1286 |
winMmeInputDeviceId = LocalDeviceIndexToWinMmeDeviceId( winMmeHostApi, inputParameters->device ); |
| 1287 |
paerror = QueryFormatSupported( inputDeviceInfo, QueryInputWaveFormatEx, |
| 1288 |
winMmeInputDeviceId, inputChannelCount, sampleRate, |
| 1289 |
((inputStreamInfo) ? inputStreamInfo->flags : 0) ); |
| 1290 |
if( paerror != paNoError ) |
| 1291 |
return paInvalidSampleRate; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
if( outputParameters ) |
| 1296 |
{ |
| 1297 |
outputChannelCount = outputParameters->channelCount; |
| 1298 |
outputSampleFormat = outputParameters->sampleFormat; |
| 1299 |
outputStreamInfo = outputParameters->hostApiSpecificStreamInfo; |
| 1300 |
|
| 1301 |
/* all standard sample formats are supported by the buffer adapter, |
| 1302 |
this implementation doesn't support any custom sample formats */ |
| 1303 |
if( outputSampleFormat & paCustomFormat ) |
| 1304 |
return paSampleFormatNotSupported; |
| 1305 |
|
| 1306 |
if( outputParameters->device == paUseHostApiSpecificDeviceSpecification |
| 1307 |
&& outputStreamInfo && (outputStreamInfo->flags & paWinMmeUseMultipleDevices) ) |
| 1308 |
{ |
| 1309 |
outputMultipleDeviceChannelCount = 0; |
| 1310 |
for( i=0; i< outputStreamInfo->deviceCount; ++i ) |
| 1311 |
{ |
| 1312 |
outputMultipleDeviceChannelCount += outputStreamInfo->devices[i].channelCount; |
| 1313 |
|
| 1314 |
outputDeviceInfo = hostApi->deviceInfos[ outputStreamInfo->devices[i].device ]; |
| 1315 |
|
| 1316 |
/* check that output device can support outputChannelCount */ |
| 1317 |
if( outputStreamInfo->devices[i].channelCount < 1 ) |
| 1318 |
return paInvalidChannelCount; |
| 1319 |
|
| 1320 |
paerror = IsOutputChannelCountSupported( (PaWinMmeDeviceInfo*)outputDeviceInfo, |
| 1321 |
outputStreamInfo->devices[i].channelCount ); |
| 1322 |
if( paerror != paNoError ) |
| 1323 |
return paerror; |
| 1324 |
|
| 1325 |
/* test for valid sample rate, see comment above */ |
| 1326 |
winMmeOutputDeviceId = LocalDeviceIndexToWinMmeDeviceId( winMmeHostApi, outputStreamInfo->devices[i].device ); |
| 1327 |
paerror = QueryFormatSupported( outputDeviceInfo, QueryOutputWaveFormatEx, |
| 1328 |
winMmeOutputDeviceId, outputStreamInfo->devices[i].channelCount, sampleRate, |
| 1329 |
((outputStreamInfo) ? outputStreamInfo->flags : 0) ); |
| 1330 |
if( paerror != paNoError ) |
| 1331 |
return paInvalidSampleRate; |
| 1332 |
} |
| 1333 |
|
| 1334 |
if( outputMultipleDeviceChannelCount != outputChannelCount ) |
| 1335 |
return paIncompatibleHostApiSpecificStreamInfo; |
| 1336 |
} |
| 1337 |
else |
| 1338 |
{ |
| 1339 |
if( outputStreamInfo && (outputStreamInfo->flags & paWinMmeUseMultipleDevices) ) |
| 1340 |
return paIncompatibleHostApiSpecificStreamInfo; /* paUseHostApiSpecificDeviceSpecification was not supplied as the output device */ |
| 1341 |
|
| 1342 |
outputDeviceInfo = hostApi->deviceInfos[ outputParameters->device ]; |
| 1343 |
|
| 1344 |
/* check that output device can support outputChannelCount */ |
| 1345 |
paerror = IsOutputChannelCountSupported( (PaWinMmeDeviceInfo*)outputDeviceInfo, outputChannelCount ); |
| 1346 |
if( paerror != paNoError ) |
| 1347 |
return paerror; |
| 1348 |
|
| 1349 |
/* test for valid sample rate, see comment above */ |
| 1350 |
winMmeOutputDeviceId = LocalDeviceIndexToWinMmeDeviceId( winMmeHostApi, outputParameters->device ); |
| 1351 |
paerror = QueryFormatSupported( outputDeviceInfo, QueryOutputWaveFormatEx, |
| 1352 |
winMmeOutputDeviceId, outputChannelCount, sampleRate, |
| 1353 |
((outputStreamInfo) ? outputStreamInfo->flags : 0) ); |
| 1354 |
if( paerror != paNoError ) |
| 1355 |
return paInvalidSampleRate; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
/* |
| 1360 |
- if a full duplex stream is requested, check that the combination |
| 1361 |
of input and output parameters is supported |
| 1362 |
|
| 1363 |
- check that the device supports sampleRate |
| 1364 |
|
| 1365 |
for mme all we can do is test that the input and output devices |
| 1366 |
support the requested sample rate and number of channels. we |
| 1367 |
cannot test for full duplex compatibility. |
| 1368 |
*/ |
| 1369 |
|
| 1370 |
return paFormatIsSupported; |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
static unsigned long ComputeHostBufferCountForFixedBufferSizeFrames( |
| 1375 |
unsigned long suggestedLatencyFrames, |
| 1376 |
unsigned long hostBufferSizeFrames, |
| 1377 |
unsigned long minimumBufferCount ) |
| 1378 |
{ |
| 1379 |
/* Calculate the number of buffers of length hostFramesPerBuffer |
| 1380 |
that fit in suggestedLatencyFrames, rounding up to the next integer. |
| 1381 |
|
| 1382 |
The value (hostBufferSizeFrames - 1) below is to ensure the buffer count is rounded up. |
| 1383 |
*/ |
| 1384 |
unsigned long resultBufferCount = ((suggestedLatencyFrames + (hostBufferSizeFrames - 1)) / hostBufferSizeFrames); |
| 1385 |
|
| 1386 |
/* We always need one extra buffer for processing while the rest are queued/playing. |
| 1387 |
i.e. latency is framesPerBuffer * (bufferCount - 1) |
| 1388 |
*/ |
| 1389 |
resultBufferCount += 1; |
| 1390 |
|
| 1391 |
if( resultBufferCount < minimumBufferCount ) /* clamp to minimum buffer count */ |
| 1392 |
resultBufferCount = minimumBufferCount; |
| 1393 |
|
| 1394 |
return resultBufferCount; |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
static unsigned long ComputeHostBufferSizeGivenHardUpperLimit( |
| 1399 |
unsigned long userFramesPerBuffer, |
| 1400 |
unsigned long absoluteMaximumBufferSizeFrames ) |
| 1401 |
{ |
| 1402 |
static unsigned long primes_[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, |
| 1403 |
29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 0 }; /* zero terminated */ |
| 1404 |
|
| 1405 |
unsigned long result = userFramesPerBuffer; |
| 1406 |
int i; |
| 1407 |
|
| 1408 |
assert( absoluteMaximumBufferSizeFrames > 67 ); /* assume maximum is large and we're only factoring by small primes */ |
| 1409 |
|
| 1410 |
/* search for the largest integer factor of userFramesPerBuffer less |
| 1411 |
than or equal to absoluteMaximumBufferSizeFrames */ |
| 1412 |
|
| 1413 |
/* repeatedly divide by smallest prime factors until a buffer size |
| 1414 |
smaller than absoluteMaximumBufferSizeFrames is found */ |
| 1415 |
while( result > absoluteMaximumBufferSizeFrames ){ |
| 1416 |
|
| 1417 |
/* search for the smallest prime factor of result */ |
| 1418 |
for( i=0; primes_[i] != 0; ++i ) |
| 1419 |
{ |
| 1420 |
unsigned long p = primes_[i]; |
| 1421 |
unsigned long divided = result / p; |
| 1422 |
if( divided*p == result ) |
| 1423 |
{ |
| 1424 |
result = divided; |
| 1425 |
break; /* continue with outer while loop */ |
| 1426 |
} |
| 1427 |
} |
| 1428 |
if( primes_[i] == 0 ) |
| 1429 |
{ /* loop failed to find a prime factor, return an approximate result */ |
| 1430 |
unsigned long d = (userFramesPerBuffer + (absoluteMaximumBufferSizeFrames-1)) |
| 1431 |
/ absoluteMaximumBufferSizeFrames; |
| 1432 |
return userFramesPerBuffer / d; |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
return result; |
| 1437 |
} |
| 1438 |
|
| 1439 |
|
| 1440 |
static PaError SelectHostBufferSizeFramesAndHostBufferCount( |
| 1441 |
unsigned long suggestedLatencyFrames, |
| 1442 |
unsigned long userFramesPerBuffer, |
| 1443 |
unsigned long minimumBufferCount, |
| 1444 |
unsigned long preferredMaximumBufferSizeFrames, /* try not to exceed this. for example, don't exceed when coalescing buffers */ |
| 1445 |
unsigned long absoluteMaximumBufferSizeFrames, /* never exceed this, a hard limit */ |
| 1446 |
unsigned long *hostBufferSizeFrames, |
| 1447 |
unsigned long *hostBufferCount ) |
| 1448 |
{ |
| 1449 |
unsigned long effectiveUserFramesPerBuffer; |
| 1450 |
unsigned long numberOfUserBuffersPerHostBuffer; |
| 1451 |
|
| 1452 |
|
| 1453 |
if( userFramesPerBuffer == paFramesPerBufferUnspecified ){ |
| 1454 |
|
| 1455 |
effectiveUserFramesPerBuffer = PA_MME_HOST_BUFFER_GRANULARITY_FRAMES_WHEN_UNSPECIFIED_; |
| 1456 |
|
| 1457 |
}else{ |
| 1458 |
|
| 1459 |
if( userFramesPerBuffer > absoluteMaximumBufferSizeFrames ){ |
| 1460 |
|
| 1461 |
/* user has requested a user buffer that's larger than absoluteMaximumBufferSizeFrames. |
| 1462 |
try to choose a buffer size that is equal or smaller than absoluteMaximumBufferSizeFrames |
| 1463 |
but is also an integer factor of userFramesPerBuffer, so as to distribute computation evenly. |
| 1464 |
the buffer processor will handle the block adaption between host and user buffer sizes. |
| 1465 |
see http://www.portaudio.com/trac/ticket/189 for discussion. |
| 1466 |
*/ |
| 1467 |
|
| 1468 |
effectiveUserFramesPerBuffer = ComputeHostBufferSizeGivenHardUpperLimit( userFramesPerBuffer, absoluteMaximumBufferSizeFrames ); |
| 1469 |
assert( effectiveUserFramesPerBuffer <= absoluteMaximumBufferSizeFrames ); |
| 1470 |
|
| 1471 |
/* try to ensure that duration of host buffering is at least as |
| 1472 |
large as duration of user buffer. */ |
| 1473 |
if( suggestedLatencyFrames < userFramesPerBuffer ) |
| 1474 |
suggestedLatencyFrames = userFramesPerBuffer; |
| 1475 |
|
| 1476 |
}else{ |
| 1477 |
|
| 1478 |
effectiveUserFramesPerBuffer = userFramesPerBuffer; |
| 1479 |
} |
| 1480 |
} |
| 1481 |
|
| 1482 |
/* compute a host buffer count based on suggestedLatencyFrames and our granularity */ |
| 1483 |
|
| 1484 |
*hostBufferSizeFrames = effectiveUserFramesPerBuffer; |
| 1485 |
|
| 1486 |
*hostBufferCount = ComputeHostBufferCountForFixedBufferSizeFrames( |
| 1487 |
suggestedLatencyFrames, *hostBufferSizeFrames, minimumBufferCount ); |
| 1488 |
|
| 1489 |
if( *hostBufferSizeFrames >= userFramesPerBuffer ) |
| 1490 |
{ |
| 1491 |
/* |
| 1492 |
If there are too many host buffers we would like to coalesce |
| 1493 |
them by packing an integer number of user buffers into each host buffer. |
| 1494 |
We try to coalesce such that hostBufferCount will lie between |
| 1495 |
PA_MME_TARGET_HOST_BUFFER_COUNT_ and (PA_MME_TARGET_HOST_BUFFER_COUNT_*2)-1. |
| 1496 |
We limit coalescing to avoid exceeding either absoluteMaximumBufferSizeFrames and |
| 1497 |
preferredMaximumBufferSizeFrames. |
| 1498 |
|
| 1499 |
First, compute a coalescing factor: the number of user buffers per host buffer. |
| 1500 |
The goal is to achieve PA_MME_TARGET_HOST_BUFFER_COUNT_ total buffer count. |
| 1501 |
Since our latency is computed based on (*hostBufferCount - 1) we compute a |
| 1502 |
coalescing factor based on (*hostBufferCount - 1) and (PA_MME_TARGET_HOST_BUFFER_COUNT_-1). |
| 1503 |
|
| 1504 |
The + (PA_MME_TARGET_HOST_BUFFER_COUNT_-2) term below is intended to round up. |
| 1505 |
*/ |
| 1506 |
numberOfUserBuffersPerHostBuffer = ((*hostBufferCount - 1) + (PA_MME_TARGET_HOST_BUFFER_COUNT_-2)) / (PA_MME_TARGET_HOST_BUFFER_COUNT_ - 1); |
| 1507 |
|
| 1508 |
if( numberOfUserBuffersPerHostBuffer > 1 ) |
| 1509 |
{ |
| 1510 |
unsigned long maxCoalescedBufferSizeFrames = (absoluteMaximumBufferSizeFrames < preferredMaximumBufferSizeFrames) /* minimum of our limits */ |
| 1511 |
? absoluteMaximumBufferSizeFrames |
| 1512 |
: preferredMaximumBufferSizeFrames; |
| 1513 |
|
| 1514 |
unsigned long maxUserBuffersPerHostBuffer = maxCoalescedBufferSizeFrames / effectiveUserFramesPerBuffer; /* don't coalesce more than this */ |
| 1515 |
|
| 1516 |
if( numberOfUserBuffersPerHostBuffer > maxUserBuffersPerHostBuffer ) |
| 1517 |
numberOfUserBuffersPerHostBuffer = maxUserBuffersPerHostBuffer; |
| 1518 |
|
| 1519 |
*hostBufferSizeFrames = effectiveUserFramesPerBuffer * numberOfUserBuffersPerHostBuffer; |
| 1520 |
|
| 1521 |
/* recompute hostBufferCount to approximate suggestedLatencyFrames now that hostBufferSizeFrames is larger */ |
| 1522 |
*hostBufferCount = ComputeHostBufferCountForFixedBufferSizeFrames( |
| 1523 |
suggestedLatencyFrames, *hostBufferSizeFrames, minimumBufferCount ); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
return paNoError; |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
static PaError CalculateMaxHostSampleFrameSizeBytes( |
| 1532 |
int channelCount, |
| 1533 |
PaSampleFormat hostSampleFormat, |
| 1534 |
const PaWinMmeStreamInfo *streamInfo, |
| 1535 |
int *hostSampleFrameSizeBytes ) |
| 1536 |
{ |
| 1537 |
unsigned int i; |
| 1538 |
/* PA WMME streams may aggregate multiple WMME devices. When the stream addresses |
| 1539 |
more than one device in a single direction, maxDeviceChannelCount is the maximum |
| 1540 |
number of channels used by a single device. |
| 1541 |
*/ |
| 1542 |
int maxDeviceChannelCount = channelCount; |
| 1543 |
int hostSampleSizeBytes = Pa_GetSampleSize( hostSampleFormat ); |
| 1544 |
if( hostSampleSizeBytes < 0 ) |
| 1545 |
{ |
| 1546 |
return hostSampleSizeBytes; /* the value of hostSampleSize here is an error code, not a sample size */ |
| 1547 |
} |
| 1548 |
|
| 1549 |
if( streamInfo && ( streamInfo->flags & paWinMmeUseMultipleDevices ) ) |
| 1550 |
{ |
| 1551 |
maxDeviceChannelCount = streamInfo->devices[0].channelCount; |
| 1552 |
for( i=1; i< streamInfo->deviceCount; ++i ) |
| 1553 |
{ |
| 1554 |
if( streamInfo->devices[i].channelCount > maxDeviceChannelCount ) |
| 1555 |
maxDeviceChannelCount = streamInfo->devices[i].channelCount; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
*hostSampleFrameSizeBytes = hostSampleSizeBytes * maxDeviceChannelCount; |
| 1560 |
|
| 1561 |
return paNoError; |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
/* CalculateBufferSettings() fills the framesPerHostInputBuffer, hostInputBufferCount, |
| 1566 |
framesPerHostOutputBuffer and hostOutputBufferCount parameters based on the values |
| 1567 |
of the other parameters. |
| 1568 |
*/ |
| 1569 |
|
| 1570 |
static PaError CalculateBufferSettings( |
| 1571 |
unsigned long *hostFramesPerInputBuffer, unsigned long *hostInputBufferCount, |
| 1572 |
unsigned long *hostFramesPerOutputBuffer, unsigned long *hostOutputBufferCount, |
| 1573 |
int inputChannelCount, PaSampleFormat hostInputSampleFormat, |
| 1574 |
PaTime suggestedInputLatency, const PaWinMmeStreamInfo *inputStreamInfo, |
| 1575 |
int outputChannelCount, PaSampleFormat hostOutputSampleFormat, |
| 1576 |
PaTime suggestedOutputLatency, const PaWinMmeStreamInfo *outputStreamInfo, |
| 1577 |
double sampleRate, unsigned long userFramesPerBuffer ) |
| 1578 |
{ |
| 1579 |
PaError result = paNoError; |
| 1580 |
|
| 1581 |
if( inputChannelCount > 0 ) /* stream has input */ |
| 1582 |
{ |
| 1583 |
int hostInputFrameSizeBytes; |
| 1584 |
result = CalculateMaxHostSampleFrameSizeBytes( |
| 1585 |
inputChannelCount, hostInputSampleFormat, inputStreamInfo, &hostInputFrameSizeBytes ); |
| 1586 |
if( result != paNoError ) |
| 1587 |
goto error; |
| 1588 |
|
| 1589 |
if( inputStreamInfo |
| 1590 |
&& ( inputStreamInfo->flags & paWinMmeUseLowLevelLatencyParameters ) ) |
| 1591 |
{ |
| 1592 |
/* input - using low level latency parameters if provided */ |
| 1593 |
|
| 1594 |
if( inputStreamInfo->bufferCount <= 0 |
| 1595 |
|| inputStreamInfo->framesPerBuffer <= 0 ) |
| 1596 |
{ |
| 1597 |
result = paIncompatibleHostApiSpecificStreamInfo; |
| 1598 |
goto error; |
| 1599 |
} |
| 1600 |
|
| 1601 |
*hostFramesPerInputBuffer = inputStreamInfo->framesPerBuffer; |
| 1602 |
*hostInputBufferCount = inputStreamInfo->bufferCount; |
| 1603 |
} |
| 1604 |
else |
| 1605 |
{ |
| 1606 |
/* input - not using low level latency parameters, so compute |
| 1607 |
hostFramesPerInputBuffer and hostInputBufferCount |
| 1608 |
based on userFramesPerBuffer and suggestedInputLatency. */ |
| 1609 |
|
| 1610 |
unsigned long minimumBufferCount = (outputChannelCount > 0) |
| 1611 |
? PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_FULL_DUPLEX_ |
| 1612 |
: PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_HALF_DUPLEX_; |
| 1613 |
|
| 1614 |
result = SelectHostBufferSizeFramesAndHostBufferCount( |
| 1615 |
(unsigned long)(suggestedInputLatency * sampleRate), /* (truncate) */ |
| 1616 |
userFramesPerBuffer, |
| 1617 |
minimumBufferCount, |
| 1618 |
(unsigned long)(PA_MME_MAX_HOST_BUFFER_SECS_ * sampleRate), /* in frames. preferred maximum */ |
| 1619 |
(PA_MME_MAX_HOST_BUFFER_BYTES_ / hostInputFrameSizeBytes), /* in frames. a hard limit. note truncation due to |
| 1620 |
division is intentional here to limit max bytes */ |
| 1621 |
hostFramesPerInputBuffer, |
| 1622 |
hostInputBufferCount ); |
| 1623 |
if( result != paNoError ) |
| 1624 |
goto error; |
| 1625 |
} |
| 1626 |
} |
| 1627 |
else |
| 1628 |
{ |
| 1629 |
*hostFramesPerInputBuffer = 0; |
| 1630 |
*hostInputBufferCount = 0; |
| 1631 |
} |
| 1632 |
|
| 1633 |
if( outputChannelCount > 0 ) /* stream has output */ |
| 1634 |
{ |
| 1635 |
if( outputStreamInfo |
| 1636 |
&& ( outputStreamInfo->flags & paWinMmeUseLowLevelLatencyParameters ) ) |
| 1637 |
{ |
| 1638 |
/* output - using low level latency parameters */ |
| 1639 |
|
| 1640 |
if( outputStreamInfo->bufferCount <= 0 |
| 1641 |
|| outputStreamInfo->framesPerBuffer <= 0 ) |
| 1642 |
{ |
| 1643 |
result = paIncompatibleHostApiSpecificStreamInfo; |
| 1644 |
goto error; |
| 1645 |
} |
| 1646 |
|
| 1647 |
*hostFramesPerOutputBuffer = outputStreamInfo->framesPerBuffer; |
| 1648 |
*hostOutputBufferCount = outputStreamInfo->bufferCount; |
| 1649 |
|
| 1650 |
if( inputChannelCount > 0 ) /* full duplex */ |
| 1651 |
{ |
| 1652 |
/* harmonize hostFramesPerInputBuffer and hostFramesPerOutputBuffer */ |
| 1653 |
|
| 1654 |
if( *hostFramesPerInputBuffer != *hostFramesPerOutputBuffer ) |
| 1655 |
{ |
| 1656 |
if( inputStreamInfo |
| 1657 |
&& ( inputStreamInfo->flags & paWinMmeUseLowLevelLatencyParameters ) ) |
| 1658 |
{ |
| 1659 |
/* a custom StreamInfo was used for specifying both input |
| 1660 |
and output buffer sizes. We require that the larger buffer size |
| 1661 |
must be a multiple of the smaller buffer size */ |
| 1662 |
|
| 1663 |
if( *hostFramesPerInputBuffer < *hostFramesPerOutputBuffer ) |
| 1664 |
{ |
| 1665 |
if( *hostFramesPerOutputBuffer % *hostFramesPerInputBuffer != 0 ) |
| 1666 |
{ |
| 1667 |
result = paIncompatibleHostApiSpecificStreamInfo; |
| 1668 |
goto error; |
| 1669 |
} |
| 1670 |
} |
| 1671 |
else |
| 1672 |
{ |
| 1673 |
assert( *hostFramesPerInputBuffer > *hostFramesPerOutputBuffer ); |
| 1674 |
if( *hostFramesPerInputBuffer % *hostFramesPerOutputBuffer != 0 ) |
| 1675 |
{ |
| 1676 |
result = paIncompatibleHostApiSpecificStreamInfo; |
| 1677 |
goto error; |
| 1678 |
} |
| 1679 |
} |
| 1680 |
} |
| 1681 |
else |
| 1682 |
{ |
| 1683 |
/* a custom StreamInfo was not used for specifying the input buffer size, |
| 1684 |
so use the output buffer size, and approximately the suggested input latency. */ |
| 1685 |
|
| 1686 |
*hostFramesPerInputBuffer = *hostFramesPerOutputBuffer; |
| 1687 |
|
| 1688 |
*hostInputBufferCount = ComputeHostBufferCountForFixedBufferSizeFrames( |
| 1689 |
(unsigned long)(suggestedInputLatency * sampleRate), |
| 1690 |
*hostFramesPerInputBuffer, |
| 1691 |
PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_FULL_DUPLEX_ ); |
| 1692 |
} |
| 1693 |
} |
| 1694 |
} |
| 1695 |
} |
| 1696 |
else |
| 1697 |
{ |
| 1698 |
/* output - no low level latency parameters, so compute hostFramesPerOutputBuffer and hostOutputBufferCount |
| 1699 |
based on userFramesPerBuffer and suggestedOutputLatency. */ |
| 1700 |
|
| 1701 |
int hostOutputFrameSizeBytes; |
| 1702 |
result = CalculateMaxHostSampleFrameSizeBytes( |
| 1703 |
outputChannelCount, hostOutputSampleFormat, outputStreamInfo, &hostOutputFrameSizeBytes ); |
| 1704 |
if( result != paNoError ) |
| 1705 |
goto error; |
| 1706 |
|
| 1707 |
/* compute the output buffer size and count */ |
| 1708 |
|
| 1709 |
result = SelectHostBufferSizeFramesAndHostBufferCount( |
| 1710 |
(unsigned long)(suggestedOutputLatency * sampleRate), /* (truncate) */ |
| 1711 |
userFramesPerBuffer, |
| 1712 |
PA_MME_MIN_HOST_OUTPUT_BUFFER_COUNT_, |
| 1713 |
(unsigned long)(PA_MME_MAX_HOST_BUFFER_SECS_ * sampleRate), /* in frames. preferred maximum */ |
| 1714 |
(PA_MME_MAX_HOST_BUFFER_BYTES_ / hostOutputFrameSizeBytes), /* in frames. a hard limit. note truncation due to |
| 1715 |
division is intentional here to limit max bytes */ |
| 1716 |
hostFramesPerOutputBuffer, |
| 1717 |
hostOutputBufferCount ); |
| 1718 |
if( result != paNoError ) |
| 1719 |
goto error; |
| 1720 |
|
| 1721 |
if( inputChannelCount > 0 ) /* full duplex */ |
| 1722 |
{ |
| 1723 |
/* harmonize hostFramesPerInputBuffer and hostFramesPerOutputBuffer */ |
| 1724 |
|
| 1725 |
/* ensure that both input and output buffer sizes are the same. |
| 1726 |
if they don't match at this stage, choose the smallest one |
| 1727 |
and use that for input and output and recompute the corresponding |
| 1728 |
buffer count accordingly. |
| 1729 |
*/ |
| 1730 |
|
| 1731 |
if( *hostFramesPerOutputBuffer != *hostFramesPerInputBuffer ) |
| 1732 |
{ |
| 1733 |
if( hostFramesPerInputBuffer < hostFramesPerOutputBuffer ) |
| 1734 |
{ |
| 1735 |
*hostFramesPerOutputBuffer = *hostFramesPerInputBuffer; |
| 1736 |
|
| 1737 |
*hostOutputBufferCount = ComputeHostBufferCountForFixedBufferSizeFrames( |
| 1738 |
(unsigned long)(suggestedOutputLatency * sampleRate), |
| 1739 |
*hostOutputBufferCount, |
| 1740 |
PA_MME_MIN_HOST_OUTPUT_BUFFER_COUNT_ ); |
| 1741 |
} |
| 1742 |
else |
| 1743 |
{ |
| 1744 |
*hostFramesPerInputBuffer = *hostFramesPerOutputBuffer; |
| 1745 |
|
| 1746 |
*hostInputBufferCount = ComputeHostBufferCountForFixedBufferSizeFrames( |
| 1747 |
(unsigned long)(suggestedInputLatency * sampleRate), |
| 1748 |
*hostFramesPerInputBuffer, |
| 1749 |
PA_MME_MIN_HOST_INPUT_BUFFER_COUNT_FULL_DUPLEX_ ); |
| 1750 |
} |
| 1751 |
} |
| 1752 |
} |
| 1753 |
} |
| 1754 |
} |
| 1755 |
else |
| 1756 |
{ |
| 1757 |
*hostFramesPerOutputBuffer = 0; |
| 1758 |
*hostOutputBufferCount = 0; |
| 1759 |
} |
| 1760 |
|
| 1761 |
error: |
| 1762 |
return result; |
| 1763 |
} |
| 1764 |
|
| 1765 |
|
| 1766 |
typedef struct |
| 1767 |
{ |
| 1768 |
HANDLE bufferEvent; |
| 1769 |
void *waveHandles; |
| 1770 |
unsigned int deviceCount; |
| 1771 |
/* unsigned int channelCount; */ |
| 1772 |
WAVEHDR **waveHeaders; /* waveHeaders[device][buffer] */ |
| 1773 |
unsigned int bufferCount; |
| 1774 |
unsigned int currentBufferIndex; |
| 1775 |
unsigned int framesPerBuffer; |
| 1776 |
unsigned int framesUsedInCurrentBuffer; |
| 1777 |
}PaWinMmeSingleDirectionHandlesAndBuffers; |
| 1778 |
|
| 1779 |
/* prototypes for functions operating on PaWinMmeSingleDirectionHandlesAndBuffers */ |
| 1780 |
|
| 1781 |
static void InitializeSingleDirectionHandlesAndBuffers( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers ); |
| 1782 |
static PaError InitializeWaveHandles( PaWinMmeHostApiRepresentation *winMmeHostApi, |
| 1783 |
PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, |
| 1784 |
unsigned long winMmeSpecificFlags, |
| 1785 |
unsigned long bytesPerHostSample, |
| 1786 |
double sampleRate, PaWinMmeDeviceAndChannelCount *devices, |
| 1787 |
unsigned int deviceCount, PaWinWaveFormatChannelMask channelMask, int isInput ); |
| 1788 |
static PaError TerminateWaveHandles( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, int isInput, int currentlyProcessingAnError ); |
| 1789 |
static PaError InitializeWaveHeaders( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, |
| 1790 |
unsigned long hostBufferCount, |
| 1791 |
PaSampleFormat hostSampleFormat, |
| 1792 |
unsigned long framesPerHostBuffer, |
| 1793 |
PaWinMmeDeviceAndChannelCount *devices, |
| 1794 |
int isInput ); |
| 1795 |
static void TerminateWaveHeaders( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, int isInput ); |
| 1796 |
|
| 1797 |
|
| 1798 |
static void InitializeSingleDirectionHandlesAndBuffers( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers ) |
| 1799 |
{ |
| 1800 |
handlesAndBuffers->bufferEvent = 0; |
| 1801 |
handlesAndBuffers->waveHandles = 0; |
| 1802 |
handlesAndBuffers->deviceCount = 0; |
| 1803 |
handlesAndBuffers->waveHeaders = 0; |
| 1804 |
handlesAndBuffers->bufferCount = 0; |
| 1805 |
} |
| 1806 |
|
| 1807 |
static PaError InitializeWaveHandles( PaWinMmeHostApiRepresentation *winMmeHostApi, |
| 1808 |
PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, |
| 1809 |
unsigned long winMmeSpecificFlags, |
| 1810 |
unsigned long bytesPerHostSample, |
| 1811 |
double sampleRate, PaWinMmeDeviceAndChannelCount *devices, |
| 1812 |
unsigned int deviceCount, PaWinWaveFormatChannelMask channelMask, int isInput ) |
| 1813 |
{ |
| 1814 |
PaError result; |
| 1815 |
MMRESULT mmresult; |
| 1816 |
signed int i, j; |
| 1817 |
PaSampleFormat sampleFormat; |
| 1818 |
int waveFormatTag; |
| 1819 |
|
| 1820 |
/* for error cleanup we expect that InitializeSingleDirectionHandlesAndBuffers() |
| 1821 |
has already been called to zero some fields */ |
| 1822 |
|
| 1823 |
result = CreateEventWithPaError( &handlesAndBuffers->bufferEvent, NULL, FALSE, FALSE, NULL ); |
| 1824 |
if( result != paNoError ) goto error; |
| 1825 |
|
| 1826 |
if( isInput ) |
| 1827 |
handlesAndBuffers->waveHandles = (void*)PaUtil_AllocateMemory( sizeof(HWAVEIN) * deviceCount ); |
| 1828 |
else |
| 1829 |
handlesAndBuffers->waveHandles = (void*)PaUtil_AllocateMemory( sizeof(HWAVEOUT) * deviceCount ); |
| 1830 |
if( !handlesAndBuffers->waveHandles ) |
| 1831 |
{ |
| 1832 |
result = paInsufficientMemory; |
| 1833 |
goto error; |
| 1834 |
} |
| 1835 |
|
| 1836 |
handlesAndBuffers->deviceCount = deviceCount; |
| 1837 |
|
| 1838 |
for( i = 0; i < (signed int)deviceCount; ++i ) |
| 1839 |
{ |
| 1840 |
if( isInput ) |
| 1841 |
((HWAVEIN*)handlesAndBuffers->waveHandles)[i] = 0; |
| 1842 |
else |
| 1843 |
((HWAVEOUT*)handlesAndBuffers->waveHandles)[i] = 0; |
| 1844 |
} |
| 1845 |
|
| 1846 |
/* @todo at the moment we only use 16 bit sample format */ |
| 1847 |
sampleFormat = paInt16; |
| 1848 |
waveFormatTag = SampleFormatAndWinWmmeSpecificFlagsToLinearWaveFormatTag( sampleFormat, winMmeSpecificFlags ); |
| 1849 |
|
| 1850 |
for( i = 0; i < (signed int)deviceCount; ++i ) |
| 1851 |
{ |
| 1852 |
PaWinWaveFormat waveFormat; |
| 1853 |
UINT winMmeDeviceId = LocalDeviceIndexToWinMmeDeviceId( winMmeHostApi, devices[i].device ); |
| 1854 |
|
| 1855 |
/* @todo: consider providing a flag or #define to not try waveformat extensible |
| 1856 |
this could just initialize j to 1 the first time round. */ |
| 1857 |
|
| 1858 |
for( j = 0; j < 2; ++j ) |
| 1859 |
{ |
| 1860 |
switch(j){ |
| 1861 |
case 0: |
| 1862 |
/* first, attempt to open the device using WAVEFORMATEXTENSIBLE, |
| 1863 |
if this fails we fall back to WAVEFORMATEX */ |
| 1864 |
|
| 1865 |
PaWin_InitializeWaveFormatExtensible( &waveFormat, devices[i].channelCount, |
| 1866 |
sampleFormat, waveFormatTag, sampleRate, channelMask ); |
| 1867 |
break; |
| 1868 |
|
| 1869 |
case 1: |
| 1870 |
/* retry with WAVEFORMATEX */ |
| 1871 |
|
| 1872 |
PaWin_InitializeWaveFormatEx( &waveFormat, devices[i].channelCount, |
| 1873 |
sampleFormat, waveFormatTag, sampleRate ); |
| 1874 |
break; |
| 1875 |
} |
| 1876 |
|
| 1877 |
/* REVIEW: consider not firing an event for input when a full duplex |
| 1878 |
stream is being used. this would probably depend on the |
| 1879 |
neverDropInput flag. */ |
| 1880 |
|
| 1881 |
if( isInput ) |
| 1882 |
{ |
| 1883 |
mmresult = waveInOpen( &((HWAVEIN*)handlesAndBuffers->waveHandles)[i], winMmeDeviceId, |
| 1884 |
(WAVEFORMATEX*)&waveFormat, |
| 1885 |
(DWORD_PTR)handlesAndBuffers->bufferEvent, (DWORD_PTR)0, CALLBACK_EVENT ); |
| 1886 |
} |
| 1887 |
else |
| 1888 |
{ |
| 1889 |
mmresult = waveOutOpen( &((HWAVEOUT*)handlesAndBuffers->waveHandles)[i], winMmeDeviceId, |
| 1890 |
(WAVEFORMATEX*)&waveFormat, |
| 1891 |
(DWORD_PTR)handlesAndBuffers->bufferEvent, (DWORD_PTR)0, CALLBACK_EVENT ); |
| 1892 |
} |
| 1893 |
|
| 1894 |
if( mmresult == MMSYSERR_NOERROR ) |
| 1895 |
{ |
| 1896 |
break; /* success */ |
| 1897 |
} |
| 1898 |
else if( j == 0 ) |
| 1899 |
{ |
| 1900 |
continue; /* try again with WAVEFORMATEX */ |
| 1901 |
} |
| 1902 |
else |
| 1903 |
{ |
| 1904 |
switch( mmresult ) |
| 1905 |
{ |
| 1906 |
case MMSYSERR_ALLOCATED: /* Specified resource is already allocated. */ |
| 1907 |
result = paDeviceUnavailable; |
| 1908 |
break; |
| 1909 |
case MMSYSERR_NODRIVER: /* No device driver is present. */ |
| 1910 |
result = paDeviceUnavailable; |
| 1911 |
break; |
| 1912 |
case MMSYSERR_NOMEM: /* Unable to allocate or lock memory. */ |
| 1913 |
result = paInsufficientMemory; |
| 1914 |
break; |
| 1915 |
|
| 1916 |
case MMSYSERR_BADDEVICEID: /* Specified device identifier is out of range. */ |
| 1917 |
/* falls through */ |
| 1918 |
|
| 1919 |
case WAVERR_BADFORMAT: /* Attempted to open with an unsupported waveform-audio format. */ |
| 1920 |
/* This can also occur if we try to open the device with an unsupported |
| 1921 |
* number of channels. This is attempted when device*ChannelCountIsKnown is |
| 1922 |
* set to 0. |
| 1923 |
*/ |
| 1924 |
/* falls through */ |
| 1925 |
default: |
| 1926 |
result = paUnanticipatedHostError; |
| 1927 |
if( isInput ) |
| 1928 |
{ |
| 1929 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 1930 |
} |
| 1931 |
else |
| 1932 |
{ |
| 1933 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 1934 |
} |
| 1935 |
} |
| 1936 |
goto error; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
} |
| 1940 |
|
| 1941 |
return result; |
| 1942 |
|
| 1943 |
error: |
| 1944 |
TerminateWaveHandles( handlesAndBuffers, isInput, 1 /* currentlyProcessingAnError */ ); |
| 1945 |
|
| 1946 |
return result; |
| 1947 |
} |
| 1948 |
|
| 1949 |
|
| 1950 |
static PaError TerminateWaveHandles( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, int isInput, int currentlyProcessingAnError ) |
| 1951 |
{ |
| 1952 |
PaError result = paNoError; |
| 1953 |
MMRESULT mmresult; |
| 1954 |
signed int i; |
| 1955 |
|
| 1956 |
if( handlesAndBuffers->waveHandles ) |
| 1957 |
{ |
| 1958 |
for( i = handlesAndBuffers->deviceCount-1; i >= 0; --i ) |
| 1959 |
{ |
| 1960 |
if( isInput ) |
| 1961 |
{ |
| 1962 |
if( ((HWAVEIN*)handlesAndBuffers->waveHandles)[i] ) |
| 1963 |
mmresult = waveInClose( ((HWAVEIN*)handlesAndBuffers->waveHandles)[i] ); |
| 1964 |
else |
| 1965 |
mmresult = MMSYSERR_NOERROR; |
| 1966 |
} |
| 1967 |
else |
| 1968 |
{ |
| 1969 |
if( ((HWAVEOUT*)handlesAndBuffers->waveHandles)[i] ) |
| 1970 |
mmresult = waveOutClose( ((HWAVEOUT*)handlesAndBuffers->waveHandles)[i] ); |
| 1971 |
else |
| 1972 |
mmresult = MMSYSERR_NOERROR; |
| 1973 |
} |
| 1974 |
|
| 1975 |
if( mmresult != MMSYSERR_NOERROR && |
| 1976 |
!currentlyProcessingAnError ) /* don't update the error state if we're already processing an error */ |
| 1977 |
{ |
| 1978 |
result = paUnanticipatedHostError; |
| 1979 |
if( isInput ) |
| 1980 |
{ |
| 1981 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 1982 |
} |
| 1983 |
else |
| 1984 |
{ |
| 1985 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 1986 |
} |
| 1987 |
/* note that we don't break here, we try to continue closing devices */ |
| 1988 |
} |
| 1989 |
} |
| 1990 |
|
| 1991 |
PaUtil_FreeMemory( handlesAndBuffers->waveHandles ); |
| 1992 |
handlesAndBuffers->waveHandles = 0; |
| 1993 |
} |
| 1994 |
|
| 1995 |
if( handlesAndBuffers->bufferEvent ) |
| 1996 |
{ |
| 1997 |
result = CloseHandleWithPaError( handlesAndBuffers->bufferEvent ); |
| 1998 |
handlesAndBuffers->bufferEvent = 0; |
| 1999 |
} |
| 2000 |
|
| 2001 |
return result; |
| 2002 |
} |
| 2003 |
|
| 2004 |
|
| 2005 |
static PaError InitializeWaveHeaders( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, |
| 2006 |
unsigned long hostBufferCount, |
| 2007 |
PaSampleFormat hostSampleFormat, |
| 2008 |
unsigned long framesPerHostBuffer, |
| 2009 |
PaWinMmeDeviceAndChannelCount *devices, |
| 2010 |
int isInput ) |
| 2011 |
{ |
| 2012 |
PaError result = paNoError; |
| 2013 |
MMRESULT mmresult; |
| 2014 |
WAVEHDR *deviceWaveHeaders; |
| 2015 |
signed int i, j; |
| 2016 |
|
| 2017 |
/* for error cleanup we expect that InitializeSingleDirectionHandlesAndBuffers() |
| 2018 |
has already been called to zero some fields */ |
| 2019 |
|
| 2020 |
|
| 2021 |
/* allocate an array of pointers to arrays of wave headers, one array of |
| 2022 |
wave headers per device */ |
| 2023 |
handlesAndBuffers->waveHeaders = (WAVEHDR**)PaUtil_AllocateMemory( sizeof(WAVEHDR*) * handlesAndBuffers->deviceCount ); |
| 2024 |
if( !handlesAndBuffers->waveHeaders ) |
| 2025 |
{ |
| 2026 |
result = paInsufficientMemory; |
| 2027 |
goto error; |
| 2028 |
} |
| 2029 |
|
| 2030 |
for( i = 0; i < (signed int)handlesAndBuffers->deviceCount; ++i ) |
| 2031 |
handlesAndBuffers->waveHeaders[i] = 0; |
| 2032 |
|
| 2033 |
handlesAndBuffers->bufferCount = hostBufferCount; |
| 2034 |
|
| 2035 |
for( i = 0; i < (signed int)handlesAndBuffers->deviceCount; ++i ) |
| 2036 |
{ |
| 2037 |
int bufferBytes = Pa_GetSampleSize( hostSampleFormat ) * |
| 2038 |
framesPerHostBuffer * devices[i].channelCount; |
| 2039 |
if( bufferBytes < 0 ) |
| 2040 |
{ |
| 2041 |
result = paInternalError; |
| 2042 |
goto error; |
| 2043 |
} |
| 2044 |
|
| 2045 |
/* Allocate an array of wave headers for device i */ |
| 2046 |
deviceWaveHeaders = (WAVEHDR *) PaUtil_AllocateMemory( sizeof(WAVEHDR)*hostBufferCount ); |
| 2047 |
if( !deviceWaveHeaders ) |
| 2048 |
{ |
| 2049 |
result = paInsufficientMemory; |
| 2050 |
goto error; |
| 2051 |
} |
| 2052 |
|
| 2053 |
for( j=0; j < (signed int)hostBufferCount; ++j ) |
| 2054 |
deviceWaveHeaders[j].lpData = 0; |
| 2055 |
|
| 2056 |
handlesAndBuffers->waveHeaders[i] = deviceWaveHeaders; |
| 2057 |
|
| 2058 |
/* Allocate a buffer for each wave header */ |
| 2059 |
for( j=0; j < (signed int)hostBufferCount; ++j ) |
| 2060 |
{ |
| 2061 |
deviceWaveHeaders[j].lpData = (char *)PaUtil_AllocateMemory( bufferBytes ); |
| 2062 |
if( !deviceWaveHeaders[j].lpData ) |
| 2063 |
{ |
| 2064 |
result = paInsufficientMemory; |
| 2065 |
goto error; |
| 2066 |
} |
| 2067 |
deviceWaveHeaders[j].dwBufferLength = bufferBytes; |
| 2068 |
deviceWaveHeaders[j].dwUser = 0xFFFFFFFF; /* indicates that *PrepareHeader() has not yet been called, for error clean up code */ |
| 2069 |
|
| 2070 |
if( isInput ) |
| 2071 |
{ |
| 2072 |
mmresult = waveInPrepareHeader( ((HWAVEIN*)handlesAndBuffers->waveHandles)[i], &deviceWaveHeaders[j], sizeof(WAVEHDR) ); |
| 2073 |
if( mmresult != MMSYSERR_NOERROR ) |
| 2074 |
{ |
| 2075 |
result = paUnanticipatedHostError; |
| 2076 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 2077 |
goto error; |
| 2078 |
} |
| 2079 |
} |
| 2080 |
else /* output */ |
| 2081 |
{ |
| 2082 |
mmresult = waveOutPrepareHeader( ((HWAVEOUT*)handlesAndBuffers->waveHandles)[i], &deviceWaveHeaders[j], sizeof(WAVEHDR) ); |
| 2083 |
if( mmresult != MMSYSERR_NOERROR ) |
| 2084 |
{ |
| 2085 |
result = paUnanticipatedHostError; |
| 2086 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 2087 |
goto error; |
| 2088 |
} |
| 2089 |
} |
| 2090 |
deviceWaveHeaders[j].dwUser = devices[i].channelCount; |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
return result; |
| 2095 |
|
| 2096 |
error: |
| 2097 |
TerminateWaveHeaders( handlesAndBuffers, isInput ); |
| 2098 |
|
| 2099 |
return result; |
| 2100 |
} |
| 2101 |
|
| 2102 |
|
| 2103 |
static void TerminateWaveHeaders( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers, int isInput ) |
| 2104 |
{ |
| 2105 |
signed int i, j; |
| 2106 |
WAVEHDR *deviceWaveHeaders; |
| 2107 |
|
| 2108 |
if( handlesAndBuffers->waveHeaders ) |
| 2109 |
{ |
| 2110 |
for( i = handlesAndBuffers->deviceCount-1; i >= 0 ; --i ) |
| 2111 |
{ |
| 2112 |
deviceWaveHeaders = handlesAndBuffers->waveHeaders[i]; /* wave headers for device i */ |
| 2113 |
if( deviceWaveHeaders ) |
| 2114 |
{ |
| 2115 |
for( j = handlesAndBuffers->bufferCount-1; j >= 0; --j ) |
| 2116 |
{ |
| 2117 |
if( deviceWaveHeaders[j].lpData ) |
| 2118 |
{ |
| 2119 |
if( deviceWaveHeaders[j].dwUser != 0xFFFFFFFF ) |
| 2120 |
{ |
| 2121 |
if( isInput ) |
| 2122 |
waveInUnprepareHeader( ((HWAVEIN*)handlesAndBuffers->waveHandles)[i], &deviceWaveHeaders[j], sizeof(WAVEHDR) ); |
| 2123 |
else |
| 2124 |
waveOutUnprepareHeader( ((HWAVEOUT*)handlesAndBuffers->waveHandles)[i], &deviceWaveHeaders[j], sizeof(WAVEHDR) ); |
| 2125 |
} |
| 2126 |
|
| 2127 |
PaUtil_FreeMemory( deviceWaveHeaders[j].lpData ); |
| 2128 |
} |
| 2129 |
} |
| 2130 |
|
| 2131 |
PaUtil_FreeMemory( deviceWaveHeaders ); |
| 2132 |
} |
| 2133 |
} |
| 2134 |
|
| 2135 |
PaUtil_FreeMemory( handlesAndBuffers->waveHeaders ); |
| 2136 |
handlesAndBuffers->waveHeaders = 0; |
| 2137 |
} |
| 2138 |
} |
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
/* PaWinMmeStream - a stream data structure specifically for this implementation */ |
| 2143 |
/* note that struct PaWinMmeStream is typedeffed to PaWinMmeStream above. */ |
| 2144 |
struct PaWinMmeStream |
| 2145 |
{ |
| 2146 |
PaUtilStreamRepresentation streamRepresentation; |
| 2147 |
PaUtilCpuLoadMeasurer cpuLoadMeasurer; |
| 2148 |
PaUtilBufferProcessor bufferProcessor; |
| 2149 |
|
| 2150 |
int primeStreamUsingCallback; |
| 2151 |
|
| 2152 |
PaWinMmeSingleDirectionHandlesAndBuffers input; |
| 2153 |
PaWinMmeSingleDirectionHandlesAndBuffers output; |
| 2154 |
|
| 2155 |
/* Processing thread management -------------- */ |
| 2156 |
HANDLE abortEvent; |
| 2157 |
HANDLE processingThread; |
| 2158 |
PA_THREAD_ID processingThreadId; |
| 2159 |
|
| 2160 |
char throttleProcessingThreadOnOverload; /* 0 -> don't throttle, non-0 -> throttle */ |
| 2161 |
int processingThreadPriority; |
| 2162 |
int highThreadPriority; |
| 2163 |
int throttledThreadPriority; |
| 2164 |
unsigned long throttledSleepMsecs; |
| 2165 |
|
| 2166 |
int isStopped; |
| 2167 |
volatile int isActive; |
| 2168 |
volatile int stopProcessing; /* stop thread once existing buffers have been returned */ |
| 2169 |
volatile int abortProcessing; /* stop thread immediately */ |
| 2170 |
|
| 2171 |
DWORD allBuffersDurationMs; /* used to calculate timeouts */ |
| 2172 |
}; |
| 2173 |
|
| 2174 |
/* updates deviceCount if PaWinMmeUseMultipleDevices is used */ |
| 2175 |
|
| 2176 |
static PaError ValidateWinMmeSpecificStreamInfo( |
| 2177 |
const PaStreamParameters *streamParameters, |
| 2178 |
const PaWinMmeStreamInfo *streamInfo, |
| 2179 |
unsigned long *winMmeSpecificFlags, |
| 2180 |
char *throttleProcessingThreadOnOverload, |
| 2181 |
unsigned long *deviceCount ) |
| 2182 |
{ |
| 2183 |
if( streamInfo ) |
| 2184 |
{ |
| 2185 |
if( streamInfo->size != sizeof( PaWinMmeStreamInfo ) |
| 2186 |
|| streamInfo->version != 1 ) |
| 2187 |
{ |
| 2188 |
return paIncompatibleHostApiSpecificStreamInfo; |
| 2189 |
} |
| 2190 |
|
| 2191 |
*winMmeSpecificFlags = streamInfo->flags; |
| 2192 |
|
| 2193 |
if( streamInfo->flags & paWinMmeDontThrottleOverloadedProcessingThread ) |
| 2194 |
*throttleProcessingThreadOnOverload = 0; |
| 2195 |
|
| 2196 |
if( streamInfo->flags & paWinMmeUseMultipleDevices ) |
| 2197 |
{ |
| 2198 |
if( streamParameters->device != paUseHostApiSpecificDeviceSpecification ) |
| 2199 |
return paInvalidDevice; |
| 2200 |
|
| 2201 |
*deviceCount = streamInfo->deviceCount; |
| 2202 |
} |
| 2203 |
} |
| 2204 |
|
| 2205 |
return paNoError; |
| 2206 |
} |
| 2207 |
|
| 2208 |
static PaError RetrieveDevicesFromStreamParameters( |
| 2209 |
struct PaUtilHostApiRepresentation *hostApi, |
| 2210 |
const PaStreamParameters *streamParameters, |
| 2211 |
const PaWinMmeStreamInfo *streamInfo, |
| 2212 |
PaWinMmeDeviceAndChannelCount *devices, |
| 2213 |
unsigned long deviceCount ) |
| 2214 |
{ |
| 2215 |
PaError result = paNoError; |
| 2216 |
unsigned int i; |
| 2217 |
int totalChannelCount; |
| 2218 |
PaDeviceIndex hostApiDevice; |
| 2219 |
|
| 2220 |
if( streamInfo && streamInfo->flags & paWinMmeUseMultipleDevices ) |
| 2221 |
{ |
| 2222 |
totalChannelCount = 0; |
| 2223 |
for( i=0; i < deviceCount; ++i ) |
| 2224 |
{ |
| 2225 |
/* validate that the device number is within range */ |
| 2226 |
result = PaUtil_DeviceIndexToHostApiDeviceIndex( &hostApiDevice, |
| 2227 |
streamInfo->devices[i].device, hostApi ); |
| 2228 |
if( result != paNoError ) |
| 2229 |
return result; |
| 2230 |
|
| 2231 |
devices[i].device = hostApiDevice; |
| 2232 |
devices[i].channelCount = streamInfo->devices[i].channelCount; |
| 2233 |
|
| 2234 |
totalChannelCount += devices[i].channelCount; |
| 2235 |
} |
| 2236 |
|
| 2237 |
if( totalChannelCount != streamParameters->channelCount ) |
| 2238 |
{ |
| 2239 |
/* channelCount must match total channels specified by multiple devices */ |
| 2240 |
return paInvalidChannelCount; /* REVIEW use of this error code */ |
| 2241 |
} |
| 2242 |
} |
| 2243 |
else |
| 2244 |
{ |
| 2245 |
devices[0].device = streamParameters->device; |
| 2246 |
devices[0].channelCount = streamParameters->channelCount; |
| 2247 |
} |
| 2248 |
|
| 2249 |
return result; |
| 2250 |
} |
| 2251 |
|
| 2252 |
static PaError ValidateInputChannelCounts( |
| 2253 |
struct PaUtilHostApiRepresentation *hostApi, |
| 2254 |
PaWinMmeDeviceAndChannelCount *devices, |
| 2255 |
unsigned long deviceCount ) |
| 2256 |
{ |
| 2257 |
unsigned int i; |
| 2258 |
PaWinMmeDeviceInfo *inputDeviceInfo; |
| 2259 |
PaError paerror; |
| 2260 |
|
| 2261 |
for( i=0; i < deviceCount; ++i ) |
| 2262 |
{ |
| 2263 |
if( devices[i].channelCount < 1 ) |
| 2264 |
return paInvalidChannelCount; |
| 2265 |
|
| 2266 |
inputDeviceInfo = |
| 2267 |
(PaWinMmeDeviceInfo*)hostApi->deviceInfos[ devices[i].device ]; |
| 2268 |
|
| 2269 |
paerror = IsInputChannelCountSupported( inputDeviceInfo, devices[i].channelCount ); |
| 2270 |
if( paerror != paNoError ) |
| 2271 |
return paerror; |
| 2272 |
} |
| 2273 |
|
| 2274 |
return paNoError; |
| 2275 |
} |
| 2276 |
|
| 2277 |
static PaError ValidateOutputChannelCounts( |
| 2278 |
struct PaUtilHostApiRepresentation *hostApi, |
| 2279 |
PaWinMmeDeviceAndChannelCount *devices, |
| 2280 |
unsigned long deviceCount ) |
| 2281 |
{ |
| 2282 |
unsigned int i; |
| 2283 |
PaWinMmeDeviceInfo *outputDeviceInfo; |
| 2284 |
PaError paerror; |
| 2285 |
|
| 2286 |
for( i=0; i < deviceCount; ++i ) |
| 2287 |
{ |
| 2288 |
if( devices[i].channelCount < 1 ) |
| 2289 |
return paInvalidChannelCount; |
| 2290 |
|
| 2291 |
outputDeviceInfo = |
| 2292 |
(PaWinMmeDeviceInfo*)hostApi->deviceInfos[ devices[i].device ]; |
| 2293 |
|
| 2294 |
paerror = IsOutputChannelCountSupported( outputDeviceInfo, devices[i].channelCount ); |
| 2295 |
if( paerror != paNoError ) |
| 2296 |
return paerror; |
| 2297 |
} |
| 2298 |
|
| 2299 |
return paNoError; |
| 2300 |
} |
| 2301 |
|
| 2302 |
|
| 2303 |
/* the following macros are intended to improve the readability of the following code */ |
| 2304 |
#define PA_IS_INPUT_STREAM_( stream ) ( stream ->input.waveHandles ) |
| 2305 |
#define PA_IS_OUTPUT_STREAM_( stream ) ( stream ->output.waveHandles ) |
| 2306 |
#define PA_IS_FULL_DUPLEX_STREAM_( stream ) ( stream ->input.waveHandles && stream ->output.waveHandles ) |
| 2307 |
#define PA_IS_HALF_DUPLEX_STREAM_( stream ) ( !(stream ->input.waveHandles && stream ->output.waveHandles) ) |
| 2308 |
|
| 2309 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 2310 |
PaStream** s, |
| 2311 |
const PaStreamParameters *inputParameters, |
| 2312 |
const PaStreamParameters *outputParameters, |
| 2313 |
double sampleRate, |
| 2314 |
unsigned long framesPerBuffer, |
| 2315 |
PaStreamFlags streamFlags, |
| 2316 |
PaStreamCallback *streamCallback, |
| 2317 |
void *userData ) |
| 2318 |
{ |
| 2319 |
PaError result; |
| 2320 |
PaWinMmeHostApiRepresentation *winMmeHostApi = (PaWinMmeHostApiRepresentation*)hostApi; |
| 2321 |
PaWinMmeStream *stream = 0; |
| 2322 |
int bufferProcessorIsInitialized = 0; |
| 2323 |
int streamRepresentationIsInitialized = 0; |
| 2324 |
PaSampleFormat hostInputSampleFormat, hostOutputSampleFormat; |
| 2325 |
int inputChannelCount, outputChannelCount; |
| 2326 |
PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 2327 |
double suggestedInputLatency, suggestedOutputLatency; |
| 2328 |
PaWinMmeStreamInfo *inputStreamInfo, *outputStreamInfo; |
| 2329 |
PaWinWaveFormatChannelMask inputChannelMask, outputChannelMask; |
| 2330 |
unsigned long framesPerHostInputBuffer; |
| 2331 |
unsigned long hostInputBufferCount; |
| 2332 |
unsigned long framesPerHostOutputBuffer; |
| 2333 |
unsigned long hostOutputBufferCount; |
| 2334 |
unsigned long framesPerBufferProcessorCall; |
| 2335 |
PaWinMmeDeviceAndChannelCount *inputDevices = 0; /* contains all devices and channel counts as local host api ids, even when PaWinMmeUseMultipleDevices is not used */ |
| 2336 |
unsigned long winMmeSpecificInputFlags = 0; |
| 2337 |
unsigned long inputDeviceCount = 0; |
| 2338 |
PaWinMmeDeviceAndChannelCount *outputDevices = 0; |
| 2339 |
unsigned long winMmeSpecificOutputFlags = 0; |
| 2340 |
unsigned long outputDeviceCount = 0; /* contains all devices and channel counts as local host api ids, even when PaWinMmeUseMultipleDevices is not used */ |
| 2341 |
char throttleProcessingThreadOnOverload = 1; |
| 2342 |
|
| 2343 |
|
| 2344 |
if( inputParameters ) |
| 2345 |
{ |
| 2346 |
inputChannelCount = inputParameters->channelCount; |
| 2347 |
inputSampleFormat = inputParameters->sampleFormat; |
| 2348 |
suggestedInputLatency = inputParameters->suggestedLatency; |
| 2349 |
|
| 2350 |
inputDeviceCount = 1; |
| 2351 |
|
| 2352 |
/* validate input hostApiSpecificStreamInfo */ |
| 2353 |
inputStreamInfo = (PaWinMmeStreamInfo*)inputParameters->hostApiSpecificStreamInfo; |
| 2354 |
result = ValidateWinMmeSpecificStreamInfo( inputParameters, inputStreamInfo, |
| 2355 |
&winMmeSpecificInputFlags, |
| 2356 |
&throttleProcessingThreadOnOverload, |
| 2357 |
&inputDeviceCount ); |
| 2358 |
if( result != paNoError ) return result; |
| 2359 |
|
| 2360 |
inputDevices = (PaWinMmeDeviceAndChannelCount*)alloca( sizeof(PaWinMmeDeviceAndChannelCount) * inputDeviceCount ); |
| 2361 |
if( !inputDevices ) return paInsufficientMemory; |
| 2362 |
|
| 2363 |
result = RetrieveDevicesFromStreamParameters( hostApi, inputParameters, inputStreamInfo, inputDevices, inputDeviceCount ); |
| 2364 |
if( result != paNoError ) return result; |
| 2365 |
|
| 2366 |
result = ValidateInputChannelCounts( hostApi, inputDevices, inputDeviceCount ); |
| 2367 |
if( result != paNoError ) return result; |
| 2368 |
|
| 2369 |
hostInputSampleFormat = |
| 2370 |
PaUtil_SelectClosestAvailableFormat( paInt16 /* native formats */, inputSampleFormat ); |
| 2371 |
|
| 2372 |
if( inputDeviceCount != 1 ){ |
| 2373 |
/* always use direct speakers when using multi-device multichannel mode */ |
| 2374 |
inputChannelMask = PAWIN_SPEAKER_DIRECTOUT; |
| 2375 |
} |
| 2376 |
else |
| 2377 |
{ |
| 2378 |
if( inputStreamInfo && inputStreamInfo->flags & paWinMmeUseChannelMask ) |
| 2379 |
inputChannelMask = inputStreamInfo->channelMask; |
| 2380 |
else |
| 2381 |
inputChannelMask = PaWin_DefaultChannelMask( inputDevices[0].channelCount ); |
| 2382 |
} |
| 2383 |
} |
| 2384 |
else |
| 2385 |
{ |
| 2386 |
inputChannelCount = 0; |
| 2387 |
inputSampleFormat = 0; |
| 2388 |
suggestedInputLatency = 0.; |
| 2389 |
inputStreamInfo = 0; |
| 2390 |
hostInputSampleFormat = 0; |
| 2391 |
} |
| 2392 |
|
| 2393 |
|
| 2394 |
if( outputParameters ) |
| 2395 |
{ |
| 2396 |
outputChannelCount = outputParameters->channelCount; |
| 2397 |
outputSampleFormat = outputParameters->sampleFormat; |
| 2398 |
suggestedOutputLatency = outputParameters->suggestedLatency; |
| 2399 |
|
| 2400 |
outputDeviceCount = 1; |
| 2401 |
|
| 2402 |
/* validate output hostApiSpecificStreamInfo */ |
| 2403 |
outputStreamInfo = (PaWinMmeStreamInfo*)outputParameters->hostApiSpecificStreamInfo; |
| 2404 |
result = ValidateWinMmeSpecificStreamInfo( outputParameters, outputStreamInfo, |
| 2405 |
&winMmeSpecificOutputFlags, |
| 2406 |
&throttleProcessingThreadOnOverload, |
| 2407 |
&outputDeviceCount ); |
| 2408 |
if( result != paNoError ) return result; |
| 2409 |
|
| 2410 |
outputDevices = (PaWinMmeDeviceAndChannelCount*)alloca( sizeof(PaWinMmeDeviceAndChannelCount) * outputDeviceCount ); |
| 2411 |
if( !outputDevices ) return paInsufficientMemory; |
| 2412 |
|
| 2413 |
result = RetrieveDevicesFromStreamParameters( hostApi, outputParameters, outputStreamInfo, outputDevices, outputDeviceCount ); |
| 2414 |
if( result != paNoError ) return result; |
| 2415 |
|
| 2416 |
result = ValidateOutputChannelCounts( hostApi, outputDevices, outputDeviceCount ); |
| 2417 |
if( result != paNoError ) return result; |
| 2418 |
|
| 2419 |
hostOutputSampleFormat = |
| 2420 |
PaUtil_SelectClosestAvailableFormat( paInt16 /* native formats */, outputSampleFormat ); |
| 2421 |
|
| 2422 |
if( outputDeviceCount != 1 ){ |
| 2423 |
/* always use direct speakers when using multi-device multichannel mode */ |
| 2424 |
outputChannelMask = PAWIN_SPEAKER_DIRECTOUT; |
| 2425 |
} |
| 2426 |
else |
| 2427 |
{ |
| 2428 |
if( outputStreamInfo && outputStreamInfo->flags & paWinMmeUseChannelMask ) |
| 2429 |
outputChannelMask = outputStreamInfo->channelMask; |
| 2430 |
else |
| 2431 |
outputChannelMask = PaWin_DefaultChannelMask( outputDevices[0].channelCount ); |
| 2432 |
} |
| 2433 |
} |
| 2434 |
else |
| 2435 |
{ |
| 2436 |
outputChannelCount = 0; |
| 2437 |
outputSampleFormat = 0; |
| 2438 |
outputStreamInfo = 0; |
| 2439 |
hostOutputSampleFormat = 0; |
| 2440 |
suggestedOutputLatency = 0.; |
| 2441 |
} |
| 2442 |
|
| 2443 |
|
| 2444 |
/* |
| 2445 |
IMPLEMENT ME: |
| 2446 |
- alter sampleRate to a close allowable rate if possible / necessary |
| 2447 |
*/ |
| 2448 |
|
| 2449 |
|
| 2450 |
/* validate platform specific flags */ |
| 2451 |
if( (streamFlags & paPlatformSpecificFlags) != 0 ) |
| 2452 |
return paInvalidFlag; /* unexpected platform specific flag */ |
| 2453 |
|
| 2454 |
|
| 2455 |
/* always disable clipping and dithering if we are outputting a raw spdif stream */ |
| 2456 |
if( (winMmeSpecificOutputFlags & paWinMmeWaveFormatDolbyAc3Spdif) |
| 2457 |
|| (winMmeSpecificOutputFlags & paWinMmeWaveFormatWmaSpdif) ){ |
| 2458 |
|
| 2459 |
streamFlags = streamFlags | paClipOff | paDitherOff; |
| 2460 |
} |
| 2461 |
|
| 2462 |
|
| 2463 |
result = CalculateBufferSettings( &framesPerHostInputBuffer, &hostInputBufferCount, |
| 2464 |
&framesPerHostOutputBuffer, &hostOutputBufferCount, |
| 2465 |
inputChannelCount, hostInputSampleFormat, suggestedInputLatency, inputStreamInfo, |
| 2466 |
outputChannelCount, hostOutputSampleFormat, suggestedOutputLatency, outputStreamInfo, |
| 2467 |
sampleRate, framesPerBuffer ); |
| 2468 |
if( result != paNoError ) goto error; |
| 2469 |
|
| 2470 |
|
| 2471 |
stream = (PaWinMmeStream*)PaUtil_AllocateMemory( sizeof(PaWinMmeStream) ); |
| 2472 |
if( !stream ) |
| 2473 |
{ |
| 2474 |
result = paInsufficientMemory; |
| 2475 |
goto error; |
| 2476 |
} |
| 2477 |
|
| 2478 |
InitializeSingleDirectionHandlesAndBuffers( &stream->input ); |
| 2479 |
InitializeSingleDirectionHandlesAndBuffers( &stream->output ); |
| 2480 |
|
| 2481 |
stream->abortEvent = 0; |
| 2482 |
stream->processingThread = 0; |
| 2483 |
|
| 2484 |
stream->throttleProcessingThreadOnOverload = throttleProcessingThreadOnOverload; |
| 2485 |
|
| 2486 |
PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 2487 |
( (streamCallback) |
| 2488 |
? &winMmeHostApi->callbackStreamInterface |
| 2489 |
: &winMmeHostApi->blockingStreamInterface ), |
| 2490 |
streamCallback, userData ); |
| 2491 |
streamRepresentationIsInitialized = 1; |
| 2492 |
|
| 2493 |
PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, sampleRate ); |
| 2494 |
|
| 2495 |
|
| 2496 |
if( inputParameters && outputParameters ) /* full duplex */ |
| 2497 |
{ |
| 2498 |
if( framesPerHostInputBuffer < framesPerHostOutputBuffer ) |
| 2499 |
{ |
| 2500 |
assert( (framesPerHostOutputBuffer % framesPerHostInputBuffer) == 0 ); /* CalculateBufferSettings() should guarantee this condition */ |
| 2501 |
|
| 2502 |
framesPerBufferProcessorCall = framesPerHostInputBuffer; |
| 2503 |
} |
| 2504 |
else |
| 2505 |
{ |
| 2506 |
assert( (framesPerHostInputBuffer % framesPerHostOutputBuffer) == 0 ); /* CalculateBufferSettings() should guarantee this condition */ |
| 2507 |
|
| 2508 |
framesPerBufferProcessorCall = framesPerHostOutputBuffer; |
| 2509 |
} |
| 2510 |
} |
| 2511 |
else if( inputParameters ) |
| 2512 |
{ |
| 2513 |
framesPerBufferProcessorCall = framesPerHostInputBuffer; |
| 2514 |
} |
| 2515 |
else if( outputParameters ) |
| 2516 |
{ |
| 2517 |
framesPerBufferProcessorCall = framesPerHostOutputBuffer; |
| 2518 |
} |
| 2519 |
|
| 2520 |
stream->input.framesPerBuffer = framesPerHostInputBuffer; |
| 2521 |
stream->output.framesPerBuffer = framesPerHostOutputBuffer; |
| 2522 |
|
| 2523 |
result = PaUtil_InitializeBufferProcessor( &stream->bufferProcessor, |
| 2524 |
inputChannelCount, inputSampleFormat, hostInputSampleFormat, |
| 2525 |
outputChannelCount, outputSampleFormat, hostOutputSampleFormat, |
| 2526 |
sampleRate, streamFlags, framesPerBuffer, |
| 2527 |
framesPerBufferProcessorCall, paUtilFixedHostBufferSize, |
| 2528 |
streamCallback, userData ); |
| 2529 |
if( result != paNoError ) goto error; |
| 2530 |
|
| 2531 |
bufferProcessorIsInitialized = 1; |
| 2532 |
|
| 2533 |
/* stream info input latency is the minimum buffering latency (unlike suggested and default which are *maximums*) */ |
| 2534 |
stream->streamRepresentation.streamInfo.inputLatency = |
| 2535 |
(double)(PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor) |
| 2536 |
+ framesPerHostInputBuffer) / sampleRate; |
| 2537 |
stream->streamRepresentation.streamInfo.outputLatency = |
| 2538 |
(double)(PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor) |
| 2539 |
+ (framesPerHostOutputBuffer * (hostOutputBufferCount-1))) / sampleRate; |
| 2540 |
stream->streamRepresentation.streamInfo.sampleRate = sampleRate; |
| 2541 |
|
| 2542 |
stream->primeStreamUsingCallback = ( (streamFlags&paPrimeOutputBuffersUsingStreamCallback) && streamCallback ) ? 1 : 0; |
| 2543 |
|
| 2544 |
/* time to sleep when throttling due to >100% cpu usage. |
| 2545 |
-a quarter of a buffer's duration */ |
| 2546 |
stream->throttledSleepMsecs = |
| 2547 |
(unsigned long)(stream->bufferProcessor.framesPerHostBuffer * |
| 2548 |
stream->bufferProcessor.samplePeriod * .25 * 1000); |
| 2549 |
|
| 2550 |
stream->isStopped = 1; |
| 2551 |
stream->isActive = 0; |
| 2552 |
|
| 2553 |
|
| 2554 |
/* for maximum compatibility with multi-device multichannel drivers, |
| 2555 |
we first open all devices, then we prepare all buffers, finally |
| 2556 |
we start all devices ( in StartStream() ). teardown in reverse order. |
| 2557 |
*/ |
| 2558 |
|
| 2559 |
if( inputParameters ) |
| 2560 |
{ |
| 2561 |
result = InitializeWaveHandles( winMmeHostApi, &stream->input, |
| 2562 |
winMmeSpecificInputFlags, |
| 2563 |
stream->bufferProcessor.bytesPerHostInputSample, sampleRate, |
| 2564 |
inputDevices, inputDeviceCount, inputChannelMask, 1 /* isInput */ ); |
| 2565 |
if( result != paNoError ) goto error; |
| 2566 |
} |
| 2567 |
|
| 2568 |
if( outputParameters ) |
| 2569 |
{ |
| 2570 |
result = InitializeWaveHandles( winMmeHostApi, &stream->output, |
| 2571 |
winMmeSpecificOutputFlags, |
| 2572 |
stream->bufferProcessor.bytesPerHostOutputSample, sampleRate, |
| 2573 |
outputDevices, outputDeviceCount, outputChannelMask, 0 /* isInput */ ); |
| 2574 |
if( result != paNoError ) goto error; |
| 2575 |
} |
| 2576 |
|
| 2577 |
if( inputParameters ) |
| 2578 |
{ |
| 2579 |
result = InitializeWaveHeaders( &stream->input, hostInputBufferCount, |
| 2580 |
hostInputSampleFormat, framesPerHostInputBuffer, inputDevices, 1 /* isInput */ ); |
| 2581 |
if( result != paNoError ) goto error; |
| 2582 |
} |
| 2583 |
|
| 2584 |
if( outputParameters ) |
| 2585 |
{ |
| 2586 |
result = InitializeWaveHeaders( &stream->output, hostOutputBufferCount, |
| 2587 |
hostOutputSampleFormat, framesPerHostOutputBuffer, outputDevices, 0 /* not isInput */ ); |
| 2588 |
if( result != paNoError ) goto error; |
| 2589 |
|
| 2590 |
stream->allBuffersDurationMs = (DWORD) (1000.0 * (framesPerHostOutputBuffer * stream->output.bufferCount) / sampleRate); |
| 2591 |
} |
| 2592 |
else |
| 2593 |
{ |
| 2594 |
stream->allBuffersDurationMs = (DWORD) (1000.0 * (framesPerHostInputBuffer * stream->input.bufferCount) / sampleRate); |
| 2595 |
} |
| 2596 |
|
| 2597 |
|
| 2598 |
if( streamCallback ) |
| 2599 |
{ |
| 2600 |
/* abort event is only needed for callback streams */ |
| 2601 |
result = CreateEventWithPaError( &stream->abortEvent, NULL, TRUE, FALSE, NULL ); |
| 2602 |
if( result != paNoError ) goto error; |
| 2603 |
} |
| 2604 |
|
| 2605 |
*s = (PaStream*)stream; |
| 2606 |
|
| 2607 |
return result; |
| 2608 |
|
| 2609 |
error: |
| 2610 |
|
| 2611 |
if( stream ) |
| 2612 |
{ |
| 2613 |
if( stream->abortEvent ) |
| 2614 |
CloseHandle( stream->abortEvent ); |
| 2615 |
|
| 2616 |
TerminateWaveHeaders( &stream->output, 0 /* not isInput */ ); |
| 2617 |
TerminateWaveHeaders( &stream->input, 1 /* isInput */ ); |
| 2618 |
|
| 2619 |
TerminateWaveHandles( &stream->output, 0 /* not isInput */, 1 /* currentlyProcessingAnError */ ); |
| 2620 |
TerminateWaveHandles( &stream->input, 1 /* isInput */, 1 /* currentlyProcessingAnError */ ); |
| 2621 |
|
| 2622 |
if( bufferProcessorIsInitialized ) |
| 2623 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 2624 |
|
| 2625 |
if( streamRepresentationIsInitialized ) |
| 2626 |
PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation ); |
| 2627 |
|
| 2628 |
PaUtil_FreeMemory( stream ); |
| 2629 |
} |
| 2630 |
|
| 2631 |
return result; |
| 2632 |
} |
| 2633 |
|
| 2634 |
|
| 2635 |
/* return non-zero if all current buffers are done */ |
| 2636 |
static int BuffersAreDone( WAVEHDR **waveHeaders, unsigned int deviceCount, int bufferIndex ) |
| 2637 |
{ |
| 2638 |
unsigned int i; |
| 2639 |
|
| 2640 |
for( i=0; i < deviceCount; ++i ) |
| 2641 |
{ |
| 2642 |
if( !(waveHeaders[i][ bufferIndex ].dwFlags & WHDR_DONE) ) |
| 2643 |
{ |
| 2644 |
return 0; |
| 2645 |
} |
| 2646 |
} |
| 2647 |
|
| 2648 |
return 1; |
| 2649 |
} |
| 2650 |
|
| 2651 |
static int CurrentInputBuffersAreDone( PaWinMmeStream *stream ) |
| 2652 |
{ |
| 2653 |
return BuffersAreDone( stream->input.waveHeaders, stream->input.deviceCount, stream->input.currentBufferIndex ); |
| 2654 |
} |
| 2655 |
|
| 2656 |
static int CurrentOutputBuffersAreDone( PaWinMmeStream *stream ) |
| 2657 |
{ |
| 2658 |
return BuffersAreDone( stream->output.waveHeaders, stream->output.deviceCount, stream->output.currentBufferIndex ); |
| 2659 |
} |
| 2660 |
|
| 2661 |
|
| 2662 |
/* return non-zero if any buffers are queued */ |
| 2663 |
static int NoBuffersAreQueued( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers ) |
| 2664 |
{ |
| 2665 |
unsigned int i, j; |
| 2666 |
|
| 2667 |
if( handlesAndBuffers->waveHandles ) |
| 2668 |
{ |
| 2669 |
for( i=0; i < handlesAndBuffers->bufferCount; ++i ) |
| 2670 |
{ |
| 2671 |
for( j=0; j < handlesAndBuffers->deviceCount; ++j ) |
| 2672 |
{ |
| 2673 |
if( !( handlesAndBuffers->waveHeaders[ j ][ i ].dwFlags & WHDR_DONE) ) |
| 2674 |
{ |
| 2675 |
return 0; |
| 2676 |
} |
| 2677 |
} |
| 2678 |
} |
| 2679 |
} |
| 2680 |
|
| 2681 |
return 1; |
| 2682 |
} |
| 2683 |
|
| 2684 |
|
| 2685 |
#define PA_CIRCULAR_INCREMENT_( current, max )\ |
| 2686 |
( (((current) + 1) >= (max)) ? (0) : (current+1) ) |
| 2687 |
|
| 2688 |
#define PA_CIRCULAR_DECREMENT_( current, max )\ |
| 2689 |
( ((current) == 0) ? ((max)-1) : (current-1) ) |
| 2690 |
|
| 2691 |
|
| 2692 |
static signed long GetAvailableFrames( PaWinMmeSingleDirectionHandlesAndBuffers *handlesAndBuffers ) |
| 2693 |
{ |
| 2694 |
signed long result = 0; |
| 2695 |
unsigned int i; |
| 2696 |
|
| 2697 |
if( BuffersAreDone( handlesAndBuffers->waveHeaders, handlesAndBuffers->deviceCount, handlesAndBuffers->currentBufferIndex ) ) |
| 2698 |
{ |
| 2699 |
/* we could calculate the following in O(1) if we kept track of the |
| 2700 |
last done buffer */ |
| 2701 |
result = handlesAndBuffers->framesPerBuffer - handlesAndBuffers->framesUsedInCurrentBuffer; |
| 2702 |
|
| 2703 |
i = PA_CIRCULAR_INCREMENT_( handlesAndBuffers->currentBufferIndex, handlesAndBuffers->bufferCount ); |
| 2704 |
while( i != handlesAndBuffers->currentBufferIndex ) |
| 2705 |
{ |
| 2706 |
if( BuffersAreDone( handlesAndBuffers->waveHeaders, handlesAndBuffers->deviceCount, i ) ) |
| 2707 |
{ |
| 2708 |
result += handlesAndBuffers->framesPerBuffer; |
| 2709 |
i = PA_CIRCULAR_INCREMENT_( i, handlesAndBuffers->bufferCount ); |
| 2710 |
} |
| 2711 |
else |
| 2712 |
break; |
| 2713 |
} |
| 2714 |
} |
| 2715 |
|
| 2716 |
return result; |
| 2717 |
} |
| 2718 |
|
| 2719 |
|
| 2720 |
static PaError AdvanceToNextInputBuffer( PaWinMmeStream *stream ) |
| 2721 |
{ |
| 2722 |
PaError result = paNoError; |
| 2723 |
MMRESULT mmresult; |
| 2724 |
unsigned int i; |
| 2725 |
|
| 2726 |
for( i=0; i < stream->input.deviceCount; ++i ) |
| 2727 |
{ |
| 2728 |
stream->input.waveHeaders[i][ stream->input.currentBufferIndex ].dwFlags &= ~WHDR_DONE; |
| 2729 |
mmresult = waveInAddBuffer( ((HWAVEIN*)stream->input.waveHandles)[i], |
| 2730 |
&stream->input.waveHeaders[i][ stream->input.currentBufferIndex ], |
| 2731 |
sizeof(WAVEHDR) ); |
| 2732 |
if( mmresult != MMSYSERR_NOERROR ) |
| 2733 |
{ |
| 2734 |
result = paUnanticipatedHostError; |
| 2735 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 2736 |
} |
| 2737 |
} |
| 2738 |
|
| 2739 |
stream->input.currentBufferIndex = |
| 2740 |
PA_CIRCULAR_INCREMENT_( stream->input.currentBufferIndex, stream->input.bufferCount ); |
| 2741 |
|
| 2742 |
stream->input.framesUsedInCurrentBuffer = 0; |
| 2743 |
|
| 2744 |
return result; |
| 2745 |
} |
| 2746 |
|
| 2747 |
|
| 2748 |
static PaError AdvanceToNextOutputBuffer( PaWinMmeStream *stream ) |
| 2749 |
{ |
| 2750 |
PaError result = paNoError; |
| 2751 |
MMRESULT mmresult; |
| 2752 |
unsigned int i; |
| 2753 |
|
| 2754 |
for( i=0; i < stream->output.deviceCount; ++i ) |
| 2755 |
{ |
| 2756 |
mmresult = waveOutWrite( ((HWAVEOUT*)stream->output.waveHandles)[i], |
| 2757 |
&stream->output.waveHeaders[i][ stream->output.currentBufferIndex ], |
| 2758 |
sizeof(WAVEHDR) ); |
| 2759 |
if( mmresult != MMSYSERR_NOERROR ) |
| 2760 |
{ |
| 2761 |
result = paUnanticipatedHostError; |
| 2762 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 2763 |
} |
| 2764 |
} |
| 2765 |
|
| 2766 |
stream->output.currentBufferIndex = |
| 2767 |
PA_CIRCULAR_INCREMENT_( stream->output.currentBufferIndex, stream->output.bufferCount ); |
| 2768 |
|
| 2769 |
stream->output.framesUsedInCurrentBuffer = 0; |
| 2770 |
|
| 2771 |
return result; |
| 2772 |
} |
| 2773 |
|
| 2774 |
|
| 2775 |
/* requeue all but the most recent input with the driver. Used for catching |
| 2776 |
up after a total input buffer underrun */ |
| 2777 |
static PaError CatchUpInputBuffers( PaWinMmeStream *stream ) |
| 2778 |
{ |
| 2779 |
PaError result = paNoError; |
| 2780 |
unsigned int i; |
| 2781 |
|
| 2782 |
for( i=0; i < stream->input.bufferCount - 1; ++i ) |
| 2783 |
{ |
| 2784 |
result = AdvanceToNextInputBuffer( stream ); |
| 2785 |
if( result != paNoError ) |
| 2786 |
break; |
| 2787 |
} |
| 2788 |
|
| 2789 |
return result; |
| 2790 |
} |
| 2791 |
|
| 2792 |
|
| 2793 |
/* take the most recent output and duplicate it to all other output buffers |
| 2794 |
and requeue them. Used for catching up after a total output buffer underrun. |
| 2795 |
*/ |
| 2796 |
static PaError CatchUpOutputBuffers( PaWinMmeStream *stream ) |
| 2797 |
{ |
| 2798 |
PaError result = paNoError; |
| 2799 |
unsigned int i, j; |
| 2800 |
unsigned int previousBufferIndex = |
| 2801 |
PA_CIRCULAR_DECREMENT_( stream->output.currentBufferIndex, stream->output.bufferCount ); |
| 2802 |
|
| 2803 |
for( i=0; i < stream->output.bufferCount - 1; ++i ) |
| 2804 |
{ |
| 2805 |
for( j=0; j < stream->output.deviceCount; ++j ) |
| 2806 |
{ |
| 2807 |
if( stream->output.waveHeaders[j][ stream->output.currentBufferIndex ].lpData |
| 2808 |
!= stream->output.waveHeaders[j][ previousBufferIndex ].lpData ) |
| 2809 |
{ |
| 2810 |
CopyMemory( stream->output.waveHeaders[j][ stream->output.currentBufferIndex ].lpData, |
| 2811 |
stream->output.waveHeaders[j][ previousBufferIndex ].lpData, |
| 2812 |
stream->output.waveHeaders[j][ stream->output.currentBufferIndex ].dwBufferLength ); |
| 2813 |
} |
| 2814 |
} |
| 2815 |
|
| 2816 |
result = AdvanceToNextOutputBuffer( stream ); |
| 2817 |
if( result != paNoError ) |
| 2818 |
break; |
| 2819 |
} |
| 2820 |
|
| 2821 |
return result; |
| 2822 |
} |
| 2823 |
|
| 2824 |
|
| 2825 |
PA_THREAD_FUNC ProcessingThreadProc( void *pArg ) |
| 2826 |
{ |
| 2827 |
PaWinMmeStream *stream = (PaWinMmeStream *)pArg; |
| 2828 |
HANDLE events[3]; |
| 2829 |
int eventCount = 0; |
| 2830 |
DWORD result = paNoError; |
| 2831 |
DWORD waitResult; |
| 2832 |
DWORD timeout = (unsigned long)(stream->allBuffersDurationMs * 0.5); |
| 2833 |
int hostBuffersAvailable; |
| 2834 |
signed int hostInputBufferIndex, hostOutputBufferIndex; |
| 2835 |
PaStreamCallbackFlags statusFlags; |
| 2836 |
int callbackResult; |
| 2837 |
int done = 0; |
| 2838 |
unsigned int channel, i; |
| 2839 |
unsigned long framesProcessed; |
| 2840 |
|
| 2841 |
/* prepare event array for call to WaitForMultipleObjects() */ |
| 2842 |
if( stream->input.bufferEvent ) |
| 2843 |
events[eventCount++] = stream->input.bufferEvent; |
| 2844 |
if( stream->output.bufferEvent ) |
| 2845 |
events[eventCount++] = stream->output.bufferEvent; |
| 2846 |
events[eventCount++] = stream->abortEvent; |
| 2847 |
|
| 2848 |
statusFlags = 0; /** @todo support paInputUnderflow, paOutputOverflow and paNeverDropInput */ |
| 2849 |
|
| 2850 |
/* loop until something causes us to stop */ |
| 2851 |
do{ |
| 2852 |
/* wait for MME to signal that a buffer is available, or for |
| 2853 |
the PA abort event to be signaled. |
| 2854 |
|
| 2855 |
When this indicates that one or more buffers are available |
| 2856 |
NoBuffersAreQueued() and Current*BuffersAreDone are used below to |
| 2857 |
poll for additional done buffers. NoBuffersAreQueued() will fail |
| 2858 |
to identify an underrun/overflow if the driver doesn't mark all done |
| 2859 |
buffers prior to signalling the event. Some drivers do this |
| 2860 |
(eg RME Digi96, and others don't eg VIA PC 97 input). This isn't a |
| 2861 |
huge problem, it just means that we won't always be able to detect |
| 2862 |
underflow/overflow. |
| 2863 |
*/ |
| 2864 |
waitResult = WaitForMultipleObjects( eventCount, events, FALSE /* wait all = FALSE */, timeout ); |
| 2865 |
if( waitResult == WAIT_FAILED ) |
| 2866 |
{ |
| 2867 |
result = paUnanticipatedHostError; |
| 2868 |
/** @todo FIXME/REVIEW: can't return host error info from an asynchronous thread. see http://www.portaudio.com/trac/ticket/143 */ |
| 2869 |
done = 1; |
| 2870 |
} |
| 2871 |
else if( waitResult == WAIT_TIMEOUT ) |
| 2872 |
{ |
| 2873 |
/* if a timeout is encountered, continue */ |
| 2874 |
} |
| 2875 |
|
| 2876 |
if( stream->abortProcessing ) |
| 2877 |
{ |
| 2878 |
/* Pa_AbortStream() has been called, stop processing immediately */ |
| 2879 |
done = 1; |
| 2880 |
} |
| 2881 |
else if( stream->stopProcessing ) |
| 2882 |
{ |
| 2883 |
/* Pa_StopStream() has been called or the user callback returned |
| 2884 |
non-zero, processing will continue until all output buffers |
| 2885 |
are marked as done. The stream will stop immediately if it |
| 2886 |
is input-only. |
| 2887 |
*/ |
| 2888 |
|
| 2889 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 2890 |
{ |
| 2891 |
if( NoBuffersAreQueued( &stream->output ) ) |
| 2892 |
done = 1; /* Will cause thread to return. */ |
| 2893 |
} |
| 2894 |
else |
| 2895 |
{ |
| 2896 |
/* input only stream */ |
| 2897 |
done = 1; /* Will cause thread to return. */ |
| 2898 |
} |
| 2899 |
} |
| 2900 |
else |
| 2901 |
{ |
| 2902 |
hostBuffersAvailable = 1; |
| 2903 |
|
| 2904 |
/* process all available host buffers */ |
| 2905 |
do |
| 2906 |
{ |
| 2907 |
hostInputBufferIndex = -1; |
| 2908 |
hostOutputBufferIndex = -1; |
| 2909 |
|
| 2910 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 2911 |
{ |
| 2912 |
if( CurrentInputBuffersAreDone( stream ) ) |
| 2913 |
{ |
| 2914 |
if( NoBuffersAreQueued( &stream->input ) ) |
| 2915 |
{ |
| 2916 |
/** @todo |
| 2917 |
if all of the other buffers are also ready then |
| 2918 |
we discard all but the most recent. This is an |
| 2919 |
input buffer overflow. FIXME: these buffers should |
| 2920 |
be passed to the callback in a paNeverDropInput |
| 2921 |
stream. http://www.portaudio.com/trac/ticket/142 |
| 2922 |
|
| 2923 |
note that it is also possible for an input overflow |
| 2924 |
to happen while the callback is processing a buffer. |
| 2925 |
that is handled further down. |
| 2926 |
*/ |
| 2927 |
result = CatchUpInputBuffers( stream ); |
| 2928 |
if( result != paNoError ) |
| 2929 |
done = 1; |
| 2930 |
|
| 2931 |
statusFlags |= paInputOverflow; |
| 2932 |
} |
| 2933 |
|
| 2934 |
hostInputBufferIndex = stream->input.currentBufferIndex; |
| 2935 |
} |
| 2936 |
} |
| 2937 |
|
| 2938 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 2939 |
{ |
| 2940 |
if( CurrentOutputBuffersAreDone( stream ) ) |
| 2941 |
{ |
| 2942 |
/* ok, we have an output buffer */ |
| 2943 |
|
| 2944 |
if( NoBuffersAreQueued( &stream->output ) ) |
| 2945 |
{ |
| 2946 |
/* |
| 2947 |
if all of the other buffers are also ready, catch up by copying |
| 2948 |
the most recently generated buffer into all but one of the output |
| 2949 |
buffers. |
| 2950 |
|
| 2951 |
note that this catch up code only handles the case where all |
| 2952 |
buffers have been played out due to this thread not having |
| 2953 |
woken up at all. a more common case occurs when this thread |
| 2954 |
is woken up, processes one buffer, but takes too long, and as |
| 2955 |
a result all the other buffers have become un-queued. that |
| 2956 |
case is handled further down. |
| 2957 |
*/ |
| 2958 |
|
| 2959 |
result = CatchUpOutputBuffers( stream ); |
| 2960 |
if( result != paNoError ) |
| 2961 |
done = 1; |
| 2962 |
|
| 2963 |
statusFlags |= paOutputUnderflow; |
| 2964 |
} |
| 2965 |
|
| 2966 |
hostOutputBufferIndex = stream->output.currentBufferIndex; |
| 2967 |
} |
| 2968 |
} |
| 2969 |
|
| 2970 |
|
| 2971 |
if( (PA_IS_FULL_DUPLEX_STREAM_(stream) && hostInputBufferIndex != -1 && hostOutputBufferIndex != -1) || |
| 2972 |
(PA_IS_HALF_DUPLEX_STREAM_(stream) && ( hostInputBufferIndex != -1 || hostOutputBufferIndex != -1 ) ) ) |
| 2973 |
{ |
| 2974 |
PaStreamCallbackTimeInfo timeInfo = {0,0,0}; /** @todo implement inputBufferAdcTime */ |
| 2975 |
|
| 2976 |
|
| 2977 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 2978 |
{ |
| 2979 |
/* set timeInfo.currentTime and calculate timeInfo.outputBufferDacTime |
| 2980 |
from the current wave out position */ |
| 2981 |
MMTIME mmtime; |
| 2982 |
double timeBeforeGetPosition, timeAfterGetPosition; |
| 2983 |
double time; |
| 2984 |
long framesInBufferRing; |
| 2985 |
long writePosition; |
| 2986 |
long playbackPosition; |
| 2987 |
HWAVEOUT firstWaveOutDevice = ((HWAVEOUT*)stream->output.waveHandles)[0]; |
| 2988 |
|
| 2989 |
mmtime.wType = TIME_SAMPLES; |
| 2990 |
timeBeforeGetPosition = PaUtil_GetTime(); |
| 2991 |
waveOutGetPosition( firstWaveOutDevice, &mmtime, sizeof(MMTIME) ); |
| 2992 |
timeAfterGetPosition = PaUtil_GetTime(); |
| 2993 |
|
| 2994 |
timeInfo.currentTime = timeAfterGetPosition; |
| 2995 |
|
| 2996 |
/* approximate time at which wave out position was measured |
| 2997 |
as half way between timeBeforeGetPosition and timeAfterGetPosition */ |
| 2998 |
time = timeBeforeGetPosition + (timeAfterGetPosition - timeBeforeGetPosition) * .5; |
| 2999 |
|
| 3000 |
framesInBufferRing = stream->output.bufferCount * stream->bufferProcessor.framesPerHostBuffer; |
| 3001 |
playbackPosition = mmtime.u.sample % framesInBufferRing; |
| 3002 |
|
| 3003 |
writePosition = stream->output.currentBufferIndex * stream->bufferProcessor.framesPerHostBuffer |
| 3004 |
+ stream->output.framesUsedInCurrentBuffer; |
| 3005 |
|
| 3006 |
if( playbackPosition >= writePosition ){ |
| 3007 |
timeInfo.outputBufferDacTime = |
| 3008 |
time + ((double)( writePosition + (framesInBufferRing - playbackPosition) ) * stream->bufferProcessor.samplePeriod ); |
| 3009 |
}else{ |
| 3010 |
timeInfo.outputBufferDacTime = |
| 3011 |
time + ((double)( writePosition - playbackPosition ) * stream->bufferProcessor.samplePeriod ); |
| 3012 |
} |
| 3013 |
} |
| 3014 |
|
| 3015 |
|
| 3016 |
PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer ); |
| 3017 |
|
| 3018 |
PaUtil_BeginBufferProcessing( &stream->bufferProcessor, &timeInfo, statusFlags ); |
| 3019 |
|
| 3020 |
/* reset status flags once they have been passed to the buffer processor */ |
| 3021 |
statusFlags = 0; |
| 3022 |
|
| 3023 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3024 |
{ |
| 3025 |
PaUtil_SetInputFrameCount( &stream->bufferProcessor, 0 /* default to host buffer size */ ); |
| 3026 |
|
| 3027 |
channel = 0; |
| 3028 |
for( i=0; i<stream->input.deviceCount; ++i ) |
| 3029 |
{ |
| 3030 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3031 |
int channelCount = (int)stream->input.waveHeaders[i][ hostInputBufferIndex ].dwUser; |
| 3032 |
|
| 3033 |
PaUtil_SetInterleavedInputChannels( &stream->bufferProcessor, channel, |
| 3034 |
stream->input.waveHeaders[i][ hostInputBufferIndex ].lpData + |
| 3035 |
stream->input.framesUsedInCurrentBuffer * channelCount * |
| 3036 |
stream->bufferProcessor.bytesPerHostInputSample, |
| 3037 |
channelCount ); |
| 3038 |
|
| 3039 |
|
| 3040 |
channel += channelCount; |
| 3041 |
} |
| 3042 |
} |
| 3043 |
|
| 3044 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3045 |
{ |
| 3046 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, 0 /* default to host buffer size */ ); |
| 3047 |
|
| 3048 |
channel = 0; |
| 3049 |
for( i=0; i<stream->output.deviceCount; ++i ) |
| 3050 |
{ |
| 3051 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3052 |
int channelCount = (int)stream->output.waveHeaders[i][ hostOutputBufferIndex ].dwUser; |
| 3053 |
|
| 3054 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, channel, |
| 3055 |
stream->output.waveHeaders[i][ hostOutputBufferIndex ].lpData + |
| 3056 |
stream->output.framesUsedInCurrentBuffer * channelCount * |
| 3057 |
stream->bufferProcessor.bytesPerHostOutputSample, |
| 3058 |
channelCount ); |
| 3059 |
|
| 3060 |
channel += channelCount; |
| 3061 |
} |
| 3062 |
} |
| 3063 |
|
| 3064 |
callbackResult = paContinue; |
| 3065 |
framesProcessed = PaUtil_EndBufferProcessing( &stream->bufferProcessor, &callbackResult ); |
| 3066 |
|
| 3067 |
stream->input.framesUsedInCurrentBuffer += framesProcessed; |
| 3068 |
stream->output.framesUsedInCurrentBuffer += framesProcessed; |
| 3069 |
|
| 3070 |
PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesProcessed ); |
| 3071 |
|
| 3072 |
if( callbackResult == paContinue ) |
| 3073 |
{ |
| 3074 |
/* nothing special to do */ |
| 3075 |
} |
| 3076 |
else if( callbackResult == paAbort ) |
| 3077 |
{ |
| 3078 |
stream->abortProcessing = 1; |
| 3079 |
done = 1; |
| 3080 |
/** @todo FIXME: should probably reset the output device immediately once the callback returns paAbort |
| 3081 |
see: http://www.portaudio.com/trac/ticket/141 |
| 3082 |
*/ |
| 3083 |
result = paNoError; |
| 3084 |
} |
| 3085 |
else |
| 3086 |
{ |
| 3087 |
/* User callback has asked us to stop with paComplete or other non-zero value */ |
| 3088 |
stream->stopProcessing = 1; /* stop once currently queued audio has finished */ |
| 3089 |
result = paNoError; |
| 3090 |
} |
| 3091 |
|
| 3092 |
|
| 3093 |
if( PA_IS_INPUT_STREAM_(stream) |
| 3094 |
&& stream->stopProcessing == 0 && stream->abortProcessing == 0 |
| 3095 |
&& stream->input.framesUsedInCurrentBuffer == stream->input.framesPerBuffer ) |
| 3096 |
{ |
| 3097 |
if( NoBuffersAreQueued( &stream->input ) ) |
| 3098 |
{ |
| 3099 |
/** @todo need to handle PaNeverDropInput here where necessary */ |
| 3100 |
result = CatchUpInputBuffers( stream ); |
| 3101 |
if( result != paNoError ) |
| 3102 |
done = 1; |
| 3103 |
|
| 3104 |
statusFlags |= paInputOverflow; |
| 3105 |
} |
| 3106 |
|
| 3107 |
result = AdvanceToNextInputBuffer( stream ); |
| 3108 |
if( result != paNoError ) |
| 3109 |
done = 1; |
| 3110 |
} |
| 3111 |
|
| 3112 |
|
| 3113 |
if( PA_IS_OUTPUT_STREAM_(stream) && !stream->abortProcessing ) |
| 3114 |
{ |
| 3115 |
if( stream->stopProcessing && |
| 3116 |
stream->output.framesUsedInCurrentBuffer < stream->output.framesPerBuffer ) |
| 3117 |
{ |
| 3118 |
/* zero remaining samples in output output buffer and flush */ |
| 3119 |
|
| 3120 |
stream->output.framesUsedInCurrentBuffer += PaUtil_ZeroOutput( &stream->bufferProcessor, |
| 3121 |
stream->output.framesPerBuffer - stream->output.framesUsedInCurrentBuffer ); |
| 3122 |
|
| 3123 |
/* we send the entire buffer to the output devices, but we could |
| 3124 |
just send a partial buffer, rather than zeroing the unused |
| 3125 |
samples. |
| 3126 |
*/ |
| 3127 |
} |
| 3128 |
|
| 3129 |
if( stream->output.framesUsedInCurrentBuffer == stream->output.framesPerBuffer ) |
| 3130 |
{ |
| 3131 |
/* check for underflow before enquing the just-generated buffer, |
| 3132 |
but recover from underflow after enquing it. This ensures |
| 3133 |
that the most recent audio segment is repeated */ |
| 3134 |
int outputUnderflow = NoBuffersAreQueued( &stream->output ); |
| 3135 |
|
| 3136 |
result = AdvanceToNextOutputBuffer( stream ); |
| 3137 |
if( result != paNoError ) |
| 3138 |
done = 1; |
| 3139 |
|
| 3140 |
if( outputUnderflow && !done && !stream->stopProcessing ) |
| 3141 |
{ |
| 3142 |
/* Recover from underflow in the case where the |
| 3143 |
underflow occurred while processing the buffer |
| 3144 |
we just finished */ |
| 3145 |
|
| 3146 |
result = CatchUpOutputBuffers( stream ); |
| 3147 |
if( result != paNoError ) |
| 3148 |
done = 1; |
| 3149 |
|
| 3150 |
statusFlags |= paOutputUnderflow; |
| 3151 |
} |
| 3152 |
} |
| 3153 |
} |
| 3154 |
|
| 3155 |
if( stream->throttleProcessingThreadOnOverload != 0 ) |
| 3156 |
{ |
| 3157 |
if( stream->stopProcessing || stream->abortProcessing ) |
| 3158 |
{ |
| 3159 |
if( stream->processingThreadPriority != stream->highThreadPriority ) |
| 3160 |
{ |
| 3161 |
SetThreadPriority( stream->processingThread, stream->highThreadPriority ); |
| 3162 |
stream->processingThreadPriority = stream->highThreadPriority; |
| 3163 |
} |
| 3164 |
} |
| 3165 |
else if( PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ) > 1. ) |
| 3166 |
{ |
| 3167 |
if( stream->processingThreadPriority != stream->throttledThreadPriority ) |
| 3168 |
{ |
| 3169 |
SetThreadPriority( stream->processingThread, stream->throttledThreadPriority ); |
| 3170 |
stream->processingThreadPriority = stream->throttledThreadPriority; |
| 3171 |
} |
| 3172 |
|
| 3173 |
/* sleep to give other processes a go */ |
| 3174 |
Sleep( stream->throttledSleepMsecs ); |
| 3175 |
} |
| 3176 |
else |
| 3177 |
{ |
| 3178 |
if( stream->processingThreadPriority != stream->highThreadPriority ) |
| 3179 |
{ |
| 3180 |
SetThreadPriority( stream->processingThread, stream->highThreadPriority ); |
| 3181 |
stream->processingThreadPriority = stream->highThreadPriority; |
| 3182 |
} |
| 3183 |
} |
| 3184 |
} |
| 3185 |
} |
| 3186 |
else |
| 3187 |
{ |
| 3188 |
hostBuffersAvailable = 0; |
| 3189 |
} |
| 3190 |
} |
| 3191 |
while( hostBuffersAvailable && |
| 3192 |
stream->stopProcessing == 0 && |
| 3193 |
stream->abortProcessing == 0 && |
| 3194 |
!done ); |
| 3195 |
} |
| 3196 |
} |
| 3197 |
while( !done ); |
| 3198 |
|
| 3199 |
stream->isActive = 0; |
| 3200 |
|
| 3201 |
if( stream->streamRepresentation.streamFinishedCallback != 0 ) |
| 3202 |
stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData ); |
| 3203 |
|
| 3204 |
PaUtil_ResetCpuLoadMeasurer( &stream->cpuLoadMeasurer ); |
| 3205 |
|
| 3206 |
return result; |
| 3207 |
} |
| 3208 |
|
| 3209 |
|
| 3210 |
/* |
| 3211 |
When CloseStream() is called, the multi-api layer ensures that |
| 3212 |
the stream has already been stopped or aborted. |
| 3213 |
*/ |
| 3214 |
static PaError CloseStream( PaStream* s ) |
| 3215 |
{ |
| 3216 |
PaError result; |
| 3217 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3218 |
|
| 3219 |
result = CloseHandleWithPaError( stream->abortEvent ); |
| 3220 |
if( result != paNoError ) goto error; |
| 3221 |
|
| 3222 |
TerminateWaveHeaders( &stream->output, 0 /* not isInput */ ); |
| 3223 |
TerminateWaveHeaders( &stream->input, 1 /* isInput */ ); |
| 3224 |
|
| 3225 |
TerminateWaveHandles( &stream->output, 0 /* not isInput */, 0 /* not currentlyProcessingAnError */ ); |
| 3226 |
TerminateWaveHandles( &stream->input, 1 /* isInput */, 0 /* not currentlyProcessingAnError */ ); |
| 3227 |
|
| 3228 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 3229 |
PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation ); |
| 3230 |
PaUtil_FreeMemory( stream ); |
| 3231 |
|
| 3232 |
error: |
| 3233 |
/** @todo REVIEW: what is the best way to clean up a stream if an error is detected? */ |
| 3234 |
return result; |
| 3235 |
} |
| 3236 |
|
| 3237 |
|
| 3238 |
static PaError StartStream( PaStream *s ) |
| 3239 |
{ |
| 3240 |
PaError result; |
| 3241 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3242 |
MMRESULT mmresult; |
| 3243 |
unsigned int i, j; |
| 3244 |
int callbackResult; |
| 3245 |
unsigned int channel; |
| 3246 |
unsigned long framesProcessed; |
| 3247 |
PaStreamCallbackTimeInfo timeInfo = {0,0,0}; /** @todo implement this for stream priming */ |
| 3248 |
|
| 3249 |
PaUtil_ResetBufferProcessor( &stream->bufferProcessor ); |
| 3250 |
|
| 3251 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3252 |
{ |
| 3253 |
for( i=0; i<stream->input.bufferCount; ++i ) |
| 3254 |
{ |
| 3255 |
for( j=0; j<stream->input.deviceCount; ++j ) |
| 3256 |
{ |
| 3257 |
stream->input.waveHeaders[j][i].dwFlags &= ~WHDR_DONE; |
| 3258 |
mmresult = waveInAddBuffer( ((HWAVEIN*)stream->input.waveHandles)[j], &stream->input.waveHeaders[j][i], sizeof(WAVEHDR) ); |
| 3259 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3260 |
{ |
| 3261 |
result = paUnanticipatedHostError; |
| 3262 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 3263 |
goto error; |
| 3264 |
} |
| 3265 |
} |
| 3266 |
} |
| 3267 |
stream->input.currentBufferIndex = 0; |
| 3268 |
stream->input.framesUsedInCurrentBuffer = 0; |
| 3269 |
} |
| 3270 |
|
| 3271 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3272 |
{ |
| 3273 |
for( i=0; i<stream->output.deviceCount; ++i ) |
| 3274 |
{ |
| 3275 |
if( (mmresult = waveOutPause( ((HWAVEOUT*)stream->output.waveHandles)[i] )) != MMSYSERR_NOERROR ) |
| 3276 |
{ |
| 3277 |
result = paUnanticipatedHostError; |
| 3278 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 3279 |
goto error; |
| 3280 |
} |
| 3281 |
} |
| 3282 |
|
| 3283 |
for( i=0; i<stream->output.bufferCount; ++i ) |
| 3284 |
{ |
| 3285 |
if( stream->primeStreamUsingCallback ) |
| 3286 |
{ |
| 3287 |
|
| 3288 |
stream->output.framesUsedInCurrentBuffer = 0; |
| 3289 |
do{ |
| 3290 |
|
| 3291 |
PaUtil_BeginBufferProcessing( &stream->bufferProcessor, |
| 3292 |
&timeInfo, |
| 3293 |
paPrimingOutput | ((stream->input.bufferCount > 0 ) ? paInputUnderflow : 0)); |
| 3294 |
|
| 3295 |
if( stream->input.bufferCount > 0 ) |
| 3296 |
PaUtil_SetNoInput( &stream->bufferProcessor ); |
| 3297 |
|
| 3298 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, 0 /* default to host buffer size */ ); |
| 3299 |
|
| 3300 |
channel = 0; |
| 3301 |
for( j=0; j<stream->output.deviceCount; ++j ) |
| 3302 |
{ |
| 3303 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3304 |
int channelCount = (int)stream->output.waveHeaders[j][i].dwUser; |
| 3305 |
|
| 3306 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, channel, |
| 3307 |
stream->output.waveHeaders[j][i].lpData + |
| 3308 |
stream->output.framesUsedInCurrentBuffer * channelCount * |
| 3309 |
stream->bufferProcessor.bytesPerHostOutputSample, |
| 3310 |
channelCount ); |
| 3311 |
|
| 3312 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3313 |
channel += channelCount; |
| 3314 |
} |
| 3315 |
|
| 3316 |
callbackResult = paContinue; |
| 3317 |
framesProcessed = PaUtil_EndBufferProcessing( &stream->bufferProcessor, &callbackResult ); |
| 3318 |
stream->output.framesUsedInCurrentBuffer += framesProcessed; |
| 3319 |
|
| 3320 |
if( callbackResult != paContinue ) |
| 3321 |
{ |
| 3322 |
/** @todo fix this, what do we do if callback result is non-zero during stream |
| 3323 |
priming? |
| 3324 |
|
| 3325 |
for complete: play out primed waveHeaders as usual |
| 3326 |
for abort: clean up immediately. |
| 3327 |
*/ |
| 3328 |
} |
| 3329 |
|
| 3330 |
}while( stream->output.framesUsedInCurrentBuffer != stream->output.framesPerBuffer ); |
| 3331 |
|
| 3332 |
} |
| 3333 |
else |
| 3334 |
{ |
| 3335 |
for( j=0; j<stream->output.deviceCount; ++j ) |
| 3336 |
{ |
| 3337 |
ZeroMemory( stream->output.waveHeaders[j][i].lpData, stream->output.waveHeaders[j][i].dwBufferLength ); |
| 3338 |
} |
| 3339 |
} |
| 3340 |
|
| 3341 |
/* we queue all channels of a single buffer frame (across all |
| 3342 |
devices, because some multidevice multichannel drivers work |
| 3343 |
better this way */ |
| 3344 |
for( j=0; j<stream->output.deviceCount; ++j ) |
| 3345 |
{ |
| 3346 |
mmresult = waveOutWrite( ((HWAVEOUT*)stream->output.waveHandles)[j], &stream->output.waveHeaders[j][i], sizeof(WAVEHDR) ); |
| 3347 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3348 |
{ |
| 3349 |
result = paUnanticipatedHostError; |
| 3350 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 3351 |
goto error; |
| 3352 |
} |
| 3353 |
} |
| 3354 |
} |
| 3355 |
stream->output.currentBufferIndex = 0; |
| 3356 |
stream->output.framesUsedInCurrentBuffer = 0; |
| 3357 |
} |
| 3358 |
|
| 3359 |
|
| 3360 |
stream->isStopped = 0; |
| 3361 |
stream->isActive = 1; |
| 3362 |
stream->stopProcessing = 0; |
| 3363 |
stream->abortProcessing = 0; |
| 3364 |
|
| 3365 |
result = ResetEventWithPaError( stream->input.bufferEvent ); |
| 3366 |
if( result != paNoError ) goto error; |
| 3367 |
|
| 3368 |
result = ResetEventWithPaError( stream->output.bufferEvent ); |
| 3369 |
if( result != paNoError ) goto error; |
| 3370 |
|
| 3371 |
|
| 3372 |
if( stream->streamRepresentation.streamCallback ) |
| 3373 |
{ |
| 3374 |
/* callback stream */ |
| 3375 |
|
| 3376 |
result = ResetEventWithPaError( stream->abortEvent ); |
| 3377 |
if( result != paNoError ) goto error; |
| 3378 |
|
| 3379 |
/* Create thread that waits for audio buffers to be ready for processing. */ |
| 3380 |
stream->processingThread = CREATE_THREAD; |
| 3381 |
if( !stream->processingThread ) |
| 3382 |
{ |
| 3383 |
result = paUnanticipatedHostError; |
| 3384 |
PA_MME_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3385 |
goto error; |
| 3386 |
} |
| 3387 |
|
| 3388 |
/** @todo could have mme specific stream parameters to allow the user |
| 3389 |
to set the callback thread priorities */ |
| 3390 |
stream->highThreadPriority = THREAD_PRIORITY_TIME_CRITICAL; |
| 3391 |
stream->throttledThreadPriority = THREAD_PRIORITY_NORMAL; |
| 3392 |
|
| 3393 |
if( !SetThreadPriority( stream->processingThread, stream->highThreadPriority ) ) |
| 3394 |
{ |
| 3395 |
result = paUnanticipatedHostError; |
| 3396 |
PA_MME_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3397 |
goto error; |
| 3398 |
} |
| 3399 |
stream->processingThreadPriority = stream->highThreadPriority; |
| 3400 |
} |
| 3401 |
else |
| 3402 |
{ |
| 3403 |
/* blocking read/write stream */ |
| 3404 |
|
| 3405 |
} |
| 3406 |
|
| 3407 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3408 |
{ |
| 3409 |
for( i=0; i < stream->input.deviceCount; ++i ) |
| 3410 |
{ |
| 3411 |
mmresult = waveInStart( ((HWAVEIN*)stream->input.waveHandles)[i] ); |
| 3412 |
PA_DEBUG(("Pa_StartStream: waveInStart returned = 0x%X.\n", mmresult)); |
| 3413 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3414 |
{ |
| 3415 |
result = paUnanticipatedHostError; |
| 3416 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 3417 |
goto error; |
| 3418 |
} |
| 3419 |
} |
| 3420 |
} |
| 3421 |
|
| 3422 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3423 |
{ |
| 3424 |
for( i=0; i < stream->output.deviceCount; ++i ) |
| 3425 |
{ |
| 3426 |
if( (mmresult = waveOutRestart( ((HWAVEOUT*)stream->output.waveHandles)[i] )) != MMSYSERR_NOERROR ) |
| 3427 |
{ |
| 3428 |
result = paUnanticipatedHostError; |
| 3429 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 3430 |
goto error; |
| 3431 |
} |
| 3432 |
} |
| 3433 |
} |
| 3434 |
|
| 3435 |
return result; |
| 3436 |
|
| 3437 |
error: |
| 3438 |
/** @todo FIXME: implement recovery as best we can |
| 3439 |
This should involve rolling back to a state as-if this function had never been called |
| 3440 |
*/ |
| 3441 |
return result; |
| 3442 |
} |
| 3443 |
|
| 3444 |
|
| 3445 |
static PaError StopStream( PaStream *s ) |
| 3446 |
{ |
| 3447 |
PaError result = paNoError; |
| 3448 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3449 |
int timeout; |
| 3450 |
DWORD waitResult; |
| 3451 |
MMRESULT mmresult; |
| 3452 |
signed int hostOutputBufferIndex; |
| 3453 |
unsigned int channel, waitCount, i; |
| 3454 |
|
| 3455 |
/** @todo |
| 3456 |
REVIEW: the error checking in this function needs review. the basic |
| 3457 |
idea is to return from this function in a known state - for example |
| 3458 |
there is no point avoiding calling waveInReset just because |
| 3459 |
the thread times out. |
| 3460 |
*/ |
| 3461 |
|
| 3462 |
if( stream->processingThread ) |
| 3463 |
{ |
| 3464 |
/* callback stream */ |
| 3465 |
|
| 3466 |
/* Tell processing thread to stop generating more data and to let current data play out. */ |
| 3467 |
stream->stopProcessing = 1; |
| 3468 |
|
| 3469 |
/* Calculate timeOut longer than longest time it could take to return all buffers. */ |
| 3470 |
timeout = (int)(stream->allBuffersDurationMs * 1.5); |
| 3471 |
if( timeout < PA_MME_MIN_TIMEOUT_MSEC_ ) |
| 3472 |
timeout = PA_MME_MIN_TIMEOUT_MSEC_; |
| 3473 |
|
| 3474 |
PA_DEBUG(("WinMME StopStream: waiting for background thread.\n")); |
| 3475 |
|
| 3476 |
waitResult = WaitForSingleObject( stream->processingThread, timeout ); |
| 3477 |
if( waitResult == WAIT_TIMEOUT ) |
| 3478 |
{ |
| 3479 |
/* try to abort */ |
| 3480 |
stream->abortProcessing = 1; |
| 3481 |
SetEvent( stream->abortEvent ); |
| 3482 |
waitResult = WaitForSingleObject( stream->processingThread, timeout ); |
| 3483 |
if( waitResult == WAIT_TIMEOUT ) |
| 3484 |
{ |
| 3485 |
PA_DEBUG(("WinMME StopStream: timed out while waiting for background thread to finish.\n")); |
| 3486 |
result = paTimedOut; |
| 3487 |
} |
| 3488 |
} |
| 3489 |
|
| 3490 |
CloseHandle( stream->processingThread ); |
| 3491 |
stream->processingThread = NULL; |
| 3492 |
} |
| 3493 |
else |
| 3494 |
{ |
| 3495 |
/* blocking read / write stream */ |
| 3496 |
|
| 3497 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3498 |
{ |
| 3499 |
if( stream->output.framesUsedInCurrentBuffer > 0 ) |
| 3500 |
{ |
| 3501 |
/* there are still unqueued frames in the current buffer, so flush them */ |
| 3502 |
|
| 3503 |
hostOutputBufferIndex = stream->output.currentBufferIndex; |
| 3504 |
|
| 3505 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, |
| 3506 |
stream->output.framesPerBuffer - stream->output.framesUsedInCurrentBuffer ); |
| 3507 |
|
| 3508 |
channel = 0; |
| 3509 |
for( i=0; i<stream->output.deviceCount; ++i ) |
| 3510 |
{ |
| 3511 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3512 |
int channelCount = (int)stream->output.waveHeaders[i][ hostOutputBufferIndex ].dwUser; |
| 3513 |
|
| 3514 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, channel, |
| 3515 |
stream->output.waveHeaders[i][ hostOutputBufferIndex ].lpData + |
| 3516 |
stream->output.framesUsedInCurrentBuffer * channelCount * |
| 3517 |
stream->bufferProcessor.bytesPerHostOutputSample, |
| 3518 |
channelCount ); |
| 3519 |
|
| 3520 |
channel += channelCount; |
| 3521 |
} |
| 3522 |
|
| 3523 |
PaUtil_ZeroOutput( &stream->bufferProcessor, |
| 3524 |
stream->output.framesPerBuffer - stream->output.framesUsedInCurrentBuffer ); |
| 3525 |
|
| 3526 |
/* we send the entire buffer to the output devices, but we could |
| 3527 |
just send a partial buffer, rather than zeroing the unused |
| 3528 |
samples. |
| 3529 |
*/ |
| 3530 |
AdvanceToNextOutputBuffer( stream ); |
| 3531 |
} |
| 3532 |
|
| 3533 |
|
| 3534 |
timeout = (stream->allBuffersDurationMs / stream->output.bufferCount) + 1; |
| 3535 |
if( timeout < PA_MME_MIN_TIMEOUT_MSEC_ ) |
| 3536 |
timeout = PA_MME_MIN_TIMEOUT_MSEC_; |
| 3537 |
|
| 3538 |
waitCount = 0; |
| 3539 |
while( !NoBuffersAreQueued( &stream->output ) && waitCount <= stream->output.bufferCount ) |
| 3540 |
{ |
| 3541 |
/* wait for MME to signal that a buffer is available */ |
| 3542 |
waitResult = WaitForSingleObject( stream->output.bufferEvent, timeout ); |
| 3543 |
if( waitResult == WAIT_FAILED ) |
| 3544 |
{ |
| 3545 |
break; |
| 3546 |
} |
| 3547 |
else if( waitResult == WAIT_TIMEOUT ) |
| 3548 |
{ |
| 3549 |
/* keep waiting */ |
| 3550 |
} |
| 3551 |
|
| 3552 |
++waitCount; |
| 3553 |
} |
| 3554 |
} |
| 3555 |
} |
| 3556 |
|
| 3557 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3558 |
{ |
| 3559 |
for( i =0; i < stream->output.deviceCount; ++i ) |
| 3560 |
{ |
| 3561 |
mmresult = waveOutReset( ((HWAVEOUT*)stream->output.waveHandles)[i] ); |
| 3562 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3563 |
{ |
| 3564 |
result = paUnanticipatedHostError; |
| 3565 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 3566 |
} |
| 3567 |
} |
| 3568 |
} |
| 3569 |
|
| 3570 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3571 |
{ |
| 3572 |
for( i=0; i < stream->input.deviceCount; ++i ) |
| 3573 |
{ |
| 3574 |
mmresult = waveInReset( ((HWAVEIN*)stream->input.waveHandles)[i] ); |
| 3575 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3576 |
{ |
| 3577 |
result = paUnanticipatedHostError; |
| 3578 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 3579 |
} |
| 3580 |
} |
| 3581 |
} |
| 3582 |
|
| 3583 |
stream->isStopped = 1; |
| 3584 |
stream->isActive = 0; |
| 3585 |
|
| 3586 |
return result; |
| 3587 |
} |
| 3588 |
|
| 3589 |
|
| 3590 |
static PaError AbortStream( PaStream *s ) |
| 3591 |
{ |
| 3592 |
PaError result = paNoError; |
| 3593 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3594 |
int timeout; |
| 3595 |
DWORD waitResult; |
| 3596 |
MMRESULT mmresult; |
| 3597 |
unsigned int i; |
| 3598 |
|
| 3599 |
/** @todo |
| 3600 |
REVIEW: the error checking in this function needs review. the basic |
| 3601 |
idea is to return from this function in a known state - for example |
| 3602 |
there is no point avoiding calling waveInReset just because |
| 3603 |
the thread times out. |
| 3604 |
*/ |
| 3605 |
|
| 3606 |
if( stream->processingThread ) |
| 3607 |
{ |
| 3608 |
/* callback stream */ |
| 3609 |
|
| 3610 |
/* Tell processing thread to abort immediately */ |
| 3611 |
stream->abortProcessing = 1; |
| 3612 |
SetEvent( stream->abortEvent ); |
| 3613 |
} |
| 3614 |
|
| 3615 |
|
| 3616 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3617 |
{ |
| 3618 |
for( i =0; i < stream->output.deviceCount; ++i ) |
| 3619 |
{ |
| 3620 |
mmresult = waveOutReset( ((HWAVEOUT*)stream->output.waveHandles)[i] ); |
| 3621 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3622 |
{ |
| 3623 |
PA_MME_SET_LAST_WAVEOUT_ERROR( mmresult ); |
| 3624 |
return paUnanticipatedHostError; |
| 3625 |
} |
| 3626 |
} |
| 3627 |
} |
| 3628 |
|
| 3629 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3630 |
{ |
| 3631 |
for( i=0; i < stream->input.deviceCount; ++i ) |
| 3632 |
{ |
| 3633 |
mmresult = waveInReset( ((HWAVEIN*)stream->input.waveHandles)[i] ); |
| 3634 |
if( mmresult != MMSYSERR_NOERROR ) |
| 3635 |
{ |
| 3636 |
PA_MME_SET_LAST_WAVEIN_ERROR( mmresult ); |
| 3637 |
return paUnanticipatedHostError; |
| 3638 |
} |
| 3639 |
} |
| 3640 |
} |
| 3641 |
|
| 3642 |
|
| 3643 |
if( stream->processingThread ) |
| 3644 |
{ |
| 3645 |
/* callback stream */ |
| 3646 |
|
| 3647 |
PA_DEBUG(("WinMME AbortStream: waiting for background thread.\n")); |
| 3648 |
|
| 3649 |
/* Calculate timeOut longer than longest time it could take to return all buffers. */ |
| 3650 |
timeout = (int)(stream->allBuffersDurationMs * 1.5); |
| 3651 |
if( timeout < PA_MME_MIN_TIMEOUT_MSEC_ ) |
| 3652 |
timeout = PA_MME_MIN_TIMEOUT_MSEC_; |
| 3653 |
|
| 3654 |
waitResult = WaitForSingleObject( stream->processingThread, timeout ); |
| 3655 |
if( waitResult == WAIT_TIMEOUT ) |
| 3656 |
{ |
| 3657 |
PA_DEBUG(("WinMME AbortStream: timed out while waiting for background thread to finish.\n")); |
| 3658 |
return paTimedOut; |
| 3659 |
} |
| 3660 |
|
| 3661 |
CloseHandle( stream->processingThread ); |
| 3662 |
stream->processingThread = NULL; |
| 3663 |
} |
| 3664 |
|
| 3665 |
stream->isStopped = 1; |
| 3666 |
stream->isActive = 0; |
| 3667 |
|
| 3668 |
return result; |
| 3669 |
} |
| 3670 |
|
| 3671 |
|
| 3672 |
static PaError IsStreamStopped( PaStream *s ) |
| 3673 |
{ |
| 3674 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3675 |
|
| 3676 |
return stream->isStopped; |
| 3677 |
} |
| 3678 |
|
| 3679 |
|
| 3680 |
static PaError IsStreamActive( PaStream *s ) |
| 3681 |
{ |
| 3682 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3683 |
|
| 3684 |
return stream->isActive; |
| 3685 |
} |
| 3686 |
|
| 3687 |
|
| 3688 |
static PaTime GetStreamTime( PaStream *s ) |
| 3689 |
{ |
| 3690 |
(void) s; /* unused parameter */ |
| 3691 |
|
| 3692 |
return PaUtil_GetTime(); |
| 3693 |
} |
| 3694 |
|
| 3695 |
|
| 3696 |
static double GetStreamCpuLoad( PaStream* s ) |
| 3697 |
{ |
| 3698 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3699 |
|
| 3700 |
return PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ); |
| 3701 |
} |
| 3702 |
|
| 3703 |
|
| 3704 |
/* |
| 3705 |
As separate stream interfaces are used for blocking and callback |
| 3706 |
streams, the following functions can be guaranteed to only be called |
| 3707 |
for blocking streams. |
| 3708 |
*/ |
| 3709 |
|
| 3710 |
static PaError ReadStream( PaStream* s, |
| 3711 |
void *buffer, |
| 3712 |
unsigned long frames ) |
| 3713 |
{ |
| 3714 |
PaError result = paNoError; |
| 3715 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3716 |
void *userBuffer; |
| 3717 |
unsigned long framesRead = 0; |
| 3718 |
unsigned long framesProcessed; |
| 3719 |
signed int hostInputBufferIndex; |
| 3720 |
DWORD waitResult; |
| 3721 |
DWORD timeout = (unsigned long)(stream->allBuffersDurationMs * 0.5); |
| 3722 |
unsigned int channel, i; |
| 3723 |
|
| 3724 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3725 |
{ |
| 3726 |
/* make a local copy of the user buffer pointer(s). this is necessary |
| 3727 |
because PaUtil_CopyInput() advances these pointers every time |
| 3728 |
it is called. |
| 3729 |
*/ |
| 3730 |
if( stream->bufferProcessor.userInputIsInterleaved ) |
| 3731 |
{ |
| 3732 |
userBuffer = buffer; |
| 3733 |
} |
| 3734 |
else |
| 3735 |
{ |
| 3736 |
userBuffer = (void*)alloca( sizeof(void*) * stream->bufferProcessor.inputChannelCount ); |
| 3737 |
if( !userBuffer ) |
| 3738 |
return paInsufficientMemory; |
| 3739 |
for( i = 0; i<stream->bufferProcessor.inputChannelCount; ++i ) |
| 3740 |
((void**)userBuffer)[i] = ((void**)buffer)[i]; |
| 3741 |
} |
| 3742 |
|
| 3743 |
do{ |
| 3744 |
if( CurrentInputBuffersAreDone( stream ) ) |
| 3745 |
{ |
| 3746 |
if( NoBuffersAreQueued( &stream->input ) ) |
| 3747 |
{ |
| 3748 |
/** @todo REVIEW: consider what to do if the input overflows. |
| 3749 |
do we requeue all of the buffers? should we be running |
| 3750 |
a thread to make sure they are always queued? |
| 3751 |
see: http://www.portaudio.com/trac/ticket/117 |
| 3752 |
*/ |
| 3753 |
|
| 3754 |
result = paInputOverflowed; |
| 3755 |
} |
| 3756 |
|
| 3757 |
hostInputBufferIndex = stream->input.currentBufferIndex; |
| 3758 |
|
| 3759 |
PaUtil_SetInputFrameCount( &stream->bufferProcessor, |
| 3760 |
stream->input.framesPerBuffer - stream->input.framesUsedInCurrentBuffer ); |
| 3761 |
|
| 3762 |
channel = 0; |
| 3763 |
for( i=0; i<stream->input.deviceCount; ++i ) |
| 3764 |
{ |
| 3765 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3766 |
int channelCount = (int)stream->input.waveHeaders[i][ hostInputBufferIndex ].dwUser; |
| 3767 |
|
| 3768 |
PaUtil_SetInterleavedInputChannels( &stream->bufferProcessor, channel, |
| 3769 |
stream->input.waveHeaders[i][ hostInputBufferIndex ].lpData + |
| 3770 |
stream->input.framesUsedInCurrentBuffer * channelCount * |
| 3771 |
stream->bufferProcessor.bytesPerHostInputSample, |
| 3772 |
channelCount ); |
| 3773 |
|
| 3774 |
channel += channelCount; |
| 3775 |
} |
| 3776 |
|
| 3777 |
framesProcessed = PaUtil_CopyInput( &stream->bufferProcessor, &userBuffer, frames - framesRead ); |
| 3778 |
|
| 3779 |
stream->input.framesUsedInCurrentBuffer += framesProcessed; |
| 3780 |
if( stream->input.framesUsedInCurrentBuffer == stream->input.framesPerBuffer ) |
| 3781 |
{ |
| 3782 |
result = AdvanceToNextInputBuffer( stream ); |
| 3783 |
if( result != paNoError ) |
| 3784 |
break; |
| 3785 |
} |
| 3786 |
|
| 3787 |
framesRead += framesProcessed; |
| 3788 |
|
| 3789 |
}else{ |
| 3790 |
/* wait for MME to signal that a buffer is available */ |
| 3791 |
waitResult = WaitForSingleObject( stream->input.bufferEvent, timeout ); |
| 3792 |
if( waitResult == WAIT_FAILED ) |
| 3793 |
{ |
| 3794 |
result = paUnanticipatedHostError; |
| 3795 |
break; |
| 3796 |
} |
| 3797 |
else if( waitResult == WAIT_TIMEOUT ) |
| 3798 |
{ |
| 3799 |
/* if a timeout is encountered, continue, |
| 3800 |
perhaps we should give up eventually |
| 3801 |
*/ |
| 3802 |
} |
| 3803 |
} |
| 3804 |
}while( framesRead < frames ); |
| 3805 |
} |
| 3806 |
else |
| 3807 |
{ |
| 3808 |
result = paCanNotReadFromAnOutputOnlyStream; |
| 3809 |
} |
| 3810 |
|
| 3811 |
return result; |
| 3812 |
} |
| 3813 |
|
| 3814 |
|
| 3815 |
static PaError WriteStream( PaStream* s, |
| 3816 |
const void *buffer, |
| 3817 |
unsigned long frames ) |
| 3818 |
{ |
| 3819 |
PaError result = paNoError; |
| 3820 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3821 |
const void *userBuffer; |
| 3822 |
unsigned long framesWritten = 0; |
| 3823 |
unsigned long framesProcessed; |
| 3824 |
signed int hostOutputBufferIndex; |
| 3825 |
DWORD waitResult; |
| 3826 |
DWORD timeout = (unsigned long)(stream->allBuffersDurationMs * 0.5); |
| 3827 |
unsigned int channel, i; |
| 3828 |
|
| 3829 |
|
| 3830 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3831 |
{ |
| 3832 |
/* make a local copy of the user buffer pointer(s). this is necessary |
| 3833 |
because PaUtil_CopyOutput() advances these pointers every time |
| 3834 |
it is called. |
| 3835 |
*/ |
| 3836 |
if( stream->bufferProcessor.userOutputIsInterleaved ) |
| 3837 |
{ |
| 3838 |
userBuffer = buffer; |
| 3839 |
} |
| 3840 |
else |
| 3841 |
{ |
| 3842 |
userBuffer = (const void*)alloca( sizeof(void*) * stream->bufferProcessor.outputChannelCount ); |
| 3843 |
if( !userBuffer ) |
| 3844 |
return paInsufficientMemory; |
| 3845 |
for( i = 0; i<stream->bufferProcessor.outputChannelCount; ++i ) |
| 3846 |
((const void**)userBuffer)[i] = ((const void**)buffer)[i]; |
| 3847 |
} |
| 3848 |
|
| 3849 |
do{ |
| 3850 |
if( CurrentOutputBuffersAreDone( stream ) ) |
| 3851 |
{ |
| 3852 |
if( NoBuffersAreQueued( &stream->output ) ) |
| 3853 |
{ |
| 3854 |
/** @todo REVIEW: consider what to do if the output |
| 3855 |
underflows. do we requeue all the existing buffers with |
| 3856 |
zeros? should we run a separate thread to keep the buffers |
| 3857 |
enqueued at all times? |
| 3858 |
see: http://www.portaudio.com/trac/ticket/117 |
| 3859 |
*/ |
| 3860 |
|
| 3861 |
result = paOutputUnderflowed; |
| 3862 |
} |
| 3863 |
|
| 3864 |
hostOutputBufferIndex = stream->output.currentBufferIndex; |
| 3865 |
|
| 3866 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, |
| 3867 |
stream->output.framesPerBuffer - stream->output.framesUsedInCurrentBuffer ); |
| 3868 |
|
| 3869 |
channel = 0; |
| 3870 |
for( i=0; i<stream->output.deviceCount; ++i ) |
| 3871 |
{ |
| 3872 |
/* we have stored the number of channels in the buffer in dwUser */ |
| 3873 |
int channelCount = (int)stream->output.waveHeaders[i][ hostOutputBufferIndex ].dwUser; |
| 3874 |
|
| 3875 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, channel, |
| 3876 |
stream->output.waveHeaders[i][ hostOutputBufferIndex ].lpData + |
| 3877 |
stream->output.framesUsedInCurrentBuffer * channelCount * |
| 3878 |
stream->bufferProcessor.bytesPerHostOutputSample, |
| 3879 |
channelCount ); |
| 3880 |
|
| 3881 |
channel += channelCount; |
| 3882 |
} |
| 3883 |
|
| 3884 |
framesProcessed = PaUtil_CopyOutput( &stream->bufferProcessor, &userBuffer, frames - framesWritten ); |
| 3885 |
|
| 3886 |
stream->output.framesUsedInCurrentBuffer += framesProcessed; |
| 3887 |
if( stream->output.framesUsedInCurrentBuffer == stream->output.framesPerBuffer ) |
| 3888 |
{ |
| 3889 |
result = AdvanceToNextOutputBuffer( stream ); |
| 3890 |
if( result != paNoError ) |
| 3891 |
break; |
| 3892 |
} |
| 3893 |
|
| 3894 |
framesWritten += framesProcessed; |
| 3895 |
} |
| 3896 |
else |
| 3897 |
{ |
| 3898 |
/* wait for MME to signal that a buffer is available */ |
| 3899 |
waitResult = WaitForSingleObject( stream->output.bufferEvent, timeout ); |
| 3900 |
if( waitResult == WAIT_FAILED ) |
| 3901 |
{ |
| 3902 |
result = paUnanticipatedHostError; |
| 3903 |
break; |
| 3904 |
} |
| 3905 |
else if( waitResult == WAIT_TIMEOUT ) |
| 3906 |
{ |
| 3907 |
/* if a timeout is encountered, continue, |
| 3908 |
perhaps we should give up eventually |
| 3909 |
*/ |
| 3910 |
} |
| 3911 |
} |
| 3912 |
}while( framesWritten < frames ); |
| 3913 |
} |
| 3914 |
else |
| 3915 |
{ |
| 3916 |
result = paCanNotWriteToAnInputOnlyStream; |
| 3917 |
} |
| 3918 |
|
| 3919 |
return result; |
| 3920 |
} |
| 3921 |
|
| 3922 |
|
| 3923 |
static signed long GetStreamReadAvailable( PaStream* s ) |
| 3924 |
{ |
| 3925 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3926 |
|
| 3927 |
if( PA_IS_INPUT_STREAM_(stream) ) |
| 3928 |
return GetAvailableFrames( &stream->input ); |
| 3929 |
else |
| 3930 |
return paCanNotReadFromAnOutputOnlyStream; |
| 3931 |
} |
| 3932 |
|
| 3933 |
|
| 3934 |
static signed long GetStreamWriteAvailable( PaStream* s ) |
| 3935 |
{ |
| 3936 |
PaWinMmeStream *stream = (PaWinMmeStream*)s; |
| 3937 |
|
| 3938 |
if( PA_IS_OUTPUT_STREAM_(stream) ) |
| 3939 |
return GetAvailableFrames( &stream->output ); |
| 3940 |
else |
| 3941 |
return paCanNotWriteToAnInputOnlyStream; |
| 3942 |
} |
| 3943 |
|
| 3944 |
|
| 3945 |
/* NOTE: the following functions are MME-stream specific, and are called directly |
| 3946 |
by client code. We need to check for many more error conditions here because |
| 3947 |
we don't have the benefit of pa_front.c's parameter checking. |
| 3948 |
*/ |
| 3949 |
|
| 3950 |
static PaError GetWinMMEStreamPointer( PaWinMmeStream **stream, PaStream *s ) |
| 3951 |
{ |
| 3952 |
PaError result; |
| 3953 |
PaUtilHostApiRepresentation *hostApi; |
| 3954 |
PaWinMmeHostApiRepresentation *winMmeHostApi; |
| 3955 |
|
| 3956 |
result = PaUtil_ValidateStreamPointer( s ); |
| 3957 |
if( result != paNoError ) |
| 3958 |
return result; |
| 3959 |
|
| 3960 |
result = PaUtil_GetHostApiRepresentation( &hostApi, paMME ); |
| 3961 |
if( result != paNoError ) |
| 3962 |
return result; |
| 3963 |
|
| 3964 |
winMmeHostApi = (PaWinMmeHostApiRepresentation*)hostApi; |
| 3965 |
|
| 3966 |
/* note, the following would be easier if there was a generic way of testing |
| 3967 |
that a stream belongs to a specific host API */ |
| 3968 |
|
| 3969 |
if( PA_STREAM_REP( s )->streamInterface == &winMmeHostApi->callbackStreamInterface |
| 3970 |
|| PA_STREAM_REP( s )->streamInterface == &winMmeHostApi->blockingStreamInterface ) |
| 3971 |
{ |
| 3972 |
/* s is a WinMME stream */ |
| 3973 |
*stream = (PaWinMmeStream *)s; |
| 3974 |
return paNoError; |
| 3975 |
} |
| 3976 |
else |
| 3977 |
{ |
| 3978 |
return paIncompatibleStreamHostApi; |
| 3979 |
} |
| 3980 |
} |
| 3981 |
|
| 3982 |
|
| 3983 |
int PaWinMME_GetStreamInputHandleCount( PaStream* s ) |
| 3984 |
{ |
| 3985 |
PaWinMmeStream *stream; |
| 3986 |
PaError result = GetWinMMEStreamPointer( &stream, s ); |
| 3987 |
|
| 3988 |
if( result == paNoError ) |
| 3989 |
return (PA_IS_INPUT_STREAM_(stream)) ? stream->input.deviceCount : 0; |
| 3990 |
else |
| 3991 |
return result; |
| 3992 |
} |
| 3993 |
|
| 3994 |
|
| 3995 |
HWAVEIN PaWinMME_GetStreamInputHandle( PaStream* s, int handleIndex ) |
| 3996 |
{ |
| 3997 |
PaWinMmeStream *stream; |
| 3998 |
PaError result = GetWinMMEStreamPointer( &stream, s ); |
| 3999 |
|
| 4000 |
if( result == paNoError |
| 4001 |
&& PA_IS_INPUT_STREAM_(stream) |
| 4002 |
&& handleIndex >= 0 |
| 4003 |
&& (unsigned int)handleIndex < stream->input.deviceCount ) |
| 4004 |
return ((HWAVEIN*)stream->input.waveHandles)[handleIndex]; |
| 4005 |
else |
| 4006 |
return 0; |
| 4007 |
} |
| 4008 |
|
| 4009 |
|
| 4010 |
int PaWinMME_GetStreamOutputHandleCount( PaStream* s) |
| 4011 |
{ |
| 4012 |
PaWinMmeStream *stream; |
| 4013 |
PaError result = GetWinMMEStreamPointer( &stream, s ); |
| 4014 |
|
| 4015 |
if( result == paNoError ) |
| 4016 |
return (PA_IS_OUTPUT_STREAM_(stream)) ? stream->output.deviceCount : 0; |
| 4017 |
else |
| 4018 |
return result; |
| 4019 |
} |
| 4020 |
|
| 4021 |
|
| 4022 |
HWAVEOUT PaWinMME_GetStreamOutputHandle( PaStream* s, int handleIndex ) |
| 4023 |
{ |
| 4024 |
PaWinMmeStream *stream; |
| 4025 |
PaError result = GetWinMMEStreamPointer( &stream, s ); |
| 4026 |
|
| 4027 |
if( result == paNoError |
| 4028 |
&& PA_IS_OUTPUT_STREAM_(stream) |
| 4029 |
&& handleIndex >= 0 |
| 4030 |
&& (unsigned int)handleIndex < stream->output.deviceCount ) |
| 4031 |
return ((HWAVEOUT*)stream->output.waveHandles)[handleIndex]; |
| 4032 |
else |
| 4033 |
return 0; |
| 4034 |
} |
| 4035 |
#endif |