AudioManager.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 "AudioDeviceList.hxx"
00024 #include "AudioIn.hxx"
00025 #include "AudioOut.hxx"
00026 #include <algorithm>
00027 using std::find;
00028 
00029 namespace CLAM{
00030 
00031 AudioManager::AudioManager(int sr,int lat)
00032 :mSampleRate(sr),
00033  mLatency(lat),
00034  mInternalBuffersNumber(8)
00035 {       
00036         _Current(true,this);
00037 }
00038 
00039 AudioManager::~AudioManager()
00040 {
00041         unsigned int i;
00042         for (i=0;i<mDevices.size(); i++)
00043         {
00044                 delete mDevices[i];
00045         }
00046         _Current(true,0);
00047 }
00048 
00049 std::vector<AudioDeviceList*>& AudioManager::DeviceLists(void)
00050 { 
00051         static std::vector<AudioDeviceList*> sDeviceLists;
00052         return sDeviceLists;
00053 }
00054 
00055 AudioDevice* AudioManager::FindDevice(const std::string& name)
00056 {
00057         unsigned int i;
00058 
00059         for (i=0;i<mDevices.size();i++)
00060         {
00061                 if (mDevices[i]->mName == name)
00062                 {
00063                         return mDevices[i];
00064                 }
00065         }
00066 
00067         return 0;
00068 }
00069 
00070 void AudioManager::Start(void) throw(Err)
00071 {
00072         unsigned int i;
00073         for (i=0;i<mDevices.size();i++)
00074         {
00075                 if (mDevices[i]->mInputs.size() || mDevices[i]->mOutputs.size())
00076                         mDevices[i]->Start();
00077         }
00078 }
00079 
00080 AudioDevice* AudioManager::FindOrCreateDevice(const std::string& name)
00081 {
00082         std::string arch =   name.substr(0,name.find(":",0));
00083         std::string device = name.substr(name.find(":",0)+1,name.size());
00084 
00085         if (arch == "" || device == "") 
00086         {
00087                 std::string msg = "AudioManager::FindOrCreateDevice(...): Invalid device name: ";
00088                 msg       += name;
00089                 throw Err(msg.c_str());
00090         }
00091         
00092         if (arch == "default")
00093         {
00094                 arch = DEFAULT_AUDIO_ARCH;
00095         }
00096 
00097         AudioDeviceList* list = FindList(arch);
00098 
00099         if (list==0)
00100         {
00101                 std::string errstr;
00102                 errstr = "AudioManager::FindOrCreateDevice(): "
00103                         "Don't have a list of \""+arch+"\" devices.\n"
00104                         " Maybe you are not specifying any library to play sound (alsa, rtaudio...)\n";
00105                 throw Err((char*) errstr.c_str());
00106         }
00107 
00108         if (list->AvailableDevices().size()==0)
00109         {
00110                 std::string errstr;
00111                 errstr = "AudioManager::FindOrCreateDevice(): "
00112                         "Don't have any \""+arch+"\" devices available.\n"
00113                         " Maybe you are not specifying any library to play sound (alsa, rtaudio...)\n";
00114                 throw Err((char*) errstr.c_str());
00115         }
00116 
00117         if (device == "default")
00118         {
00119                 device = list->DefaultDevice();
00120         }
00121 
00122         if (find(list->AvailableDevices().begin(),
00123                          list->AvailableDevices().end(),
00124                          device) == 
00125                 list->AvailableDevices().end())
00126         {
00127                 std::string errstr;
00128                 errstr = "AudioManager::FindOrCreateDevice(): "
00129                         "No device \""+device+"\" available in architecture \""+arch+"\".\n"
00130                         " Maybe you are not specifying any library to play sound (alsa, rtaudio...)\n";
00131                 throw Err((char*) errstr.c_str());
00132         }
00133 
00134         std::string real_name = arch+":"+device;
00135 
00136         AudioDevice* audiodevice = FindDevice(real_name);
00137 
00138         if (audiodevice==0) {
00139                 
00140                 audiodevice = list->Create(real_name,device);
00141 
00142                 if (audiodevice==0)
00143                 {
00144                         std::string errstr;
00145                         errstr = "AudioManager::FindOrCreateDevice(): Don't know how to make device "+real_name;
00146                         throw Err((char*) errstr.c_str());
00147                 }
00148                 else
00149                 {
00150                         mDevices.push_back(audiodevice);
00151                 }
00152         }
00153         return audiodevice;
00154 }
00155 
00156 
00157 bool AudioManager::Register(AudioIn& in)
00158 {
00159         AudioDevice* device = FindOrCreateDevice(in.mConfig.GetDevice());
00160         return device->Register(this,in);
00161 }
00162 
00163 bool AudioManager::Register(AudioOut& out)
00164 {
00165         AudioDevice* device = FindOrCreateDevice(out.mConfig.GetDevice());
00166         return device->Register(this,out);
00167 }
00168 
00169 AudioDeviceList* AudioManager::FindList(const std::string& arch)
00170 {
00171         unsigned int i;
00172         std::string tmp = arch;
00173 
00174         if (tmp == "default")
00175                 tmp = DEFAULT_AUDIO_ARCH;
00176         for (i=0;i<DeviceLists().size();i++)
00177         {
00178                 if (DeviceLists()[i]->ArchName() == tmp)
00179                 {
00180                         return DeviceLists()[i];
00181                 }
00182         }
00183 
00184         return 0;
00185 }
00186 
00187 };//CLAM
00188 
Generated by  doxygen 1.6.3