1 |
schoenebeck |
9 |
/*************************************************************************** |
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 |
|
|
#ifndef __AUDIOTHREAD_H__ |
24 |
|
|
#define __AUDIOTHREAD_H__ |
25 |
|
|
|
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <stdlib.h> |
28 |
|
|
#include <math.h> |
29 |
|
|
#include <unistd.h> |
30 |
|
|
#include <fcntl.h> |
31 |
|
|
|
32 |
|
|
#include "global.h" |
33 |
|
|
#include "thread.h" |
34 |
|
|
#include "ringbuffer.h" |
35 |
|
|
#include "voice.h" |
36 |
|
|
#include "audioio.h" |
37 |
|
|
#include "gig.h" |
38 |
|
|
|
39 |
senoner |
10 |
#include "rtelmemorypool.h" |
40 |
|
|
|
41 |
schoenebeck |
9 |
#define PITCHBEND_SEMITONES 12 |
42 |
|
|
#define MAX_AUDIO_VOICES 64 |
43 |
|
|
|
44 |
|
|
// preload 64k samples = 128kB of data in RAM for 16 bit mono samples |
45 |
senoner |
10 |
#define NUM_RAM_PRELOAD_SAMPLES 32768 |
46 |
schoenebeck |
9 |
|
47 |
|
|
class AudioThread : public Thread { |
48 |
|
|
public: |
49 |
schoenebeck |
12 |
int ActiveVoiceCount; ///< number of currently active voices |
50 |
schoenebeck |
13 |
int ActiveVoiceCountMax; |
51 |
schoenebeck |
12 |
|
52 |
schoenebeck |
9 |
AudioThread(AudioIO* pAudioIO, DiskThread* pDiskThread, gig::Instrument* pInstrument); |
53 |
|
|
~AudioThread(); |
54 |
|
|
void ProcessNoteOn(uint8_t Pitch, uint8_t Velocity); |
55 |
|
|
void ProcessNoteOff(uint8_t Pitch, uint8_t Velocity); |
56 |
senoner |
10 |
void ProcessContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value); |
57 |
schoenebeck |
9 |
protected: |
58 |
|
|
int Main(); ///< Implementation of virtual method from class Thread |
59 |
|
|
private: |
60 |
|
|
enum command_type_t { |
61 |
|
|
command_type_note_on, |
62 |
senoner |
10 |
command_type_note_off, |
63 |
|
|
command_type_continuous_controller |
64 |
schoenebeck |
9 |
}; |
65 |
|
|
struct command_t { |
66 |
|
|
command_type_t type; |
67 |
senoner |
10 |
uint8_t channel; |
68 |
schoenebeck |
9 |
uint8_t pitch; |
69 |
|
|
uint8_t velocity; |
70 |
senoner |
10 |
uint8_t number; |
71 |
|
|
uint8_t value; |
72 |
schoenebeck |
9 |
} command; |
73 |
schoenebeck |
12 |
struct sustained_key_t { |
74 |
|
|
int midikey; |
75 |
|
|
int velocity; |
76 |
|
|
}; |
77 |
senoner |
10 |
|
78 |
schoenebeck |
12 |
RingBuffer<command_t>* pCommandQueue; |
79 |
|
|
float* pAudioSumBuffer; ///< Audio sum of all voices (32 bit) |
80 |
|
|
Voice** pVoices; ///< The voice pool, containing all Voices (active and inactice voices) in unsorted order |
81 |
|
|
RTEList<Voice *>* pActiveVoices[128]; ///< Contains all active voices sorted by MIDI key number |
82 |
senoner |
10 |
/* ActiveVoicePool is a memory pool of limited size (size=MAX VOICES) of active voices. |
83 |
|
|
it can be allocated dynamically in real time and the allocated elements can be added to |
84 |
|
|
the linked lists represented by ActiveVoices[MIDIKey]. This means we can have unlimited |
85 |
|
|
active voices per key. This if for example useful to manage the sustain pedal messages |
86 |
|
|
*/ |
87 |
schoenebeck |
12 |
RTELMemoryPool<Voice *>* ActiveVoicePool; |
88 |
senoner |
10 |
/* SustainedVoicePool is a dynamically allocated pool (size=MAX VOICES) and list of notes |
89 |
|
|
notes that were sustained and where the corresponding MIDI note-off arrived |
90 |
|
|
but cannot processed yet. Basically when the sustain pedal is pressed and the |
91 |
|
|
note-off on a certain midi key arrives. notes are not deleted from the |
92 |
|
|
ActiveVoices[MIDIKey] list but an element is added in the SustainedVoicePool, |
93 |
|
|
which is a dynamically allocated pool with a builtin list. |
94 |
|
|
Then the pedal is finally released, this list is traversed and all elements |
95 |
|
|
in the lists ActiveVoices[MIDIKey] ( where MIDIKey is contained in the list of |
96 |
|
|
sustained voices) are processed (voices are released) |
97 |
|
|
*/ |
98 |
schoenebeck |
12 |
RTELMemoryPool<sustained_key_t>* SustainedKeyPool; |
99 |
|
|
AudioIO* pAudioIO; |
100 |
|
|
DiskThread* pDiskThread; |
101 |
|
|
gig::Instrument* pInstrument; |
102 |
|
|
bool SustainPedal; ///< true if sustain pedal is down |
103 |
|
|
uint8_t PrevHoldCCValue; |
104 |
senoner |
10 |
|
105 |
schoenebeck |
9 |
void ActivateVoice(uint8_t MIDIKey, uint8_t Velocity); |
106 |
|
|
void ReleaseVoice(uint8_t MIDIKey, uint8_t Velocity); |
107 |
schoenebeck |
12 |
void ReleaseVoice(Voice* pVoice); |
108 |
senoner |
10 |
void ContinuousController(uint8_t Channel, uint8_t Number, uint8_t Value); |
109 |
schoenebeck |
9 |
void CacheInitialSamples(gig::Sample* pSample); |
110 |
|
|
}; |
111 |
|
|
|
112 |
|
|
#endif // __AUDIOTHREAD_H__ |