-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio.c
124 lines (102 loc) · 3.27 KB
/
audio.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
#include "threadlib.h"
#include "audio.h"
#include "memory.h"
PlayList playlist;
Queue audio_queue;
Filter global_filter;
FixedAllocator pls_allocator;
PlayListSample playlistsample_make(Sampler sampler) {
PlayListSample pl = (PlayListSample)fixed_allocator_alloc(pls_allocator);
pl->sampler = sampler;
return pl;
}
void playlistsample_free(PlayListSample pls) {
RELEASE_SAMPLER(pls->sampler);
fixed_allocator_free(pls_allocator, pls);
}
PlayList playlist_make() {
PlayList pl = malloc(sizeof(struct PlayList_));
pl->head = NULL;
pl->next_sample = 0;
return pl;
}
void playlist_insert_sampler(PlayList list, PlayListSample sample) {
if(list->head == NULL) {
list->head = sample;
sample->node.next = NULL;
return;
}
if(START(sample->sampler) < START(list->head->sampler)) {
sample->node.next = (DLLNode)list->head;
list->head = sample;
return;
}
PlayListSample last_node = list->head;
PlayListSample current_node = (PlayListSample)list->head->node.next;
while(current_node != NULL) {
if(START(sample->sampler) < START(current_node->sampler)) {
// insert before this node
sample->node.next = (DLLNode)current_node;
last_node->node.next = (DLLNode)sample;
return; // done
}
last_node = current_node;
current_node = (PlayListSample)current_node->node.next;
}
// must be after the end
sample->node.next = NULL;
last_node->node.next = (DLLNode)sample;
}
void playlist_fill_buffer(PlayList list, int16_t* buffer, int nsamples) {
int ii;
long next_sample = list->next_sample;
list->next_sample += nsamples;
for(ii = 0; ii < nsamples; ii+=2) {
long sample = next_sample + ii;
PlayListSample node;
/* mixing strategy outlined at:
* http://www.vttoth.com/CMS/index.php/technical-notes/68
*/
float value = 0;
for(node = list->head; node != NULL;
node = (PlayListSample)node->node.next) {
if(START(node->sampler) > sample) break;
int16_t sampled = SAMPLE(node->sampler, sample);
float normalized = (float)sampled / INT16_MAX;
value = value + normalized - (value * normalized);
}
buffer[ii] = INT16_MAX * value; //filter_value(global_filter, value);
buffer[ii+1] = buffer[ii];
}
/* remove any nodes that are no longer playable */
while(list->head != NULL &&
END(list->head->sampler) < list->next_sample) {
PlayListSample node = list->head;
list->head = (PlayListSample)node->node.next;
playlistsample_free(node);
}
}
void audio_init() {
sampler_init();
pls_allocator = fixed_allocator_make(sizeof(struct PlayListSample_),
NUM_SAMPLERS,
"pls_allocator");
playlist = playlist_make();
audio_queue = queue_make();
global_filter = lowpass_make(0, 0);
native_audio_init();
}
void audio_enqueue(Sampler sampler) {
enqueue(audio_queue, (DLLNode)playlistsample_make(sampler));
}
long audio_current_sample() {
/* race condition but we don't care */
return playlist->next_sample;
}
void audio_fill_buffer(int16_t* buffer, int nsamples) {
PlayListSample sample;
while((sample = (PlayListSample)dequeue_noblock(audio_queue)) != NULL) {
playlist_insert_sampler(playlist, sample);
}
playlist_fill_buffer(playlist, buffer, nsamples);
}