📄 pa_win_wmme.c
字号:
/* * $Id: pa_win_wmme.c,v 1.11 2004/09/15 13:55:04 jamescrook Exp $ * pa_win_wmme.c * Implementation of PortAudio for Windows MultiMedia Extensions (WMME) * * PortAudio Portable Real-Time Audio Library * Latest Version at: http://www.portaudio.com * * Authors: Ross Bencina and Phil Burk * Copyright (c) 1999-2000 Ross Bencina and Phil Burk * * Permission is hereby granted, free of charge, to any person obtainingF * a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * Any person wishing to distribute modifications to the Software is * requested to send the modifications to the original developer so that * they can be incorporated into the canonical version. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * *//* All memory allocations and frees are marked with MEM for quick review.*//* Modification History: PLB = Phil Burk JM = Julien Maillard RDB = Ross Bencina PLB20010402 - sDevicePtrs now allocates based on sizeof(pointer) PLB20010413 - check for excessive numbers of channels PLB20010422 - apply Mike Berry's changes for CodeWarrior on PC including condition including of memory.h, and explicit typecasting on memory allocation PLB20010802 - use GlobalAlloc for sDevicesPtr instead of PaHost_AllocFastMemory PLB20010816 - pass process instead of thread to SetPriorityClass() PLB20010927 - use number of frames instead of real-time for CPULoad calculation. JM20020118 - prevent hung thread when buffers underflow. PLB20020321 - detect Win XP versus NT, 9x; fix DBUG typo; removed init of CurrentCount RDB20020411 - various renaming cleanups, factored streamData alloc and cpu usage init RDB20020417 - stopped counting WAVE_MAPPER when there were no real devices refactoring, renaming and fixed a few edge case bugs PLB20020612 - added 8000.0 Hz to custom sampling rates array*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include <windows.h>#include <mmsystem.h>#include <process.h>/* PLB20010422 - "memory.h" doesn't work on CodeWarrior for PC. Thanks Mike Berry for the mod. */#ifndef __MWERKS__#include <malloc.h>#include <memory.h>#endif /* __MWERKS__ */#include "portaudio.h"#include "pa_host.h"#include "pa_trace.h"/************************************************* Constants ********/#define PA_TRACK_MEMORY (0)#define PA_USE_TIMER_CALLBACK (0) /* Select between two options for background task. 0=thread, 1=timer *//* Switches for debugging. */#define PA_SIMULATE_UNDERFLOW (0) /* Set to one to force an underflow of the output buffer. *//* To trace program, enable TRACE_REALTIME_EVENTS in pa_trace.h */#define PA_TRACE_RUN (0)#define PA_TRACE_START_STOP (1)#define PA_USE_HIGH_LATENCY (0) /* For debugging glitches. */#if PA_USE_HIGH_LATENCY #define PA_MIN_MSEC_PER_HOST_BUFFER (100) #define PA_MAX_MSEC_PER_HOST_BUFFER (300) /* Do not exceed unless user buffer exceeds */ #define PA_MIN_NUM_HOST_BUFFERS (4) #define PA_MAX_NUM_HOST_BUFFERS (16) /* OK to exceed if necessary */ #define PA_WIN_9X_LATENCY (400)#else #define PA_MIN_MSEC_PER_HOST_BUFFER (10) #define PA_MAX_MSEC_PER_HOST_BUFFER (100) /* Do not exceed unless user buffer exceeds */ #define PA_MIN_NUM_HOST_BUFFERS (3) #define PA_MAX_NUM_HOST_BUFFERS (16) /* OK to exceed if necessary */ #define PA_WIN_9X_LATENCY (200)#endif#define MIN_TIMEOUT_MSEC (1000)/*** Use higher latency for NT because it is even worse at real-time** operation than Win9x.*/#define PA_WIN_NT_LATENCY (PA_WIN_9X_LATENCY * 2)#define PA_WIN_WDM_LATENCY (PA_WIN_9X_LATENCY)#if PA_SIMULATE_UNDERFLOWstatic gUnderCallbackCounter = 0;#define UNDER_SLEEP_AT (40)#define UNDER_SLEEP_FOR (500)#endif#define PRINT(x) { printf x; fflush(stdout); }#define ERR_RPT(x) PRINT(x)#define DBUG(x) /* PRINT(x) /**/#define DBUGX(x) /* PRINT(x) *//************************************************* Definitions ********//************************************************************** * Structure for internal host specific stream data. * This is allocated on a per stream basis. */typedef struct PaWMMEStreamData{ /* Input -------------- */ HWAVEIN hWaveIn; WAVEHDR *inputBuffers; int currentInputBuffer; int bytesPerHostInputBuffer; int bytesPerUserInputBuffer; /* native buffer size in bytes */ /* Output -------------- */ HWAVEOUT hWaveOut; WAVEHDR *outputBuffers; int currentOutputBuffer; int bytesPerHostOutputBuffer; int bytesPerUserOutputBuffer; /* native buffer size in bytes */ /* Run Time -------------- */ PaTimestamp framesPlayed; long lastPosition; /* used to track frames played. */ /* For measuring CPU utilization. */ LARGE_INTEGER entryCount; double inverseTicksPerHostBuffer; /* Init Time -------------- */ int numHostBuffers; int framesPerHostBuffer; int userBuffersPerHostBuffer; CRITICAL_SECTION streamLock; /* Mutext to prevent threads from colliding. */ INT streamLockInited;#if PA_USE_TIMER_CALLBACK BOOL ifInsideCallback; /* Test for reentrancy. */ MMRESULT timerID;#else HANDLE abortEvent; int abortEventInited; HANDLE bufferEvent; int bufferEventInited; HANDLE engineThread; DWORD engineThreadID;#endif}PaWMMEStreamData;/************************************************* Shared Data ********//* FIXME - put Mutex around this shared data. */static int sNumInputDevices = 0;static int sNumOutputDevices = 0;static int sNumDevices = 0;static PaDeviceInfo **sDevicePtrs = NULL;static int sDefaultInputDeviceID = paNoDevice;static int sDefaultOutputDeviceID = paNoDevice;static int sPaHostError = 0;static const char sMapperSuffixInput[] = " - Input";static const char sMapperSuffixOutput[] = " - Output";#if PA_TRACK_MEMORYstatic int sNumAllocations = 0;#endif/************************************************* Macros ********//* Convert external PA ID to an internal ID that includes WAVE_MAPPER */#define PaDeviceIdToWinId(id) (((id) < sNumInputDevices) ? (id - 1) : (id - sNumInputDevices - 1))/************************************************* Prototypes **********/void Pa_InitializeNumDevices( void );PaError Pa_AllocateDevicePtrs( void );static void CALLBACK Pa_TimerCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);PaError PaHost_GetTotalBufferFrames( internalPortAudioStream *past );static PaError PaHost_UpdateStreamTime( PaWMMEStreamData *wmmeStreamData );static PaError PaHost_BackgroundManager( internalPortAudioStream *past );static void *PaHost_AllocateTrackedMemory( long numBytes );static void PaHost_FreeTrackedMemory( void *addr );/*******************************************************************/static PaError PaHost_AllocateWMMEStreamData( internalPortAudioStream *stream ){ PaError result = paNoError; PaWMMEStreamData *wmmeStreamData; wmmeStreamData = (PaWMMEStreamData *) PaHost_AllocateFastMemory(sizeof(PaWMMEStreamData)); /* MEM */ if( wmmeStreamData == NULL ) { result = paInsufficientMemory; goto error; } memset( wmmeStreamData, 0, sizeof(PaWMMEStreamData) ); stream->past_DeviceData = (void *) wmmeStreamData; return result; error: return result;}/*******************************************************************/static void PaHost_FreeWMMEStreamData( internalPortAudioStream *internalStream ){ PaWMMEStreamData *wmmeStreamData = (PaWMMEStreamData *) internalStream->past_DeviceData; PaHost_FreeFastMemory( wmmeStreamData, sizeof(PaWMMEStreamData) ); /* MEM */ internalStream->past_DeviceData = NULL;}/*************************************************************************/static PaWMMEStreamData* PaHost_GetWMMEStreamData( internalPortAudioStream* internalStream ){ PaWMMEStreamData *result = NULL; if( internalStream != NULL ) { result = (PaWMMEStreamData *) internalStream->past_DeviceData; } return result;}/********************************* BEGIN CPU UTILIZATION MEASUREMENT ****//* FIXME: the cpu usage code should be factored out into a common module */static void Pa_InitializeCpuUsageScalar( internalPortAudioStream *stream ){ PaWMMEStreamData *wmmeStreamData = (PaWMMEStreamData *) stream->past_DeviceData; LARGE_INTEGER frequency; if( QueryPerformanceFrequency( &frequency ) == 0 ) { wmmeStreamData->inverseTicksPerHostBuffer = 0.0; } else { wmmeStreamData->inverseTicksPerHostBuffer = stream->past_SampleRate / ( (double)frequency.QuadPart * stream->past_FramesPerUserBuffer * wmmeStreamData->userBuffersPerHostBuffer ); DBUG(("inverseTicksPerHostBuffer = %g\n", wmmeStreamData->inverseTicksPerHostBuffer )); }}static void Pa_StartUsageCalculation( internalPortAudioStream *stream ){ PaWMMEStreamData *wmmeStreamData = (PaWMMEStreamData *) stream->past_DeviceData; if( wmmeStreamData == NULL ) return; /* Query system timer for usage analysis and to prevent overuse of CPU. */ QueryPerformanceCounter( &wmmeStreamData->entryCount );}static void Pa_EndUsageCalculation( internalPortAudioStream *stream ){ LARGE_INTEGER CurrentCount; PaWMMEStreamData *wmmeStreamData = (PaWMMEStreamData *) stream->past_DeviceData; if( wmmeStreamData == NULL ) return; /* * Measure CPU utilization during this callback. Note that this calculation * assumes that we had the processor the whole time. */#define LOWPASS_COEFFICIENT_0 (0.9)#define LOWPASS_COEFFICIENT_1 (0.99999 - LOWPASS_COEFFICIENT_0) if( QueryPerformanceCounter( &CurrentCount ) ) { LONGLONG InsideCount = CurrentCount.QuadPart - wmmeStreamData->entryCount.QuadPart; double newUsage = InsideCount * wmmeStreamData->inverseTicksPerHostBuffer; stream->past_Usage = (LOWPASS_COEFFICIENT_0 * stream->past_Usage) + (LOWPASS_COEFFICIENT_1 * newUsage); }}/****************************************** END CPU UTILIZATION *******/static void Pa_InitializeNumDevices( void ){ sNumInputDevices = waveInGetNumDevs(); if( sNumInputDevices > 0 ) { sNumInputDevices += 1; /* add one extra for the WAVE_MAPPER */ sDefaultInputDeviceID = 0; } else { sDefaultInputDeviceID = paNoDevice; } sNumOutputDevices = waveOutGetNumDevs(); if( sNumOutputDevices > 0 ) { sNumOutputDevices += 1; /* add one extra for the WAVE_MAPPER */ sDefaultOutputDeviceID = sNumInputDevices; } else { sDefaultOutputDeviceID = paNoDevice; } sNumDevices = sNumInputDevices + sNumOutputDevices;}static PaError Pa_AllocateDevicePtrs( void ){ int numBytes; int i; /* Allocate structures to hold device info. */ /* PLB20010402 - was allocating too much memory. */ /* numBytes = sNumDevices * sizeof(PaDeviceInfo); // PLB20010402 */ if( sNumDevices > 0 ) { numBytes = sNumDevices * sizeof(PaDeviceInfo *); /* PLB20010402 */ sDevicePtrs = (PaDeviceInfo **) PaHost_AllocateTrackedMemory( numBytes ); /* MEM */ if( sDevicePtrs == NULL ) return paInsufficientMemory; for( i = 0; i < sNumDevices; i++ ) sDevicePtrs[i] = NULL; /* RDB20020417 explicitly set each ptr to NULL */ } else { sDevicePtrs = NULL; } return paNoError;}/*************************************************************************/long Pa_GetHostError(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -