⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pa_dsound.c

📁 Audacity是一款用於錄音和編輯聲音的、免費的開放源碼軟體。它可以執行於Mac OS X、Microsoft Windows、GNU/Linux和其它作業系統
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * $Id: pa_dsound.c,v 1.3 2003/03/02 08:01:45 dmazzoni Exp $ * PortAudio Portable Real-Time Audio Library * Latest Version at: http://www.softsynth.com/portaudio/ * DirectSound Implementation * * Copyright (c) 1999-2000 Phil Burk * * Permission is hereby granted, free of charge, to any person obtaining * 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. * *//* Modifications *    7/19/01 Mike Berry - casts for compiling with __MWERKS__ CodeWarrior *    9/27/01 Phil Burk - use number of frames instead of real-time for CPULoad calculation. *    4/19/02 Phil Burk - Check for Win XP for system latency calculation. *//* Compiler flags: SUPPORT_AUDIO_CAPTURE - define this flag if you want to SUPPORT_AUDIO_CAPTURE */#include <stdio.h>#include <stdlib.h>#ifndef __MWERKS__#include <malloc.h>#include <memory.h>#endif //__MWERKS__#include <math.h>#include "portaudio.h"#include "pa_host.h"#include "pa_trace.h"#include "dsound_wrapper.h"#define PRINT(x) { printf x; fflush(stdout); }#define ERR_RPT(x) PRINT(x)#define DBUG(x)  /* PRINT(x) */#define DBUGX(x) /* PRINT(x) */#define PA_USE_HIGH_LATENCY   (0)#if PA_USE_HIGH_LATENCY#define PA_WIN_9X_LATENCY     (500)#define PA_WIN_NT_LATENCY     (600)#else#define PA_WIN_9X_LATENCY     (140)#define PA_WIN_NT_LATENCY     (280)#endif#define PA_WIN_WDM_LATENCY       (120)/* Trigger an underflow for testing purposes. Should normally be (0). */#define PA_SIMULATE_UNDERFLOW (0)#if PA_SIMULATE_UNDERFLOWstatic  gUnderCallbackCounter = 0;#define UNDER_START_GAP       (10)#define UNDER_STOP_GAP        (UNDER_START_GAP + 4)#endif/************************************************* Definitions ********/typedef struct internalPortAudioStream internalPortAudioStream;typedef struct internalPortAudioDevice{    GUID                             pad_GUID;    GUID                            *pad_lpGUID;    double                           pad_SampleRates[10]; /* for pointing to from pad_Info FIXME?!*/    PaDeviceInfo                     pad_Info;}internalPortAudioDevice;/* Define structure to contain all DirectSound and Windows specific data. */typedef struct PaHostSoundControl{    DSoundWrapper    pahsc_DSoundWrapper;    MMRESULT         pahsc_TimerID;    BOOL             pahsc_IfInsideCallback;  /* Test for reentrancy. */    short           *pahsc_NativeBuffer;    unsigned int     pahsc_BytesPerBuffer;    /* native buffer size in bytes */    double           pahsc_ValidFramesWritten;    int              pahsc_FramesPerDSBuffer;    /* For measuring CPU utilization. */    LARGE_INTEGER    pahsc_EntryCount;    double           pahsc_InverseTicksPerUserBuffer;}PaHostSoundControl;/************************************************* Shared Data ********//* FIXME - put Mutex around this shared data. */static int sNumDevices = 0;static int sDeviceIndex = 0;static internalPortAudioDevice *sDevices = NULL;static int sDefaultInputDeviceID = paNoDevice;static int sDefaultOutputDeviceID = paNoDevice;static int sEnumerationError;static int sPaHostError = 0;/************************************************* Prototypes **********/static internalPortAudioDevice *Pa_GetInternalDevice( PaDeviceID id );static BOOL CALLBACK Pa_EnumProc(LPGUID lpGUID,                                 LPCTSTR lpszDesc,                                 LPCTSTR lpszDrvName,                                 LPVOID lpContext );static BOOL CALLBACK Pa_CountDevProc(LPGUID lpGUID,                                     LPCTSTR lpszDesc,                                     LPCTSTR lpszDrvName,                                     LPVOID lpContext );static Pa_QueryDevices( void );static void CALLBACK Pa_TimerCallback(UINT uID, UINT uMsg,                                      DWORD dwUser, DWORD dw1, DWORD dw2);/********************************* BEGIN CPU UTILIZATION MEASUREMENT ****/static void Pa_StartUsageCalculation( internalPortAudioStream   *past ){    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;    if( pahsc == NULL ) return;    /* Query system timer for usage analysis and to prevent overuse of CPU. */    QueryPerformanceCounter( &pahsc->pahsc_EntryCount );}static void Pa_EndUsageCalculation( internalPortAudioStream   *past ){    LARGE_INTEGER CurrentCount = { 0, 0 };    PaHostSoundControl *pahsc = (PaHostSoundControl *) past->past_DeviceData;    if( pahsc == 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 - pahsc->pahsc_EntryCount.QuadPart;        double newUsage = InsideCount * pahsc->pahsc_InverseTicksPerUserBuffer;        past->past_Usage = (LOWPASS_COEFFICIENT_0 * past->past_Usage) +                           (LOWPASS_COEFFICIENT_1 * newUsage);    }}/****************************************** END CPU UTILIZATION *******/static PaError Pa_QueryDevices( void ){    int numBytes;    sDefaultInputDeviceID = paNoDevice;    sDefaultOutputDeviceID = paNoDevice;    /* Enumerate once just to count devices. */    sNumDevices = 0; // for default device    DirectSoundEnumerate( (LPDSENUMCALLBACK)Pa_CountDevProc, NULL );#if SUPPORT_AUDIO_CAPTURE    DirectSoundCaptureEnumerate( (LPDSENUMCALLBACK)Pa_CountDevProc, NULL );#endif /* SUPPORT_AUDIO_CAPTURE */    /* Allocate structures to hold device info. */    numBytes = sNumDevices * sizeof(internalPortAudioDevice);    sDevices = (internalPortAudioDevice *)PaHost_AllocateFastMemory( numBytes ); /* MEM */    if( sDevices == NULL ) return paInsufficientMemory;    /* Enumerate again to fill in structures. */    sDeviceIndex = 0;    sEnumerationError = 0;    DirectSoundEnumerate( (LPDSENUMCALLBACK)Pa_EnumProc, (void *)0 );#if SUPPORT_AUDIO_CAPTURE    if( sEnumerationError != paNoError ) return sEnumerationError;    sEnumerationError = 0;    DirectSoundCaptureEnumerate( (LPDSENUMCALLBACK)Pa_EnumProc, (void *)1 );#endif /* SUPPORT_AUDIO_CAPTURE */    return sEnumerationError;}/************************************************************************************/long Pa_GetHostError(){    return sPaHostError;}/************************************************************************************** Just count devices so we know how much memory to allocate.*/static BOOL CALLBACK Pa_CountDevProc(LPGUID lpGUID,                                     LPCTSTR lpszDesc,                                     LPCTSTR lpszDrvName,                                     LPVOID lpContext ){    sNumDevices++;    return TRUE;}/************************************************************************************** Extract capabilities info from each device.*/static BOOL CALLBACK Pa_EnumProc(LPGUID lpGUID,                                 LPCTSTR lpszDesc,                                 LPCTSTR lpszDrvName,                                 LPVOID lpContext ){    HRESULT    hr;    LPDIRECTSOUND          lpDirectSound;#if SUPPORT_AUDIO_CAPTURE    LPDIRECTSOUNDCAPTURE   lpDirectSoundCapture;#endif /* SUPPORT_AUDIO_CAPTURE */    int        isInput  = (int) lpContext;  /* Passed from Pa_CountDevices() */    internalPortAudioDevice *pad;    if( sDeviceIndex >= sNumDevices )    {        sEnumerationError = paInternalError;        return FALSE;    }    pad = &sDevices[sDeviceIndex];    /* Copy GUID to static array. Set pointer. */    if( lpGUID == NULL )    {        pad->pad_lpGUID = NULL;    }    else    {        memcpy( &pad->pad_GUID, lpGUID, sizeof(GUID) );        pad->pad_lpGUID = &pad->pad_GUID;    }    pad->pad_Info.sampleRates = pad->pad_SampleRates;  /* Point to array. */    /* Allocate room for descriptive name. */    if( lpszDesc != NULL )    {        int len = strlen(lpszDesc);        pad->pad_Info.name = (char *)malloc( len+1 );        if( pad->pad_Info.name == NULL )        {            sEnumerationError = paInsufficientMemory;            return FALSE;        }        memcpy( (void *) pad->pad_Info.name, lpszDesc, len+1 );    }#if SUPPORT_AUDIO_CAPTURE    if( isInput )    {        /********** Input ******************************/        DSCCAPS     caps;        if( lpGUID == NULL ) sDefaultInputDeviceID = sDeviceIndex;        hr = DirectSoundCaptureCreate(  lpGUID, &lpDirectSoundCapture,   NULL );        if( hr != DS_OK )        {            pad->pad_Info.maxInputChannels = 0;            DBUG(("Cannot create Capture for %s. Result = 0x%x\n", lpszDesc, hr ));        }        else        {            /* Query device characteristics. */            caps.dwSize = sizeof(caps);            IDirectSoundCapture_GetCaps( lpDirectSoundCapture, &caps );            /* printf("caps.dwFormats = 0x%x\n", caps.dwFormats ); */            pad->pad_Info.maxInputChannels = caps.dwChannels;            /* Determine sample rates from flags. */            if( caps.dwChannels == 2 )            {                int index = 0;                if( caps.dwFormats & WAVE_FORMAT_1S16) pad->pad_SampleRates[index++] = 11025.0;                if( caps.dwFormats & WAVE_FORMAT_2S16) pad->pad_SampleRates[index++] = 22050.0;                if( caps.dwFormats & WAVE_FORMAT_4S16) pad->pad_SampleRates[index++] = 44100.0;                pad->pad_Info.numSampleRates = index;            }            else if( caps.dwChannels == 1 )            {                int index = 0;                if( caps.dwFormats & WAVE_FORMAT_1M16) pad->pad_SampleRates[index++] = 11025.0;                if( caps.dwFormats & WAVE_FORMAT_2M16) pad->pad_SampleRates[index++] = 22050.0;                if( caps.dwFormats & WAVE_FORMAT_4M16) pad->pad_SampleRates[index++] = 44100.0;                pad->pad_Info.numSampleRates = index;            }            else pad->pad_Info.numSampleRates = 0;            IDirectSoundCapture_Release( lpDirectSoundCapture );        }    }    else#endif /* SUPPORT_AUDIO_CAPTURE */    {        /********** Output ******************************/        DSCAPS     caps;        if( lpGUID == NULL ) sDefaultOutputDeviceID = sDeviceIndex;        /* Create interfaces for each object. */        hr = DirectSoundCreate(  lpGUID, &lpDirectSound,   NULL );        if( hr != DS_OK )        {            pad->pad_Info.maxOutputChannels = 0;            DBUG(("Cannot create dsound for %s. Result = 0x%x\n", lpszDesc, hr ));        }        else        {            /* Query device characteristics. */            caps.dwSize = sizeof(caps);            IDirectSound_GetCaps( lpDirectSound, &caps );            pad->pad_Info.maxOutputChannels = ( caps.dwFlags & DSCAPS_PRIMARYSTEREO ) ? 2 : 1;            /* Get sample rates. */            pad->pad_SampleRates[0] = (double) caps.dwMinSecondarySampleRate;            pad->pad_SampleRates[1] = (double) caps.dwMaxSecondarySampleRate;            if( caps.dwFlags & DSCAPS_CONTINUOUSRATE ) pad->pad_Info.numSampleRates = -1;            else if( caps.dwMinSecondarySampleRate == caps.dwMaxSecondarySampleRate )            {                if( caps.dwMinSecondarySampleRate == 0 )                {                    /*                    ** On my Thinkpad 380Z, DirectSoundV6 returns min-max=0 !!                    ** But it supports continuous sampling.                    ** So fake range of rates, and hope it really supports it.                    */                    pad->pad_SampleRates[0] = 11025.0f;                    pad->pad_SampleRates[1] = 48000.0f;                    pad->pad_Info.numSampleRates = -1; /* continuous range */                    DBUG(("PA - Reported rates both zero. Setting to fake values for device #%d\n", sDeviceIndex ));                }                else                {                    pad->pad_Info.numSampleRates = 1;                }            }            else if( (caps.dwMinSecondarySampleRate < 1000.0) && (caps.dwMaxSecondarySampleRate > 50000.0) )            {                /* The EWS88MT drivers lie, lie, lie. The say they only support two rates, 100 & 100000.                ** But we know that they really support a range of rates!                ** So when we see a ridiculous set of rates, assume it is a range.                */                pad->pad_Info.numSampleRates = -1;                DBUG(("PA - Sample rate range used instead of two odd values for device #%d\n", sDeviceIndex ));            }            else pad->pad_Info.numSampleRates = 2;            IDirectSound_Release( lpDirectSound );        }    }    pad->pad_Info.nativeSampleFormats = paInt16;    sDeviceIndex++;    return( TRUE );}/*************************************************************************/int Pa_CountDevices(){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -