/[svn]/linuxsampler/trunk/src/Sampler.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/Sampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (show annotations) (download)
Mon Jun 28 04:30:11 2004 UTC (19 years, 9 months ago) by senkov
File size: 10907 byte(s)
* Updated parser, lscp server and sampler class for new MIDI_INPUT
* Minor fixes (and major new bugs) here and there
* Consolidated 3 SET CHANNEL MIDI_xxx commands into one:
SET CHANNEL MIDI_INPUT

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include <sstream>
24
25 #include "Sampler.h"
26
27 #include "audiodriver/AudioOutputDeviceFactory.h"
28 #include "mididriver/MidiInputDeviceFactory.h"
29 #include "engines/gig/Engine.h"
30
31 namespace LinuxSampler {
32
33 // ******************************************************************
34 // * SamplerChannel
35
36 SamplerChannel::SamplerChannel(Sampler* pS) {
37 pSampler = pS;
38 pEngine = NULL;
39 pMidiInputPort = NULL;
40 pAudioOutputDevice = NULL;
41 iIndex = -1;
42 midiChannel = MidiInputDevice::MidiInputPort::midi_chan_all;
43 }
44
45 SamplerChannel::~SamplerChannel() {
46 if (pEngine) {
47 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
48 if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
49 delete pEngine;
50 }
51 }
52
53 void SamplerChannel::LoadEngine(Engine::type_t EngineType) {
54 dmsg(2,("SamplerChannel: Loading engine..."));
55
56 // create new engine
57 Engine* pNewEngine = NULL;
58 switch (EngineType) {
59 case Engine::type_gig:
60 pNewEngine = new gig::Engine;
61 break;
62 default:
63 throw LinuxSamplerException("Unknown engine type");
64 }
65
66 // disconnect old engine
67 if (pEngine) {
68 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
69 if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
70 delete pEngine;
71 }
72
73 // connect new engine
74 pEngine = pNewEngine;
75 if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, (MidiInputDevice::MidiInputPort::midi_chan_t) Index());
76 if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);
77 dmsg(2,("OK\n"));
78 }
79
80 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
81 // disconnect old device
82 if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);
83
84 // connect new device
85 pAudioOutputDevice = pDevice;
86 if (pEngine) pAudioOutputDevice->Connect(pEngine);
87 }
88
89 void SamplerChannel::SetMidiInputPort(MidiInputDevice* pDevice, int midiPort, MidiInputDevice::MidiInputPort::midi_chan_t MidiChannel) {
90 // disconnect old port
91 if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);
92
93 // connect new port
94 pMidiInputPort = pDevice->GetPort(midiPort);
95 if (!pMidiInputPort)
96 throw LinuxSamplerException("MIDI port doesn't exist");
97 if (pEngine) {
98 pMidiInputPort->Connect(pEngine, MidiChannel);
99 this->midiChannel = MidiChannel;
100 }
101 }
102
103 Engine* SamplerChannel::GetEngine() {
104 return pEngine;
105 }
106
107 MidiInputDevice::MidiInputPort* SamplerChannel::GetMidiInputPort() {
108 return pMidiInputPort;
109 }
110
111 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
112 return pAudioOutputDevice;
113 }
114
115 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
116 MidiInputDevice::MidiInputPort* port = GetMidiInputPort();
117 if (!port)
118 return NULL;
119 return port->GetDevice();
120 }
121
122 uint SamplerChannel::Index() {
123 if (iIndex >= 0) return iIndex;
124
125 std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();
126 for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {
127 if (*iter == this) {
128 iIndex = i;
129 return i;
130 }
131 }
132
133 throw LinuxSamplerException("SamplerChannel index not found");
134 }
135
136
137 // ******************************************************************
138 // * Sampler
139
140 Sampler::Sampler() {
141 }
142
143 Sampler::~Sampler() {
144 // delete sampler channels
145 {
146 std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();
147 for (; iter != vSamplerChannels.end(); iter++) delete *iter;
148 }
149
150 // delete midi input devices
151 {
152 MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
153 for (; iter != mMidiInputDevices.end(); iter++) {
154 MidiInputDevice* pDevice = iter->second;
155 pDevice->StopListen();
156 delete pDevice;
157 }
158 }
159
160 // delete audio output devices
161 {
162 AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
163 for (; iter != mAudioOutputDevices.end(); iter++) {
164 AudioOutputDevice* pDevice = iter->second;
165 pDevice->Stop();
166 delete pDevice;
167 }
168 }
169 }
170
171 uint Sampler::SamplerChannels() {
172 return vSamplerChannels.size();
173 }
174
175 SamplerChannel* Sampler::AddSamplerChannel() {
176 SamplerChannel* pChannel = new SamplerChannel(this);
177 vSamplerChannels.push_back(pChannel);
178 return pChannel;
179 }
180
181 SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
182 if (uiSamplerChannel >= SamplerChannels()) return NULL;
183 return vSamplerChannels[uiSamplerChannel];
184 }
185
186 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
187 std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();
188 for (; iterChan != vSamplerChannels.end(); iterChan++) {
189 if (*iterChan == pSamplerChannel) {
190 vSamplerChannels.erase(iterChan);
191 delete pSamplerChannel;
192 return;
193 }
194 }
195 }
196
197 void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
198 SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
199 if (!pChannel) return;
200 RemoveSamplerChannel(pChannel);
201 }
202
203 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
204 return AudioOutputDeviceFactory::AvailableDrivers();
205 }
206
207 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
208 // create new device
209 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
210
211 // activate device
212 pDevice->Play();
213
214 // add new audio device to the audio device list
215 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
216 if (!mAudioOutputDevices[i]) {
217 mAudioOutputDevices[i] = pDevice;
218 break;
219 }
220 }
221
222 return pDevice;
223 }
224
225 uint Sampler::AudioOutputDevices() {
226 return mAudioOutputDevices.size();
227 }
228
229 uint Sampler::MidiInputDevices() {
230 return mMidiInputDevices.size();
231 }
232
233 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
234 return mAudioOutputDevices;
235 }
236
237 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
238 return mMidiInputDevices;
239 }
240
241 void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {
242 AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
243 for (; iter != mAudioOutputDevices.end(); iter++) {
244 if (iter->second == pDevice) {
245 // check if there are still sampler engines connected to this device
246 for (uint i = 0; i < SamplerChannels(); i++)
247 if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
248
249 // disable device
250 pDevice->Stop();
251
252 // remove device from the device list
253 mAudioOutputDevices.erase(iter);
254
255 // destroy and free device from memory
256 delete pDevice;
257 }
258 }
259 }
260
261 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
262 MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
263 for (; iter != mMidiInputDevices.end(); iter++) {
264 if (iter->second == pDevice) {
265 // check if there are still sampler engines connected to this device
266 for (uint i = 0; i < SamplerChannels(); i++)
267 if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
268
269 // disable device
270 pDevice->StopListen();
271
272 // remove device from the device list
273 mMidiInputDevices.erase(iter);
274
275 // destroy and free device from memory
276 delete pDevice;
277 }
278 }
279 }
280
281 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
282 // create new device
283 MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);
284
285 // activate device
286 pDevice->Listen();
287
288 // add new device to the midi device list
289 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
290 if (!mMidiInputDevices[i]) {
291 mMidiInputDevices[i] = pDevice;
292 break;
293 }
294 }
295
296 return pDevice;
297 }
298
299 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC