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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 76 - (show annotations) (download)
Fri May 14 19:10:21 2004 UTC (19 years, 11 months ago) by senkov
File size: 9444 byte(s)
fix small make problem when jack is not installed

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 "Sampler.h"
24
25 #include "audiodriver/AudioOutputDeviceAlsa.h"
26 #include "audiodriver/AudioOutputDeviceJack.h"
27 #include "mididriver/MidiInputDeviceAlsa.h"
28 #include "engines/gig/Engine.h"
29
30 namespace LinuxSampler {
31
32 // ******************************************************************
33 // * SamplerChannel
34
35 SamplerChannel::SamplerChannel(Sampler* pS) {
36 pSampler = pS;
37 pEngine = NULL;
38 pMidiInputDevice = NULL;
39 pAudioOutputDevice = NULL;
40 iIndex = -1;
41 }
42
43 SamplerChannel::~SamplerChannel() {
44 if (pEngine) {
45 if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);
46 if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
47 delete pEngine;
48 }
49 }
50
51 void SamplerChannel::LoadEngine(Engine::type_t EngineType) {
52 dmsg(2,("SamplerChannel: Loading engine..."));
53
54 // create new engine
55 Engine* pNewEngine = NULL;
56 switch (EngineType) {
57 case Engine::type_gig:
58 pNewEngine = new gig::Engine;
59 break;
60 default:
61 throw LinuxSamplerException("Unknown engine type");
62 }
63
64 // disconnect old engine
65 if (pEngine) {
66 if (pMidiInputDevice) pMidiInputDevice->Disconnect(pEngine);
67 if (pAudioOutputDevice) pAudioOutputDevice->Disconnect(pEngine);
68 delete pEngine;
69 }
70
71 // connect new engine
72 pEngine = pNewEngine;
73 if (pMidiInputDevice) pMidiInputDevice->Connect(pNewEngine, (MidiInputDevice::midi_chan_t) Index());
74 if (pAudioOutputDevice) pAudioOutputDevice->Connect(pNewEngine);
75 dmsg(2,("OK\n"));
76 }
77
78 void SamplerChannel::SetAudioOutputDevice(AudioOutputDevice::type_t AudioType) {
79 // get / create desired audio device
80 AudioOutputDevice* pDevice = pSampler->GetAudioOutputDevice(AudioType);
81 if (!pDevice) pDevice = pSampler->CreateAudioOutputDevice(AudioType);
82
83 // disconnect old device
84 if (pAudioOutputDevice && pEngine) pAudioOutputDevice->Disconnect(pEngine);
85
86 // connect new device
87 pAudioOutputDevice = pDevice;
88 if (pEngine) pAudioOutputDevice->Connect(pEngine);
89 }
90
91 void SamplerChannel::SetMidiInputDevice(MidiInputDevice::type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel) {
92 // get / create desired midi device
93 MidiInputDevice* pDevice = pSampler->GetMidiInputDevice(MidiType);
94 if (!pDevice) pDevice = pSampler->CreateMidiInputDevice(MidiType);
95
96 // disconnect old device
97 if (pMidiInputDevice && pEngine) pMidiInputDevice->Disconnect(pEngine);
98
99 // connect new device
100 pMidiInputDevice = pDevice;
101 if (pEngine) pMidiInputDevice->Connect(pEngine, MidiChannel);
102 }
103
104 Engine* SamplerChannel::GetEngine() {
105 return pEngine;
106 }
107
108 MidiInputDevice* SamplerChannel::GetMidiInputDevice() {
109 return pMidiInputDevice;
110 }
111
112 AudioOutputDevice* SamplerChannel::GetAudioOutputDevice() {
113 return pAudioOutputDevice;
114 }
115
116 uint SamplerChannel::Index() {
117 if (iIndex >= 0) return iIndex;
118
119 std::vector<SamplerChannel*>::iterator iter = pSampler->vSamplerChannels.begin();
120 for (int i = 0; iter != pSampler->vSamplerChannels.end(); i++, iter++) {
121 if (*iter == this) {
122 iIndex = i;
123 return i;
124 }
125 }
126
127 throw LinuxSamplerException("SamplerChannel index not found");
128 }
129
130
131 // ******************************************************************
132 // * Sampler
133
134 Sampler::Sampler() {
135 }
136
137 Sampler::~Sampler() {
138 // delete sampler channels
139 {
140 std::vector<SamplerChannel*>::iterator iter = vSamplerChannels.begin();
141 for (; iter != vSamplerChannels.end(); iter++) delete *iter;
142 }
143
144 // delete midi input devices
145 {
146 MidiInputDeviceMap::iterator iter = MidiInputDevices.begin();
147 for (; iter != MidiInputDevices.end(); iter++) {
148 MidiInputDevice* pDevice = iter->second;
149 pDevice->StopListen();
150 delete pDevice;
151 }
152 }
153
154 // delete audio output devices
155 {
156 AudioOutputDeviceMap::iterator iter = AudioOutputDevices.begin();
157 for (; iter != AudioOutputDevices.end(); iter++) {
158 AudioOutputDevice* pDevice = iter->second;
159 pDevice->Stop();
160 delete pDevice;
161 }
162 }
163 }
164
165 uint Sampler::SamplerChannels() {
166 return vSamplerChannels.size();
167 }
168
169 SamplerChannel* Sampler::AddSamplerChannel() {
170 SamplerChannel* pChannel = new SamplerChannel(this);
171 vSamplerChannels.push_back(pChannel);
172 return pChannel;
173 }
174
175 SamplerChannel* Sampler::GetSamplerChannel(uint uiSamplerChannel) {
176 if (uiSamplerChannel >= SamplerChannels()) return NULL;
177 return vSamplerChannels[uiSamplerChannel];
178 }
179
180 void Sampler::RemoveSamplerChannel(SamplerChannel* pSamplerChannel) {
181 std::vector<SamplerChannel*>::iterator iterChan = vSamplerChannels.begin();
182 for (; iterChan != vSamplerChannels.end(); iterChan++) {
183 if (*iterChan == pSamplerChannel) {
184 vSamplerChannels.erase(iterChan);
185 delete pSamplerChannel;
186 return;
187 }
188 }
189 }
190
191 void Sampler::RemoveSamplerChannel(uint uiSamplerChannel) {
192 SamplerChannel* pChannel = GetSamplerChannel(uiSamplerChannel);
193 if (!pChannel) return;
194 RemoveSamplerChannel(pChannel);
195 }
196
197 AudioOutputDevice* Sampler::CreateAudioOutputDevice(AudioOutputDevice::type_t AudioType) {
198 // check if device already created
199 AudioOutputDevice* pDevice = GetAudioOutputDevice(AudioType);
200 if (pDevice) return pDevice;
201
202 // create new device
203 switch (AudioType) {
204 case AudioOutputDevice::type_alsa:
205 pDevice = new AudioOutputDeviceAlsa;
206 break;
207 #if HAVE_JACK
208 case AudioOutputDevice::type_jack:
209 pDevice = new AudioOutputDeviceJack;
210 break;
211 #endif
212 default:
213 throw LinuxSamplerException("Unknown audio output device type");
214 }
215
216 // activate device
217 pDevice->Play();
218
219 // add new audio device to the audio device list
220 AudioOutputDevices[AudioType] = pDevice;
221
222 return pDevice;
223 }
224
225 AudioOutputDevice* Sampler::GetAudioOutputDevice(AudioOutputDevice::type_t AudioType) {
226 AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType);
227 return (iter != AudioOutputDevices.end()) ? iter->second : NULL;
228 }
229
230 MidiInputDevice* Sampler::CreateMidiInputDevice(MidiInputDevice::type_t MidiType) {
231 // check if device already created
232 MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);
233 if (pDevice) return pDevice;
234
235 // create new device
236 switch (MidiType) {
237 case MidiInputDevice::type_alsa:
238 pDevice = new MidiInputDeviceAlsa;
239 break;
240 default:
241 throw LinuxSamplerException("Unknown audio output device type");
242 }
243
244 // activate device
245 pDevice->Listen();
246
247 // add new MIDI device to the MIDI device list
248 MidiInputDevices[MidiType] = pDevice;
249
250 return pDevice;
251 }
252
253 MidiInputDevice* Sampler::GetMidiInputDevice(MidiInputDevice::type_t MidiType) {
254 MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);
255 return (iter != MidiInputDevices.end()) ? iter->second : NULL;
256 }
257
258 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC