CLAM-Development  1.4.0
AudioManager.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include "AudioManager.hxx"
23 #include "AudioDeviceList.hxx"
24 #include "AudioIn.hxx"
25 #include "AudioOut.hxx"
26 #include <algorithm>
27 using std::find;
28 
29 namespace CLAM{
30 
32 :mSampleRate(sr),
33  mLatency(lat),
34  mInternalBuffersNumber(8)
35 {
36  _Current(true,this);
37 }
38 
40 {
41  unsigned int i;
42  for (i=0;i<mDevices.size(); i++)
43  {
44  delete mDevices[i];
45  }
46  _Current(true,0);
47 }
48 
49 std::vector<AudioDeviceList*>& AudioManager::DeviceLists(void)
50 {
51  static std::vector<AudioDeviceList*> sDeviceLists;
52  return sDeviceLists;
53 }
54 
55 AudioDevice* AudioManager::FindDevice(const std::string& name)
56 {
57  unsigned int i;
58 
59  for (i=0;i<mDevices.size();i++)
60  {
61  if (mDevices[i]->mName == name)
62  {
63  return mDevices[i];
64  }
65  }
66 
67  return 0;
68 }
69 
70 void AudioManager::Start(void) throw(Err)
71 {
72  unsigned int i;
73  for (i=0;i<mDevices.size();i++)
74  {
75  if (mDevices[i]->mInputs.size() || mDevices[i]->mOutputs.size())
76  mDevices[i]->Start();
77  }
78 }
79 
81 {
82  std::string arch = name.substr(0,name.find(":",0));
83  std::string device = name.substr(name.find(":",0)+1,name.size());
84 
85  if (arch == "" || device == "")
86  {
87  std::string msg = "AudioManager::FindOrCreateDevice(...): Invalid device name: ";
88  msg += name;
89  throw Err(msg.c_str());
90  }
91 
92  if (arch == "default")
93  {
94  arch = DEFAULT_AUDIO_ARCH;
95  }
96 
97  AudioDeviceList* list = FindList(arch);
98 
99  if (list==0)
100  {
101  std::string errstr;
102  errstr = "AudioManager::FindOrCreateDevice(): "
103  "Don't have a list of \""+arch+"\" devices.\n"
104  " Maybe you are not specifying any library to play sound (alsa, rtaudio...)\n";
105  throw Err((char*) errstr.c_str());
106  }
107 
108  if (list->AvailableDevices().size()==0)
109  {
110  std::string errstr;
111  errstr = "AudioManager::FindOrCreateDevice(): "
112  "Don't have any \""+arch+"\" devices available.\n"
113  " Maybe you are not specifying any library to play sound (alsa, rtaudio...)\n";
114  throw Err((char*) errstr.c_str());
115  }
116 
117  if (device == "default")
118  {
119  device = list->DefaultDevice();
120  }
121 
122  if (find(list->AvailableDevices().begin(),
123  list->AvailableDevices().end(),
124  device) ==
125  list->AvailableDevices().end())
126  {
127  std::string errstr;
128  errstr = "AudioManager::FindOrCreateDevice(): "
129  "No device \""+device+"\" available in architecture \""+arch+"\".\n"
130  " Maybe you are not specifying any library to play sound (alsa, rtaudio...)\n";
131  throw Err((char*) errstr.c_str());
132  }
133 
134  std::string real_name = arch+":"+device;
135 
136  AudioDevice* audiodevice = FindDevice(real_name);
137 
138  if (audiodevice==0) {
139 
140  audiodevice = list->Create(real_name,device);
141 
142  if (audiodevice==0)
143  {
144  std::string errstr;
145  errstr = "AudioManager::FindOrCreateDevice(): Don't know how to make device "+real_name;
146  throw Err((char*) errstr.c_str());
147  }
148  else
149  {
150  mDevices.push_back(audiodevice);
151  }
152  }
153  return audiodevice;
154 }
155 
156 
158 {
159  AudioDevice* device = FindOrCreateDevice(in.mConfig.GetDevice());
160  return device->Register(this,in);
161 }
162 
164 {
165  AudioDevice* device = FindOrCreateDevice(out.mConfig.GetDevice());
166  return device->Register(this,out);
167 }
168 
169 AudioDeviceList* AudioManager::FindList(const std::string& arch)
170 {
171  unsigned int i;
172  std::string tmp = arch;
173 
174  if (tmp == "default")
175  tmp = DEFAULT_AUDIO_ARCH;
176  for (i=0;i<DeviceLists().size();i++)
177  {
178  if (DeviceLists()[i]->ArchName() == tmp)
179  {
180  return DeviceLists()[i];
181  }
182  }
183 
184  return 0;
185 }
186 
187 };//CLAM
188