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