CLAM-Development  1.4.0
AudioDevice.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 "AudioDevice.hxx"
24 #include "AudioIn.hxx"
25 #include "AudioOut.hxx"
26 
27 #include <algorithm>
28 using std::find;
29 
30 using namespace CLAM;
31 
32 AudioManager& AudioDevice::_AudioManager(void)
33 {
34  if (mAudioManager==0)
35  throw Err("This AudioDevice is not associated with any AudioManager");
36 
37  return *mAudioManager;
38 }
39 
40 void AudioDevice::_SetAudioManager(AudioManager* am)
41 {
42  if (mAudioManager==0) mAudioManager = am;
43  else if (mAudioManager!=am)
44  {
45  throw Err("An AudioDevice can only be associated with one AudioManager");
46  }
47 }
48 
50 {
51  unsigned int i;
52  for (i=0; i<mInputs.size(); i++)
53  if (dynamic_cast<const AudioIOConfig&>(mInputs[i]->GetConfig()).GetChannelID() ==
54  dynamic_cast<const AudioIOConfig&>(in.GetConfig()).GetChannelID())
55  {
56  in.mpDevice = 0;
57  return false;
58  }
59  mInputs.push_back(&in);
60  _SetAudioManager(am);
61  in.mpDevice = this;
62  return true;
63 }
64 
66 {
67  unsigned int i;
68  for (i=0; i<mOutputs.size(); i++)
69  if (dynamic_cast<const AudioIOConfig&>(mOutputs[i]->GetConfig()).GetChannelID() ==
70  dynamic_cast<const AudioIOConfig&>(out.GetConfig()).GetChannelID())
71  {
72  out.mpDevice = 0;
73  return false;
74  }
75  mOutputs.push_back(&out);
76  _SetAudioManager(am);
77  out.mpDevice = this;
78  return true;
79 }
80 
82 {
83  if (in.mpDevice != this)
84  {
85  throw(Err("AudioDevice::Unregister(): I am not this AudioIn object's device."));
86  }
87  std::vector<AudioIn*>::iterator it = std::find(mInputs.begin(),mInputs.end(),&in);
88  if (it == mInputs.end())
89  {
90  throw(Err("AudioDevice::Unregister(): AudioIn object not registered in this device."));
91  }
92  mInputs.erase(it);
93  in.mpDevice = 0;
94 }
95 
97 {
98  if (out.mpDevice != this)
99  {
100  throw(Err("AudioDevice::Unregister(): I am not this AudioOut object's device."));
101  }
102  std::vector<AudioOut*>::iterator it = std::find(mOutputs.begin(),mOutputs.end(),&out);
103  if (it == mOutputs.end())
104  {
105  throw(Err("AudioDevice::Unregister(): AudioOut object not registered in this device."));
106  }
107  mOutputs.erase(it);
108  out.mpDevice = 0;
109 }
110 
112 {
113  info.mSampleRate = SampleRate();
114  info.mName = mName;
115  info.mNChannels = mNChannels;
118 }
119 
121 {
122  /* determine the sample rate */
123  unsigned int i;
124  int sr = 0;
125  for (i=0;i<mInputs.size();i++)
126  {
127  if (sr == 0) sr = mInputs[i]->mConfig.GetSampleRate();
128  else if (mInputs[i]->mConfig.GetSampleRate() &&
129  mInputs[i]->mConfig.GetSampleRate() != sr)
130  {
131  throw Err("AudioDevice::Register():"
132  "all audio ports need the same samplerate");
133  }
134  }
135  for (i=0;i<mOutputs.size();i++)
136  {
137  if (sr == 0) sr = mOutputs[i]->mConfig.GetSampleRate();
138  else if (mOutputs[i]->mConfig.GetSampleRate() &&
139  mOutputs[i]->mConfig.GetSampleRate() != sr )
140  {
141  throw Err("AudioDevice::Register():"
142  "all audio ports need the same samplerate");
143  }
144  }
145 
146  if (sr == 0)
147  sr = _AudioManager().SampleRate();
148 
149  for (i=0;i<mInputs.size();i++)
150  {
151  mInputs[i]->mConfig.SetSampleRate(sr);
152  }
153  for (i=0;i<mOutputs.size();i++)
154  {
155  mOutputs[i]->mConfig.SetSampleRate(sr);
156  }
157 
158  return sr;
159 }
160 
162 {
163  return _AudioManager().Latency();
164 }
165 
166 void AudioDevice::SetLatency(int latency)
167 {
168  _AudioManager().SetLatency(latency);
169 }
170 
171 void AudioDevice::SetNChannels(int channels)
172 {
173  mForceNChannels = true;
174  mNChannels = channels;
175 }
176