/[svn]/linuxsampler/tags/v0_1_0/src/audiothread.h
ViewVC logotype

Diff of /linuxsampler/tags/v0_1_0/src/audiothread.h

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

revision 10 by senoner, Tue Nov 11 23:30:47 2003 UTC revision 37 by schoenebeck, Wed Mar 10 22:01:36 2004 UTC
# Line 23  Line 23 
23  #ifndef __AUDIOTHREAD_H__  #ifndef __AUDIOTHREAD_H__
24  #define __AUDIOTHREAD_H__  #define __AUDIOTHREAD_H__
25    
 #include <stdio.h>  
 #include <stdlib.h>  
26  #include <math.h>  #include <math.h>
27  #include <unistd.h>  #include <unistd.h>
28  #include <fcntl.h>  #include <fcntl.h>
29    #include <pthread.h>
30    #include <sstream>
31    
32  #include "global.h"  #include "global.h"
33  #include "thread.h"  #include "thread.h"
34  #include "ringbuffer.h"  #include "ringbuffer.h"
 #include "voice.h"  
35  #include "audioio.h"  #include "audioio.h"
36    #include "voice.h"
37  #include "gig.h"  #include "gig.h"
   
38  #include "rtelmemorypool.h"  #include "rtelmemorypool.h"
39    #include "modulationsystem.h"
40    #include "network/lscp.h"
41    
42  #define DEBUG                   0  #define PITCHBEND_SEMITONES             12
43  #define PITCHBEND_SEMITONES     12  #define MAX_AUDIO_VOICES                64
 #define MAX_AUDIO_VOICES        64  
44    
45  // preload 64k samples = 128kB of data in RAM for 16 bit mono samples  // preload 64k samples = 128kB of data in RAM for 16 bit mono samples
46  #define NUM_RAM_PRELOAD_SAMPLES 32768  #define NUM_RAM_PRELOAD_SAMPLES 32768
47    
48  class AudioThread : public Thread {  // just symbol prototyping
49    class Voice;
50    
51    //FIXME: Class name "AudioThread" is now misleading, because there is no thread anymore, but the name will change soon to "Engine" when we restructure the source tree
52    class AudioThread {
53      public:      public:
54          AudioThread(AudioIO* pAudioIO, DiskThread* pDiskThread, gig::Instrument* pInstrument);          double       Volume;               ///< overall volume (a value < 1.0 means attenuation, a value > 1.0 means amplification)
55         ~AudioThread();          int          ActiveVoiceCount;     ///< number of currently active voices
56          void ProcessNoteOn(uint8_t Pitch, uint8_t Velocity);          int          ActiveVoiceCountMax;  ///< the maximum voice usage since application start
57          void ProcessNoteOff(uint8_t Pitch, uint8_t Velocity);          DiskThread*  pDiskThread;
         void ProcessContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value);  
   
         // the number of currently active streams  
         // printed on the console the main thread (along with the active streams count)  
         int ActiveVoiceCount;  
58    
59            AudioThread(AudioIO* pAudioIO);
60           ~AudioThread();
61            result_t      LoadInstrument(const char* FileName, uint Instrument);
62            void          Reset();
63            void          SendNoteOn(uint8_t Key, uint8_t Velocity);
64            void          SendNoteOff(uint8_t Key, uint8_t Velocity);
65            void          SendPitchbend(int Pitch);
66            void          SendControlChange(uint8_t Controller, uint8_t Value);
67            int           RenderAudio(uint Samples);
68            inline float* GetAudioSumBuffer(uint Channel) {
69                return pAudioSumBuffer[Channel];
70            };
71      protected:      protected:
72          int Main(); ///< Implementation of virtual method from class Thread          struct midi_key_info_t {
73      private:              RTEList<Voice>*                      pActiveVoices;         ///< Contains the active voices associated with the MIDI key.
74          enum command_type_t {              bool                                 KeyPressed;            ///< Is true if the respective MIDI key is currently pressed.
75              command_type_note_on,              bool                                 Active;                ///< If the key contains active voices.
76              command_type_note_off,              uint*                                pSelf;                 ///< hack to allow fast deallocation of the key from the list of active keys
77              command_type_continuous_controller              RTEList<ModulationSystem::Event>*    pEvents;               ///< Key specific events (only Note-on, Note-off and sustain pedal currently)
78          };          };
79          struct command_t {          
80              command_type_t type;          uint8_t                                  ControllerTable[128];  ///< Reflects the current values (0-127) of all MIDI controllers for this engine / sampler channel.
81              uint8_t        channel;          RingBuffer<ModulationSystem::Event>*     pEventQueue;           ///< Input event queue.
82              uint8_t        pitch;          float*                                   pAudioSumBuffer[2];    ///< Audio sum of all voices (32 bit, index 0 = left channel, index 1 = right channel)
83              uint8_t        velocity;          midi_key_info_t                          pMIDIKeyInfo[128];     ///< Contains all active voices sorted by MIDI key number and other informations to the respective MIDI key
84              uint8_t        number;          RTELMemoryPool<Voice>*                   pVoicePool;            ///< Contains all voices that can be activated.
85              uint8_t        value;          RTELMemoryPool<uint>*                    pActiveKeys;           ///< Holds all keys in it's allocation list with active voices.
86          } command;          RTELMemoryPool<ModulationSystem::Event>* pEventPool;            ///< Contains all Event objects that can be used.
87          RingBuffer<command_t>* pCommandQueue;          RTEList<ModulationSystem::Event>*        pEvents;               ///< All events for the current audio fragment.
88          float*                 pAudioSumBuffer;    ///< Audio sum of all voices (32 bit)          RTEList<ModulationSystem::Event>*        pCCEvents;             ///< All control change events for the current audio fragment.
89          Voice**                pVoices;            ///< The voice pool, containing all Voices (active and inactice voices) in unsorted order          RTEList<ModulationSystem::Event>*        pSynthesisEvents[ModulationSystem::destination_count];  ///< Events directly affecting synthesis parameter (like pitch, volume and filter).
90            AudioIO*                                 pAudioIO;
91          RTEList<Voice *> *pActiveVoices[128];  ///< Contains all active voices sorted by MIDI key number          RIFF::File*                              pRIFF;
92          /* ActiveVoicePool is a memory pool of limited size (size=MAX VOICES) of active voices.          gig::File*                               pGig;
93             it can be allocated dynamically in real time and the allocated elements can be added to          gig::Instrument*                         pInstrument;
94             the linked lists represented by ActiveVoices[MIDIKey]. This means we can have unlimited          bool                                     SustainPedal;          ///< true if sustain pedal is down
95             active voices per key. This if for example useful to manage the sustain pedal messages          int                                      Pitch;                 ///< Current (absolute) MIDI pitch value.
96           */          bool                                     SuspensionRequested;
97          RTELMemoryPool<Voice *> *ActiveVoicePool;          pthread_mutex_t                          __render_state_mutex;
98          /* SustainedVoicePool is a dynamically allocated pool (size=MAX VOICES) and list of notes          pthread_cond_t                           __render_exit_condition;
99             notes that were sustained and where the corresponding MIDI note-off arrived  
100             but cannot processed yet. Basically when the sustain pedal is pressed and the          void ProcessNoteOn(ModulationSystem::Event* pNoteOnEvent);
101             note-off on a certain midi key arrives. notes are not deleted from the          void ProcessNoteOff(ModulationSystem::Event* pNoteOffEvent);
102             ActiveVoices[MIDIKey] list but an element is added in the SustainedVoicePool,          void ProcessPitchbend(ModulationSystem::Event* pPitchbendEvent);
103             which is a dynamically allocated pool with a builtin list.          void ProcessControlChange(ModulationSystem::Event* pControlChangeEvent);
104             Then the pedal is finally released, this list is traversed and all elements          void KillVoice(Voice* pVoice);
            in the lists ActiveVoices[MIDIKey] ( where MIDIKey is contained in the list of  
            sustained voices) are processed (voices are released)  
         */  
   
         typedef struct {  
                          int midikey;  
                          int velocity;  
                        } sustained_key_t;  
   
         RTELMemoryPool<sustained_key_t> *SustainedKeyPool;  
   
         uint8_t PrevHoldCCValue;  
         // SustainPedal = 1 if the sustain pedal is down, otherwise it is 0  
         uint8_t SustainPedal;  
   
   
   
         AudioIO*               pAudioIO;  
         DiskThread*            pDiskThread;  
         gig::Instrument*       pInstrument;  
   
         void ActivateVoice(uint8_t MIDIKey, uint8_t Velocity);  
         void ReleaseVoice(uint8_t MIDIKey, uint8_t Velocity);  
         void ContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value);  
         
105          void CacheInitialSamples(gig::Sample* pSample);          void CacheInitialSamples(gig::Sample* pSample);
106            void ResetInternal();
107    
108            friend class Voice;
109  };  };
110    
111  #endif // __AUDIOTHREAD_H__  #endif // __AUDIOTHREAD_H__

Legend:
Removed from v.10  
changed lines
  Added in v.37

  ViewVC Help
Powered by ViewVC