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

Diff of /linuxsampler/trunk/src/Sampler.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 53 by schoenebeck, Mon Apr 26 17:15:51 2004 UTC revision 1130 by iliev, Sun Mar 25 18:59:14 2007 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck         *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   Copyright (C) 2005, 2006 Christian Schoenebeck                        *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 25  Line 26 
26    
27  #include <vector>  #include <vector>
28  #include <map>  #include <map>
29    #include "EventListeners.h"
30  #include "common/global.h"  #include "common/global.h"
31  #include "common/LinuxSamplerException.h"  #include "common/Exception.h"
32  #include "engines/common/Engine.h"  #include "engines/EngineChannel.h"
33  #include "mididriver/MidiInputDevice.h"  #include "drivers/midi/MidiInputDevice.h"
34  #include "audiodriver/AudioOutputDevice.h"  #include "drivers/audio/AudioOutputDevice.h"
35    
36  namespace LinuxSampler {  namespace LinuxSampler {
37    
     /**  
      * Which sampler engine to be used.  
      */  
     enum engine_type_t {  
         engine_type_gig  
     };  
   
     /**  
      * Which audio output system to be used.  
      */  
     enum audio_output_type_t {  
         audio_output_type_alsa,  
         audio_output_type_jack  
     };  
   
     /**  
      * Which MIDI input system to be used.  
      */  
     enum midi_input_type_t {  
         midi_input_type_alsa  
     };  
   
38      // just symbol prototyping      // just symbol prototyping
39      class Sampler;      class Sampler;
40    
41        /** @brief LinuxSampler sampler channel
42         *
43         * Encapsulates a channel of a specific sampler engine type, one
44         * connection to a MIDI input device and one connection to an audio
45         * output device. You cannot create an instance of this class on your
46         * own, you have to use the AddSamplerChannel() method of the Sampler
47         * object to create a new sampler channel.
48         */
49      class SamplerChannel {      class SamplerChannel {
50          public:          public:
51              SamplerChannel(Sampler* pS);              /**
52             ~SamplerChannel();               * Assign a sampler engine type to this sampler channel.
53              void               LoadEngine(engine_type_t EngineType);               *
54              void               SetAudioOutputDevice(audio_output_type_t AudioType);               * @param EngineType - type of the engine to use
55              void               SetMidiInputDevice(midi_input_type_t MidiType, MidiInputDevice::midi_chan_t MidiChannel = MidiInputDevice::midi_chan_all);               * @throws Exception - if \a EngineType is invalid
56              Engine*            GetEngine();               * @see Sampler::AvailableEngineTypes()
57              MidiInputDevice*   GetMidiInputDevice();               */
58                void SetEngineType(String EngineType) throw (Exception);
59    
60                /**
61                 * Connect this sampler channel to an audio output device, that
62                 * is an instance of an audio output driver. If this sampler
63                 * channel was already connected to an audio output device, then
64                 * the old connection will automatically be removed before.
65                 *
66                 * @param pDevice - audio output device to connect to
67                 */
68                void SetAudioOutputDevice(AudioOutputDevice* pDevice);
69    
70                /**
71                 * Connect this sampler channel to a MIDI input device.
72                 *
73                 * @param pDevice - MIDI input device to connect to
74                 */
75                void SetMidiInputDevice(MidiInputDevice *pDevice);
76    
77                /**
78                 * Connect this sampler channel to a MIDI input port.
79                 *
80                 * @param MidiPort - MIDI port to connect to
81                 */
82                void SetMidiInputPort(int MidiPort);
83    
84                /**
85                 * Define on which MIDI channel(s) this sampler channel should
86                 * listen to. By default, that is after creation of a new
87                 * sampler channel, the sampler channel will listen to all MIDI
88                 * channels.
89                 *
90                 * @param MidiChannel - MIDI channel to listen
91                 */
92                void SetMidiInputChannel(midi_chan_t MidiChannel);
93    
94                /**
95                 * Connect this sampler channel to a MIDI input triplet.
96                 *
97                 * @param pDevice - MIDI input device to connect to
98                 * @param iMidiPort - MIDI port to connect to
99                 * @param MidiChannel - optional: MIDI channel on which the
100                 *                      sampler channel should listen to
101                 *                      (default: listen on all MIDI channels)
102                 */
103                void SetMidiInput(MidiInputDevice* pDevice, int iMidiPort, midi_chan_t MidiChannel = midi_chan_all);
104    
105                /**
106                 * Returns the EngineChannel object that was deployed on this
107                 * sampler channel appropriate to the given sampler engine type.
108                 *
109                 * @returns  pointer to engine or NULL if no engine deployed
110                 */
111                EngineChannel* GetEngineChannel();
112    
113                /**
114                 * Returns the MIDI input channel to which this sampler
115                 * channel is currently connected to.
116                 *
117                 * @returns  The MIDI input channel on which the sampler
118                 *           channel is listening to.
119                 */
120                midi_chan_t GetMidiInputChannel();
121    
122                /**
123                 * Returns the MIDI input port number to which this sampler
124                 * channel is currently connected to.
125                 *
126                 * @returns  MIDI input port number or -1 if not connected
127                 */
128                int GetMidiInputPort();
129    
130                /**
131                 * Returns the audio output device to which this sampler channel
132                 * is currently connected to.
133                 *
134                 * @returns  pointer to audio output device or NULL if not
135                 *           connected
136                 */
137              AudioOutputDevice* GetAudioOutputDevice();              AudioOutputDevice* GetAudioOutputDevice();
138              uint               Index();  
139                /**
140                 * Returns the MIDI input device to which this sampler channel
141                 * is currently connected to.
142                 *
143                 * @returns  pointer to MIDI input device or NULL if not
144                 *           connected
145                 */
146                MidiInputDevice* GetMidiInputDevice();
147    
148                /**
149                 * Returns the index number of this sampler channel within the
150                 * Sampler instance.
151                 */
152                uint Index();
153                
154                /**
155                 * Registers the specified listener to be notified
156                 * when the engine type of this sampler channel is changed.
157                 */
158                void AddEngineChangeListener(EngineChangeListener* l);
159    
160                /**
161                 * Removes the specified listener.
162                 */
163                void RemoveEngineChangeListener(EngineChangeListener* l);
164    
165                /**
166                 * Removes the specified listener.
167                 */
168                void RemoveAllEngineChangeListeners();
169    
170                /**
171                 * Notifies listeners that the engine
172                 * type of this sampler channel is changed.
173                 */
174                void fireEngineChanged();
175    
176    
177          protected:          protected:
178                SamplerChannel(Sampler* pS);
179                virtual ~SamplerChannel();
180    
181                /** Getting MIDI input device port given its index number. */
182                MidiInputPort* __GetMidiInputDevicePort(int iMidiPort);
183    
184              Sampler*           pSampler;              Sampler*           pSampler;
185              Engine*            pEngine;              EngineChannel*     pEngineChannel;
             MidiInputDevice*   pMidiInputDevice;  
186              AudioOutputDevice* pAudioOutputDevice;              AudioOutputDevice* pAudioOutputDevice;
187                MidiInputDevice*   pMidiInputDevice;
188              int                iIndex;              int                iIndex;
189    
190                friend class Sampler;
191            private:
192                int                iMidiPort;   ///< Don't access directly, read GetMidiInputPort() instead !
193                midi_chan_t        midiChannel; ///< Don't access directly, read GetMidiInputChannel() instead !
194                ListenerList<EngineChangeListener*> llEngineChangeListeners;
195      };      };
196    
197        /** @brief LinuxSampler main class
198         *
199         * This is the toplevel class for a LinuxSampler instance.
200         *
201         * LinuxSampler can have arbitrary numbers of sampler channels. Each
202         * sampler channel can individually be deployed with it's own sampler
203         * engine, connected to an arbitrary MIDI input device and connected to
204         * an arbitrary audio output device. Here an example setup:
205         * @code
206         * S.Channel    MIDI in    S.Engine         Audio out
207         * -------------------------------------------------------------------
208         *   0          Alsa   ->  gig::Engine  ->  Jack
209         *   1          VSTi   ->  Akai::Engine ->  VSTi
210         *   2          Jack   ->  DLS::Engine  ->  Jack
211         *   3          Jack   ->  SF::Engine   ->  Alsa
212         *
213         * ... (and so on) ...
214         * @endcode
215         *
216         * Note that not all audio and MIDI backends and sampler engines listed
217         * in the example above might already been implemented!
218         *
219         * As you can see in the example setup, LinuxSampler is capable to use
220         * several, different audio output and MIDI input systems
221         * simultaniously at the same time. Here the example setup shown in the
222         * aspect of MIDI input and audio output devices / drivers:
223         * @code
224         *                      ######################### #########################
225         *                      # AudioOutputDeviceJack # # AudioOutputDeviceVSTi #
226         *                      ######################### #########################
227         *                                        ^   ^           ^
228         *  /------------>|Sampler Channel 0|-----/   |           |
229         *  |  /--------->|Sampler Channel 1|---------------------/
230         *  |  |  /------>|Sampler Channel 2|---------/
231         *  |  |  |  /--->|Sampler Channel 3|------------>#########################
232         *  |  |  |  |    ... (and so on) ...             # AudioOutputDeviceAlsa #
233         *  |  |  |  |                                    #########################
234         *  |  |  |  \-----------------------------------------------------\
235         *  |  |  \--------------------------------------------\           |
236         *  |  \--------------------\                          |           |
237         *  |                       |                          |           |
238         * ####################### ####################### #######################
239         * # MidiInputDeviceAlsa # # MidiInputDeviceVSTi # # MidiInputDeviceJack #
240         * ####################### ####################### #######################
241         * @endcode
242         *
243         * As you can see in this example setup, one device (that is midi input
244         * driver / audio output driver) can be connected multiple times to
245         * different sampler channels.
246         *
247         * It's even possible to create multiple instances of the same driver, for
248         * example multiple instances of the Alsa output driver to use multiple
249         * sound cards at the same time, or multiple instances of the JACK audio
250         * output driver to leverage SMP systems or boxes with several hard discs.
251         */
252      class Sampler {      class Sampler {
253          public:          public:
254                /**
255                 * Constructor. Create a LinuxSampler instance.
256                 */
257              Sampler();              Sampler();
            ~Sampler();  
             uint               SamplerChannels();  
             SamplerChannel*    AddSamplerChannel();  
             SamplerChannel*    GetSamplerChannel(uint uiSamplerChannel);  
             void               RemoveSamplerChannel(SamplerChannel* pSamplerChannel);  
             void               RemoveSamplerChannel(uint uiSamplerChannel);  
             AudioOutputDevice* CreateAudioOutputDevice(audio_output_type_t AudioType);  
             AudioOutputDevice* GetAudioOutputDevice(audio_output_type_t AudioType);  
             MidiInputDevice*   CreateMidiInputDevice(midi_input_type_t MidiType);  
             MidiInputDevice*   GetMidiInputDevice(midi_input_type_t MidiType);  
         protected:  
             typedef std::map<audio_output_type_t, AudioOutputDevice*> AudioOutputDeviceMap;  
             typedef std::map<midi_input_type_t, MidiInputDevice*> MidiInputDeviceMap;  
258    
259              std::vector<SamplerChannel*> vSamplerChannels;   ///< contains all created sampler channels              /**
260              AudioOutputDeviceMap         AudioOutputDevices; ///< contains all created audio output devices               * Destructor.
261              MidiInputDeviceMap           MidiInputDevices;               */
262                virtual ~Sampler();
263    
264                /**
265                 * Returns the number of sampler channels currently allocated.
266                 */
267                uint SamplerChannels();
268    
269                /**
270                 * Create and add a new sampler channel to this Sampler
271                 * instance. For race condition reasons the new channel will use
272                 * an index past the last already existing sampler channel
273                 * index (in case the index limit was not reached yet, otherwise
274                 * a free index starting from 0 is searched).
275                 *
276                 * @returns  pointer to new sampler channel
277                 */
278                SamplerChannel* AddSamplerChannel();
279    
280                /**
281                 * Returns the sampler channel of the given sampler channel
282                 * index.
283                 *
284                 * @returns  pointer to sought sampler channel
285                 */
286                SamplerChannel* GetSamplerChannel(uint uiSamplerChannel);
287    
288                /**
289                 * Returns all created sampler channels.
290                 */
291                std::map<uint, SamplerChannel*> GetSamplerChannels();
292    
293                /**
294                 * Destroy and remove the given sampler channel from this
295                 * Sampler instance.
296                 *
297                 * @param pSamplerChannel - pointer to sampler channel to remove
298                 */
299                void RemoveSamplerChannel(SamplerChannel* pSamplerChannel);
300    
301                /**
302                 * Destroy and remove the given sampler channel from this
303                 * Sampler instance.
304                 *
305                 * @param uiSamplerChannel - index of the sampler channel to
306                 *                           remove
307                 */
308                void RemoveSamplerChannel(uint uiSamplerChannel);
309    
310                /**
311                 * Registers the specified listener to be notified
312                 * when the number of sampler chanels is changed.
313                 */
314                void AddChannelCountListener(ChannelCountListener* l);
315    
316                /**
317                 * Removes the specified listener.
318                 */
319                void RemoveChannelCountListener(ChannelCountListener* l);
320    
321                /**
322                 * Registers the specified listener to be notified
323                 * when the number of audio output devices is changed.
324                 */
325                void AddAudioDeviceCountListener(AudioDeviceCountListener* l);
326    
327                /**
328                 * Removes the specified listener.
329                 */
330                void RemoveAudioDeviceCountListener(AudioDeviceCountListener* l);
331    
332                
333                /**
334                 * Registers the specified listener to be notified
335                 * when the number of MIDI input devices is changed.
336                 */
337                void AddMidiDeviceCountListener(MidiDeviceCountListener* l);
338    
339                /**
340                 * Removes the specified listener.
341                 */
342                void RemoveMidiDeviceCountListener(MidiDeviceCountListener* l);
343    
344                /**
345                 * Registers the specified listener to be notified when the number
346                 * of active voices in a particular sampler channel is changed.
347                 */
348                void AddVoiceCountListener(VoiceCountListener* l);
349    
350                /**
351                 * Removes the specified listener.
352                 */
353                void RemoveVoiceCountListener(VoiceCountListener* l);
354    
355                /**
356                 * Notifies listeners that the number of active voices
357                 * on the specified sampler channel is changed.
358                 * @param ChannelId The numerical ID of the sampler channel.
359                 * @param NewCount The new number of active voices.
360                 */
361                void fireVoiceCountChanged(int ChannelId, int NewCount);
362    
363                /**
364                 * Registers the specified listener to be notified when the number
365                 * of active disk streams in a particular sampler channel is changed.
366                 */
367                void AddStreamCountListener(StreamCountListener* l);
368    
369                /**
370                 * Removes the specified listener.
371                 */
372                void RemoveStreamCountListener(StreamCountListener* l);
373    
374                /**
375                 * Notifies listeners that the number of active disk streams
376                 * on the specified sampler channel is changed.
377                 * @param ChannelId The numerical ID of the sampler channel.
378                 * @param NewCount The new number of active disk streams.
379                 */
380                void fireStreamCountChanged(int ChannelId, int NewCount);
381    
382                /**
383                 * Registers the specified listener to be
384                 * notified when the fill state of the disk stream
385                 * buffers on a specific sampler channel is changed.
386                 */
387                void AddBufferFillListener(BufferFillListener* l);
388    
389                /**
390                 * Removes the specified listener.
391                 */
392                void RemoveBufferFillListener(BufferFillListener* l);
393    
394                /**
395                 * Notifies listeners that the fill state of the disk stream
396                 * buffers on the specified sampler channel is changed.
397                 * @param ChannelId The numerical ID of the sampler channel.
398                 * @param FillData The buffer fill data for the specified sampler channel.
399                 */
400                void fireBufferFillChanged(int ChannelId, String FillData);
401    
402                /**
403                 * Registers the specified listener to be notified
404                 * when total number of active voices is changed.
405                 */
406                void AddTotalVoiceCountListener(TotalVoiceCountListener* l);
407    
408                /**
409                 * Removes the specified listener.
410                 */
411                void RemoveTotalVoiceCountListener(TotalVoiceCountListener* l);
412    
413                /**
414                 * Notifies listeners that the total number of active voices is changed.
415                 * @param NewCount The new number of active voices.
416                 */
417                void fireTotalVoiceCountChanged(int NewCount);
418    
419                /**
420                 * Registers the specified listener to be notified when the number
421                 * of effect sends on a particular sampler channel is changed.
422                 */
423                void AddFxSendCountListener(FxSendCountListener* l);
424    
425                /**
426                 * Removes the specified listener.
427                 */
428                void RemoveFxSendCountListener(FxSendCountListener* l);
429    
430                /**
431                 * Returns the names of all available audio output drivers.
432                 */
433                std::vector<String> AvailableAudioOutputDrivers();
434    
435                /**
436                 * Returns the names of all available MIDI input drivers.
437                 */
438                std::vector<String> AvailableMidiInputDrivers();
439    
440                /**
441                 * Returns the names of all available sampler engine types.
442                 * @see SamplerChannel::SetEngineType()
443                 */
444                std::vector<String> AvailableEngineTypes();
445    
446                /**
447                 * Create an audio output device.
448                 *
449                 * @param AudioDriver - name of the audio driver
450                 * @param Parameters - eventually needed driver parameters to
451                 *                     create the device
452                 * @returns  pointer to created audio output device
453                 * @throws Exception  if device could not be created
454                 */
455                AudioOutputDevice* CreateAudioOutputDevice(String AudioDriver, std::map<String,String> Parameters) throw (Exception);
456    
457                /**
458                 * Create a midi input device.
459                 *
460                 * @param MidiDriver - name of the midi driver
461                 * @param Parameters - eventually needed driver parameters to
462                 *                     create the device
463                 * @returns  pointer to created midi input device
464                 * @throws Exception  if device could not be created
465                 */
466                MidiInputDevice* CreateMidiInputDevice(String MidiDriver, std::map<String,String> Parameters) throw (Exception);
467    
468                /**
469                 * Returns the number of all created audio output devices.
470                 */
471                uint AudioOutputDevices();
472    
473                /**
474                 * Returns the number of all created MIDI input devices.
475                 */
476                uint MidiInputDevices();
477    
478                /**
479                 * Returns all created audio output devices.
480                 */
481                std::map<uint, AudioOutputDevice*> GetAudioOutputDevices();
482    
483                /**
484                 * Returns all created MIDI input devices.
485                 */
486                std::map<uint, MidiInputDevice*> GetMidiInputDevices();
487    
488                /**
489                 * Destroy the given audio output device and takes care if there
490                 * are still sampler angines connected to this device, etc.
491                 *
492                 * @throws Exception  if sampler channels are still
493                 *                    connected to the device
494                 */
495                void DestroyAudioOutputDevice(AudioOutputDevice* pDevice) throw (Exception);
496    
497                /**
498                 * Destroy the given MIDI input device and takes care if there
499                 * are still sampler angines connected to this device, etc.
500                 *
501                 * @throws Exception  if sampler channels are still
502                 *                    connected to the device
503                 */
504                void DestroyMidiInputDevice(MidiInputDevice* pDevice) throw (Exception);
505    
506                /**
507                 * Gets the current number of all active voices.
508                 * @returns The current number of all active voices.
509                 */
510                int GetVoiceCount();
511    
512                /**
513                 * Reset the whole sampler. Destroy all engines, sampler
514                 * channels, MIDI input devices and audio output devices.
515                 */
516                void Reset();
517    
518            protected:
519                /**
520                 * Notifies listeners that the number of sampler channels has been changed.
521                 * @param NewCount The new number of sampler channels.
522                 */
523                void fireChannelCountChanged(int NewCount);
524    
525                /**
526                 * Notifies listeners that the number of audio output devices has been changed.
527                 * @param NewCount The new number of audio output devices.
528                 */
529                void fireAudioDeviceCountChanged(int NewCount);
530    
531                /**
532                 * Notifies listeners that the number of MIDI input devices has been changed.
533                 * @param NewCount The new number of MIDI input devices.
534                 */
535                void fireMidiDeviceCountChanged(int NewCount);
536    
537                /**
538                 * Notifies listeners that the number of effect sends
539                 * on a particular sampler channel is changed.
540                 * @param ChannelId The numerical ID of the sampler channel.
541                 * @param NewCount The new number of sampler channels.
542                 */
543                void fireFxSendCountChanged(int ChannelId, int NewCount);
544    
545                typedef std::map<uint, AudioOutputDevice*> AudioOutputDeviceMap;
546                typedef std::map<uint, MidiInputDevice*> MidiInputDeviceMap;
547                typedef std::map<uint, SamplerChannel*> SamplerChannelMap;
548    
549                SamplerChannelMap     mSamplerChannels;    ///< contains all created sampler channels
550                AudioOutputDeviceMap  mAudioOutputDevices; ///< contains all created audio output devices
551                MidiInputDeviceMap    mMidiInputDevices;   ///< contains all created MIDI input devices
552    
553              friend class SamplerChannel;              friend class SamplerChannel;
554    
555            private:
556                ListenerList<ChannelCountListener*> llChannelCountListeners;
557                ListenerList<AudioDeviceCountListener*> llAudioDeviceCountListeners;
558                ListenerList<MidiDeviceCountListener*> llMidiDeviceCountListeners;
559                ListenerList<VoiceCountListener*> llVoiceCountListeners;
560                ListenerList<StreamCountListener*> llStreamCountListeners;
561                ListenerList<BufferFillListener*> llBufferFillListeners;
562                ListenerList<TotalVoiceCountListener*> llTotalVoiceCountListeners;
563                ListenerList<FxSendCountListener*> llFxSendCountListeners;
564    
565                class EventHandler : public EngineChangeListener, public FxSendCountListener {
566                    public:
567                        void SetSampler(Sampler* pSampler) { this->pSampler = pSampler; }
568    
569                        /**
570                         * Invoked when the engine type of the
571                         * specified sampler channel is changed.
572                         * @param ChannelId The numerical ID of the sampler
573                         * channel, which engine type has been changed.
574                         */
575                        virtual void EngineChanged(int ChannelId);
576    
577                        /**
578                         * Invoked when the number of effect sends
579                         * on the specified sampler channel has changed.
580                         * @param ChannelId The numerical ID of the sampler channel.
581                         * @param NewCount The new number of effect sends.
582                         */
583                        virtual void FxSendCountChanged(int ChannelId, int NewCount);
584    
585                    private:
586                        Sampler* pSampler;
587                } eventHandler;
588      };      };
589  }  }
590    

Legend:
Removed from v.53  
changed lines
  Added in v.1130

  ViewVC Help
Powered by ViewVC