AudioDevice.cxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #include "AudioManager.hxx"
00023 #include "AudioDevice.hxx"
00024 #include "AudioIn.hxx"
00025 #include "AudioOut.hxx"
00026 
00027 #include <algorithm>
00028 using std::find;
00029 
00030 using namespace CLAM;
00031 
00032 AudioManager& AudioDevice::_AudioManager(void)
00033 {
00034         if (mAudioManager==0) 
00035                 throw Err("This AudioDevice is not associated with any AudioManager");
00036 
00037         return *mAudioManager; 
00038 }
00039 
00040 void AudioDevice::_SetAudioManager(AudioManager* am)
00041 {
00042         if (mAudioManager==0) mAudioManager = am;
00043         else if (mAudioManager!=am)
00044         {
00045                 throw Err("An AudioDevice can only be associated with one AudioManager");
00046         }
00047 }
00048 
00049 bool AudioDevice::Register(AudioManager* am,AudioIn& in)
00050 {
00051         unsigned int i;
00052         for (i=0; i<mInputs.size(); i++)
00053                 if (dynamic_cast<const AudioIOConfig&>(mInputs[i]->GetConfig()).GetChannelID() ==
00054                         dynamic_cast<const AudioIOConfig&>(in.GetConfig()).GetChannelID()) 
00055                 {
00056                         in.mpDevice = 0;
00057                         return false;
00058                 }
00059         mInputs.push_back(&in);
00060         _SetAudioManager(am);
00061         in.mpDevice = this;
00062         return true;
00063 }
00064 
00065 bool AudioDevice::Register(AudioManager* am,AudioOut& out)
00066 {
00067         unsigned int i;
00068         for (i=0; i<mOutputs.size(); i++)
00069                 if (dynamic_cast<const AudioIOConfig&>(mOutputs[i]->GetConfig()).GetChannelID() ==
00070                         dynamic_cast<const AudioIOConfig&>(out.GetConfig()).GetChannelID())
00071                 {
00072                         out.mpDevice = 0;
00073                         return false;
00074                 }
00075         mOutputs.push_back(&out);
00076         _SetAudioManager(am);
00077         out.mpDevice = this;
00078         return true;
00079 }
00080 
00081 void AudioDevice::Unregister(AudioIn& in)
00082 {
00083         if (in.mpDevice != this)
00084         {
00085                 throw(Err("AudioDevice::Unregister(): I am not this AudioIn object's device."));
00086         }
00087         std::vector<AudioIn*>::iterator it = std::find(mInputs.begin(),mInputs.end(),&in);
00088         if (it == mInputs.end())
00089         {
00090                 throw(Err("AudioDevice::Unregister(): AudioIn object not registered in this device."));
00091         }
00092         mInputs.erase(it);
00093         in.mpDevice = 0;
00094 }
00095 
00096 void AudioDevice::Unregister(AudioOut& out)
00097 {
00098         if (out.mpDevice != this)
00099         {
00100                 throw(Err("AudioDevice::Unregister(): I am not this AudioOut object's device."));
00101         }
00102         std::vector<AudioOut*>::iterator it = std::find(mOutputs.begin(),mOutputs.end(),&out);
00103         if (it == mOutputs.end())
00104         {
00105                 throw(Err("AudioDevice::Unregister(): AudioOut object not registered in this device."));
00106         }
00107         mOutputs.erase(it);
00108         out.mpDevice = 0;
00109 }
00110 
00111 void AudioDevice::GetInfo(AudioDevice::TInfo &info)
00112 {
00113         info.mSampleRate = SampleRate();
00114         info.mName = mName;
00115         info.mNChannels = mNChannels;
00116         info.mNReadChannels = mNReadChannels;
00117         info.mNWriteChannels = mNWriteChannels;
00118 }
00119 
00120 int AudioDevice::SampleRate(void)
00121 {
00122         /* determine the sample rate */
00123         unsigned int i;
00124         int sr = 0;
00125         for (i=0;i<mInputs.size();i++)
00126         {
00127                 if (sr == 0) sr = mInputs[i]->mConfig.GetSampleRate();
00128                 else if (mInputs[i]->mConfig.GetSampleRate() && 
00129                         mInputs[i]->mConfig.GetSampleRate() != sr)
00130                 {
00131                         throw Err("AudioDevice::Register():"
00132                                       "all audio ports need the same samplerate");
00133                 }
00134         }
00135         for (i=0;i<mOutputs.size();i++)
00136         {
00137                 if (sr == 0) sr = mOutputs[i]->mConfig.GetSampleRate();
00138                 else if (mOutputs[i]->mConfig.GetSampleRate() && 
00139                         mOutputs[i]->mConfig.GetSampleRate() != sr )
00140                 {
00141                         throw Err("AudioDevice::Register():"
00142                                       "all audio ports need the same samplerate");
00143                 }
00144         }
00145         
00146         if (sr == 0)
00147                 sr = _AudioManager().SampleRate();
00148         
00149         for (i=0;i<mInputs.size();i++)
00150         {
00151                 mInputs[i]->mConfig.SetSampleRate(sr);
00152         }
00153         for (i=0;i<mOutputs.size();i++)
00154         {
00155                 mOutputs[i]->mConfig.SetSampleRate(sr);
00156         }
00157         
00158         return sr;
00159 }
00160 
00161 int AudioDevice::Latency(void)
00162 {
00163         return _AudioManager().Latency();
00164 }
00165 
00166 void AudioDevice::SetLatency(int latency)
00167 {
00168         _AudioManager().SetLatency(latency);
00169 }
00170 
00171 void AudioDevice::SetNChannels(int channels)
00172 {
00173         mForceNChannels = true;
00174         mNChannels = channels;
00175 }
00176 
Generated by  doxygen 1.6.3