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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 359 - (hide annotations) (download)
Sun Feb 6 21:01:38 2005 UTC (19 years, 2 months ago) by senkov
File size: 14298 byte(s)
* Added implementation for NOTIFY:CHANNELS event

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 61 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 53 * *
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 schoenebeck 123 #include <sstream>
24    
25 schoenebeck 53 #include "Sampler.h"
26    
27 schoenebeck 203 #include "drivers/audio/AudioOutputDeviceFactory.h"
28     #include "drivers/midi/MidiInputDeviceFactory.h"
29 schoenebeck 53 #include "engines/gig/Engine.h"
30 senkov 359 #include "network/lscpserver.h"
31 schoenebeck 53
32     namespace LinuxSampler {
33    
34     // ******************************************************************
35     // * SamplerChannel
36    
37     SamplerChannel::SamplerChannel(Sampler* pS) {
38     pSampler = pS;
39     pEngine = NULL;
40 capela 159 pMidiInputDevice = NULL;
41 schoenebeck 53 pAudioOutputDevice = NULL;
42 capela 159 midiPort = 0;
43 schoenebeck 221 midiChannel = MidiInputPort::midi_chan_all;
44 schoenebeck 53 iIndex = -1;
45     }
46    
47     SamplerChannel::~SamplerChannel() {
48     if (pEngine) {
49 schoenebeck 221 MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
50 senkov 155 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
51 schoenebeck 53 if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
52     delete pEngine;
53     }
54     }
55    
56 schoenebeck 64 void SamplerChannel::LoadEngine(Engine::type_t EngineType) {
57     dmsg(2,("SamplerChannel: Loading engine..."));
58 schoenebeck 53
59     // create new engine
60     Engine* pNewEngine = NULL;
61     switch (EngineType) {
62 schoenebeck 64 case Engine::type_gig:
63 schoenebeck 53 pNewEngine = new gig::Engine;
64     break;
65     default:
66     throw LinuxSamplerException("Unknown engine type");
67     }
68    
69 capela 159 // dereference midi input port.
70 schoenebeck 221 MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
71 schoenebeck 53 // disconnect old engine
72     if (pEngine) {
73 senkov 155 if (pMidiInputPort) pMidiInputPort->Disconnect(pEngine);
74 schoenebeck 53 if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
75     delete pEngine;
76     }
77    
78     // connect new engine
79     pEngine = pNewEngine;
80 capela 159 if (pMidiInputPort) pMidiInputPort->Connect(pNewEngine, this->midiChannel);
81 schoenebeck 53 if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);
82 schoenebeck 64 dmsg(2,("OK\n"));
83 schoenebeck 53 }
84    
85 schoenebeck 123 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice* pDevice) {
86 schoenebeck 53 // disconnect old device
87     if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);
88     // connect new device
89     pAudioOutputDevice = pDevice;
90     if (pEngine) pAudioOutputDevice->Connect(pEngine);
91     }
92    
93 capela 159 void SamplerChannel::SetMidiInputDevice(MidiInputDevice* pDevice) {
94     SetMidiInput(pDevice, this->midiPort, this->midiChannel);
95     }
96 schoenebeck 203
97 capela 159 void SamplerChannel::SetMidiInputPort(int MidiPort) {
98     SetMidiInput(pMidiInputDevice, MidiPort, this->midiChannel);
99     }
100 schoenebeck 203
101 schoenebeck 221 void SamplerChannel::SetMidiInputChannel(MidiInputPort::midi_chan_t MidiChannel) {
102 capela 159 SetMidiInput(pMidiInputDevice, this->midiPort, MidiChannel);
103     }
104 schoenebeck 203
105 schoenebeck 221 void SamplerChannel::SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, MidiInputPort::midi_chan_t MidiChannel) {
106 capela 159 // dereference old midi input port.
107 schoenebeck 221 MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
108 capela 159 // disconnect old device port
109 senkov 155 if (pMidiInputPort && pEngine) pMidiInputPort->Disconnect(pEngine);
110 capela 159 // new device, port and channel
111     pMidiInputDevice = pDevice;
112 schoenebeck 221 this->midiPort = iMidiPort;
113 capela 159 this->midiChannel = MidiChannel;
114     // connect new device port
115     pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
116     if (pMidiInputPort && pEngine) pMidiInputPort->Connect(pEngine, MidiChannel);
117     // Ooops.
118     if (pMidiInputPort == NULL)
119 schoenebeck 221 throw LinuxSamplerException("There is no MIDI input port with index " + ToString(iMidiPort) + ".");
120 schoenebeck 53 }
121    
122     Engine* SamplerChannel::GetEngine() {
123     return pEngine;
124     }
125    
126 schoenebeck 221 MidiInputPort::midi_chan_t SamplerChannel::GetMidiInputChannel() {
127 capela 159 return this->midiChannel;
128 schoenebeck 53 }
129    
130 capela 159 int SamplerChannel::GetMidiInputPort() {
131 schoenebeck 221 MidiInputPort* pMidiInputPort = GetMidiInputDevicePort(this->midiPort);
132 capela 159 return (pMidiInputPort ? (int) pMidiInputPort->GetPortNumber() : -1);
133     }
134    
135 schoenebeck 53 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
136     return pAudioOutputDevice;
137     }
138    
139 senkov 155 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
140 capela 159 return pMidiInputDevice;
141 senkov 155 }
142    
143 schoenebeck 53 uint SamplerChannel::Index() {
144     if (iIndex >= 0) return iIndex;
145    
146 schoenebeck 209 Sampler::SamplerChannelMap::iterator iter = pSampler->mSamplerChannels.begin();
147     for (; iter != pSampler->mSamplerChannels.end(); iter++) {
148     if (iter->second == this) {
149     iIndex = iter->first;
150     return iIndex;
151 schoenebeck 53 }
152     }
153    
154 schoenebeck 209 throw LinuxSamplerException("Internal error: SamplerChannel index not found");
155 schoenebeck 53 }
156    
157 schoenebeck 221 MidiInputPort* SamplerChannel::GetMidiInputDevicePort(int iMidiPort) {
158     MidiInputPort* pMidiInputPort = NULL;
159 capela 159 if (pMidiInputDevice)
160 schoenebeck 221 pMidiInputPort = pMidiInputDevice->GetPort(iMidiPort);
161 capela 159 return pMidiInputPort;
162     }
163 schoenebeck 53
164 schoenebeck 212
165    
166 schoenebeck 53 // ******************************************************************
167     // * Sampler
168    
169     Sampler::Sampler() {
170     }
171    
172     Sampler::~Sampler() {
173 schoenebeck 212 Reset();
174 schoenebeck 53 }
175    
176     uint Sampler::SamplerChannels() {
177 schoenebeck 209 return mSamplerChannels.size();
178 schoenebeck 53 }
179    
180     SamplerChannel* Sampler::AddSamplerChannel() {
181 schoenebeck 209 // if there's no sampler channel yet
182     if (!mSamplerChannels.size()) {
183     SamplerChannel* pChannel = new SamplerChannel(this);
184     mSamplerChannels[0] = pChannel;
185 senkov 359 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, 1));
186 schoenebeck 209 return pChannel;
187     }
188    
189     // get the highest used sampler channel index
190     uint lastIndex = (--(mSamplerChannels.end()))->first;
191    
192     // check if we reached the index limit
193     if (lastIndex + 1 < lastIndex) {
194     // search for an unoccupied sampler channel index starting from 0
195     for (uint i = 0; i < lastIndex; i++) {
196     if (mSamplerChannels.find(i) != mSamplerChannels.end()) continue;
197     // we found an unused index, so insert the new channel there
198     SamplerChannel* pChannel = new SamplerChannel(this);
199     mSamplerChannels[i] = pChannel;
200 senkov 359 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, i));
201 schoenebeck 209 return pChannel;
202     }
203     throw LinuxSamplerException("Internal error: could not find unoccupied sampler channel index.");
204     }
205    
206     // we have not reached the index limit so we just add the channel past the highest index
207 schoenebeck 53 SamplerChannel* pChannel = new SamplerChannel(this);
208 schoenebeck 209 mSamplerChannels[lastIndex + 1] = pChannel;
209 senkov 359 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, lastIndex + 1));
210 schoenebeck 53 return pChannel;
211     }
212    
213     SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
214 schoenebeck 209 return (mSamplerChannels.find(uiSamplerChannel) != mSamplerChannels.end()) ? mSamplerChannels[uiSamplerChannel] : NULL;
215 schoenebeck 53 }
216    
217 schoenebeck 209 std::map<uint, SamplerChannel*> Sampler::GetSamplerChannels() {
218     return mSamplerChannels;
219     }
220    
221 schoenebeck 53 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
222 schoenebeck 209 SamplerChannelMap::iterator iterChan = mSamplerChannels.begin();
223     for (; iterChan != mSamplerChannels.end(); iterChan++) {
224     if (iterChan->second == pSamplerChannel) {
225     mSamplerChannels.erase(iterChan);
226 schoenebeck 53 delete pSamplerChannel;
227 senkov 359 LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_channels, mSamplerChannels.size()));
228 schoenebeck 53 return;
229     }
230     }
231     }
232    
233     void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
234     SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
235     if (!pChannel) return;
236     RemoveSamplerChannel(pChannel);
237     }
238    
239 schoenebeck 123 std::vector<String> Sampler::AvailableAudioOutputDrivers() {
240     return AudioOutputDeviceFactory::AvailableDrivers();
241     }
242 schoenebeck 53
243 schoenebeck 123 AudioOutputDevice* Sampler::CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
244 schoenebeck 53 // create new device
245 schoenebeck 123 AudioOutputDevice* pDevice = AudioOutputDeviceFactory::Create(AudioDriver, Parameters);
246 schoenebeck 53
247 schoenebeck 64 // add new audio device to the audio device list
248 schoenebeck 123 for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
249     if (!mAudioOutputDevices[i]) {
250     mAudioOutputDevices[i] = pDevice;
251     break;
252     }
253     }
254 schoenebeck 64
255 schoenebeck 53 return pDevice;
256     }
257    
258 schoenebeck 123 uint Sampler::AudioOutputDevices() {
259     return mAudioOutputDevices.size();
260 schoenebeck 53 }
261    
262 senkov 155 uint Sampler::MidiInputDevices() {
263     return mMidiInputDevices.size();
264     }
265    
266 schoenebeck 123 std::map<uint, AudioOutputDevice*> Sampler::GetAudioOutputDevices() {
267     return mAudioOutputDevices;
268     }
269    
270 senkov 155 std::map<uint, MidiInputDevice*> Sampler::GetMidiInputDevices() {
271     return mMidiInputDevices;
272     }
273    
274 schoenebeck 123 void Sampler::DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (LinuxSamplerException) {
275     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
276     for (; iter != mAudioOutputDevices.end(); iter++) {
277     if (iter->second == pDevice) {
278     // check if there are still sampler engines connected to this device
279     for (uint i = 0; i < SamplerChannels(); i++)
280     if (GetSamplerChannel(i)->GetAudioOutputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the audio output device.");
281    
282     // disable device
283     pDevice->Stop();
284    
285     // remove device from the device list
286     mAudioOutputDevices.erase(iter);
287    
288     // destroy and free device from memory
289     delete pDevice;
290     }
291     }
292     }
293    
294 senkov 155 void Sampler::DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (LinuxSamplerException) {
295     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
296     for (; iter != mMidiInputDevices.end(); iter++) {
297     if (iter->second == pDevice) {
298     // check if there are still sampler engines connected to this device
299     for (uint i = 0; i < SamplerChannels(); i++)
300     if (GetSamplerChannel(i)->GetMidiInputDevice() == pDevice) throw LinuxSamplerException("Sampler channel " + ToString(i) + " is still connected to the midi input device.");
301 schoenebeck 53
302 senkov 155 // disable device
303     pDevice->StopListen();
304    
305     // remove device from the device list
306     mMidiInputDevices.erase(iter);
307    
308     // destroy and free device from memory
309     delete pDevice;
310     }
311 schoenebeck 53 }
312 senkov 155 }
313 schoenebeck 53
314 senkov 155 MidiInputDevice* Sampler::CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (LinuxSamplerException) {
315     // create new device
316     MidiInputDevice* pDevice = MidiInputDeviceFactory::Create(MidiDriver, Parameters);
317    
318     // add new device to the midi device list
319     for (uint i = 0; ; i++) { // seek for a free place starting from the beginning
320     if (!mMidiInputDevices[i]) {
321     mMidiInputDevices[i] = pDevice;
322     break;
323     }
324     }
325 schoenebeck 64
326 schoenebeck 53 return pDevice;
327     }
328    
329 schoenebeck 212 void Sampler::Reset() {
330     // delete sampler channels
331     try {
332 senkov 329 while (true) {
333     SamplerChannelMap::iterator iter = mSamplerChannels.begin();
334     if (iter == mSamplerChannels.end()) break;
335     RemoveSamplerChannel(iter->second);
336 schoenebeck 212 }
337     }
338     catch(...) {
339     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all sampler channels, exiting.\n" << std::flush;
340     exit(EXIT_FAILURE);
341     }
342    
343     // delete midi input devices
344     try {
345 senkov 329 while (true) {
346     MidiInputDeviceMap::iterator iter = mMidiInputDevices.begin();
347     if (iter == mMidiInputDevices.end()) break;
348     DestroyMidiInputDevice(iter->second);
349 schoenebeck 212 }
350     }
351     catch(...) {
352     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all MIDI input devices, exiting.\n" << std::flush;
353     exit(EXIT_FAILURE);
354     }
355    
356     // delete audio output devices
357     try {
358 senkov 329 while (true) {
359     AudioOutputDeviceMap::iterator iter = mAudioOutputDevices.begin();
360     if (iter == mAudioOutputDevices.end()) break;
361     DestroyAudioOutputDevice(iter->second);
362 schoenebeck 212 }
363     }
364     catch(...) {
365     std::cerr << "Sampler::Reset(): Exception occured while trying to delete all audio output devices, exiting.\n" << std::flush;
366     exit(EXIT_FAILURE);
367     }
368     }
369    
370 schoenebeck 53 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC