-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvoice.c
267 lines (230 loc) · 6.48 KB
/
voice.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* voice.c
*
* Created on: 8 Apr 2013
* Author: ntuckett
*/
#include "voice.h"
#include <stddef.h>
#include "midi.h"
#include "logging.h"
#define MAX_VOICE_CALLBACKS 4
typedef struct voice_callback_info_t voice_callback_info_t;
typedef struct voice_callback_info_t
{
voice_callback_t callback;
void* data;
voice_callback_info_t* next_callback;
} voice_callback_info_t;
static voice_callback_info_t* voice_callback_head = NULL;
static voice_callback_info_t* voice_callback_free = NULL;
static voice_callback_info_t voice_callback_info[MAX_VOICE_CALLBACKS];
static void init_voice(voice_t *voice)
{
voice->midi_channel = 0;
voice->last_state = NOTE_NOT_PLAYING;
voice->current_state = NOTE_NOT_PLAYING;
voice->play_counter = 0;
osc_init(&voice->oscillator);
filter_init(&voice->filter);
voice->filter_def = voice->filter.definition;
}
void voices_initialise(voice_t *voices, int voice_count)
{
for (int i = 0; i < voice_count; i++)
{
init_voice(voices + i);
voices[i].index = i;
}
for (int i = 0; i < MAX_VOICE_CALLBACKS; i++)
{
voice_callback_info[i].next_callback = voice_callback_free;
voice_callback_free = voice_callback_info + i;
}
}
void voices_add_callback(voice_callback_t callback, void* callback_data)
{
if (voice_callback_free != NULL)
{
voice_callback_info_t* callback_info = voice_callback_free;
voice_callback_free = voice_callback_free->next_callback;
callback_info->callback = callback;
callback_info->data = callback_data;
callback_info->next_callback = voice_callback_head;
voice_callback_head = callback_info;
}
else
{
LOG_ERROR("No voice callbacks available");
}
}
void voices_remove_callback(voice_callback_t callback)
{
voice_callback_info_t* callback_info = voice_callback_head;
voice_callback_info_t** last_link = &voice_callback_head;
while (callback_info != NULL)
{
if (callback_info->callback == callback)
{
*last_link = callback_info->next_callback;
callback_info->callback = NULL;
callback_info->data = NULL;
callback_info->next_callback = voice_callback_free;
voice_callback_free = callback_info;
break;
}
else
{
last_link = &callback_info->next_callback;
callback_info = callback_info->next_callback;
}
}
}
void voice_make_callback(voice_event_t event, voice_t* voice)
{
voice_callback_info_t* callback_info = voice_callback_head;
while (callback_info != NULL)
{
callback_info->callback(event, voice, callback_info->data);
callback_info = callback_info->next_callback;
}
}
void voice_preupdate(voice_t *voice, int32_t timestep_ms, filter_definition_t *filter_def)
{
if (voice->current_state != voice->last_state)
{
if (voice->current_state == NOTE_NOT_PLAYING)
{
voice->oscillator.level = 0;
}
else if (voice->current_state >= 0)
{
voice->oscillator.phase_accumulator = 0;
voice_make_callback(VOICE_EVENT_NOTE_STARTING, voice);
filter_silence(&voice->filter);
}
if (voice->last_state == NOTE_NOT_PLAYING)
{
voice->oscillator.last_level = -1;
}
voice->last_state = voice->current_state;
}
voice->filter_def = *filter_def;
}
int voice_update(voice_t *voice, int32_t master_level, sample_t *voice_buffer, int buffer_samples, int32_t timestep_ms)
{
int voice_state = VOICE_IDLE;
if (voice->current_state != NOTE_NOT_PLAYING)
{
voice->oscillator.level = (voice->oscillator.level * master_level) / LEVEL_MAX;
// If this is a new note from scratch, avoid interpolating the initial level across the chunk.
if (voice->oscillator.last_level < 0)
{
voice->oscillator.last_level = voice->oscillator.level;
}
if (!filter_definitions_same(&voice->filter_def, &voice->filter.definition))
{
voice->filter.definition = voice->filter_def;
filter_update(&voice->filter);
}
if (voice->oscillator.level > 0 || voice->oscillator.last_level != 0)
{
osc_output(&voice->oscillator, voice_buffer, buffer_samples);
filter_apply(&voice->filter, voice_buffer, buffer_samples);
voice->oscillator.last_level = voice->oscillator.level;
voice_state = VOICE_ACTIVE;
}
else if (voice->current_state == NOTE_ENDING)
{
voice_kill(voice);
voice_state = VOICE_GONE_IDLE;
}
}
return voice_state;
}
void voice_play_note(voice_t *voice, int midi_note, waveform_type_t waveform)
{
int initial_state = voice->current_state;
voice->note = midi_note;
voice->current_state = midi_note;
voice->oscillator.waveform = waveform;
voice->frequency = midi_get_note_frequency(midi_note);
if (initial_state == NOTE_NOT_PLAYING)
{
voice_make_callback(VOICE_EVENT_VOICE_STARTING, voice);
}
}
void voice_stop_note(voice_t *voice)
{
if (PLAYING_NOTE(voice->current_state))
{
voice->current_state = NOTE_ENDING;
voice_make_callback(VOICE_EVENT_NOTE_ENDING, voice);
}
}
void voice_kill(voice_t * voice)
{
if (voice->current_state != NOTE_NOT_PLAYING)
{
voice->current_state = NOTE_NOT_PLAYING;
voice_make_callback(VOICE_EVENT_VOICE_ENDED, voice);
}
}
voice_t *voice_find_next_likely_free(voice_t *voices, int voice_count, int midi_channel)
{
int candidate_voice = -1;
int lowest_play_counter = INT_MAX;
for (int i = 0; i < voice_count; i++)
{
if (voices[i].midi_channel == midi_channel)
{
if (voices[i].current_state == NOTE_NOT_PLAYING)
{
candidate_voice = i;
break;
}
else
{
if (voices[i].play_counter < lowest_play_counter)
{
lowest_play_counter = voices[i].play_counter;
candidate_voice = i;
}
}
}
}
if (candidate_voice >= 0)
{
return voices + candidate_voice;
}
else
{
return NULL;
}
}
voice_t *voice_find_playing_note(voice_t *voices, int voice_count, int midi_channel, int midi_note)
{
for (int i = 0; i < voice_count; i++)
{
if (voices[i].current_state == midi_note && voices[i].midi_channel == midi_channel)
{
return voices + i;
}
}
return NULL;
}