This repository was archived by the owner on Mar 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixer.c
210 lines (183 loc) · 5.78 KB
/
mixer.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
#include "pm.h"
/* --------------------------------------------------------------------------------------------------------- */
/* voice functions (low-level mixing) */
void channel_remove_voice(channel_t *channel, voice_t *voice)
{
int n;
if (!channel)
return;
if (channel->fg_voice == voice)
channel->fg_voice = NULL;
for (n = 0; n < channel->num_voices; n++) {
if (channel->voices[n] == voice) {
channel->voices[n] = channel->voices[--channel->num_voices];
break;
}
}
}
void voice_stop(voice_t *voice)
{
voice->data = NULL;
voice->inst_bg = NULL;
voice->noteon = 0;
channel_remove_voice(voice->host, voice);
}
/* This could be optimized a lot by writing separate functions for non-looping, forwards, and pingpong, 8-
and 16-bit, surround, maybe for pure left/right/center panning, etc. Essentially, instead of having one
function that does a lot, have a lot of little ones that are as short as possible, because any conditionals
will slow down the mixing. (although it's not really possible to get rid of the sample length check...) A
further optimization would be to check if the voice's volume is zero and increment the position without doing
any mixing. The non-mixing, non-looping function might even be reduced to voice->cur += (size * voice->inc)
and stopping the sample if it goes past the length. */
static void voice_get_sample(voice_t *voice, int32_t *l, int32_t *r)
{
int32_t s;
/* get the sample */
if (voice->flags & SAMP_16BIT)
s = ((int16_t *) voice->data)[voice->cur >> FRACBITS];
else
s = voice->data[voice->cur >> FRACBITS] << 8;
/* increment the position */
voice->cur += voice->inc;
/* check loops */
if (voice->flags & SAMP_LOOP) {
if (voice->flags & SAMP_PINGPONG) {
/* if we're going BACKWARDS, and the position is before the loop start, or if we're
going FORWARDS, and the position is after the loop end, change direction */
if ((voice->inc < 0 && voice->cur < voice->loop_start)
|| (voice->inc > 0 && voice->cur >= voice->loop_end)) {
voice->inc = -voice->inc;
voice->cur += 2 * voice->inc; /* adjust for previous increment */
}
} else {
if (voice->cur >= voice->loop_end)
voice->cur = voice->loop_start | (voice->cur & FRACMASK);
}
} else if (voice->cur >= voice->length) {
/* kill it */
voice_stop(voice);
}
if (voice->panning == PAN_SURROUND) {
int32_t q = (s * voice->lvol) >> 14;
*l += q;
*r -= q;
} else {
/* finally, send it back */
*l += (s * voice->lvol) >> 14;
*r += (s * voice->rvol) >> 14;
}
}
void voice_process(voice_t *voice, int32_t *buffer, int size)
{
/* don't mix muted channels (this could surely be handled less stupidly,
but it's only a temporary hack to get it working) */
if (voice->host->flags & CHAN_MUTE) {
int32_t junk[2];
while (voice->data && size) {
voice_get_sample(voice, junk, junk + 1);
size--;
}
return;
}
while (voice->data && size) {
voice_get_sample(voice, buffer, buffer + 1);
buffer += 2;
size--;
}
}
static void voice_calc_volume(voice_t *voice)
{
if (voice->panning == PAN_SURROUND || voice->panning == 32) {
/* this optimization appears worth it */
voice->rvol = voice->lvol = voice->volume * voice->nfc / 32;
} else {
voice->lvol = voice->volume * (64 - voice->panning) * voice->nfc / 1024;
voice->rvol = voice->volume * voice->panning * voice->nfc / 1024;
}
}
void voice_start(voice_t *voice, sample_t *sample)
{
voice->noteon = 1;
voice->slide = 0;
voice->portamento = 0;
voice->data = sample->data;
voice->length = sample->length << FRACBITS;
voice->loop_start = sample->loop_start << FRACBITS;
voice->loop_end = sample->loop_end << FRACBITS;
voice->flags = sample->flags;
voice->cur = 0;
voice->nfc = 1024;
voice->fadeout = 0;
voice->vibrato_pos = 0;
voice->vibrato_depth = sample->vibrato_depth; /* initial */
voice->vibrato_speed = sample->vibrato_speed;
voice->vibrato_rate = sample->vibrato_rate;
voice->vibrato_table = sample->vibrato_table;
memset(&voice->pitch_env, 0, sizeof(envelope_memory_t));
memset(&voice->vol_env, 0, sizeof(envelope_memory_t));
memset(&voice->pan_env, 0, sizeof(envelope_memory_t));
}
void voice_vibrato(voice_t *voice, const int *tab, int speed, int depth, int rate)
{
voice->vibrato_pos = 0;
voice->vibrato_speed = speed;
voice->vibrato_table = tab;
voice->vibrato_rate = rate;
voice->vibrato_depth = 256-depth;
}
void voice_set_period(voice_t *voice, int period)
{
voice->period = period;
}
void voice_apply_frequency(song_t *song, voice_t *voice, int frequency)
{
voice->frequency = frequency;
if (!song->mixing_rate) return;
voice->inc = (frequency << FRACBITS) / song->mixing_rate;
}
void voice_set_volume(voice_t *voice, int volume)
{
voice->fvolume = CLAMP(volume, 0, 128);
}
void voice_apply_volume_panning(voice_t *voice, int volume, int panning)
{
if (panning == PAN_SURROUND)
voice->panning = PAN_SURROUND;
else
voice->panning = CLAMP(panning, 0, 64);
voice->volume = CLAMP(volume, 0, 128);
voice_calc_volume(voice);
}
void voice_set_panning(voice_t *voice, int panning)
{
if (panning == PAN_SURROUND)
voice->fpanning = PAN_SURROUND;
else
voice->fpanning = CLAMP(panning, 0, 64);
}
void voice_set_position(voice_t *voice, int position)
{
voice->cur = position << FRACBITS;
}
void voice_fade(voice_t *voice)
{
if (voice->nfc > voice->fadeout) {
voice->nfc -= voice->fadeout;
} else {
voice_stop(voice);
}
}
voice_t *voice_find_free(voice_t *voices, int num_voices)
{
voice_t *voice = voices;
voice_t *lsv = NULL; /* least significant background voice -- used if all voices are playing */
int n;
for (n = 0; n < num_voices; n++, voice++) {
if (voice->data == NULL)
return voice;
/* check if this is a background voice that's quieter than the current lsv */
if (voice->host->fg_voice != voice && (lsv == NULL || lsv->volume > voice->volume))
lsv = voice;
}
return lsv;
}