-
Notifications
You must be signed in to change notification settings - Fork 2
/
envelope.c
202 lines (176 loc) · 5.48 KB
/
envelope.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
// 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/>.
/*
* envelope.c
*
* Created on: 24 Nov 2012
* Author: ntuckett
*/
#include "envelope.h"
#include <stdlib.h>
#include "logging.h"
#include "fixed_point_math.h"
#define ENV_FIXED_PRECISION 12
#define MAX_ENVELOPE_CALLBACKS 4
typedef struct envelope_callback_info_t envelope_callback_info_t;
typedef struct envelope_callback_info_t
{
envelope_callback_t callback;
void* data;
envelope_callback_info_t* next_callback;
} envelope_callback_info_t;
static envelope_callback_info_t* envelope_callback_head = NULL;
static envelope_callback_info_t* envelope_callback_free = NULL;
static envelope_callback_info_t envelope_callback_info[MAX_ENVELOPE_CALLBACKS];
static int32_t calculate_level(envelope_instance_t *instance, envelope_stage_t* stage, int32_t elapsed_time_ms)
{
int32_t start_level;
if (stage->start_level == LEVEL_CURRENT)
{
start_level = instance->ref_level;
}
else
{
start_level = stage->start_level;
}
if (stage->duration > 0) {
fixed_t level_scale = fixed_divide_at(elapsed_time_ms, stage->duration, ENV_FIXED_PRECISION);
int32_t stage_delta = fixed_mul_at(stage->end_level - start_level, level_scale, ENV_FIXED_PRECISION);
return start_level + stage_delta;
}
else
{
return stage->end_level;
}
}
void envelopes_initialise()
{
for (int i = 0; i < MAX_ENVELOPE_CALLBACKS; i++)
{
envelope_callback_info[i].next_callback = envelope_callback_free;
envelope_callback_free = envelope_callback_info + i;
}
}
void envelopes_add_callback(envelope_callback_t callback, void* callback_data)
{
if (envelope_callback_free != NULL)
{
envelope_callback_info_t* callback_info = envelope_callback_free;
envelope_callback_free = envelope_callback_free->next_callback;
callback_info->callback = callback;
callback_info->data = callback_data;
callback_info->next_callback = envelope_callback_head;
envelope_callback_head = callback_info;
}
else
{
LOG_ERROR("No voice callbacks available");
}
}
void envelopes_remove_callback(envelope_callback_t callback)
{
envelope_callback_info_t* callback_info = envelope_callback_head;
envelope_callback_info_t** last_link = &envelope_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 = envelope_callback_free;
envelope_callback_free = callback_info;
break;
}
else
{
last_link = &callback_info->next_callback;
callback_info = callback_info->next_callback;
}
}
}
void envelope_make_callback(envelope_event_t event, envelope_instance_t* envelope)
{
envelope_callback_info_t* callback_info = envelope_callback_head;
while (callback_info != NULL)
{
callback_info->callback(event, envelope, callback_info->data);
callback_info = callback_info->next_callback;
}
}
void envelope_init(envelope_instance_t *instance, envelope_t* envelope)
{
instance->stage = ENVELOPE_STAGE_INACTIVE;
instance->envelope = envelope;
}
void envelope_start(envelope_instance_t *instance)
{
instance->time_ms = 0;
instance->ref_level = instance->envelope->stages[0].start_level;
instance->last_level = instance->ref_level;
instance->stage = 0;
instance->ref_time_ms = 0;
envelope_make_callback(ENVELOPE_EVENT_STARTING, instance);
}
int32_t envelope_step(envelope_instance_t *instance, int32_t timestep_ms)
{
int32_t level = instance->last_level;
instance->time_ms += timestep_ms;
if (instance->stage >= 0)
{
envelope_stage_t *stage = instance->envelope->stages + instance->stage;
while (1) {
int32_t stage_time_elapsed = instance->time_ms - instance->ref_time_ms;
if (stage->duration != DURATION_HELD && stage_time_elapsed > stage->duration)
{
instance->stage++;
if (instance->stage == instance->envelope->stage_count)
{
level = instance->envelope->stages[instance->stage - 1].end_level;
instance->stage = ENVELOPE_STAGE_INACTIVE;
envelope_make_callback(ENVELOPE_EVENT_COMPLETED, instance);
break;
}
else
{
level = calculate_level(instance, stage, stage->duration);
instance->ref_time_ms += stage->duration;
instance->ref_level = level;
stage++;
envelope_make_callback(ENVELOPE_EVENT_STAGE_CHANGE, instance);
}
}
else
{
level = calculate_level(instance, stage, stage_time_elapsed);
break;
}
}
instance->last_level = level;
}
return level;
}
int envelope_completed(envelope_instance_t *instance)
{
return instance->stage == ENVELOPE_STAGE_INACTIVE;
}
void envelope_go_to_stage(envelope_instance_t *instance, int32_t stage_id)
{
instance->stage = stage_id;
instance->ref_level = instance->last_level;
instance->ref_time_ms = instance->time_ms;
envelope_make_callback(ENVELOPE_EVENT_STAGE_CHANGE, instance);
}