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

📄 rtaudio.cpp

📁 Mobile STK for Symbian OS V0.1
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/************************************************************************//*! \class RtAudio    \brief Realtime audio i/o C++ classes.    RtAudio provides a common API (Application Programming Interface)    for realtime audio input/output across Linux (native ALSA, Jack,    and OSS), SGI, Macintosh OS X (CoreAudio), and Windows    (DirectSound and ASIO) operating systems.    RtAudio WWW site: http://music.mcgill.ca/~gary/rtaudio/    RtAudio: realtime audio i/o C++ classes    Copyright (c) 2001-2005 Gary P. Scavone    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.*//************************************************************************/// RtAudio: Version 3.0.2 (14 October 2005)// Modified by Robin Davies, 1 October 2005// - Improvements to DirectX pointer chasing. // - Backdoor RtDsStatistics hook provides DirectX performance information.// - Bug fix for non-power-of-two Asio granularity used by Edirol PCR-A30.// - Auto-call CoInitialize for DSOUND and ASIO platforms.#include "RtAudio.h"#if !defined(SYMBIAN)#include <iostream>#include <stdio.h>// Static variable definitions.const unsigned int RtApi::MAX_SAMPLE_RATES = 14;const unsigned int RtApi::SAMPLE_RATES[] = {  4000, 5512, 8000, 9600, 11025, 16000, 22050,  32000, 44100, 48000, 88200, 96000, 176400, 192000};#if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)  #define MUTEX_INITIALIZE(A) InitializeCriticalSection(A)  #define MUTEX_DESTROY(A)    DeleteCriticalSection(A);  #define MUTEX_LOCK(A)      EnterCriticalSection(A)  #define MUTEX_UNLOCK(A)     LeaveCriticalSection(A)#else // pthread API  #define MUTEX_INITIALIZE(A) pthread_mutex_init(A, NULL)  #define MUTEX_DESTROY(A)    pthread_mutex_destroy(A);  #define MUTEX_LOCK(A)       pthread_mutex_lock(A)  #define MUTEX_UNLOCK(A)     pthread_mutex_unlock(A)#endif// *************************************************** ////// Public common (OS-independent) methods.//// *************************************************** //RtAudio :: RtAudio( RtAudioApi api ){  initialize( api );}RtAudio :: RtAudio( int outputDevice, int outputChannels,                    int inputDevice, int inputChannels,                    RtAudioFormat format, int sampleRate,                    int *bufferSize, int numberOfBuffers, RtAudioApi api ){  initialize( api );  try {    rtapi_->openStream( outputDevice, outputChannels,                        inputDevice, inputChannels,                        format, sampleRate,                        bufferSize, numberOfBuffers );  }  catch (RtError &exception) {    // Deallocate the RtApi instance.    delete rtapi_;    throw exception;  }}RtAudio :: RtAudio( int outputDevice, int outputChannels,                    int inputDevice, int inputChannels,                    RtAudioFormat format, int sampleRate,                    int *bufferSize, int *numberOfBuffers, RtAudioApi api ){  initialize( api );  try {    rtapi_->openStream( outputDevice, outputChannels,                        inputDevice, inputChannels,                        format, sampleRate,                        bufferSize, numberOfBuffers );  }  catch (RtError &exception) {    // Deallocate the RtApi instance.    delete rtapi_;    throw exception;  }}RtAudio :: ~RtAudio(){  delete rtapi_;}void RtAudio :: openStream( int outputDevice, int outputChannels,                            int inputDevice, int inputChannels,                            RtAudioFormat format, int sampleRate,                            int *bufferSize, int numberOfBuffers ){  rtapi_->openStream( outputDevice, outputChannels, inputDevice,                      inputChannels, format, sampleRate,                      bufferSize, numberOfBuffers );}void RtAudio :: openStream( int outputDevice, int outputChannels,                            int inputDevice, int inputChannels,                            RtAudioFormat format, int sampleRate,                            int *bufferSize, int *numberOfBuffers ){  rtapi_->openStream( outputDevice, outputChannels, inputDevice,                      inputChannels, format, sampleRate,                      bufferSize, *numberOfBuffers );}void RtAudio::initialize( RtAudioApi api ){  rtapi_ = 0;  // First look for a compiled match to a specified API value. If one  // of these constructors throws an error, it will be passed up the  // inheritance chain.#if defined(__LINUX_JACK__)  if ( api == LINUX_JACK )    rtapi_ = new RtApiJack();#endif#if defined(__LINUX_ALSA__)  if ( api == LINUX_ALSA )    rtapi_ = new RtApiAlsa();#endif#if defined(__LINUX_OSS__)  if ( api == LINUX_OSS )    rtapi_ = new RtApiOss();#endif#if defined(__WINDOWS_ASIO__)  if ( api == WINDOWS_ASIO )    rtapi_ = new RtApiAsio();#endif#if defined(__WINDOWS_DS__)  if ( api == WINDOWS_DS )    rtapi_ = new RtApiDs();#endif#if defined(__IRIX_AL__)  if ( api == IRIX_AL )    rtapi_ = new RtApiAl();#endif#if defined(__MACOSX_CORE__)  if ( api == MACOSX_CORE )    rtapi_ = new RtApiCore();#endif  if ( rtapi_ ) return;  if ( api > 0 ) {    // No compiled support for specified API value.    throw RtError( "RtAudio: no compiled support for specified API argument!", RtError::INVALID_PARAMETER );  }  // No specified API ... search for "best" option.  try {#if defined(__LINUX_JACK__)    rtapi_ = new RtApiJack();#elif defined(__WINDOWS_ASIO__)    rtapi_ = new RtApiAsio();#elif defined(__IRIX_AL__)    rtapi_ = new RtApiAl();#elif defined(__MACOSX_CORE__)    rtapi_ = new RtApiCore();#else    ;#endif  }  catch (RtError &) {#if defined(__RTAUDIO_DEBUG__)    fprintf(stderr, "\nRtAudio: no devices found for first api option (JACK, ASIO, Al, or CoreAudio).\n\n");#endif    rtapi_ = 0;  }  if ( rtapi_ ) return;// Try second API support  if ( rtapi_ == 0 ) {    try {#if defined(__LINUX_ALSA__)      rtapi_ = new RtApiAlsa();#elif defined(__WINDOWS_DS__)      rtapi_ = new RtApiDs();#else      ;#endif    }    catch (RtError &) {#if defined(__RTAUDIO_DEBUG__)      fprintf(stderr, "\nRtAudio: no devices found for second api option (Alsa or DirectSound).\n\n");#endif      rtapi_ = 0;    }  }  if ( rtapi_ ) return;  // Try third API support  if ( rtapi_ == 0 ) {#if defined(__LINUX_OSS__)    try {      rtapi_ = new RtApiOss();    }    catch (RtError &error) {      rtapi_ = 0;    }#else    ;#endif  }  if ( rtapi_ == 0 ) {    // No devices found.    throw RtError( "RtAudio: no devices found for compiled audio APIs!", RtError::NO_DEVICES_FOUND );  }}RtApi :: RtApi(){  stream_.mode = UNINITIALIZED;  stream_.state = STREAM_STOPPED;  stream_.apiHandle = 0;  MUTEX_INITIALIZE(&stream_.mutex);}RtApi :: ~RtApi(){  MUTEX_DESTROY(&stream_.mutex);}void RtApi :: openStream( int outputDevice, int outputChannels,                         int inputDevice, int inputChannels,                         RtAudioFormat format, int sampleRate,                         int *bufferSize, int *numberOfBuffers ){  this->openStream( outputDevice, outputChannels, inputDevice,                    inputChannels, format, sampleRate,                    bufferSize, *numberOfBuffers );  *numberOfBuffers = stream_.nBuffers;}void RtApi :: openStream( int outputDevice, int outputChannels,                         int inputDevice, int inputChannels,                         RtAudioFormat format, int sampleRate,                         int *bufferSize, int numberOfBuffers ){  if ( stream_.mode != UNINITIALIZED ) {    sprintf(message_, "RtApi: only one open stream allowed per class instance.");    error(RtError::INVALID_STREAM);  }  if (outputChannels < 1 && inputChannels < 1) {    sprintf(message_,"RtApi: one or both 'channel' parameters must be greater than zero.");    error(RtError::INVALID_PARAMETER);  }  if ( formatBytes(format) == 0 ) {    sprintf(message_,"RtApi: 'format' parameter value is undefined.");    error(RtError::INVALID_PARAMETER);  }  if ( outputChannels > 0 ) {    if (outputDevice > nDevices_ || outputDevice < 0) {      sprintf(message_,"RtApi: 'outputDevice' parameter value (%d) is invalid.", outputDevice);      error(RtError::INVALID_PARAMETER);    }  }  if ( inputChannels > 0 ) {    if (inputDevice > nDevices_ || inputDevice < 0) {      sprintf(message_,"RtApi: 'inputDevice' parameter value (%d) is invalid.", inputDevice);      error(RtError::INVALID_PARAMETER);    }  }  std::string errorMessages;  clearStreamInfo();  bool result = FAILURE;  int device, defaultDevice = 0;  StreamMode mode;  int channels;  if ( outputChannels > 0 ) {    mode = OUTPUT;    channels = outputChannels;    if ( outputDevice == 0 ) { // Try default device first.      defaultDevice = getDefaultOutputDevice();      device = defaultDevice;    }    else      device = outputDevice - 1;    for ( int i=-1; i<nDevices_; i++ ) {      if ( i >= 0 ) {         if ( i == defaultDevice ) continue;        device = i;      }      if ( devices_[device].probed == false ) {        // If the device wasn't successfully probed before, try it        // (again) now.        clearDeviceInfo(&devices_[device]);        probeDeviceInfo(&devices_[device]);      }      if ( devices_[device].probed )        result = probeDeviceOpen(device, mode, channels, sampleRate,                                 format, bufferSize, numberOfBuffers);      if ( result == SUCCESS ) break;      errorMessages.append( "    " );      errorMessages.append( message_ );      errorMessages.append( "\n" );      if ( outputDevice > 0 ) break;      clearStreamInfo();    }  }  if ( inputChannels > 0 && ( result == SUCCESS || outputChannels <= 0 ) ) {    mode = INPUT;    channels = inputChannels;    if ( inputDevice == 0 ) { // Try default device first.      defaultDevice = getDefaultInputDevice();      device = defaultDevice;    }    else      device = inputDevice - 1;    for ( int i=-1; i<nDevices_; i++ ) {      if (i >= 0 ) {         if ( i == defaultDevice ) continue;        device = i;      }      if ( devices_[device].probed == false ) {        // If the device wasn't successfully probed before, try it        // (again) now.        clearDeviceInfo(&devices_[device]);        probeDeviceInfo(&devices_[device]);      }      if ( devices_[device].probed )        result = probeDeviceOpen( device, mode, channels, sampleRate,                                  format, bufferSize, numberOfBuffers );      if ( result == SUCCESS ) break;      errorMessages.append( "    " );      errorMessages.append( message_ );      errorMessages.append( "\n" );      if ( inputDevice > 0 ) break;    }  }  if ( result == SUCCESS )    return;  // If we get here, all attempted probes failed.  Close any opened  // devices and clear the stream structure.  if ( stream_.mode != UNINITIALIZED ) closeStream();  clearStreamInfo();  if ( ( outputDevice == 0 && outputChannels > 0 )       || ( inputDevice == 0 && inputChannels > 0 ) )    sprintf(message_,"RtApi: no devices found for given stream parameters: \n%s",            errorMessages.c_str());  else    sprintf(message_,"RtApi: unable to open specified device(s) with given stream parameters: \n%s",            errorMessages.c_str());  error(RtError::INVALID_PARAMETER);  return;}int RtApi :: getDeviceCount(void){  return devices_.size();}RtApi::StreamState RtApi :: getStreamState( void ) const

⌨️ 快捷键说明

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