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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 53 - (hide annotations) (download)
Mon Apr 26 17:15:51 2004 UTC (19 years, 11 months ago) by schoenebeck
File size: 9178 byte(s)
* completely restructured source tree
* implemented multi channel support
* implemented instrument manager, which controls sharing of instruments
  between multiple sampler engines / sampler channels
* created abstract classes 'AudioOutputDevice' and 'MidiInputDevice' for
  convenient implementation of further audio output driver and MIDI input
  driver for LinuxSampler
* implemented following LSCP commands: 'SET CHANNEL MIDI INPUT TYPE',
  'LOAD ENGINE', 'GET CHANNELS', 'ADD CHANNEL', 'REMOVE CHANNEL',
  'SET CHANNEL AUDIO OUTPUT TYPE'
* temporarily removed all command line options
* LSCP server is now launched by default

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003 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(1,("SamplerChannel: Loading engine\n"));
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(1,("SamplerChannel: Engine loaded.\n"));
76     }
77    
78     void SamplerChannel::SetAudioOutputDevice(audio_output_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(midi_input_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     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(audio_output_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 audio_output_type_alsa:
205     pDevice = new AudioOutputDeviceAlsa;
206     break;
207     case audio_output_type_jack:
208     pDevice = new AudioOutputDeviceJack;
209     break;
210     default:
211     throw LinuxSamplerException("Unknown audio output device type");
212     }
213    
214     // activate device
215     pDevice->Play();
216    
217     return pDevice;
218     }
219    
220     AudioOutputDevice* Sampler::GetAudioOutputDevice(audio_output_type_t AudioType) {
221     AudioOutputDeviceMap::iterator iter = AudioOutputDevices.find(AudioType);
222     return (iter != AudioOutputDevices.end()) ? iter->second : NULL;
223     }
224    
225     MidiInputDevice* Sampler::CreateMidiInputDevice(midi_input_type_t MidiType) {
226     // check if device already created
227     MidiInputDevice* pDevice = GetMidiInputDevice(MidiType);
228     if (pDevice) return pDevice;
229    
230     // create new device
231     switch (MidiType) {
232     case midi_input_type_alsa:
233     pDevice = new MidiInputDeviceAlsa;
234     break;
235     default:
236     throw LinuxSamplerException("Unknown audio output device type");
237     }
238    
239     // activate device
240     pDevice->Listen();
241    
242     return pDevice;
243     }
244    
245     MidiInputDevice* Sampler::GetMidiInputDevice(midi_input_type_t MidiType) {
246     MidiInputDeviceMap::iterator iter = MidiInputDevices.find(MidiType);
247     return (iter != MidiInputDevices.end()) ? iter->second : NULL;
248     }
249    
250     } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC