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> |
30 |
#include "global.h" |
#include "global.h" |
31 |
#include "thread.h" |
#include "thread.h" |
32 |
#include "ringbuffer.h" |
#include "ringbuffer.h" |
|
#include "voice.h" |
|
33 |
#include "audioio.h" |
#include "audioio.h" |
34 |
|
#include "voice.h" |
35 |
#include "gig.h" |
#include "gig.h" |
36 |
#include "rtelmemorypool.h" |
#include "rtelmemorypool.h" |
37 |
#include "modulationsystem.h" |
#include "modulationsystem.h" |
42 |
// 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 |
43 |
#define NUM_RAM_PRELOAD_SAMPLES 32768 |
#define NUM_RAM_PRELOAD_SAMPLES 32768 |
44 |
|
|
45 |
class AudioThread : public Thread { |
//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 |
46 |
|
class AudioThread { |
47 |
public: |
public: |
48 |
double Volume; ///< overall volume (a value < 1.0 means attenuation, a value > 1.0 means amplification) |
double Volume; ///< overall volume (a value < 1.0 means attenuation, a value > 1.0 means amplification) |
49 |
int ActiveVoiceCount; ///< number of currently active voices |
int ActiveVoiceCount; ///< number of currently active voices |
51 |
|
|
52 |
AudioThread(AudioIO* pAudioIO, DiskThread* pDiskThread, gig::Instrument* pInstrument); |
AudioThread(AudioIO* pAudioIO, DiskThread* pDiskThread, gig::Instrument* pInstrument); |
53 |
~AudioThread(); |
~AudioThread(); |
54 |
void SendNoteOn(uint8_t Pitch, uint8_t Velocity); |
void SendNoteOn(uint8_t Pitch, uint8_t Velocity); |
55 |
void SendNoteOff(uint8_t Pitch, uint8_t Velocity); |
void SendNoteOff(uint8_t Pitch, uint8_t Velocity); |
56 |
void SendControlChange(uint8_t Channel, uint8_t Number, uint8_t Value); |
void SendControlChange(uint8_t Channel, uint8_t Number, uint8_t Value); |
57 |
protected: |
int RenderAudio(uint Samples); |
58 |
int Main(); ///< Implementation of virtual method from class Thread |
inline float* GetAudioSumBuffer(uint Channel) { |
59 |
|
return pAudioSumBuffer[Channel]; |
60 |
|
}; |
61 |
private: |
private: |
62 |
enum command_type_t { |
enum command_type_t { |
63 |
command_type_note_on, |
command_type_note_on, |
73 |
uint8_t value; |
uint8_t value; |
74 |
} command; |
} command; |
75 |
struct midi_key_info_t { |
struct midi_key_info_t { |
76 |
RTEList<Voice*>* pActiveVoices; ///< Contains the active voices associated with the MIDI key. |
RTEList<Voice*>* pActiveVoices; ///< Contains the active voices associated with the MIDI key. |
77 |
RTEList<Voice*>::NodeHandle hSustainPtr; ///< Points to the voice element in the active voice list which has not received a note-off yet (this pointer is needed for sustain pedal handling) |
RTEList<Voice*>::NodeHandle hSustainPtr; ///< Points to the voice element in the active voice list which has not received a note-off yet (this pointer is needed for sustain pedal handling) |
78 |
bool Sustained; ///< Is true if the MIDI key is currently sustained, thus if Note-off arrived while sustain pedal pressed. |
bool Sustained; ///< Is true if the MIDI key is currently sustained, thus if Note-off arrived while sustain pedal pressed. |
79 |
bool KeyPressed; ///< Is true if the respective MIDI key is currently pressed. |
bool KeyPressed; ///< Is true if the respective MIDI key is currently pressed. |
80 |
uint* pSustainPoolNode; ///< FIXME: hack to allow fast deallocation of the key from the sustained key pool |
uint* pSustainPoolNode; ///< FIXME: hack to allow fast deallocation of the key from the sustained key pool |
81 |
}; |
}; |
82 |
|
|
83 |
RingBuffer<command_t>* pCommandQueue; |
RingBuffer<command_t>* pCommandQueue; |
84 |
float* pAudioSumBuffer; ///< Audio sum of all voices (32 bit) |
float* pAudioSumBuffer[2]; ///< Audio sum of all voices (32 bit, index 0 = left channel, index 1 = right channel) |
85 |
Voice** pVoices; ///< The voice pool, containing all Voices (active and inactice voices) in unsorted order |
Voice** pVoices; ///< The voice pool, containing all Voices (active and inactice voices) in unsorted order |
86 |
midi_key_info_t pMIDIKeyInfo[128]; ///< Contains all active voices sorted by MIDI key number and other informations to the respective MIDI key |
midi_key_info_t pMIDIKeyInfo[128]; ///< Contains all active voices sorted by MIDI key number and other informations to the respective MIDI key |
87 |
/* ActiveVoicePool is a memory pool of limited size (size=MAX VOICES) of active voices. |
/* ActiveVoicePool is a memory pool of limited size (size=MAX VOICES) of active voices. |
88 |
it can be allocated dynamically in real time and the allocated elements can be added to |
it can be allocated dynamically in real time and the allocated elements can be added to |
89 |
the linked lists represented by ActiveVoices[MIDIKey]. This means we can have unlimited |
the linked lists represented by ActiveVoices[MIDIKey]. This means we can have unlimited |
90 |
active voices per key. This if for example useful to manage the sustain pedal messages |
active voices per key. This if for example useful to manage the sustain pedal messages |
91 |
*/ |
*/ |
92 |
RTELMemoryPool<Voice*>* ActiveVoicePool; |
RTELMemoryPool<Voice*>* ActiveVoicePool; |
93 |
RTELMemoryPool<uint>* SustainedKeyPool; ///< Contains the MIDI key numbers of all currently sustained keys. |
RTELMemoryPool<uint>* SustainedKeyPool; ///< Contains the MIDI key numbers of all currently sustained keys. |
94 |
AudioIO* pAudioIO; |
AudioIO* pAudioIO; |
95 |
DiskThread* pDiskThread; |
DiskThread* pDiskThread; |
96 |
gig::Instrument* pInstrument; |
gig::Instrument* pInstrument; |
97 |
bool SustainPedal; ///< true if sustain pedal is down |
bool SustainPedal; ///< true if sustain pedal is down |
98 |
uint8_t PrevHoldCCValue; |
uint8_t PrevHoldCCValue; |
99 |
|
|
100 |
void ProcessNoteOn(uint8_t MIDIKey, uint8_t Velocity); |
void ProcessNoteOn(uint8_t MIDIKey, uint8_t Velocity); |