summaryrefslogtreecommitdiff
path: root/apps/codecs/dumb/src/it/itrender.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/dumb/src/it/itrender.c')
-rw-r--r--apps/codecs/dumb/src/it/itrender.c3512
1 files changed, 3512 insertions, 0 deletions
diff --git a/apps/codecs/dumb/src/it/itrender.c b/apps/codecs/dumb/src/it/itrender.c
new file mode 100644
index 0000000000..103654fd77
--- /dev/null
+++ b/apps/codecs/dumb/src/it/itrender.c
@@ -0,0 +1,3512 @@
1/* _______ ____ __ ___ ___
2 * \ _ \ \ / \ / \ \ / / ' ' '
3 * | | \ \ | | || | \/ | . .
4 * | | | | | | || ||\ /| |
5 * | | | | | | || || \/ | | ' ' '
6 * | | | | | | || || | | . .
7 * | |_/ / \ \__// || | |
8 * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
9 * / \
10 * / . \
11 * itrender.c - Code to render an Impulse Tracker / / \ \
12 * module. | < / \_
13 * | \/ /\ /
14 * Written - painstakingly - by entheh. \_ / > /
15 * | \ / /
16 * | ' /
17 * \__/
18 */
19
20#include <math.h>
21#include <stdlib.h>
22
23#include "dumb.h"
24#include "internal/it.h"
25
26
27
28static IT_PLAYING *dup_playing(IT_PLAYING *src, IT_CHANNEL *dstchannel, IT_CHANNEL *srcchannel)
29{
30 IT_PLAYING *dst;
31
32 if (!src) return NULL;
33
34 dst = malloc(sizeof(*dst));
35 if (!dst) return NULL;
36
37 dst->flags = src->flags;
38
39 ASSERT(src->channel);
40 dst->channel = &dstchannel[src->channel - srcchannel];
41 dst->sample = src->sample;
42 dst->instrument = src->instrument;
43 dst->env_instrument = src->env_instrument;
44
45 dst->sampnum = src->sampnum;
46 dst->instnum = src->instnum;
47
48 dst->channel_volume = src->channel_volume;
49
50 dst->volume = src->volume;
51 dst->pan = src->pan;
52
53 dst->note = src->note;
54
55 dst->filter_cutoff = src->filter_cutoff;
56 dst->filter_resonance = src->filter_resonance;
57
58 dst->true_filter_cutoff = src->true_filter_cutoff;
59 dst->true_filter_resonance = src->true_filter_resonance;
60
61 dst->vibrato_speed = src->vibrato_speed;
62 dst->vibrato_depth = src->vibrato_depth;
63 dst->vibrato_n = src->vibrato_n;
64 dst->vibrato_time = src->vibrato_time;
65
66 dst->tremolo_speed = src->tremolo_speed;
67 dst->tremolo_depth = src->tremolo_depth;
68 dst->tremolo_time = src->tremolo_time;
69
70 dst->sample_vibrato_time = src->sample_vibrato_time;
71 dst->sample_vibrato_depth = src->sample_vibrato_depth;
72
73 dst->slide = src->slide;
74 dst->delta = src->delta;
75
76 dst->volume_envelope = src->volume_envelope;
77 dst->pan_envelope = src->pan_envelope;
78 dst->pitch_envelope = src->pitch_envelope;
79
80 dst->fadeoutcount = src->fadeoutcount;
81
82 dst->filter_state[0] = src->filter_state[0];
83 dst->filter_state[1] = src->filter_state[1];
84
85 dst->resampler[0] = src->resampler[0];
86 dst->resampler[1] = src->resampler[1];
87 dst->resampler[1].pickup_data = dst->resampler[0].pickup_data = dst;
88 dst->time_lost = src->time_lost;
89
90 return dst;
91}
92
93
94
95static void dup_channel(IT_CHANNEL *dst, IT_CHANNEL *src)
96{
97 dst->flags = src->flags;
98
99 dst->volume = src->volume;
100 dst->volslide = src->volslide;
101 dst->xm_volslide = src->xm_volslide;
102
103 dst->pan = src->pan;
104 dst->truepan = src->truepan;
105
106 dst->channelvolume = src->channelvolume;
107 dst->channelvolslide = src->channelvolslide;
108
109 dst->instrument = src->instrument;
110 dst->note = src->note;
111
112 dst->SFmacro = src->SFmacro;
113
114 dst->filter_cutoff = src->filter_cutoff;
115 dst->filter_resonance = src->filter_resonance;
116
117 dst->note_cut_count = src->note_cut_count;
118 dst->note_delay_count = src->note_delay_count;
119 dst->note_delay_entry = src->note_delay_entry;
120
121 dst->arpeggio = src->arpeggio;
122 dst->retrig = src->retrig;
123 dst->xm_retrig = src->xm_retrig;
124 dst->retrig_tick = src->retrig_tick;
125
126 dst->tremor_time = src->tremor_time;
127
128 dst->portamento = src->portamento;
129 dst->toneporta = src->toneporta;
130 dst->destnote = src->destnote;
131
132 dst->sample = src->sample;
133 dst->truenote = src->truenote;
134
135 dst->midi_state = src->midi_state;
136
137 dst->lastvolslide = src->lastvolslide;
138 dst->lastDKL = src->lastDKL;
139 dst->lastEF = src->lastEF;
140 dst->lastG = src->lastG;
141 dst->lastHspeed = src->lastHspeed;
142 dst->lastHdepth = src->lastHdepth;
143 dst->lastRspeed = src->lastRspeed;
144 dst->lastRdepth = src->lastRdepth;
145 dst->lastI = src->lastI;
146 dst->lastJ = src->lastJ;
147 dst->lastN = src->lastN;
148 dst->lastO = src->lastO;
149 dst->high_offset = src->high_offset;
150 dst->lastQ = src->lastQ;
151 dst->lastS = src->lastS;
152 dst->pat_loop_row = src->pat_loop_row;
153 dst->pat_loop_count = src->pat_loop_count;
154 dst->lastW = src->lastW;
155
156 dst->xm_lastE1 = src->xm_lastE1;
157 dst->xm_lastE2 = src->xm_lastE2;
158 dst->xm_lastEA = src->xm_lastEA;
159 dst->xm_lastEB = src->xm_lastEB;
160 dst->xm_lastX1 = src->xm_lastX1;
161 dst->xm_lastX2 = src->xm_lastX2;
162
163 dst->playing = dup_playing(src->playing, dst, src);
164}
165
166
167
168/* Allocate the new callbacks first, then pass them to this function!
169 * It will free them on failure.
170 */
171static DUMB_IT_SIGRENDERER *dup_sigrenderer(DUMB_IT_SIGRENDERER *src, int n_channels, IT_CALLBACKS *callbacks)
172{
173 DUMB_IT_SIGRENDERER *dst;
174 int i;
175
176 if (!src) {
177 if (callbacks) free(callbacks);
178 return NULL;
179 }
180
181 dst = malloc(sizeof(*dst));
182 if (!dst) {
183 if (callbacks) free(callbacks);
184 return NULL;
185 }
186
187 dst->sigdata = src->sigdata;
188
189 dst->n_channels = n_channels;
190
191 dst->globalvolume = src->globalvolume;
192 dst->globalvolslide = src->globalvolslide;
193
194 dst->tempo = src->tempo;
195 dst->temposlide = src->temposlide;
196
197 for (i = 0; i < DUMB_IT_N_CHANNELS; i++)
198 dup_channel(&dst->channel[i], &src->channel[i]);
199
200 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++)
201 dst->playing[i] = dup_playing(src->playing[i], dst->channel, src->channel);
202
203 dst->tick = src->tick;
204 dst->speed = src->speed;
205 dst->rowcount = src->rowcount;
206
207 dst->order = src->order;
208 dst->row = src->row;
209 dst->processorder = src->processorder;
210 dst->processrow = src->processrow;
211 dst->breakrow = src->breakrow;
212 dst->pat_loop_row = src->pat_loop_row;
213
214 dst->n_rows = src->n_rows;
215
216 dst->entry_start = src->entry_start;
217 dst->entry = src->entry;
218 dst->entry_end = src->entry_end;
219
220 dst->time_left = src->time_left;
221 dst->sub_time_left = src->sub_time_left;
222
223 dst->click_remover = NULL;
224
225 dst->callbacks = callbacks;
226
227 return dst;
228}
229
230
231
232static IT_MIDI default_midi = {
233 /* unsigned char SFmacro[16][16]; */
234 {
235 {0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
236 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
237 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
238 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
239 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
240 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
241 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
242 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
243 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
244 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
245 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
246 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
247 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
248 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
249 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
250 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
251 },
252 /* unsigned char SFmacrolen[16]; */
253 {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
254 /* unsigned short SFmacroz[16]; */
255 /* Bitfield; bit 0 set = z in first position */
256 {
257 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
258 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
259 },
260 /* unsigned char Zmacro[128][16]; */
261 {
262 {0xF0, 0xF0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
263 {0xF0, 0xF0, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
264 {0xF0, 0xF0, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
265 {0xF0, 0xF0, 0x01, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
266 {0xF0, 0xF0, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
267 {0xF0, 0xF0, 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
268 {0xF0, 0xF0, 0x01, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
269 {0xF0, 0xF0, 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
270 {0xF0, 0xF0, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
271 {0xF0, 0xF0, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
272 {0xF0, 0xF0, 0x01, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
273 {0xF0, 0xF0, 0x01, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
274 {0xF0, 0xF0, 0x01, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
275 {0xF0, 0xF0, 0x01, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
276 {0xF0, 0xF0, 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
277 {0xF0, 0xF0, 0x01, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
278
279 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
280 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
281 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
282 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
283 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
284 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
285 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
286 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
287 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
288 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
289 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
290 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
291 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
292 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
293 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
294 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
295
296 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
297 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
298 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
299 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
300 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
301 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
302 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
303 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
304 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
305 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
306 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
307 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
308 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
309 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
310 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
311 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
312
313 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
314 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
315 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
316 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
317 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
318 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
319 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
320 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
321 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
322 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
323 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
324 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
325 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
326 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
327 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
328 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
329
330 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
331 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
332 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
333 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
334 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
335 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
336 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
337 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
338 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
339 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
340 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
341 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
342 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
343 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
344 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
345 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
346
347 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
348 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
349 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
350 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
351 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
352 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
353 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
354 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
355 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
356 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
357 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
358 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
359 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
360 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
361 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
362 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
363
364 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
365 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
366 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
367 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
368 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
369 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
370 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
371 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
372 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
373 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
374 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
375 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
376 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
377 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
378 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
379 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
380
381 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
382 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
383 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
384 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
385 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
386 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
387 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
388 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
389 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
390 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
391 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
392 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
393 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
394 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
395 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
396 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
397 },
398 /* unsigned char Zmacrolen[128]; */
399 {
400 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
401 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
402 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
403 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
404 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
405 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
406 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
407 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
408 }
409};
410
411
412
413static void it_reset_filter_state(IT_FILTER_STATE *state)
414{
415 state->currsample = 0;
416 state->prevsample = 0;
417}
418
419
420
421#define LOG10 2.30258509299
422
423/* IMPORTANT: This function expects one extra sample in 'src' so it can apply
424 * click removal. It reads size samples, starting from src[0], and writes its
425 * output starting at dst[pos]. The pos parameter is required for getting
426 * click removal right.
427 */
428static void it_filter(DUMB_CLICK_REMOVER *cr, IT_FILTER_STATE *state, sample_t *dst, long pos, sample_t *src, long size, int sampfreq, int cutoff, int resonance)
429{
430 float currsample = state->currsample;
431 float prevsample = state->prevsample;
432
433 float a, b, c;
434
435 {
436 float inv_angle = (float)(sampfreq * pow(0.5, 0.25 + cutoff*(1.0/(24<<IT_ENVELOPE_SHIFT))) * (1.0/(2*3.14159265358979323846*110.0)));
437 float loss = (float)exp(resonance*(-LOG10*1.2/128.0));
438 float d, e;
439#if 0
440 loss *= 2; // This is the mistake most players seem to make!
441#endif
442
443#if 1
444 d = (1.0f - loss) / inv_angle;
445 if (d > 2.0f) d = 2.0f;
446 d = (loss - d) * inv_angle;
447 e = inv_angle * inv_angle;
448 a = 1.0f / (1.0f + d + e);
449 c = -e * a;
450 b = 1.0f - a - c;
451#else
452 a = 1.0f / (inv_angle*inv_angle + inv_angle*loss + loss);
453 c = -(inv_angle*inv_angle) * a;
454 b = 1.0f - a - c;
455#endif
456 }
457
458 dst += pos;
459
460 if (cr) {
461 float startstep = src[0]*a + currsample*b + prevsample*c;
462 dumb_record_click(cr, pos, (sample_t)startstep);
463 }
464
465#define INT_FILTERS
466#ifdef INT_FILTERS
467#define MULSCA(a, b) ((int)((LONG_LONG)((a) << 4) * (b) >> 32))
468#define SCALEB 12
469 {
470 int ai = (int)(a * (1 << (16+SCALEB)));
471 int bi = (int)(b * (1 << (16+SCALEB)));
472 int ci = (int)(c * (1 << (16+SCALEB)));
473 sample_t csi = (sample_t)currsample;
474 sample_t psi = (sample_t)prevsample;
475 sample_t *dst_end = dst + size;
476 while (dst < dst_end) {
477 {
478 sample_t nsi = MULSCA(*src++, ai) + MULSCA(csi, bi) + MULSCA(psi, ci);
479 psi = csi;
480 csi = nsi;
481 }
482 *dst++ += csi;
483 }
484 currsample = csi;
485 prevsample = psi;
486 }
487#else
488 {
489 int i = size % 3;
490 while (i > 0) {
491 {
492 float newsample = *src++*a + currsample*b + prevsample*c;
493 prevsample = currsample;
494 currsample = newsample;
495 }
496 *dst++ += (sample_t)currsample;
497 i--;
498 }
499 i = size / 3;
500 while (i > 0) {
501 float newsample;
502 /* Gotta love unrolled loops! */
503 *dst++ += (sample_t)(newsample = *src++*a + currsample*b + prevsample*c);
504 *dst++ += (sample_t)(prevsample = *src++*a + newsample*b + currsample*c);
505 *dst++ += (sample_t)(currsample = *src++*a + prevsample*b + newsample*c);
506 i--;
507 }
508 }
509#endif
510
511 if (cr) {
512 float endstep = *src*a + currsample*b + prevsample*c;
513 dumb_record_click(cr, pos + size, -(sample_t)endstep);
514 }
515
516 state->currsample = currsample;
517 state->prevsample = prevsample;
518}
519
520#undef LOG10
521
522
523
524static signed char it_sine[256] = {
525 0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 16, 17, 19, 20, 22, 23,
526 24, 26, 27, 29, 30, 32, 33, 34, 36, 37, 38, 39, 41, 42, 43, 44,
527 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59,
528 59, 60, 60, 61, 61, 62, 62, 62, 63, 63, 63, 64, 64, 64, 64, 64,
529 64, 64, 64, 64, 64, 64, 63, 63, 63, 62, 62, 62, 61, 61, 60, 60,
530 59, 59, 58, 57, 56, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46,
531 45, 44, 43, 42, 41, 39, 38, 37, 36, 34, 33, 32, 30, 29, 27, 26,
532 24, 23, 22, 20, 19, 17, 16, 14, 12, 11, 9, 8, 6, 5, 3, 2,
533 0, -2, -3, -5, -6, -8, -9,-11,-12,-14,-16,-17,-19,-20,-22,-23,
534 -24,-26,-27,-29,-30,-32,-33,-34,-36,-37,-38,-39,-41,-42,-43,-44,
535 -45,-46,-47,-48,-49,-50,-51,-52,-53,-54,-55,-56,-56,-57,-58,-59,
536 -59,-60,-60,-61,-61,-62,-62,-62,-63,-63,-63,-64,-64,-64,-64,-64,
537 -64,-64,-64,-64,-64,-64,-63,-63,-63,-62,-62,-62,-61,-61,-60,-60,
538 -59,-59,-58,-57,-56,-56,-55,-54,-53,-52,-51,-50,-49,-48,-47,-46,
539 -45,-44,-43,-42,-41,-39,-38,-37,-36,-34,-33,-32,-30,-29,-27,-26,
540 -24,-23,-22,-20,-19,-17,-16,-14,-12,-11, -9, -8, -6, -5, -3, -2
541};
542
543
544
545#if 0
546/** WARNING: use these! */
547/** JULIEN: Plus for XM compatibility it could be interesting to rename
548 * it_sawtooth[] to it_rampdown[], and add an it_rampup[].
549 * Also, still for XM compat', twood be good if it was possible to tell the
550 * the player not to retrig' the waveform on a new instrument.
551 * Both of these are only for completness though, as I don't think it would
552 * be very noticeable ;)
553 */
554/** ENTHEH: IT also has the 'don't retrig' thingy :) */
555
556static signed char it_sawtooth[256] = {
557 64, 63, 63, 62, 62, 61, 61, 60, 60, 59, 59, 58, 58, 57, 57, 56,
558 56, 55, 55, 54, 54, 53, 53, 52, 52, 51, 51, 50, 50, 49, 49, 48,
559 48, 47, 47, 46, 46, 45, 45, 44, 44, 43, 43, 42, 42, 41, 41, 40,
560 40, 39, 39, 38, 38, 37, 37, 36, 36, 35, 35, 34, 34, 33, 33, 32,
561 32, 31, 31, 30, 30, 29, 29, 28, 28, 27, 27, 26, 26, 25, 25, 24,
562 24, 23, 23, 22, 22, 21, 21, 20, 20, 19, 19, 18, 18, 17, 17, 16,
563 16, 15, 15, 14, 14, 13, 13, 12, 12, 11, 11, 10, 10, 9, 9, 8,
564 8, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0,
565 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, -8,
566 -8, -9, -9,-10,-10,-11,-11,-12,-12,-13,-13,-14,-14,-15,-15,-16,
567 -16,-17,-17,-18,-18,-19,-19,-20,-20,-21,-21,-22,-22,-23,-23,-24,
568 -24,-25,-25,-26,-26,-27,-27,-28,-28,-29,-29,-30,-30,-31,-31,-32,
569 -32,-33,-33,-34,-34,-35,-35,-36,-36,-37,-37,-38,-38,-39,-39,-40,
570 -40,-41,-41,-42,-42,-43,-43,-44,-44,-45,-45,-46,-46,-47,-47,-48,
571 -48,-49,-49,-50,-50,-51,-51,-52,-52,-53,-53,-54,-54,-55,-55,-56,
572 -56,-57,-57,-58,-58,-59,-59,-60,-60,-61,-61,-62,-62,-63,-63,-64
573};
574
575
576
577static signed char it_squarewave[256] = {
578 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
579 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
580 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
581 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
582 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
583 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
584 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
585 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
586 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
587 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
588 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
589 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
590 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
591 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
592 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
593 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
594};
595
596#endif
597
598
599
600static void reset_tick_counts(DUMB_IT_SIGRENDERER *sigrenderer)
601{
602 int i;
603
604 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
605 IT_CHANNEL *channel = &sigrenderer->channel[i];
606 channel->note_cut_count = 0;
607 channel->note_delay_count = 0;
608 }
609}
610
611
612
613static void reset_effects(DUMB_IT_SIGRENDERER *sigrenderer)
614{
615 int i;
616
617 sigrenderer->globalvolslide = 0;
618 sigrenderer->temposlide = 0;
619
620 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
621 IT_CHANNEL *channel = &sigrenderer->channel[i];
622 channel->volslide = 0;
623 channel->xm_volslide = 0;
624 channel->channelvolslide = 0;
625 channel->arpeggio = 0;
626 channel->retrig = 0;
627 if (channel->xm_retrig) {
628 channel->xm_retrig = 0;
629 channel->retrig_tick = 0;
630 }
631 channel->tremor_time &= 127;
632 channel->portamento = 0;
633 channel->toneporta = 0;
634 if (channel->playing) {
635 channel->playing->vibrato_n = 0;
636 channel->playing->tremolo_speed = 0;
637 channel->playing->tremolo_depth = 0;
638 }
639 }
640}
641
642
643
644static void update_tremor(IT_CHANNEL *channel)
645{
646 if ((channel->tremor_time & 128) && channel->playing) {
647 if (channel->tremor_time == 128)
648 channel->tremor_time = (channel->lastI >> 4) | 192;
649 else if (channel->tremor_time == 192)
650 channel->tremor_time = (channel->lastI & 15) | 128;
651 else
652 channel->tremor_time--;
653 }
654}
655
656
657
658static void it_pickup_loop(DUMB_RESAMPLER *resampler, void *data)
659{
660 resampler->pos -= resampler->end - resampler->start;
661 ((IT_PLAYING *)data)->time_lost += resampler->end - resampler->start;
662}
663
664
665
666static void it_pickup_pingpong_loop(DUMB_RESAMPLER *resampler, void *data)
667{
668 if (resampler->dir < 0) {
669 resampler->pos = (resampler->start << 1) - 1 - resampler->pos;
670 resampler->subpos ^= 65535;
671 resampler->dir = 1;
672 ((IT_PLAYING *)data)->time_lost += (resampler->end - resampler->start) << 1;
673 } else {
674 resampler->pos = (resampler->end << 1) - 1 - resampler->pos;
675 resampler->subpos ^= 65535;
676 resampler->dir = -1;
677 }
678}
679
680
681
682static void it_pickup_stop_at_end(DUMB_RESAMPLER *resampler, void *data)
683{
684 (void)data;
685
686 if (resampler->dir < 0) {
687 resampler->pos = (resampler->start << 1) - 1 - resampler->pos;
688 resampler->subpos ^= 65535;
689 /* By rights, time_lost would be updated here. However, there is no
690 * need at this point; it will not be used.
691 *
692 * ((IT_PLAYING *)data)->time_lost += (resampler->src_end - resampler->src_start) << 1;
693 */
694 resampler->dir = 1;
695 } else
696 resampler->dir = 0;
697}
698
699
700
701static void it_playing_update_resamplers(IT_PLAYING *playing)
702{
703 if ((playing->sample->flags & IT_SAMPLE_SUS_LOOP) && !(playing->flags & IT_PLAYING_SUSTAINOFF)) {
704 playing->resampler[0].start = playing->sample->sus_loop_start;
705 playing->resampler[0].end = playing->sample->sus_loop_end;
706 if (playing->sample->flags & IT_SAMPLE_PINGPONG_SUS_LOOP)
707 playing->resampler[0].pickup = &it_pickup_pingpong_loop;
708 else
709 playing->resampler[0].pickup = &it_pickup_loop;
710 } else if (playing->sample->flags & IT_SAMPLE_LOOP) {
711 playing->resampler[0].start = playing->sample->loop_start;
712 playing->resampler[0].end = playing->sample->loop_end;
713 if (playing->sample->flags & IT_SAMPLE_PINGPONG_LOOP)
714 playing->resampler[0].pickup = &it_pickup_pingpong_loop;
715 else
716 playing->resampler[0].pickup = &it_pickup_loop;
717 } else {
718 if (playing->sample->flags & IT_SAMPLE_SUS_LOOP)
719 playing->resampler[0].start = playing->sample->sus_loop_start;
720 else
721 playing->resampler[0].start = 0;
722 playing->resampler[0].end = playing->sample->length;
723 playing->resampler[0].pickup = &it_pickup_stop_at_end;
724 }
725 playing->resampler[1].start = playing->resampler[0].start;
726 playing->resampler[1].end = playing->resampler[0].end;
727 playing->resampler[1].pickup = playing->resampler[0].pickup;
728 ASSERT(playing->resampler[0].pickup_data == playing);
729 ASSERT(playing->resampler[1].pickup_data == playing);
730}
731
732
733
734/* This should be called whenever the sample or sample position changes. */
735static void it_playing_reset_resamplers(IT_PLAYING *playing, long pos)
736{
737 dumb_reset_resampler(&playing->resampler[0], playing->sample->left, pos, 0, 0);
738 dumb_reset_resampler(&playing->resampler[1], playing->sample->right, pos, 0, 0);
739 playing->resampler[1].pickup_data = playing->resampler[0].pickup_data = playing;
740 playing->time_lost = 0;
741 playing->flags &= ~IT_PLAYING_DEAD;
742 it_playing_update_resamplers(playing);
743}
744
745
746
747static void update_retrig(IT_CHANNEL *channel)
748{
749 if (channel->xm_retrig) {
750 channel->retrig_tick--;
751 if (channel->retrig_tick <= 0) {
752 if (channel->playing) it_playing_reset_resamplers(channel->playing, 0);
753 channel->retrig_tick = channel->xm_retrig;
754 }
755 } else if (channel->retrig & 0x0F) {
756 channel->retrig_tick--;
757 if (channel->retrig_tick <= 0) {
758 if (channel->retrig < 0x10) {
759 } else if (channel->retrig < 0x20) {
760 channel->volume--;
761 if (channel->volume > 64) channel->volume = 0;
762 } else if (channel->retrig < 0x30) {
763 channel->volume -= 2;
764 if (channel->volume > 64) channel->volume = 0;
765 } else if (channel->retrig < 0x40) {
766 channel->volume -= 4;
767 if (channel->volume > 64) channel->volume = 0;
768 } else if (channel->retrig < 0x50) {
769 channel->volume -= 8;
770 if (channel->volume > 64) channel->volume = 0;
771 } else if (channel->retrig < 0x60) {
772 channel->volume -= 16;
773 if (channel->volume > 64) channel->volume = 0;
774 } else if (channel->retrig < 0x70) {
775 channel->volume <<= 1;
776 channel->volume /= 3;
777 } else if (channel->retrig < 0x80) {
778 channel->volume >>= 1;
779 } else if (channel->retrig < 0x90) {
780 } else if (channel->retrig < 0xA0) {
781 channel->volume++;
782 if (channel->volume > 64) channel->volume = 64;
783 } else if (channel->retrig < 0xB0) {
784 channel->volume += 2;
785 if (channel->volume > 64) channel->volume = 64;
786 } else if (channel->retrig < 0xC0) {
787 channel->volume += 4;
788 if (channel->volume > 64) channel->volume = 64;
789 } else if (channel->retrig < 0xD0) {
790 channel->volume += 8;
791 if (channel->volume > 64) channel->volume = 64;
792 } else if (channel->retrig < 0xE0) {
793 channel->volume += 16;
794 if (channel->volume > 64) channel->volume = 64;
795 } else if (channel->retrig < 0xF0) {
796 channel->volume *= 3;
797 channel->volume >>= 1;
798 if (channel->volume > 64) channel->volume = 64;
799 } else {
800 channel->volume <<= 1;
801 if (channel->volume > 64) channel->volume = 64;
802 }
803 if (channel->playing) it_playing_reset_resamplers(channel->playing, 0);
804 channel->retrig_tick = channel->retrig & 0x0F;
805 }
806 }
807}
808
809
810
811static void update_smooth_effects(DUMB_IT_SIGRENDERER *sigrenderer)
812{
813 int i;
814
815 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
816 IT_CHANNEL *channel = &sigrenderer->channel[i];
817 IT_PLAYING *playing = channel->playing;
818
819 if (playing) {
820 playing->vibrato_time += playing->vibrato_n *
821 (playing->vibrato_speed << 2);
822 playing->tremolo_time += playing->tremolo_speed << 2;
823 }
824 }
825}
826
827
828
829static void update_effects(DUMB_IT_SIGRENDERER *sigrenderer)
830{
831 int i;
832
833 if (sigrenderer->globalvolslide) {
834 sigrenderer->globalvolume += sigrenderer->globalvolslide;
835 if (sigrenderer->globalvolume > 128) {
836 if (sigrenderer->globalvolslide >= 0)
837 sigrenderer->globalvolume = 128;
838 else
839 sigrenderer->globalvolume = 0;
840 }
841 }
842
843 if (sigrenderer->temposlide) {
844 sigrenderer->tempo += sigrenderer->temposlide;
845 if (sigrenderer->tempo < 32) {
846 if (sigrenderer->temposlide >= 0)
847 sigrenderer->tempo = 255;
848 else
849 sigrenderer->tempo = 32;
850 }
851 }
852
853 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
854 IT_CHANNEL *channel = &sigrenderer->channel[i];
855 IT_PLAYING *playing = channel->playing;
856
857 if (channel->xm_volslide) {
858 channel->volume += channel->xm_volslide;
859 if (channel->volume > 64) {
860 if (channel->xm_volslide >= 0)
861 channel->volume = 64;
862 else
863 channel->volume = 0;
864 }
865 }
866
867 if (channel->volslide) {
868 channel->volume += channel->volslide;
869 if (channel->volume > 64) {
870 if (channel->volslide >= 0)
871 channel->volume = 64;
872 else
873 channel->volume = 0;
874 }
875 }
876
877 if (channel->channelvolslide) {
878 channel->channelvolume += channel->channelvolslide;
879 if (channel->channelvolume > 64) {
880 if (channel->channelvolslide >= 0)
881 channel->channelvolume = 64;
882 else
883 channel->channelvolume = 0;
884 }
885 if (channel->playing)
886 channel->playing->channel_volume = channel->channelvolume;
887 }
888
889 update_tremor(channel);
890
891 channel->arpeggio = (channel->arpeggio << 4) | (channel->arpeggio >> 8);
892 channel->arpeggio &= 0xFFF;
893
894 update_retrig(channel);
895
896 if (playing) {
897 playing->slide += channel->portamento;
898
899 if (sigrenderer->sigdata->flags & IT_LINEAR_SLIDES) {
900 if (channel->toneporta && channel->destnote < 120) {
901 int currpitch = ((playing->note - 60) << 8) + playing->slide;
902 int destpitch = (channel->destnote - 60) << 8;
903 if (currpitch > destpitch) {
904 currpitch -= channel->toneporta;
905 if (currpitch < destpitch) {
906 currpitch = destpitch;
907 channel->destnote = IT_NOTE_OFF;
908 }
909 } else if (currpitch < destpitch) {
910 currpitch += channel->toneporta;
911 if (currpitch > destpitch) {
912 currpitch = destpitch;
913 channel->destnote = IT_NOTE_OFF;
914 }
915 }
916 playing->slide = currpitch - ((playing->note - 60) << 8);
917 }
918 } else {
919 if (channel->toneporta && channel->destnote < 120) {
920 float amiga_multiplier = playing->sample->C5_speed * (1.0f / AMIGA_DIVISOR);
921
922 float deltanote = (float)pow(DUMB_SEMITONE_BASE, 60 - playing->note);
923 /* deltanote is 1.0 for C-5, 0.5 for C-6, etc. */
924
925 float deltaslid = deltanote - playing->slide * amiga_multiplier;
926
927 float destdelta = (float)pow(DUMB_SEMITONE_BASE, 60 - channel->destnote);
928 if (deltaslid < destdelta) {
929 playing->slide -= channel->toneporta;
930 deltaslid = deltanote - playing->slide * amiga_multiplier;
931 if (deltaslid > destdelta) {
932 playing->note = channel->destnote;
933 playing->slide = 0;
934 channel->destnote = IT_NOTE_OFF;
935 }
936 } else {
937 playing->slide += channel->toneporta;
938 deltaslid = deltanote - playing->slide * amiga_multiplier;
939 if (deltaslid < destdelta) {
940 playing->note = channel->destnote;
941 playing->slide = 0;
942 channel->destnote = IT_NOTE_OFF;
943 }
944 }
945 }
946 }
947 }
948 }
949
950 update_smooth_effects(sigrenderer);
951}
952
953
954
955static void update_pattern_variables(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
956{
957 IT_CHANNEL *channel = &sigrenderer->channel[(int)entry->channel];
958
959 if (entry->mask & IT_ENTRY_EFFECT) {
960 if (entry->effect == IT_S) {
961 unsigned char effectvalue = entry->effectvalue;
962 if (effectvalue == 0)
963 effectvalue = channel->lastS;
964 channel->lastS = effectvalue;
965 switch (effectvalue >> 4) {
966 //case IT_S7:
967 case IT_S_PATTERN_LOOP:
968 {
969 unsigned char v = effectvalue & 15;
970 if (v == 0)
971 channel->pat_loop_row = sigrenderer->processrow;
972 else {
973 if (channel->pat_loop_count == 0) {
974 channel->pat_loop_count = v;
975 sigrenderer->pat_loop_row = channel->pat_loop_row;
976 } else {
977 if (--channel->pat_loop_count)
978 sigrenderer->pat_loop_row = channel->pat_loop_row;
979 else if (!(sigrenderer->sigdata->flags & IT_WAS_AN_XM))
980 channel->pat_loop_row = sigrenderer->processrow + 1;
981 }
982 }
983 }
984 break;
985 case IT_S_PATTERN_DELAY:
986 sigrenderer->rowcount = 1 + (effectvalue & 15);
987 break;
988 }
989 }
990 }
991}
992
993
994
995/* This function guarantees that channel->sample will always be valid if it
996 * is nonzero. In other words, to check if it is valid, simply check if it is
997 * nonzero.
998 */
999static void instrument_to_sample(DUMB_IT_SIGDATA *sigdata, IT_CHANNEL *channel)
1000{
1001 if (sigdata->flags & IT_USE_INSTRUMENTS) {
1002 if (channel->instrument >= 1 && channel->instrument <= sigdata->n_instruments) {
1003 if (channel->note < 120) {
1004 channel->sample = sigdata->instrument[channel->instrument-1].map_sample[channel->note];
1005 channel->truenote = sigdata->instrument[channel->instrument-1].map_note[channel->note];
1006 } else
1007 channel->sample = 0;
1008 } else
1009 channel->sample = 0;
1010 } else {
1011 channel->sample = channel->instrument;
1012 channel->truenote = channel->note;
1013 }
1014 if (!(channel->sample >= 1 && channel->sample <= sigdata->n_samples && (sigdata->sample[channel->sample-1].flags & IT_SAMPLE_EXISTS)))
1015 channel->sample = 0;
1016}
1017
1018
1019
1020static void fix_sample_looping(IT_PLAYING *playing)
1021{
1022 if ((playing->sample->flags & (IT_SAMPLE_LOOP | IT_SAMPLE_SUS_LOOP)) ==
1023 (IT_SAMPLE_LOOP | IT_SAMPLE_SUS_LOOP)) {
1024 if (playing->resampler[0].dir < 0) {
1025 playing->resampler[1].pos = playing->resampler[0].pos = (playing->sample->sus_loop_end << 1) - 1 - playing->resampler[0].pos;
1026 playing->resampler[1].subpos = playing->resampler[0].subpos ^= 65535;
1027 playing->resampler[1].dir = playing->resampler[0].dir = 1;
1028 }
1029
1030 playing->resampler[1].pos = playing->resampler[0].pos += playing->time_lost;
1031 }
1032}
1033
1034
1035
1036static void retrigger_it_envelopes(DUMB_IT_SIGDATA *sigdata, IT_CHANNEL *channel)
1037{
1038 channel->playing->volume_envelope.next_node = 0;
1039 channel->playing->volume_envelope.tick = -1;
1040 channel->playing->pan_envelope.next_node = 0;
1041 channel->playing->pan_envelope.tick = -1;
1042 channel->playing->pitch_envelope.next_node = 0;
1043 channel->playing->pitch_envelope.tick = -1;
1044 channel->playing->fadeoutcount = 1024;
1045 // Should we remove IT_PLAYING_BACKGROUND? Test with sample with sustain loop...
1046 channel->playing->flags &= ~(IT_PLAYING_BACKGROUND | IT_PLAYING_SUSTAINOFF | IT_PLAYING_FADING | IT_PLAYING_DEAD);
1047 it_playing_update_resamplers(channel->playing);
1048
1049 if (channel->sample)
1050 if (sigdata->flags & IT_USE_INSTRUMENTS)
1051 channel->playing->env_instrument = &sigdata->instrument[channel->instrument-1];
1052}
1053
1054
1055
1056static void it_retrigger_note(DUMB_IT_SIGRENDERER *sigrenderer, IT_CHANNEL *channel)
1057{
1058 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
1059 unsigned char nna;
1060 int i;
1061
1062 if (channel->playing) {
1063#ifdef INVALID_NOTES_CAUSE_NOTE_CUT
1064 if (channel->note == IT_NOTE_OFF)
1065 nna = NNA_NOTE_OFF;
1066 else if (channel->note >= 120 || !channel->playing->instrument || (channel->playing->flags & IT_PLAYING_DEAD))
1067 nna = NNA_NOTE_CUT;
1068 else
1069 nna = channel->playing->instrument->new_note_action;
1070#else
1071 if (channel->note == IT_NOTE_CUT)
1072 nna = NNA_NOTE_CUT;
1073 if (channel->note >= 120)
1074 nna = NNA_NOTE_OFF;
1075 else if (!channel->playing->instrument || (channel->playing->flags & IT_PLAYING_DEAD))
1076 nna = NNA_NOTE_CUT;
1077 else
1078 nna = channel->playing->instrument->new_note_action;
1079#endif
1080
1081 switch (nna) {
1082 case NNA_NOTE_CUT:
1083 free(channel->playing);
1084 channel->playing = NULL;
1085 break;
1086 case NNA_NOTE_OFF:
1087 channel->playing->flags |= IT_PLAYING_BACKGROUND | IT_PLAYING_SUSTAINOFF;
1088 fix_sample_looping(channel->playing);
1089 it_playing_update_resamplers(channel->playing);
1090 if (channel->playing->instrument)
1091 if ((channel->playing->instrument->volume_envelope.flags & (IT_ENVELOPE_ON | IT_ENVELOPE_LOOP_ON)) != IT_ENVELOPE_ON)
1092 channel->playing->flags |= IT_PLAYING_FADING;
1093 break;
1094 case NNA_NOTE_FADE:
1095 channel->playing->flags |= IT_PLAYING_BACKGROUND | IT_PLAYING_FADING;
1096 break;
1097 }
1098 }
1099
1100 if (channel->sample == 0 || channel->note >= 120)
1101 return;
1102
1103 channel->destnote = IT_NOTE_OFF;
1104
1105 if (channel->playing) {
1106 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++) {
1107 if (!sigrenderer->playing[i]) {
1108 sigrenderer->playing[i] = channel->playing;
1109 channel->playing = NULL;
1110 break;
1111 }
1112 }
1113/** WARNING - come up with some more heuristics for replacing old notes */
1114#if 0
1115 if (channel->playing) {
1116 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++) {
1117 if (sigrenderer->playing[i]->flags & IT_PLAYING_BACKGROUND) {
1118 write_seqtime();
1119 sequence_c(SEQUENCE_STOP_SIGNAL);
1120 sequence_c(i);
1121 channel->VChannel = &module->VChannel[i];
1122 break;
1123 }
1124 }
1125 }
1126#endif
1127 }
1128
1129 if (channel->playing)
1130 free(channel->playing);
1131
1132 channel->playing = malloc(sizeof(*channel->playing));
1133
1134 if (!channel->playing)
1135 return;
1136
1137 channel->playing->flags = 0;
1138 channel->playing->channel = channel;
1139 channel->playing->sample = &sigdata->sample[channel->sample-1];
1140 if (sigdata->flags & IT_USE_INSTRUMENTS)
1141 channel->playing->instrument = &sigdata->instrument[channel->instrument-1];
1142 else
1143 channel->playing->instrument = NULL;
1144 channel->playing->env_instrument = channel->playing->instrument;
1145 channel->playing->sampnum = channel->sample;
1146 channel->playing->instnum = channel->instrument;
1147 channel->playing->channel_volume = channel->channelvolume;
1148 channel->playing->note = channel->truenote;
1149 channel->playing->filter_cutoff = 127;
1150 channel->playing->filter_resonance = 0;
1151 channel->playing->true_filter_cutoff = 127 << 8;
1152 channel->playing->true_filter_resonance = 0;
1153 channel->playing->vibrato_speed = 0;
1154 channel->playing->vibrato_depth = 0;
1155 channel->playing->vibrato_n = 0;
1156 channel->playing->vibrato_time = 0;
1157 channel->playing->tremolo_speed = 0;
1158 channel->playing->tremolo_depth = 0;
1159 channel->playing->tremolo_time = 0;
1160 channel->playing->sample_vibrato_time = 0;
1161 channel->playing->sample_vibrato_depth = 0;
1162 channel->playing->slide = 0;
1163 channel->playing->volume_envelope.next_node = 0;
1164 channel->playing->volume_envelope.tick = -1;
1165 channel->playing->pan_envelope.next_node = 0;
1166 channel->playing->pan_envelope.tick = -1;
1167 channel->playing->pitch_envelope.next_node = 0;
1168 channel->playing->pitch_envelope.tick = -1;
1169 channel->playing->fadeoutcount = 1024;
1170 it_reset_filter_state(&channel->playing->filter_state[0]);
1171 it_reset_filter_state(&channel->playing->filter_state[1]);
1172 it_playing_reset_resamplers(channel->playing, 0);
1173
1174 /** WARNING - is everything initialised? */
1175}
1176
1177
1178
1179static void get_default_volpan(DUMB_IT_SIGDATA *sigdata, IT_CHANNEL *channel)
1180{
1181 if (channel->sample == 0)
1182 return;
1183
1184 channel->volume = sigdata->sample[channel->sample-1].default_volume;
1185
1186 {
1187 int pan = sigdata->sample[channel->sample-1].default_pan;
1188 if (pan >= 128 && pan <= 192) {
1189 channel->pan = pan - 128;
1190 return;
1191 }
1192 }
1193
1194 if (sigdata->flags & IT_USE_INSTRUMENTS) {
1195 IT_INSTRUMENT *instrument = &sigdata->instrument[channel->instrument-1];
1196 if (instrument->default_pan <= 64)
1197 channel->pan = instrument->default_pan;
1198 if (instrument->filter_cutoff >= 128)
1199 channel->filter_cutoff = instrument->filter_cutoff - 128;
1200 if (instrument->filter_resonance >= 128)
1201 channel->filter_resonance = instrument->filter_resonance - 128;
1202 }
1203}
1204
1205
1206
1207static void get_true_pan(DUMB_IT_SIGDATA *sigdata, IT_CHANNEL *channel)
1208{
1209 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
1210
1211 if (!IT_IS_SURROUND_SHIFTED(channel->truepan) && (sigdata->flags & IT_USE_INSTRUMENTS)) {
1212 IT_INSTRUMENT *instrument = &sigdata->instrument[channel->instrument-1];
1213 int truepan = channel->truepan;
1214 truepan += (channel->note - instrument->pp_centre) * instrument->pp_separation << (IT_ENVELOPE_SHIFT - 3);
1215 channel->truepan = MID(0, truepan, 64 << IT_ENVELOPE_SHIFT);
1216 }
1217}
1218
1219
1220
1221static void post_process_it_volpan(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
1222{
1223 IT_CHANNEL *channel = &sigrenderer->channel[(int)entry->channel];
1224
1225 if (entry->mask & IT_ENTRY_VOLPAN) {
1226 if (entry->volpan <= 84) {
1227 /* Volume */
1228 /* Fine volume slide up */
1229 /* Fine volume slide down */
1230 } else if (entry->volpan <= 94) {
1231 /* Volume slide up */
1232 unsigned char v = entry->volpan - 85;
1233 if (v == 0)
1234 v = channel->lastvolslide;
1235 channel->lastvolslide = v;
1236 /* = effect Dx0 where x == entry->volpan - 85 */
1237 channel->volslide = v;
1238 } else if (entry->volpan <= 104) {
1239 /* Volume slide down */
1240 unsigned char v = entry->volpan - 95;
1241 if (v == 0)
1242 v = channel->lastvolslide;
1243 channel->lastvolslide = v;
1244 /* = effect D0x where x == entry->volpan - 95 */
1245 channel->volslide = -v;
1246 } else if (entry->volpan <= 114) {
1247 /* Portamento down */
1248 unsigned char v = (entry->volpan - 105) << 2;
1249 if (v == 0)
1250 v = channel->lastEF;
1251 channel->lastEF = v;
1252 channel->portamento -= v << 4;
1253 } else if (entry->volpan <= 124) {
1254 /* Portamento up */
1255 unsigned char v = (entry->volpan - 115) << 2;
1256 if (v == 0)
1257 v = channel->lastEF;
1258 channel->lastEF = v;
1259 channel->portamento += v << 4;
1260 } else if (entry->volpan <= 202) {
1261 /* Pan */
1262 /* Tone Portamento */
1263 } else if (entry->volpan <= 212) {
1264 /* Vibrato */
1265 unsigned char v = entry->volpan - 203;
1266 if (v == 0)
1267 v = channel->lastHdepth;
1268 else {
1269 v <<= 2;
1270 channel->lastHdepth = v;
1271 }
1272 if (channel->playing) {
1273 channel->playing->vibrato_speed = channel->lastHspeed;
1274 channel->playing->vibrato_depth = v;
1275 channel->playing->vibrato_n++;
1276 }
1277 }
1278 }
1279}
1280
1281
1282
1283static void it_send_midi(DUMB_IT_SIGRENDERER *sigrenderer, IT_CHANNEL *channel, unsigned char byte)
1284{
1285 if (sigrenderer->callbacks->midi)
1286 if ((*sigrenderer->callbacks->midi)(sigrenderer->callbacks->midi_data, channel - sigrenderer->channel, byte))
1287 return;
1288
1289 switch (channel->midi_state) {
1290 case 4: /* Ready to receive resonance parameter */
1291 if (byte < 0x80) channel->filter_resonance = byte;
1292 channel->midi_state = 0;
1293 break;
1294 case 3: /* Ready to receive cutoff parameter */
1295 if (byte < 0x80) channel->filter_cutoff = byte;
1296 channel->midi_state = 0;
1297 break;
1298 case 2: /* Ready for byte specifying which parameter will follow */
1299 if (byte == 0) /* Cutoff */
1300 channel->midi_state = 3;
1301 else if (byte == 1) /* Resonance */
1302 channel->midi_state = 4;
1303 else
1304 channel->midi_state = 0;
1305 break;
1306 default: /* Counting initial F0 bytes */
1307 switch (byte) {
1308 case 0xF0:
1309 channel->midi_state++;
1310 break;
1311 case 0xFA:
1312 case 0xFC:
1313 case 0xFF:
1314 /* Reset filter parameters for all channels */
1315 {
1316 int i;
1317 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
1318 sigrenderer->channel[i].filter_cutoff = 127;
1319 sigrenderer->channel[i].filter_resonance = 0;
1320 //// should we be resetting channel[i].playing->filter_* here?
1321 }
1322 }
1323 /* Fall through */
1324 default:
1325 channel->midi_state = 0;
1326 break;
1327 }
1328 }
1329}
1330
1331
1332
1333/* Returns 1 if a callback caused termination of playback. */
1334static int process_effects(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
1335{
1336 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
1337
1338 IT_CHANNEL *channel = &sigrenderer->channel[(int)entry->channel];
1339
1340 if (entry->mask & IT_ENTRY_EFFECT) {
1341 switch (entry->effect) {
1342/*
1343Notes about effects (as compared to other module formats)
1344
1345C This is now in *HEX*. (Used to be in decimal in ST3)
1346E/F/G/H/U You need to check whether the song uses Amiga/Linear slides.
1347H/U Vibrato in Impulse Tracker is two times finer than in
1348 any other tracker and is updated EVERY tick.
1349 If "Old Effects" is *ON*, then the vibrato is played in the
1350 normal manner (every non-row tick and normal depth)
1351E/F/G These commands ALL share the same memory.
1352Oxx Offsets to samples are to the 'xx00th' SAMPLE. (ie. for
1353 16 bit samples, the offset is xx00h*2)
1354 Oxx past the sample end will be ignored, unless "Old Effects"
1355 is ON, in which case the Oxx will play from the end of the
1356 sample.
1357Yxy This uses a table 4 times larger (hence 4 times slower) than
1358 vibrato or tremelo. If the waveform is set to random, then
1359 the 'speed' part of the command is interpreted as a delay.
1360*/
1361 case IT_SET_SPEED:
1362 if (entry->effectvalue)
1363 sigrenderer->tick = sigrenderer->speed = entry->effectvalue;
1364 else if (sigdata->flags & IT_WAS_AN_XM) {
1365 sigrenderer->speed = 0;
1366 if (sigrenderer->callbacks->xm_speed_zero && (*sigrenderer->callbacks->xm_speed_zero)(sigrenderer->callbacks->xm_speed_zero_data))
1367 return 1;
1368 }
1369 break;
1370
1371 case IT_JUMP_TO_ORDER: sigrenderer->processorder = entry->effectvalue - 1; sigrenderer->processrow = 0xFFFE; break;
1372 case IT_BREAK_TO_ROW: sigrenderer->breakrow = entry->effectvalue; sigrenderer->processrow = 0xFFFE; break;
1373
1374 case IT_VOLSLIDE_VIBRATO:
1375 if (channel->playing) {
1376 channel->playing->vibrato_speed = channel->lastHspeed;
1377 channel->playing->vibrato_depth = channel->lastHdepth;
1378 channel->playing->vibrato_n++;
1379 }
1380 /* Fall through and process volume slide. */
1381 case IT_VOLUME_SLIDE:
1382 case IT_VOLSLIDE_TONEPORTA:
1383 /* The tone portamento component is handled elsewhere. */
1384 {
1385 unsigned char v = entry->effectvalue;
1386 if (!(sigdata->flags & IT_WAS_A_MOD)) {
1387 if (v == 0)
1388 v = channel->lastDKL;
1389 channel->lastDKL = v;
1390 }
1391 if ((v & 0x0F) == 0) { /* Dx0 */
1392 channel->volslide = v >> 4;
1393 if (channel->volslide == 15 && !(sigdata->flags & IT_WAS_AN_XM)) {
1394 channel->volume += 15;
1395 if (channel->volume > 64) channel->volume = 64;
1396 }
1397 } else if ((v & 0xF0) == 0) { /* D0x */
1398 channel->volslide = -v;
1399 if (channel->volslide == -15 && !(sigdata->flags & IT_WAS_AN_XM)) {
1400 channel->volume -= 15;
1401 if (channel->volume > 64) channel->volume = 0;
1402 }
1403 } else if ((v & 0x0F) == 0x0F) { /* DxF */
1404 channel->volume += v >> 4;
1405 if (channel->volume > 64) channel->volume = 64;
1406 } else if ((v & 0xF0) == 0xF0) { /* DFx */
1407 channel->volume -= v & 15;
1408 if (channel->volume > 64) channel->volume = 0;
1409 }
1410 }
1411 break;
1412 case IT_XM_FINE_VOLSLIDE_DOWN:
1413 {
1414 unsigned char v = entry->effectvalue;
1415 if (v == 0)
1416 v = channel->xm_lastEB;
1417 channel->xm_lastEB = v;
1418 channel->volume -= v;
1419 if (channel->volume > 64) channel->volume = 0;
1420 }
1421 break;
1422 case IT_XM_FINE_VOLSLIDE_UP:
1423 {
1424 unsigned char v = entry->effectvalue;
1425 if (v == 0)
1426 v = channel->xm_lastEA;
1427 channel->xm_lastEA = v;
1428 channel->volume += v;
1429 if (channel->volume > 64) channel->volume = 64;
1430 }
1431 break;
1432 case IT_PORTAMENTO_DOWN:
1433 {
1434 unsigned char v = entry->effectvalue;
1435 if (sigdata->flags & IT_WAS_AN_XM) {
1436 if (!(sigdata->flags & IT_WAS_A_MOD)) {
1437 if (v == 0xF0)
1438 v |= channel->xm_lastE2;
1439 else if (v >= 0xF0)
1440 channel->xm_lastE2 = v & 15;
1441 else if (v == 0xE0)
1442 v |= channel->xm_lastX2;
1443 else
1444 channel->xm_lastX2 = v & 15;
1445 }
1446 } else {
1447 if (v == 0)
1448 v = channel->lastEF;
1449 channel->lastEF = v;
1450 }
1451 if (channel->playing) {
1452 if ((v & 0xF0) == 0xF0)
1453 channel->playing->slide -= (v & 15) << 4;
1454 else if ((v & 0xF0) == 0xE0)
1455 channel->playing->slide -= (v & 15) << 2;
1456 else
1457 channel->portamento -= v << 4;
1458 }
1459 }
1460 break;
1461 case IT_PORTAMENTO_UP:
1462 {
1463 unsigned char v = entry->effectvalue;
1464 if (sigdata->flags & IT_WAS_AN_XM) {
1465 if (!(sigdata->flags & IT_WAS_A_MOD)) {
1466 if (v == 0xF0)
1467 v |= channel->xm_lastE1;
1468 else if (v >= 0xF0)
1469 channel->xm_lastE1 = v & 15;
1470 else if (v == 0xE0)
1471 v |= channel->xm_lastX1;
1472 else
1473 channel->xm_lastX1 = v & 15;
1474 }
1475 } else {
1476 if (v == 0)
1477 v = channel->lastEF;
1478 channel->lastEF = v;
1479 }
1480 if (channel->playing) {
1481 if ((v & 0xF0) == 0xF0)
1482 channel->playing->slide += (v & 15) << 4;
1483 else if ((v & 0xF0) == 0xE0)
1484 channel->playing->slide += (v & 15) << 2;
1485 else
1486 channel->portamento += v << 4;
1487 }
1488 }
1489 break;
1490 case IT_XM_PORTAMENTO_DOWN:
1491 {
1492 unsigned char v = entry->effectvalue;
1493 if (!(sigdata->flags & IT_WAS_A_MOD)) {
1494 if (v == 0)
1495 v = channel->lastJ;
1496 channel->lastJ = v;
1497 }
1498 if (channel->playing)
1499 channel->portamento -= v << 4;
1500 }
1501 break;
1502 case IT_XM_PORTAMENTO_UP:
1503 {
1504 unsigned char v = entry->effectvalue;
1505 if (!(sigdata->flags & IT_WAS_A_MOD)) {
1506 if (v == 0)
1507 v = channel->lastEF;
1508 channel->lastEF = v;
1509 }
1510 if (channel->playing)
1511 channel->portamento += v << 4;
1512 }
1513 break;
1514 case IT_VIBRATO:
1515 {
1516 unsigned char speed = entry->effectvalue >> 4;
1517 unsigned char depth = entry->effectvalue & 15;
1518 if (speed == 0)
1519 speed = channel->lastHspeed;
1520 channel->lastHspeed = speed;
1521 if (depth == 0)
1522 depth = channel->lastHdepth;
1523 else {
1524 if (sigdata->flags & IT_OLD_EFFECTS)
1525 depth <<= 3;
1526 else
1527 depth <<= 2;
1528 channel->lastHdepth = depth;
1529 }
1530 if (channel->playing) {
1531 channel->playing->vibrato_speed = speed;
1532 channel->playing->vibrato_depth = depth;
1533 channel->playing->vibrato_n++;
1534 }
1535 }
1536 break;
1537 case IT_TREMOR:
1538 {
1539 unsigned char v = entry->effectvalue;
1540 if (v == 0)
1541 v = channel->lastI;
1542 else if (!(sigdata->flags & IT_OLD_EFFECTS)) {
1543 if (v & 0xF0) v -= 0x10;
1544 if (v & 0x0F) v -= 0x01;
1545 }
1546 channel->lastI = v;
1547 channel->tremor_time |= 128;
1548 }
1549 update_tremor(channel);
1550 break;
1551 case IT_ARPEGGIO:
1552 {
1553 unsigned char v = entry->effectvalue;
1554 /* XM files have no memory for arpeggio (000 = no effect)
1555 * and we use lastJ for portamento down instead.
1556 */
1557 if (!(sigdata->flags & IT_WAS_AN_XM)) {
1558 if (v == 0)
1559 v = channel->lastJ;
1560 channel->lastJ = v;
1561 }
1562 channel->arpeggio = v;
1563 }
1564 break;
1565 case IT_SET_CHANNEL_VOLUME:
1566 if (sigdata->flags & IT_WAS_AN_XM)
1567 channel->volume = MIN(entry->effectvalue, 64);
1568 else if (entry->effectvalue <= 64)
1569 channel->channelvolume = entry->effectvalue;
1570#ifdef VOLUME_OUT_OF_RANGE_SETS_MAXIMUM
1571 else
1572 channel->channelvolume = 64;
1573#endif
1574 if (channel->playing)
1575 channel->playing->channel_volume = channel->channelvolume;
1576 break;
1577 case IT_CHANNEL_VOLUME_SLIDE:
1578 {
1579 unsigned char v = entry->effectvalue;
1580 if (v == 0)
1581 v = channel->lastN;
1582 channel->lastN = v;
1583 if ((v & 0x0F) == 0) { /* Nx0 */
1584 channel->channelvolslide = v >> 4;
1585 } else if ((v & 0xF0) == 0) { /* N0x */
1586 channel->channelvolslide = -v;
1587 } else {
1588 if ((v & 0x0F) == 0x0F) { /* NxF */
1589 channel->channelvolume += v >> 4;
1590 if (channel->channelvolume > 64) channel->channelvolume = 64;
1591 } else if ((v & 0xF0) == 0xF0) { /* NFx */
1592 channel->channelvolume -= v & 15;
1593 if (channel->channelvolume > 64) channel->channelvolume = 0;
1594 } else
1595 break;
1596 if (channel->playing)
1597 channel->playing->channel_volume = channel->channelvolume;
1598 }
1599 }
1600 break;
1601 case IT_SET_SAMPLE_OFFSET:
1602 {
1603 unsigned char v = entry->effectvalue;
1604 if (sigdata->flags & IT_WAS_A_MOD) {
1605 if (v == 0) break;
1606 } else {
1607 if (v == 0)
1608 v = channel->lastO;
1609 channel->lastO = v;
1610 }
1611 /* Note: we set the offset even if tone portamento is
1612 * specified. Impulse Tracker does the same.
1613 */
1614 if (entry->mask & IT_ENTRY_NOTE) {
1615 if (channel->playing) {
1616 int offset = ((int)channel->high_offset << 16) | ((int)v << 8);
1617 IT_PLAYING *playing = channel->playing;
1618 IT_SAMPLE *sample = playing->sample;
1619 int end;
1620 if ((sample->flags & IT_SAMPLE_SUS_LOOP) && !(playing->flags & IT_PLAYING_SUSTAINOFF))
1621 end = sample->sus_loop_end;
1622 else if (sample->flags & IT_SAMPLE_LOOP)
1623 end = sample->loop_end;
1624 else
1625 end = sample->length;
1626 if (offset < end)
1627 it_playing_reset_resamplers(playing, offset);
1628 else if (sigdata->flags & IT_OLD_EFFECTS)
1629 it_playing_reset_resamplers(playing, end);
1630 }
1631 }
1632 }
1633 break;
1634 //case IT_PANNING_SLIDE:
1635 /** JULIEN: guess what? the docs are wrong! (how unusual ;)
1636 * Pxy seems to memorize its previous value... and there
1637 * might be other mistakes like that... (sigh!)
1638 */
1639 /** ENTHEH: umm... but... the docs say that Pxy memorises its
1640 * value... don't they? :o
1641 */
1642 case IT_RETRIGGER_NOTE:
1643 {
1644 unsigned char v = entry->effectvalue;
1645 if (sigdata->flags & IT_WAS_AN_XM) {
1646 if ((v & 0x0F) == 0) v |= channel->lastQ & 0x0F;
1647 if ((v & 0xF0) == 0) v |= channel->lastQ & 0xF0;
1648 } else {
1649 if (v == 0)
1650 v = channel->lastQ;
1651 }
1652 channel->lastQ = v;
1653 if ((v & 0x0F) == 0) v |= 0x01;
1654 channel->retrig = v;
1655 if (entry->mask & IT_ENTRY_NOTE) {
1656 channel->retrig_tick = v & 0x0F;
1657 /* Emulate a bug */
1658 if (sigdata->flags & IT_WAS_AN_XM)
1659 update_retrig(channel);
1660 } else
1661 update_retrig(channel);
1662 }
1663 break;
1664 case IT_XM_RETRIGGER_NOTE:
1665 channel->retrig_tick = channel->xm_retrig = entry->effectvalue;
1666 if (entry->effectvalue == 0)
1667 if (channel->playing) it_playing_reset_resamplers(channel->playing, 0);
1668 break;
1669 case IT_TREMOLO:
1670 {
1671 unsigned char speed = entry->effectvalue >> 4;
1672 unsigned char depth = entry->effectvalue & 15;
1673 if (speed == 0)
1674 speed = channel->lastRspeed;
1675 channel->lastRspeed = speed;
1676 if (depth == 0)
1677 depth = channel->lastRdepth;
1678 channel->lastRdepth = depth;
1679 if (channel->playing) {
1680 channel->playing->tremolo_speed = speed;
1681 channel->playing->tremolo_depth = depth;
1682 }
1683 }
1684 break;
1685 case IT_S:
1686 {
1687 /* channel->lastS was set in update_pattern_variables(). */
1688 unsigned char effectvalue = channel->lastS;
1689 switch (effectvalue >> 4) {
1690 //case IT_S_SET_FILTER:
1691 //case IT_S_SET_GLISSANDO_CONTROL:
1692 //case IT_S_FINETUNE:
1693 //case IT_S_SET_VIBRATO_WAVEFORM:
1694 //case IT_S_SET_TREMOLO_WAVEFORM:
1695 //case IT_S_SET_PANBRELLO_WAVEFORM:
1696 /* Waveforms for commands S3x, S4x and S5x:
1697 * 0: Sine wave
1698 * 1: Ramp down
1699 * 2: Square wave
1700 * 3: Random wave
1701 */
1702 case IT_S_FINE_PATTERN_DELAY:
1703 sigrenderer->tick += effectvalue & 15;
1704 break;
1705 //case IT_S7:
1706 case IT_S_SET_PAN:
1707 channel->pan =
1708 ((effectvalue & 15) << 2) |
1709 ((effectvalue & 15) >> 2);
1710 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
1711 break;
1712 case IT_S_SET_SURROUND_SOUND:
1713 if ((effectvalue & 15) == 1)
1714 channel->pan = IT_SURROUND;
1715 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
1716 break;
1717 case IT_S_SET_HIGH_OFFSET:
1718 channel->high_offset = effectvalue & 15;
1719 break;
1720 //case IT_S_PATTERN_LOOP:
1721 case IT_S_DELAYED_NOTE_CUT:
1722 channel->note_cut_count = effectvalue & 15;
1723 if (!channel->note_cut_count) {
1724 if (sigdata->flags & IT_WAS_AN_XM)
1725 channel->volume = 0;
1726 else
1727 channel->note_cut_count = 1;
1728 }
1729 break;
1730 case IT_S_SET_MIDI_MACRO:
1731 channel->SFmacro = effectvalue & 15;
1732 break;
1733 }
1734 }
1735 break;
1736 case IT_SET_SONG_TEMPO:
1737 {
1738 unsigned char v = entry->effectvalue;
1739 if (v == 0)
1740 v = channel->lastW;
1741 channel->lastW = v;
1742 if (v < 0x10)
1743 sigrenderer->temposlide = -v;
1744 else if (v < 0x20)
1745 sigrenderer->temposlide = v & 15;
1746 else
1747 sigrenderer->tempo = v;
1748 }
1749 break;
1750 case IT_FINE_VIBRATO:
1751 {
1752 unsigned char speed = entry->effectvalue >> 4;
1753 unsigned char depth = entry->effectvalue & 15;
1754 if (speed == 0)
1755 speed = channel->lastHspeed;
1756 channel->lastHspeed = speed;
1757 if (depth == 0)
1758 depth = channel->lastHdepth;
1759 else {
1760 if (sigdata->flags & IT_OLD_EFFECTS)
1761 depth <<= 1;
1762 channel->lastHdepth = depth;
1763 }
1764 if (channel->playing) {
1765 channel->playing->vibrato_speed = speed;
1766 channel->playing->vibrato_depth = depth;
1767 channel->playing->vibrato_n++;
1768 }
1769 }
1770 break;
1771 case IT_SET_GLOBAL_VOLUME:
1772 if (entry->effectvalue <= 128)
1773 sigrenderer->globalvolume = entry->effectvalue;
1774#ifdef VOLUME_OUT_OF_RANGE_SETS_MAXIMUM
1775 else
1776 sigrenderer->globalvolume = 128;
1777#endif
1778 break;
1779 case IT_GLOBAL_VOLUME_SLIDE:
1780 {
1781 unsigned char v = entry->effectvalue;
1782 if (v == 0)
1783 v = channel->lastW;
1784 channel->lastW = v;
1785 if ((v & 0x0F) == 0) { /* Wx0 */
1786 sigrenderer->globalvolslide =
1787 (sigdata->flags & IT_WAS_AN_XM) ? (v >> 4)*2 : (v >> 4);
1788 } else if ((v & 0xF0) == 0) { /* W0x */
1789 sigrenderer->globalvolslide =
1790 (sigdata->flags & IT_WAS_AN_XM) ? (-v)*2 : (-v);
1791 } else if ((v & 0x0F) == 0x0F) { /* WxF */
1792 sigrenderer->globalvolume += v >> 4;
1793 if (sigrenderer->globalvolume > 128) sigrenderer->globalvolume = 128;
1794 } else if ((v & 0xF0) == 0xF0) { /* WFx */
1795 sigrenderer->globalvolume -= v & 15;
1796 if (sigrenderer->globalvolume > 128) sigrenderer->globalvolume = 0;
1797 }
1798 }
1799 break;
1800 case IT_SET_PANNING:
1801 channel->pan = (entry->effectvalue + 2) >> 2;
1802 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
1803 break;
1804 //case IT_PANBRELLO:
1805 case IT_MIDI_MACRO:
1806 {
1807 IT_MIDI *midi = sigdata->midi ? sigdata->midi : &default_midi;
1808 if (entry->effectvalue >= 0x80) {
1809 int n = midi->Zmacrolen[entry->effectvalue-0x80];
1810 int i;
1811 for (i = 0; i < n; i++)
1812 it_send_midi(sigrenderer, channel, midi->Zmacro[entry->effectvalue-0x80][i]);
1813 } else {
1814 int n = midi->SFmacrolen[channel->SFmacro];
1815 int i, j;
1816 for (i = 0, j = 1; i < n; i++, j <<= 1)
1817 it_send_midi(sigrenderer, channel,
1818 midi->SFmacroz[channel->SFmacro] & j ?
1819 entry->effectvalue : midi->SFmacro[channel->SFmacro][i]);
1820 }
1821 }
1822 break;
1823 }
1824 }
1825
1826 if (!(sigdata->flags & IT_WAS_AN_XM))
1827 post_process_it_volpan(sigrenderer, entry);
1828
1829 return 0;
1830}
1831
1832
1833
1834static int process_it_note_data(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
1835{
1836 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
1837 IT_CHANNEL *channel = &sigrenderer->channel[(int)entry->channel];
1838
1839 // When tone portamento and instrument are specified:
1840 // If Gxx is off:
1841 // - same sample, do nothing but portamento
1842 // - diff sample, retrigger all but keep current note+slide + do porta
1843 // - if instrument is invalid, nothing; if sample is invalid, cut
1844 // If Gxx is on:
1845 // - same sample or new sample invalid, retrigger envelopes
1846 // - diff sample/inst, start using new envelopes
1847 // When tone portamento is specified alone, sample won't change.
1848 // TODO: consider what happens with instrument alone after all this...
1849
1850 if (entry->mask & (IT_ENTRY_NOTE | IT_ENTRY_INSTRUMENT)) {
1851 if (entry->mask & IT_ENTRY_INSTRUMENT)
1852 channel->instrument = entry->instrument;
1853 instrument_to_sample(sigdata, channel);
1854 if (channel->note < 120) {
1855 if ((sigdata->flags & IT_USE_INSTRUMENTS) && channel->sample == 0)
1856 return 1;
1857 if (entry->mask & IT_ENTRY_INSTRUMENT)
1858 get_default_volpan(sigdata, channel);
1859 } else
1860 it_retrigger_note(sigrenderer, channel);
1861 }
1862
1863 /** WARNING: This is not ideal, since channel->playing might not get allocated owing to lack of memory... */
1864 if (channel->playing &&
1865 (((entry->mask & IT_ENTRY_VOLPAN) && entry->volpan >= 193 && entry->volpan <= 202) ||
1866 ((entry->mask & IT_ENTRY_EFFECT) && (entry->effect == IT_TONE_PORTAMENTO || entry->effect == IT_VOLSLIDE_TONEPORTA))))
1867 {
1868 if (entry->mask & IT_ENTRY_INSTRUMENT) {
1869 if (sigdata->flags & IT_COMPATIBLE_GXX)
1870 retrigger_it_envelopes(sigdata, channel);
1871 else if ((!(sigdata->flags & IT_USE_INSTRUMENTS) ||
1872 (channel->instrument >= 1 && channel->instrument <= sigdata->n_instruments)) &&
1873 channel->sample != channel->playing->sampnum)
1874 {
1875 unsigned char note = channel->playing->note;
1876 int slide = channel->playing->slide;
1877 it_retrigger_note(sigrenderer, channel);
1878 if (channel->playing) {
1879 channel->playing->note = note;
1880 channel->playing->slide = slide;
1881 // Should we be preserving sample_vibrato_time? depth?
1882 }
1883 }
1884 }
1885
1886 if ((entry->mask & IT_ENTRY_VOLPAN) && entry->volpan >= 193 && entry->volpan <= 202) {
1887 /* Tone Portamento in the volume column */
1888 static const unsigned char slidetable[] = {0, 1, 4, 8, 16, 32, 64, 96, 128, 255};
1889 unsigned char v = slidetable[entry->volpan - 193];
1890 if (sigdata->flags & IT_COMPATIBLE_GXX) {
1891 if (v == 0)
1892 v = channel->lastG;
1893 channel->lastG = v;
1894 } else {
1895 if (v == 0)
1896 v = channel->lastEF;
1897 channel->lastEF = v;
1898 }
1899 if (entry->mask & IT_ENTRY_NOTE)
1900 if (channel->sample)
1901 channel->destnote = channel->truenote;
1902 channel->toneporta = v << 4;
1903 } else {
1904 /* Tone Portamento in the effect column */
1905 unsigned char v;
1906 if (entry->effect == IT_TONE_PORTAMENTO)
1907 v = entry->effectvalue;
1908 else
1909 v = 0;
1910 if (sigdata->flags & IT_COMPATIBLE_GXX) {
1911 if (v == 0)
1912 v = channel->lastG;
1913 channel->lastG = v;
1914 } else {
1915 if (v == 0)
1916 v = channel->lastEF;
1917 channel->lastEF = v;
1918 }
1919 if (entry->mask & IT_ENTRY_NOTE)
1920 if (channel->sample)
1921 channel->destnote = channel->truenote;
1922 channel->toneporta = v << 4;
1923 }
1924 } else if ((entry->mask & IT_ENTRY_NOTE) ||
1925 ((entry->mask & IT_ENTRY_INSTRUMENT) && (!channel->playing || entry->instrument != channel->playing->instnum)))
1926 {
1927 if (channel->note < 120) {
1928 get_true_pan(sigdata, channel);
1929 it_retrigger_note(sigrenderer, channel);
1930 }
1931 }
1932
1933 if (entry->mask & IT_ENTRY_VOLPAN) {
1934 if (entry->volpan <= 64) {
1935 /* Volume */
1936 channel->volume = entry->volpan;
1937 } else if (entry->volpan <= 74) {
1938 /* Fine volume slide up */
1939 unsigned char v = entry->volpan - 65;
1940 if (v == 0)
1941 v = channel->lastvolslide;
1942 channel->lastvolslide = v;
1943 /* = effect DxF where x == entry->volpan - 65 */
1944 channel->volume += v;
1945 if (channel->volume > 64) channel->volume = 64;
1946 } else if (entry->volpan <= 84) {
1947 /* Fine volume slide down */
1948 unsigned char v = entry->volpan - 75;
1949 if (v == 0)
1950 v = channel->lastvolslide;
1951 channel->lastvolslide = v;
1952 /* = effect DFx where x == entry->volpan - 75 */
1953 channel->volume -= v;
1954 if (channel->volume > 64) channel->volume = 0;
1955 } else if (entry->volpan < 128) {
1956 /* Volume slide up */
1957 /* Volume slide down */
1958 /* Portamento down */
1959 /* Portamento up */
1960 } else if (entry->volpan <= 192) {
1961 /* Pan */
1962 channel->pan = entry->volpan - 128;
1963 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
1964 }
1965 /* else */
1966 /* Tone Portamento */
1967 /* Vibrato */
1968 }
1969 return 0;
1970}
1971
1972
1973
1974static void retrigger_xm_envelopes(IT_PLAYING *playing)
1975{
1976 playing->volume_envelope.next_node = 0;
1977 playing->volume_envelope.tick = -1;
1978 playing->pan_envelope.next_node = 0;
1979 playing->pan_envelope.tick = -1;
1980 playing->fadeoutcount = 1024;
1981}
1982
1983
1984
1985static void process_xm_note_data(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
1986{
1987 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
1988 IT_CHANNEL *channel = &sigrenderer->channel[(int)entry->channel];
1989
1990 if (entry->mask & IT_ENTRY_INSTRUMENT) {
1991 channel->instrument = entry->instrument;
1992 instrument_to_sample(sigdata, channel);
1993 if (channel->playing) {
1994 /* Retrigger vol/pan envelopes if enabled, and cancel fadeout.
1995 * Also reset vol/pan to that of _original_ instrument.
1996 */
1997 channel->playing->flags &= ~(IT_PLAYING_SUSTAINOFF | IT_PLAYING_FADING);
1998 it_playing_update_resamplers(channel->playing);
1999
2000 channel->volume = channel->playing->sample->default_volume;
2001 if (channel->pan >= 128 && channel->pan <= 192)
2002 channel->pan = channel->playing->sample->default_pan - 128;
2003
2004 retrigger_xm_envelopes(channel->playing);
2005 }
2006 }
2007
2008 if (entry->mask & IT_ENTRY_NOTE) {
2009 if (!(entry->mask & IT_ENTRY_INSTRUMENT))
2010 instrument_to_sample(sigdata, channel);
2011
2012 if (channel->note >= 120) {
2013 if (channel->playing) {
2014 if (!(sigdata->instrument[channel->instrument-1].volume_envelope.flags & IT_ENVELOPE_ON))
2015 if (!(entry->mask & IT_ENTRY_INSTRUMENT))
2016 channel->volume = 0;
2017 channel->playing->flags |= IT_PLAYING_SUSTAINOFF | IT_PLAYING_FADING;
2018 it_playing_update_resamplers(channel->playing);
2019 }
2020 } else if (channel->sample == 0) {
2021 /** If we get here, one of the following is the case:
2022 ** 1. The instrument has never been specified on this channel.
2023 ** 2. The specified instrument is invalid.
2024 ** 3. The instrument has no sample mapped to the selected note.
2025 ** What should happen?
2026 **
2027 ** Experimentation shows that any existing note stops and cannot
2028 ** be brought back. A subsequent instrument change fixes that.
2029 **/
2030 if (channel->playing) {
2031 free(channel->playing);
2032 channel->playing = NULL;
2033 }
2034 return;
2035 } else if (channel->playing && (entry->mask & IT_ENTRY_VOLPAN) && ((entry->volpan>>4) == 0xF)) {
2036 /* Don't retrigger note; portamento in the volume column. */
2037 } else if (channel->playing &&
2038 (entry->mask & IT_ENTRY_EFFECT) &&
2039 (entry->effect == IT_TONE_PORTAMENTO ||
2040 entry->effect == IT_VOLSLIDE_TONEPORTA)) {
2041 /* Don't retrigger note; portamento in the effects column. */
2042 } else {
2043 channel->destnote = IT_NOTE_OFF;
2044
2045 if (!channel->playing) {
2046 channel->playing = malloc(sizeof(*channel->playing));
2047 if (!channel->playing)
2048 return;
2049 // Adding the following seems to do the trick for the case where a piece starts with an instrument alone and then some notes alone.
2050 retrigger_xm_envelopes(channel->playing);
2051 }
2052
2053 channel->playing->flags = 0;
2054 channel->playing->channel = channel;
2055 channel->playing->sample = &sigdata->sample[channel->sample-1];
2056 if (sigdata->flags & IT_USE_INSTRUMENTS)
2057 channel->playing->instrument = &sigdata->instrument[channel->instrument-1];
2058 else
2059 channel->playing->instrument = NULL;
2060 channel->playing->env_instrument = channel->playing->instrument;
2061 channel->playing->sampnum = channel->sample;
2062 channel->playing->instnum = channel->instrument;
2063 channel->playing->channel_volume = channel->channelvolume;
2064 channel->playing->note = channel->truenote;
2065 channel->playing->filter_cutoff = 127;
2066 channel->playing->filter_resonance = 0;
2067 channel->playing->true_filter_cutoff = 127 << 8;
2068 channel->playing->true_filter_resonance = 0;
2069 channel->playing->vibrato_speed = 0;
2070 channel->playing->vibrato_depth = 0;
2071 channel->playing->vibrato_n = 0;
2072 channel->playing->vibrato_time = 0;
2073 channel->playing->tremolo_speed = 0;
2074 channel->playing->tremolo_depth = 0;
2075 channel->playing->tremolo_time = 0;
2076 channel->playing->sample_vibrato_time = 0;
2077 channel->playing->sample_vibrato_depth = 0;
2078 channel->playing->slide = 0;
2079 it_reset_filter_state(&channel->playing->filter_state[0]); // Are these
2080 it_reset_filter_state(&channel->playing->filter_state[1]); // necessary?
2081 it_playing_reset_resamplers(channel->playing, 0);
2082
2083 /** WARNING - is everything initialised? */
2084 }
2085 }
2086
2087 if ((entry->mask & (IT_ENTRY_NOTE | IT_ENTRY_INSTRUMENT)) == (IT_ENTRY_NOTE | IT_ENTRY_INSTRUMENT)) {
2088 if (channel->playing) retrigger_xm_envelopes(channel->playing);
2089 get_default_volpan(sigdata, channel);
2090 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
2091 }
2092
2093 if ((entry->mask & IT_ENTRY_VOLPAN) && ((entry->volpan>>4) == 0xF)) {
2094 /* Tone Portamento */
2095 unsigned char v = (entry->volpan & 15) << 4;
2096 if (v == 0)
2097 v = channel->lastG;
2098 channel->lastG = v;
2099 if (entry->mask & IT_ENTRY_NOTE)
2100 if (channel->sample)
2101 channel->destnote = channel->truenote;
2102 channel->toneporta = v << 4;
2103 } else if ((entry->mask & IT_ENTRY_EFFECT) &&
2104 (entry->effect == IT_TONE_PORTAMENTO ||
2105 entry->effect == IT_VOLSLIDE_TONEPORTA)) {
2106 unsigned char v;
2107 if (entry->effect == IT_TONE_PORTAMENTO)
2108 v = entry->effectvalue;
2109 else
2110 v = 0;
2111 if (v == 0)
2112 v = channel->lastG;
2113 channel->lastG = v;
2114 if (entry->mask & IT_ENTRY_NOTE)
2115 if (channel->sample)
2116 channel->destnote = channel->truenote;
2117 channel->toneporta = v << 4;
2118 }
2119
2120 if (entry->mask & IT_ENTRY_VOLPAN) {
2121 int effect = entry->volpan >> 4;
2122 int value = entry->volpan & 15;
2123 switch (effect) {
2124 case 0x6: /* Volume slide down */
2125 channel->xm_volslide = -value;
2126 break;
2127 case 0x7: /* Volume slide up */
2128 channel->xm_volslide = value;
2129 break;
2130 case 0x8: /* Fine volume slide down */
2131 channel->volume -= value;
2132 if (channel->volume > 64) channel->volume = 0;
2133 break;
2134 case 0x9: /* Fine volume slide up */
2135 channel->volume += value;
2136 if (channel->volume > 64) channel->volume = 64;
2137 break;
2138 case 0xA: /* Set vibrato speed */
2139 if (value)
2140 channel->lastHspeed = value;
2141 if (channel->playing)
2142 channel->playing->vibrato_speed = channel->lastHspeed;
2143 break;
2144 case 0xB: /* Vibrato */
2145 if (value)
2146 channel->lastHdepth = value << 2; /** WARNING: correct ? */
2147 if (channel->playing) {
2148 channel->playing->vibrato_depth = channel->lastHdepth;
2149 channel->playing->vibrato_speed = channel->lastHspeed;
2150 channel->playing->vibrato_n++;
2151 }
2152 break;
2153 case 0xC: /* Set panning */
2154 channel->pan = (value*64)/15;
2155 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
2156 break;
2157 case 0xD: /* Pan slide left */
2158 // TODO
2159 // channel->xm_panslide = -value;
2160 break;
2161 case 0xE: /* Pan slide Right */
2162 // TODO
2163 // channel->xm_panslide = value;
2164 break;
2165 case 0xF: /* Tone porta */
2166 break;
2167 default: /* Volume */
2168 channel->volume = entry->volpan - 0x10;
2169 break;
2170 }
2171 }
2172}
2173
2174
2175
2176/* This function assumes !IT_IS_END_ROW(entry). */
2177static int process_note_data(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
2178{
2179 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
2180
2181 if (sigdata->flags & IT_WAS_AN_XM)
2182 process_xm_note_data(sigrenderer, entry);
2183 else
2184 if (process_it_note_data(sigrenderer, entry)) return 0;
2185
2186 return process_effects(sigrenderer, entry);
2187}
2188
2189
2190
2191static int process_entry(DUMB_IT_SIGRENDERER *sigrenderer, IT_ENTRY *entry)
2192{
2193 IT_CHANNEL *channel = &sigrenderer->channel[(int)entry->channel];
2194
2195 if (entry->mask & IT_ENTRY_NOTE)
2196 channel->note = entry->note;
2197
2198 if ((entry->mask & IT_ENTRY_EFFECT) && entry->effect == IT_S) {
2199 /* channel->lastS was set in update_pattern_variables(). */
2200 unsigned char effectvalue = channel->lastS;
2201 if (effectvalue >> 4 == IT_S_NOTE_DELAY) {
2202 channel->note_delay_count = effectvalue & 15;
2203 if (channel->note_delay_count == 0)
2204 channel->note_delay_count = 1;
2205 channel->note_delay_entry = entry;
2206 return 0;
2207 }
2208 }
2209
2210 return process_note_data(sigrenderer, entry);
2211}
2212
2213
2214
2215static void update_tick_counts(DUMB_IT_SIGRENDERER *sigrenderer)
2216{
2217 int i;
2218
2219 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
2220 IT_CHANNEL *channel = &sigrenderer->channel[i];
2221
2222 if (channel->note_cut_count) {
2223 channel->note_cut_count--;
2224 if (channel->note_cut_count == 0) {
2225 if (sigrenderer->sigdata->flags & IT_WAS_AN_XM)
2226 channel->volume = 0;
2227 else if (channel->playing) {
2228 free(channel->playing);
2229 channel->playing = NULL;
2230 }
2231 }
2232 } else if (channel->note_delay_count) {
2233 channel->note_delay_count--;
2234 if (channel->note_delay_count == 0)
2235 process_note_data(sigrenderer, channel->note_delay_entry);
2236 /* Don't bother checking the return value; if the note
2237 * was delayed, there can't have been a speed=0.
2238 */
2239 }
2240 }
2241}
2242
2243
2244
2245static int envelope_get_y(IT_ENVELOPE *envelope, IT_PLAYING_ENVELOPE *pe)
2246{
2247 int ys, ye;
2248 int ts, te;
2249 int t;
2250
2251 if (pe->next_node <= 0)
2252 return envelope->node_y[0] << IT_ENVELOPE_SHIFT;
2253
2254 if (pe->next_node >= envelope->n_nodes)
2255 return envelope->node_y[envelope->n_nodes-1] << IT_ENVELOPE_SHIFT;
2256
2257 ys = envelope->node_y[pe->next_node-1] << IT_ENVELOPE_SHIFT;
2258 ts = envelope->node_t[pe->next_node-1];
2259 te = envelope->node_t[pe->next_node];
2260
2261 if (ts == te)
2262 return ys;
2263
2264 ye = envelope->node_y[pe->next_node] << IT_ENVELOPE_SHIFT;
2265
2266 t = pe->tick;
2267
2268 return ys + (ye - ys) * (t - ts) / (te - ts);
2269}
2270
2271
2272
2273static int it_envelope_end(IT_PLAYING *playing, IT_ENVELOPE *envelope, IT_PLAYING_ENVELOPE *pe)
2274{
2275 if (pe->next_node >= envelope->n_nodes)
2276 return 1;
2277
2278 if (pe->tick < envelope->node_t[pe->next_node]) return 0;
2279
2280 if ((envelope->flags & IT_ENVELOPE_LOOP_ON) &&
2281 envelope->loop_end >= pe->next_node &&
2282 envelope->node_t[envelope->loop_end] <= pe->tick) return 0;
2283
2284 if ((envelope->flags & IT_ENVELOPE_SUSTAIN_LOOP) &&
2285 !(playing->flags & IT_PLAYING_SUSTAINOFF) &&
2286 envelope->sus_loop_end >= pe->next_node &&
2287 envelope->node_t[envelope->sus_loop_end] <= pe->tick) return 0;
2288
2289 if (envelope->node_t[envelope->n_nodes-1] <= pe->tick) return 1;
2290
2291 return 0;
2292}
2293
2294
2295
2296/* This returns 1 if the envelope finishes. */
2297static int update_it_envelope(IT_PLAYING *playing, IT_ENVELOPE *envelope, IT_PLAYING_ENVELOPE *pe)
2298{
2299 if (!(envelope->flags & IT_ENVELOPE_ON))
2300 return 0;
2301
2302 if (pe->next_node >= envelope->n_nodes)
2303 return 1;
2304
2305 while (pe->tick >= envelope->node_t[pe->next_node]) {
2306 if ((envelope->flags & IT_ENVELOPE_LOOP_ON) && pe->next_node == envelope->loop_end) {
2307 pe->next_node = envelope->loop_start;
2308 pe->tick = envelope->node_t[envelope->loop_start];
2309 return it_envelope_end(playing, envelope, pe);
2310 }
2311 if ((envelope->flags & IT_ENVELOPE_SUSTAIN_LOOP) && !(playing->flags & IT_PLAYING_SUSTAINOFF) && pe->next_node == envelope->sus_loop_end) {
2312 pe->next_node = envelope->sus_loop_start;
2313 pe->tick = envelope->node_t[envelope->sus_loop_start];
2314 return it_envelope_end(playing, envelope, pe);
2315 }
2316
2317 pe->next_node++;
2318
2319 if (pe->next_node >= envelope->n_nodes)
2320 return 1;
2321 }
2322
2323 pe->tick++;
2324
2325 return it_envelope_end(playing, envelope, pe);
2326}
2327
2328
2329
2330static void update_it_envelopes(IT_PLAYING *playing)
2331{
2332 IT_ENVELOPE *envelope = &playing->env_instrument->volume_envelope;
2333
2334 if (update_it_envelope(playing, envelope, &playing->volume_envelope)) {
2335 playing->flags |= IT_PLAYING_FADING;
2336 if (envelope->n_nodes && envelope->node_y[envelope->n_nodes-1] == 0)
2337 playing->flags |= IT_PLAYING_DEAD;
2338 }
2339
2340 update_it_envelope(playing, &playing->env_instrument->pan_envelope, &playing->pan_envelope);
2341 update_it_envelope(playing, &playing->env_instrument->pitch_envelope, &playing->pitch_envelope);
2342}
2343
2344
2345
2346static int xm_envelope_is_sustaining(IT_PLAYING *playing, IT_ENVELOPE *envelope, IT_PLAYING_ENVELOPE *pe)
2347{
2348 if ((envelope->flags & IT_ENVELOPE_SUSTAIN_LOOP) && !(playing->flags & IT_PLAYING_SUSTAINOFF))
2349 if (envelope->sus_loop_start < envelope->n_nodes)
2350 if (pe->tick == envelope->node_t[envelope->sus_loop_start])
2351 return 1;
2352 return 0;
2353}
2354
2355
2356
2357static void update_xm_envelope(IT_PLAYING *playing, IT_ENVELOPE *envelope, IT_PLAYING_ENVELOPE *pe)
2358{
2359 if (!(envelope->flags & IT_ENVELOPE_ON))
2360 return;
2361
2362 if (xm_envelope_is_sustaining(playing, envelope, pe))
2363 return;
2364
2365 if (pe->tick >= envelope->node_t[envelope->n_nodes-1])
2366 return;
2367
2368 pe->tick++;
2369
2370 /* pe->next_node must be kept up to date for envelope_get_y(). */
2371 while (pe->tick > envelope->node_t[pe->next_node])
2372 pe->next_node++;
2373
2374 if ((envelope->flags & IT_ENVELOPE_LOOP_ON) && envelope->loop_end < envelope->n_nodes) {
2375 if (pe->tick == envelope->node_t[envelope->loop_end]) {
2376 pe->next_node = MID(0, envelope->loop_start, envelope->n_nodes - 1);
2377 pe->tick = envelope->node_t[pe->next_node];
2378 }
2379 }
2380
2381 if (xm_envelope_is_sustaining(playing, envelope, pe))
2382 return;
2383
2384 if (pe->tick >= envelope->node_t[envelope->n_nodes-1])
2385 return;
2386}
2387
2388
2389
2390static void update_xm_envelopes(IT_PLAYING *playing)
2391{
2392 update_xm_envelope(playing, &playing->env_instrument->volume_envelope, &playing->volume_envelope);
2393 update_xm_envelope(playing, &playing->env_instrument->pan_envelope, &playing->pan_envelope);
2394}
2395
2396
2397
2398static void update_fadeout(DUMB_IT_SIGDATA *sigdata, IT_PLAYING *playing)
2399{
2400 if (playing->flags & IT_PLAYING_FADING) {
2401 playing->fadeoutcount -= playing->env_instrument->fadeout;
2402 if (playing->fadeoutcount <= 0) {
2403 playing->fadeoutcount = 0;
2404 if (!(sigdata->flags & IT_WAS_AN_XM))
2405 playing->flags |= IT_PLAYING_DEAD;
2406 }
2407 }
2408}
2409
2410
2411
2412static void process_playing(DUMB_IT_SIGDATA *sigdata, IT_PLAYING *playing)
2413{
2414 if (playing->instrument) {
2415 if (sigdata->flags & IT_WAS_AN_XM)
2416 update_xm_envelopes(playing);
2417 else
2418 update_it_envelopes(playing);
2419 update_fadeout(sigdata, playing);
2420 }
2421
2422 //Calculate final volume if required
2423 //Calculate final pan if required
2424
2425 if (sigdata->flags & IT_WAS_AN_XM) {
2426 /* 'depth' is used to store the tick number for XM files. */
2427 if (playing->sample_vibrato_depth < playing->sample->vibrato_rate)
2428 playing->sample_vibrato_depth++;
2429 } else {
2430 playing->sample_vibrato_depth += playing->sample->vibrato_rate;
2431 if (playing->sample_vibrato_depth > playing->sample->vibrato_depth << 8)
2432 playing->sample_vibrato_depth = playing->sample->vibrato_depth << 8;
2433 }
2434
2435 playing->sample_vibrato_time += playing->sample->vibrato_speed;
2436}
2437
2438
2439
2440static void process_all_playing(DUMB_IT_SIGRENDERER *sigrenderer)
2441{
2442 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
2443 int i;
2444
2445 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
2446 IT_CHANNEL *channel = &sigrenderer->channel[i];
2447 IT_PLAYING *playing = channel->playing;
2448
2449 if (playing) {
2450 int vibrato_shift = it_sine[playing->vibrato_time];
2451 vibrato_shift *= playing->vibrato_n;
2452 vibrato_shift *= playing->vibrato_depth;
2453 vibrato_shift >>= 4;
2454
2455 if (sigdata->flags & IT_OLD_EFFECTS)
2456 vibrato_shift = -vibrato_shift;
2457
2458 playing->volume = channel->volume;
2459 playing->pan = channel->truepan;
2460
2461 if (sigdata->flags & IT_LINEAR_SLIDES) {
2462 int currpitch = ((playing->note - 60) << 8) + playing->slide
2463 + vibrato_shift;
2464
2465 /* We add a feature here, which is that of keeping the pitch
2466 * within range. Otherwise it crashes. Trust me. It happened.
2467 * The limit 32768 gives almost 11 octaves either way.
2468 */
2469 if (currpitch < -32768)
2470 currpitch = -32768;
2471 else if (currpitch > 32767)
2472 currpitch = 32767;
2473
2474 playing->delta = (float)pow(DUMB_PITCH_BASE, currpitch);
2475 playing->delta *= playing->sample->C5_speed / 65536.0f;
2476 } else {
2477 int slide = playing->slide + vibrato_shift;
2478
2479 playing->delta = (float)pow(DUMB_SEMITONE_BASE, 60 - playing->note);
2480 /* playing->delta is 1.0 for C-5, 0.5 for C-6, etc. */
2481
2482 playing->delta *= 1.0f / playing->sample->C5_speed;
2483
2484 playing->delta -= slide / AMIGA_DIVISOR;
2485
2486 if (playing->delta < (1.0f / 65536.0f) / 32768.0f) {
2487 // Should XM notes die if Amiga slides go out of range?
2488 playing->flags |= IT_PLAYING_DEAD;
2489 continue;
2490 }
2491
2492 playing->delta = (1.0f / 65536.0f) / playing->delta;
2493 }
2494
2495 playing->delta *= (float)pow(DUMB_SEMITONE_BASE, channel->arpeggio >> 8);
2496
2497 playing->filter_cutoff = channel->filter_cutoff;
2498 playing->filter_resonance = channel->filter_resonance;
2499 }
2500 }
2501
2502 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
2503 if (sigrenderer->channel[i].playing) {
2504 process_playing(sigdata, sigrenderer->channel[i].playing);
2505 if (!(sigdata->flags & IT_WAS_AN_XM)) {
2506 if ((sigrenderer->channel[i].playing->flags & (IT_PLAYING_BACKGROUND | IT_PLAYING_DEAD)) == (IT_PLAYING_BACKGROUND | IT_PLAYING_DEAD)) {
2507 free(sigrenderer->channel[i].playing);
2508 sigrenderer->channel[i].playing = NULL;
2509 }
2510 }
2511 }
2512 }
2513
2514 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++) {
2515 if (sigrenderer->playing[i]) {
2516 process_playing(sigdata, sigrenderer->playing[i]);
2517 if (sigrenderer->playing[i]->flags & IT_PLAYING_DEAD) {
2518 free(sigrenderer->playing[i]);
2519 sigrenderer->playing[i] = NULL;
2520 }
2521 }
2522 }
2523}
2524
2525
2526
2527static int process_tick(DUMB_IT_SIGRENDERER *sigrenderer)
2528{
2529 DUMB_IT_SIGDATA *sigdata = sigrenderer->sigdata;
2530
2531 // Set note vol/freq to vol/freq set for each channel
2532
2533 if (sigrenderer->speed && --sigrenderer->tick == 0) {
2534 reset_tick_counts(sigrenderer);
2535 sigrenderer->tick = sigrenderer->speed;
2536 sigrenderer->rowcount--;
2537 if (sigrenderer->rowcount == 0) {
2538 sigrenderer->rowcount = 1;
2539 if (sigrenderer->pat_loop_row >= 0) {
2540 int n = sigrenderer->pat_loop_row - 1;
2541 sigrenderer->row = sigrenderer->processrow = n;
2542 sigrenderer->pat_loop_row = -1;
2543 if (n < 0)
2544 sigrenderer->entry = NULL;
2545 else {
2546 sigrenderer->entry = sigrenderer->entry_start;
2547 while (n) {
2548 while (sigrenderer->entry < sigrenderer->entry_end) {
2549 if (IT_IS_END_ROW(sigrenderer->entry)) {
2550 sigrenderer->entry++;
2551 break;
2552 }
2553 sigrenderer->entry++;
2554 }
2555 n--;
2556 }
2557 }
2558 }
2559
2560 sigrenderer->processrow++;
2561
2562 if (sigrenderer->processrow >= sigrenderer->n_rows) {
2563 IT_PATTERN *pattern;
2564 int n;
2565
2566 sigrenderer->processrow = sigrenderer->breakrow;
2567 sigrenderer->breakrow = 0;
2568
2569 for (;;) {
2570 sigrenderer->processorder++;
2571
2572 if (sigrenderer->processorder >= sigdata->n_orders) {
2573 sigrenderer->processorder = sigdata->restart_position;
2574 if (sigrenderer->processorder >= sigdata->n_orders) {
2575 /* Restarting beyond end. We'll loop for now. */
2576 sigrenderer->processorder = -1;
2577 continue;
2578 }
2579 }
2580
2581 n = sigdata->order[sigrenderer->processorder];
2582
2583 if (n < sigdata->n_patterns)
2584 break;
2585
2586#ifdef INVALID_ORDERS_END_SONG
2587 if (n != IT_ORDER_SKIP)
2588 sigrenderer->processorder = -1;
2589#else
2590 if (n == IT_ORDER_END)
2591 sigrenderer->processorder = -1;
2592#endif
2593 }
2594
2595 pattern = &sigdata->pattern[n];
2596
2597 sigrenderer->n_rows = pattern->n_rows;
2598
2599 if (sigrenderer->processrow >= sigrenderer->n_rows)
2600 sigrenderer->processrow = 0;
2601
2602/** WARNING - everything pertaining to a new pattern initialised? */
2603
2604 sigrenderer->entry = sigrenderer->entry_start = pattern->entry;
2605 sigrenderer->entry_end = sigrenderer->entry + pattern->n_entries;
2606
2607 if (sigrenderer->order >= sigrenderer->processorder) {
2608 if (sigrenderer->callbacks->loop) {
2609 if ((*sigrenderer->callbacks->loop)(sigrenderer->callbacks->loop_data))
2610 return 1;
2611 if (sigrenderer->speed == 0)
2612 goto speed0; /* I love goto */
2613 }
2614 }
2615 sigrenderer->order = sigrenderer->processorder;
2616
2617 n = sigrenderer->processrow;
2618 while (n) {
2619 while (sigrenderer->entry < sigrenderer->entry_end) {
2620 if (IT_IS_END_ROW(sigrenderer->entry)) {
2621 sigrenderer->entry++;
2622 break;
2623 }
2624 sigrenderer->entry++;
2625 }
2626 n--;
2627 }
2628 sigrenderer->row = sigrenderer->processrow;
2629 } else {
2630 if (sigrenderer->entry) {
2631 while (sigrenderer->entry < sigrenderer->entry_end) {
2632 if (IT_IS_END_ROW(sigrenderer->entry)) {
2633 sigrenderer->entry++;
2634 break;
2635 }
2636 sigrenderer->entry++;
2637 }
2638 sigrenderer->row++;
2639 } else {
2640 sigrenderer->entry = sigrenderer->entry_start;
2641 sigrenderer->row = 0;
2642 }
2643 }
2644
2645 reset_effects(sigrenderer);
2646
2647 {
2648 IT_ENTRY *entry = sigrenderer->entry;
2649
2650 while (entry < sigrenderer->entry_end && !IT_IS_END_ROW(entry))
2651 update_pattern_variables(sigrenderer, entry++);
2652 }
2653
2654 {
2655 IT_ENTRY *entry = sigrenderer->entry;
2656
2657 while (entry < sigrenderer->entry_end && !IT_IS_END_ROW(entry))
2658 if (process_entry(sigrenderer, entry++))
2659 return 1;
2660 }
2661
2662 if (!(sigdata->flags & IT_OLD_EFFECTS))
2663 update_smooth_effects(sigrenderer);
2664 } else {
2665 {
2666 IT_ENTRY *entry = sigrenderer->entry;
2667
2668 while (entry < sigrenderer->entry_end && !IT_IS_END_ROW(entry))
2669 process_effects(sigrenderer, entry++);
2670 /* Don't bother checking the return value; if there
2671 * was a pattern delay, there can't be a speed=0.
2672 */
2673 }
2674
2675 update_effects(sigrenderer);
2676 }
2677 } else {
2678 speed0:
2679 update_effects(sigrenderer);
2680 update_tick_counts(sigrenderer);
2681 }
2682
2683 process_all_playing(sigrenderer);
2684
2685 sigrenderer->time_left += TICK_TIME_DIVIDEND / sigrenderer->tempo;
2686
2687 return 0;
2688}
2689
2690
2691
2692int dumb_it_max_to_mix = 64;
2693
2694
2695
2696static float calculate_volume(DUMB_IT_SIGRENDERER *sigrenderer, IT_PLAYING *playing, float volume)
2697{
2698 if (volume != 0) {
2699 int vol;
2700
2701 if (playing->channel->flags & IT_CHANNEL_MUTED)
2702 return 0;
2703
2704 if ((playing->channel->tremor_time & 192) == 128)
2705 return 0;
2706
2707 vol = it_sine[playing->tremolo_time];
2708 vol *= playing->tremolo_depth;
2709
2710 vol = (playing->volume << 5) + vol;
2711
2712 if (vol <= 0)
2713 return 0;
2714
2715 if (vol > 64 << 5)
2716 vol = 64 << 5;
2717
2718 volume *= vol; /* 64 << 5 */
2719 volume *= playing->sample->global_volume; /* 64 */
2720 volume *= playing->channel_volume; /* 64 */
2721 volume *= sigrenderer->globalvolume; /* 128 */
2722 volume *= sigrenderer->sigdata->mixing_volume; /* 128 */
2723 volume *= 1.0f / ((64 << 5) * 64.0f * 64.0f * 128.0f * 128.0f);
2724
2725 if (volume && playing->instrument) {
2726 if (playing->env_instrument->volume_envelope.flags & IT_ENVELOPE_ON) {
2727 volume *= envelope_get_y(&playing->env_instrument->volume_envelope, &playing->volume_envelope);
2728 volume *= 1.0f / (64 << IT_ENVELOPE_SHIFT);
2729 }
2730 volume *= playing->instrument->global_volume; /* 128 */
2731 volume *= playing->fadeoutcount; /* 1024 */
2732 volume *= 1.0f / (128.0f * 1024.0f);
2733 }
2734 }
2735
2736 return volume;
2737}
2738
2739
2740
2741static int apply_pan_envelope(IT_PLAYING *playing)
2742{
2743 int pan = playing->pan;
2744 if (pan <= 64 << IT_ENVELOPE_SHIFT && playing->env_instrument && (playing->env_instrument->pan_envelope.flags & IT_ENVELOPE_ON)) {
2745 int p = envelope_get_y(&playing->env_instrument->pan_envelope, &playing->pan_envelope);
2746 if (pan > 32 << IT_ENVELOPE_SHIFT)
2747 p *= (64 << IT_ENVELOPE_SHIFT) - pan;
2748 else
2749 p *= pan;
2750 pan += p >> (5 + IT_ENVELOPE_SHIFT);
2751 }
2752 return pan;
2753}
2754
2755
2756
2757/* Note: if a click remover is provided, and store_end_sample is set, then
2758 * the end point will be computed twice. This situation should not arise.
2759 */
2760static long render_playing(DUMB_IT_SIGRENDERER *sigrenderer, IT_PLAYING *playing, float volume, float delta, long pos, long size, sample_t **samples, int store_end_sample, int *left_to_mix)
2761{
2762 int pan;
2763
2764 long size_rendered;
2765
2766 if (playing->flags & IT_PLAYING_DEAD)
2767 return 0;
2768
2769 if (*left_to_mix <= 0)
2770 volume = 0;
2771
2772 pan = apply_pan_envelope(playing);
2773
2774#define RESAMPLERV(rv, resampler, dst, volume) \
2775{ \
2776 rv = dumb_resample(resampler, dst, size, volume, delta); \
2777 if (store_end_sample) \
2778 (dst)[rv] = RESAMPLE_VALUE(resampler, volume); \
2779}
2780
2781#define RESAMPLE(resampler, dst, volume) \
2782{ \
2783 int i; \
2784 RESAMPLERV(i, resampler, dst, volume); \
2785}
2786
2787#define RESAMPLE_VALUE(resampler, volume) \
2788 dumb_resample_get_current_sample(resampler, volume)
2789
2790 if (volume == 0) {
2791 size_rendered = dumb_resample(&playing->resampler[0], NULL, size, 0, delta);
2792 if (playing->sample->flags & IT_SAMPLE_STEREO)
2793 dumb_resample(&playing->resampler[1], NULL, size, 0, delta);
2794 } else {
2795 if (sigrenderer->n_channels == 2) {
2796 float vol = volume;
2797 DUMB_RESAMPLER start = playing->resampler[0];
2798 if (!IT_IS_SURROUND_SHIFTED(pan)) vol *= 2.0f - pan * (1.0f / (32 << IT_ENVELOPE_SHIFT));
2799 if (sigrenderer->click_remover && sigrenderer->click_remover[0])
2800 dumb_record_click(sigrenderer->click_remover[0], pos, RESAMPLE_VALUE(&playing->resampler[0], vol));
2801 RESAMPLERV(size_rendered, &playing->resampler[0], samples[0] + pos, vol);
2802 if (sigrenderer->click_remover && sigrenderer->click_remover[0])
2803 dumb_record_click(sigrenderer->click_remover[0], pos + size_rendered, -RESAMPLE_VALUE(&playing->resampler[0], vol));
2804 vol = -vol;
2805 if (!IT_IS_SURROUND_SHIFTED(pan)) vol += 2.0f * volume;
2806 if (playing->sample->flags & IT_SAMPLE_STEREO) {
2807 if (sigrenderer->click_remover && sigrenderer->click_remover[1])
2808 dumb_record_click(sigrenderer->click_remover[1], pos, RESAMPLE_VALUE(&playing->resampler[1], vol));
2809 RESAMPLE(&playing->resampler[1], samples[1] + pos, vol);
2810 if (sigrenderer->click_remover && sigrenderer->click_remover[1])
2811 dumb_record_click(sigrenderer->click_remover[1], pos + size_rendered, -RESAMPLE_VALUE(&playing->resampler[1], vol));
2812 } else {
2813 playing->resampler[0] = start;
2814 if (sigrenderer->click_remover && sigrenderer->click_remover[1])
2815 dumb_record_click(sigrenderer->click_remover[1], pos, RESAMPLE_VALUE(&playing->resampler[0], vol));
2816 RESAMPLE(&playing->resampler[0], samples[1] + pos, vol);
2817 if (sigrenderer->click_remover && sigrenderer->click_remover[1])
2818 dumb_record_click(sigrenderer->click_remover[1], pos + size_rendered, -RESAMPLE_VALUE(&playing->resampler[0], vol));
2819 }
2820 } else {
2821 if (playing->sample->flags & IT_SAMPLE_STEREO) {
2822 float vol = 0.5f * volume;
2823 if (!IT_IS_SURROUND_SHIFTED(pan)) vol *= 2.0f - pan * (1.0f / (32 << IT_ENVELOPE_SHIFT));
2824 if (sigrenderer->click_remover && sigrenderer->click_remover[0]) {
2825 sample_t startstep, endstep;
2826 startstep = RESAMPLE_VALUE(&playing->resampler[0], vol);
2827 RESAMPLE(&playing->resampler[0], samples[0] + pos, vol);
2828 endstep = RESAMPLE_VALUE(&playing->resampler[0], vol);
2829 if (!IT_IS_SURROUND_SHIFTED(pan)) vol = 2.0f * volume - vol;
2830 startstep += RESAMPLE_VALUE(&playing->resampler[1], vol);
2831 RESAMPLERV(size_rendered, &playing->resampler[1], samples[0] + pos, vol);
2832 endstep += RESAMPLE_VALUE(&playing->resampler[1], vol);
2833 dumb_record_click(sigrenderer->click_remover[0], pos, startstep);
2834 dumb_record_click(sigrenderer->click_remover[0], pos + size_rendered, -endstep);
2835 } else {
2836 RESAMPLE(&playing->resampler[0], samples[0] + pos, vol);
2837 if (!IT_IS_SURROUND_SHIFTED(pan)) vol = 2.0f * volume - vol;
2838 RESAMPLERV(size_rendered, &playing->resampler[1], samples[0] + pos, vol);
2839 }
2840 } else {
2841 if (sigrenderer->click_remover && sigrenderer->click_remover[0])
2842 dumb_record_click(sigrenderer->click_remover[0], pos, RESAMPLE_VALUE(&playing->resampler[0], volume));
2843 RESAMPLERV(size_rendered, &playing->resampler[0], samples[0] + pos, volume);
2844 if (sigrenderer->click_remover && sigrenderer->click_remover[0])
2845 dumb_record_click(sigrenderer->click_remover[0], pos + size_rendered, -RESAMPLE_VALUE(&playing->resampler[0], volume));
2846 }
2847 }
2848 (*left_to_mix)--;
2849 }
2850
2851 if (playing->resampler[0].dir == 0)
2852 playing->flags |= IT_PLAYING_DEAD;
2853
2854 return size_rendered;
2855}
2856
2857
2858
2859typedef struct IT_TO_MIX
2860{
2861 IT_PLAYING *playing;
2862 float volume;
2863}
2864IT_TO_MIX;
2865
2866
2867
2868static int it_to_mix_compare(const void *e1, const void *e2)
2869{
2870 if (((const IT_TO_MIX *)e1)->volume > ((const IT_TO_MIX *)e2)->volume)
2871 return -1;
2872
2873 if (((const IT_TO_MIX *)e1)->volume < ((const IT_TO_MIX *)e2)->volume)
2874 return 1;
2875
2876 return 0;
2877}
2878
2879
2880
2881static void apply_pitch_modifications(DUMB_IT_SIGDATA *sigdata, IT_PLAYING *playing, float *delta, int *cutoff)
2882{
2883 {
2884 int sample_vibrato_shift = it_sine[playing->sample_vibrato_time];
2885
2886 if (sigdata->flags & IT_WAS_AN_XM) {
2887 int depth = playing->sample->vibrato_depth; /* True depth */
2888 if (playing->sample->vibrato_rate) {
2889 depth *= playing->sample_vibrato_depth; /* Tick number */
2890 depth /= playing->sample->vibrato_rate; /* XM sweep */
2891 }
2892 sample_vibrato_shift *= depth;
2893 } else
2894 sample_vibrato_shift *= playing->sample_vibrato_depth >> 8;
2895
2896 sample_vibrato_shift >>= 4;
2897
2898 *delta *= (float)pow(DUMB_PITCH_BASE, sample_vibrato_shift);
2899 }
2900
2901 if (playing->env_instrument &&
2902 (playing->env_instrument->pitch_envelope.flags & IT_ENVELOPE_ON))
2903 {
2904 int p = envelope_get_y(&playing->env_instrument->pitch_envelope, &playing->pitch_envelope);
2905 if (playing->env_instrument->pitch_envelope.flags & IT_ENVELOPE_PITCH_IS_FILTER)
2906 *cutoff = (*cutoff * (p+(32<<IT_ENVELOPE_SHIFT))) >> (6 + IT_ENVELOPE_SHIFT);
2907 else
2908 *delta *= (float)pow(DUMB_PITCH_BASE, p >> (IT_ENVELOPE_SHIFT - 7));
2909 }
2910}
2911
2912
2913
2914static void render(DUMB_IT_SIGRENDERER *sigrenderer, float volume, float delta, long pos, long size, sample_t **samples)
2915{
2916 int i;
2917
2918 int n_to_mix = 0;
2919 IT_TO_MIX to_mix[DUMB_IT_TOTAL_CHANNELS];
2920 int left_to_mix = dumb_it_max_to_mix;
2921
2922 sample_t **samples_to_filter = NULL;
2923
2924 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
2925 if (sigrenderer->channel[i].playing && !(sigrenderer->channel[i].playing->flags & IT_PLAYING_DEAD)) {
2926 to_mix[n_to_mix].playing = sigrenderer->channel[i].playing;
2927 to_mix[n_to_mix].volume = volume == 0 ? 0 : calculate_volume(sigrenderer, sigrenderer->channel[i].playing, volume);
2928 n_to_mix++;
2929 }
2930 }
2931
2932 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++) {
2933 if (sigrenderer->playing[i]) { /* Won't be dead; it would have been freed. */
2934 to_mix[n_to_mix].playing = sigrenderer->playing[i];
2935 to_mix[n_to_mix].volume = volume == 0 ? 0 : calculate_volume(sigrenderer, sigrenderer->playing[i], volume);
2936 n_to_mix++;
2937 }
2938 }
2939
2940 if (volume != 0)
2941 qsort(to_mix, n_to_mix, sizeof(IT_TO_MIX), &it_to_mix_compare);
2942
2943 for (i = 0; i < n_to_mix; i++) {
2944 IT_PLAYING *playing = to_mix[i].playing;
2945 float note_delta = delta * playing->delta;
2946 int cutoff = playing->filter_cutoff << IT_ENVELOPE_SHIFT;
2947
2948 apply_pitch_modifications(sigrenderer->sigdata, playing, &note_delta, &cutoff);
2949
2950 if (cutoff != 127 << IT_ENVELOPE_SHIFT || playing->filter_resonance != 0) {
2951 playing->true_filter_cutoff = cutoff;
2952 playing->true_filter_resonance = playing->filter_resonance;
2953 }
2954
2955 if (to_mix[i].volume && (playing->true_filter_cutoff != 127 << IT_ENVELOPE_SHIFT || playing->true_filter_resonance != 0)) {
2956 if (!samples_to_filter) {
2957 samples_to_filter = create_sample_buffer(sigrenderer->n_channels, size + 1);
2958 if (!samples_to_filter) {
2959 render_playing(sigrenderer, playing, 0, note_delta, pos, size, NULL, 0, &left_to_mix);
2960 continue;
2961 }
2962 }
2963 {
2964 long size_rendered;
2965 DUMB_CLICK_REMOVER **cr = sigrenderer->click_remover;
2966 dumb_silence(samples_to_filter[0], sigrenderer->n_channels * (size + 1));
2967 sigrenderer->click_remover = NULL;
2968 size_rendered = render_playing(sigrenderer, playing, to_mix[i].volume, note_delta, 0, size, samples_to_filter, 1, &left_to_mix);
2969 sigrenderer->click_remover = cr;
2970 it_filter(cr ? cr[0] : NULL, &playing->filter_state[0], samples[0], pos, samples_to_filter[0], size_rendered,
2971 65536.0f/delta, playing->true_filter_cutoff, playing->true_filter_resonance);
2972 if (sigrenderer->n_channels == 2)
2973 it_filter(cr ? cr[1] : NULL, &playing->filter_state[1], samples[1], pos, samples_to_filter[1], size_rendered,
2974 65536.0f/delta, playing->true_filter_cutoff, playing->true_filter_resonance);
2975 // warning: filtering is not prevented by low left_to_mix!
2976 }
2977 } else {
2978 it_reset_filter_state(&playing->filter_state[0]);
2979 it_reset_filter_state(&playing->filter_state[1]);
2980 render_playing(sigrenderer, playing, to_mix[i].volume, note_delta, pos, size, samples, 0, &left_to_mix);
2981 }
2982 }
2983
2984 destroy_sample_buffer(samples_to_filter);
2985
2986 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
2987 if (sigrenderer->channel[i].playing) {
2988 if ((sigrenderer->channel[i].playing->flags & (IT_PLAYING_BACKGROUND | IT_PLAYING_DEAD)) == (IT_PLAYING_BACKGROUND | IT_PLAYING_DEAD)) {
2989 free(sigrenderer->channel[i].playing);
2990 sigrenderer->channel[i].playing = NULL;
2991 }
2992 }
2993 }
2994
2995 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++) {
2996 if (sigrenderer->playing[i]) {
2997 if (sigrenderer->playing[i]->flags & IT_PLAYING_DEAD) {
2998 free(sigrenderer->playing[i]);
2999 sigrenderer->playing[i] = NULL;
3000 }
3001 }
3002 }
3003}
3004
3005
3006
3007static DUMB_IT_SIGRENDERER *init_sigrenderer(DUMB_IT_SIGDATA *sigdata, int n_channels, int startorder, IT_CALLBACKS *callbacks, DUMB_CLICK_REMOVER **cr)
3008{
3009 DUMB_IT_SIGRENDERER *sigrenderer;
3010 int i;
3011
3012 if (startorder > sigdata->n_orders) {
3013 free(callbacks);
3014 dumb_destroy_click_remover_array(n_channels, cr);
3015 return NULL;
3016 }
3017
3018 sigrenderer = malloc(sizeof(*sigrenderer));
3019 if (!sigrenderer) {
3020 free(callbacks);
3021 dumb_destroy_click_remover_array(n_channels, cr);
3022 return NULL;
3023 }
3024
3025 sigrenderer->callbacks = callbacks;
3026 sigrenderer->click_remover = cr;
3027
3028 sigrenderer->sigdata = sigdata;
3029 sigrenderer->n_channels = n_channels;
3030 sigrenderer->globalvolume = sigdata->global_volume;
3031 sigrenderer->tempo = sigdata->tempo;
3032
3033 for (i = 0; i < DUMB_IT_N_CHANNELS; i++) {
3034 IT_CHANNEL *channel = &sigrenderer->channel[i];
3035#if IT_CHANNEL_MUTED != 1
3036#error this is wrong
3037#endif
3038 channel->flags = sigdata->channel_pan[i] >> 7;
3039 channel->volume = (sigdata->flags & IT_WAS_AN_XM) ? 0 : 64;
3040 channel->pan = sigdata->channel_pan[i] & 0x7F;
3041 channel->truepan = channel->pan << IT_ENVELOPE_SHIFT;
3042 channel->channelvolume = sigdata->channel_volume[i];
3043 channel->instrument = 0;
3044 channel->note = 0;
3045 channel->SFmacro = 0;
3046 channel->filter_cutoff = 127;
3047 channel->filter_resonance = 0;
3048 channel->xm_retrig = 0;
3049 channel->retrig_tick = 0;
3050 channel->tremor_time = 0;
3051 channel->midi_state = 0;
3052 channel->lastvolslide = 0;
3053 channel->lastDKL = 0;
3054 channel->lastEF = 0;
3055 channel->lastG = 0;
3056 channel->lastHspeed = 0;
3057 channel->lastHdepth = 0;
3058 channel->lastRspeed = 0;
3059 channel->lastRdepth = 0;
3060 channel->lastI = 0;
3061 channel->lastJ = 0;
3062 channel->lastN = 0;
3063 channel->lastO = 0;
3064 channel->high_offset = 0;
3065 channel->lastQ = 0;
3066 channel->lastS = 0;
3067 channel->pat_loop_row = 0;
3068 channel->pat_loop_count = 0;
3069 channel->lastW = 0;
3070 channel->xm_lastE1 = 0;
3071 channel->xm_lastE2 = 0;
3072 channel->xm_lastEA = 0;
3073 channel->xm_lastEB = 0;
3074 channel->xm_lastX1 = 0;
3075 channel->xm_lastX2 = 0;
3076 channel->playing = NULL;
3077 }
3078
3079 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++)
3080 sigrenderer->playing[i] = NULL;
3081
3082 sigrenderer->speed = sigdata->speed;
3083
3084 sigrenderer->processrow = 0;
3085 sigrenderer->breakrow = 0;
3086 sigrenderer->pat_loop_row = -1;
3087 sigrenderer->rowcount = 1;
3088
3089 reset_tick_counts(sigrenderer);
3090
3091 sigrenderer->tick = sigrenderer->speed;
3092
3093 {
3094 IT_PATTERN *pattern;
3095 int n;
3096
3097 sigrenderer->processorder = startorder;
3098 for (;;) {
3099 n = sigdata->order[sigrenderer->processorder];
3100
3101 if (n < sigdata->n_patterns)
3102 break;
3103
3104#ifdef INVALID_ORDERS_END_SONG
3105 if (n != IT_ORDER_SKIP)
3106#else
3107 if (n == IT_ORDER_END)
3108#endif
3109 {
3110 _dumb_it_end_sigrenderer(sigrenderer);
3111 return NULL;
3112 }
3113
3114 sigrenderer->processorder++;
3115 if (sigrenderer->processorder >= sigdata->n_orders)
3116 sigrenderer->processorder = 0;
3117 if (sigrenderer->processorder == startorder) {
3118 _dumb_it_end_sigrenderer(sigrenderer);
3119 return NULL;
3120 }
3121 }
3122
3123 pattern = &sigdata->pattern[n];
3124
3125 sigrenderer->n_rows = pattern->n_rows;
3126
3127/** WARNING - everything pertaining to a new pattern initialised? */
3128
3129 sigrenderer->entry = sigrenderer->entry_start = pattern->entry;
3130 sigrenderer->entry_end = sigrenderer->entry + pattern->n_entries;
3131
3132 sigrenderer->order = sigrenderer->processorder;
3133 sigrenderer->row = 0;
3134 }
3135
3136 reset_effects(sigrenderer);
3137
3138 {
3139 IT_ENTRY *entry = sigrenderer->entry;
3140
3141 while (entry < sigrenderer->entry_end && !IT_IS_END_ROW(entry))
3142 update_pattern_variables(sigrenderer, entry++);
3143 }
3144
3145 {
3146 IT_ENTRY *entry = sigrenderer->entry;
3147
3148 while (entry < sigrenderer->entry_end && !IT_IS_END_ROW(entry)) {
3149 if (process_entry(sigrenderer, entry++)) {
3150 /* Oops, that song ended quickly! */
3151 _dumb_it_end_sigrenderer(sigrenderer);
3152 return NULL;
3153 }
3154 }
3155 }
3156
3157 if (!(sigdata->flags & IT_OLD_EFFECTS))
3158 update_smooth_effects(sigrenderer);
3159
3160 process_all_playing(sigrenderer);
3161
3162 sigrenderer->time_left = TICK_TIME_DIVIDEND / sigrenderer->tempo;
3163 sigrenderer->sub_time_left = 0;
3164
3165 return sigrenderer;
3166}
3167
3168
3169
3170void dumb_it_set_loop_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (*callback)(void *data), void *data)
3171{
3172 if (sigrenderer) {
3173 sigrenderer->callbacks->loop = callback;
3174 sigrenderer->callbacks->loop_data = data;
3175 }
3176}
3177
3178
3179
3180void dumb_it_set_xm_speed_zero_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (*callback)(void *data), void *data)
3181{
3182 if (sigrenderer) {
3183 sigrenderer->callbacks->xm_speed_zero = callback;
3184 sigrenderer->callbacks->xm_speed_zero_data = data;
3185 }
3186}
3187
3188
3189
3190void dumb_it_set_midi_callback(DUMB_IT_SIGRENDERER *sigrenderer, int (*callback)(void *data, int channel, unsigned char byte), void *data)
3191{
3192 if (sigrenderer) {
3193 sigrenderer->callbacks->midi = callback;
3194 sigrenderer->callbacks->midi_data = data;
3195 }
3196}
3197
3198
3199
3200static IT_CALLBACKS *create_callbacks(void)
3201{
3202 IT_CALLBACKS *callbacks = malloc(sizeof(*callbacks));
3203 if (!callbacks) return NULL;
3204 callbacks->loop = NULL;
3205 callbacks->xm_speed_zero = NULL;
3206 callbacks->midi = NULL;
3207 return callbacks;
3208}
3209
3210
3211
3212static DUMB_IT_SIGRENDERER *dumb_it_init_sigrenderer(DUMB_IT_SIGDATA *sigdata, int n_channels, int startorder)
3213{
3214 IT_CALLBACKS *callbacks;
3215
3216 if (!sigdata) return NULL;
3217
3218 callbacks = create_callbacks();
3219 if (!callbacks) return NULL;
3220
3221 return init_sigrenderer(sigdata, n_channels, startorder, callbacks,
3222 dumb_create_click_remover_array(n_channels));
3223}
3224
3225
3226
3227DUH_SIGRENDERER *dumb_it_start_at_order(DUH *duh, int n_channels, int startorder)
3228{
3229 DUMB_IT_SIGDATA *itsd = duh_get_it_sigdata(duh);
3230 DUMB_IT_SIGRENDERER *itsr = dumb_it_init_sigrenderer(itsd, n_channels, startorder);
3231 return duh_encapsulate_it_sigrenderer(itsr, n_channels, 0);
3232}
3233
3234
3235
3236static sigrenderer_t *it_start_sigrenderer(DUH *duh, sigdata_t *vsigdata, int n_channels, long pos)
3237{
3238 DUMB_IT_SIGDATA *sigdata = vsigdata;
3239 DUMB_IT_SIGRENDERER *sigrenderer;
3240
3241 (void)duh;
3242
3243 {
3244 IT_CALLBACKS *callbacks = create_callbacks();
3245 if (!callbacks) return NULL;
3246
3247 if (sigdata->checkpoint) {
3248 IT_CHECKPOINT *checkpoint = sigdata->checkpoint;
3249 while (checkpoint->next && checkpoint->next->time < pos)
3250 checkpoint = checkpoint->next;
3251 sigrenderer = dup_sigrenderer(checkpoint->sigrenderer, n_channels, callbacks);
3252 if (!sigrenderer) return NULL;
3253 sigrenderer->click_remover = dumb_create_click_remover_array(n_channels);
3254 pos -= checkpoint->time;
3255 } else {
3256 sigrenderer = init_sigrenderer(sigdata, n_channels, 0, callbacks,
3257 dumb_create_click_remover_array(n_channels));
3258 if (!sigrenderer) return NULL;
3259 }
3260 }
3261
3262 for (;;) {
3263 if (sigrenderer->time_left >= pos)
3264 break;
3265
3266 render(sigrenderer, 0, 1.0f, 0, sigrenderer->time_left, NULL);
3267
3268 pos -= sigrenderer->time_left;
3269 sigrenderer->time_left = 0;
3270
3271 if (process_tick(sigrenderer)) {
3272 _dumb_it_end_sigrenderer(sigrenderer);
3273 return NULL;
3274 }
3275 }
3276
3277 render(sigrenderer, 0, 1.0f, 0, pos, NULL);
3278 sigrenderer->time_left -= pos;
3279
3280 /** WARNING - everything initialised? */
3281
3282 return sigrenderer;
3283}
3284
3285
3286
3287static long it_sigrenderer_get_samples(
3288 sigrenderer_t *vsigrenderer,
3289 float volume, float delta,
3290 long size, sample_t **samples
3291)
3292{
3293 DUMB_IT_SIGRENDERER *sigrenderer = vsigrenderer;
3294 long pos;
3295 int dt;
3296 long todo;
3297 LONG_LONG t;
3298
3299 if (sigrenderer->order < 0) return 0;
3300
3301 pos = 0;
3302 dt = (int)(delta * 65536.0f + 0.5f);
3303
3304 /* When samples is finally used in render_playing(), it won't be used if
3305 * volume is 0.
3306 */
3307 if (!samples) volume = 0;
3308
3309 for (;;) {
3310 todo = (long)((((LONG_LONG)sigrenderer->time_left << 16) | sigrenderer->sub_time_left) / dt);
3311
3312 if (todo >= size)
3313 break;
3314
3315 render(sigrenderer, volume, delta, pos, todo, samples);
3316
3317 pos += todo;
3318 size -= todo;
3319
3320 t = sigrenderer->sub_time_left - (LONG_LONG)todo * dt;
3321 sigrenderer->sub_time_left = (long)t & 65535;
3322 sigrenderer->time_left += (long)(t >> 16);
3323
3324 if (process_tick(sigrenderer)) {
3325 sigrenderer->order = -1;
3326 sigrenderer->row = -1;
3327 return pos;
3328 }
3329 }
3330
3331 render(sigrenderer, volume, delta, pos, size, samples);
3332
3333 pos += size;
3334
3335 t = sigrenderer->sub_time_left - (LONG_LONG)size * dt;
3336 sigrenderer->sub_time_left = (long)t & 65535;
3337 sigrenderer->time_left += (long)(t >> 16);
3338
3339 dumb_remove_clicks_array(sigrenderer->n_channels, sigrenderer->click_remover, samples, pos, 512.0f / delta);
3340
3341 return pos;
3342}
3343
3344
3345
3346static void it_sigrenderer_get_current_sample(sigrenderer_t *vsigrenderer, float volume, sample_t *samples)
3347{
3348 DUMB_IT_SIGRENDERER *sigrenderer = vsigrenderer;
3349 (void)volume; // for consideration: in any of these such functions, is 'volume' going to be required?
3350 dumb_click_remover_get_offset_array(sigrenderer->n_channels, sigrenderer->click_remover, samples);
3351}
3352
3353
3354
3355void _dumb_it_end_sigrenderer(sigrenderer_t *vsigrenderer)
3356{
3357 DUMB_IT_SIGRENDERER *sigrenderer = vsigrenderer;
3358
3359 int i;
3360
3361 if (sigrenderer) {
3362 for (i = 0; i < DUMB_IT_N_CHANNELS; i++)
3363 if (sigrenderer->channel[i].playing)
3364 free(sigrenderer->channel[i].playing);
3365
3366 for (i = 0; i < DUMB_IT_N_NNA_CHANNELS; i++)
3367 if (sigrenderer->playing[i])
3368 free(sigrenderer->playing[i]);
3369
3370 dumb_destroy_click_remover_array(sigrenderer->n_channels, sigrenderer->click_remover);
3371
3372 if (sigrenderer->callbacks)
3373 free(sigrenderer->callbacks);
3374
3375 free(vsigrenderer);
3376 }
3377}
3378
3379
3380
3381DUH_SIGTYPE_DESC _dumb_sigtype_it = {
3382 SIGTYPE_IT,
3383 NULL,
3384 &it_start_sigrenderer,
3385 NULL,
3386 &it_sigrenderer_get_samples,
3387 &it_sigrenderer_get_current_sample,
3388 &_dumb_it_end_sigrenderer,
3389 &_dumb_it_unload_sigdata
3390};
3391
3392
3393
3394DUH_SIGRENDERER *duh_encapsulate_it_sigrenderer(DUMB_IT_SIGRENDERER *it_sigrenderer, int n_channels, long pos)
3395{
3396 return duh_encapsulate_raw_sigrenderer(it_sigrenderer, &_dumb_sigtype_it, n_channels, pos);
3397}
3398
3399
3400
3401DUMB_IT_SIGRENDERER *duh_get_it_sigrenderer(DUH_SIGRENDERER *sigrenderer)
3402{
3403 return duh_get_raw_sigrenderer(sigrenderer, SIGTYPE_IT);
3404}
3405
3406
3407
3408/* Values of 64 or more will access NNA channels here. */
3409void dumb_it_sr_get_channel_state(DUMB_IT_SIGRENDERER *sr, int channel, DUMB_IT_CHANNEL_STATE *state)
3410{
3411 IT_PLAYING *playing;
3412 int t; /* temporary var for holding accurate pan and filter cutoff */
3413 float delta;
3414 ASSERT(channel < DUMB_IT_TOTAL_CHANNELS);
3415 if (!sr) { state->sample = 0; return; }
3416 if (channel >= DUMB_IT_N_CHANNELS) {
3417 playing = sr->playing[channel - DUMB_IT_N_CHANNELS];
3418 if (!playing) { state->sample = 0; return; }
3419 } else {
3420 playing = sr->channel[channel].playing;
3421 if (!playing) { state->sample = 0; return; }
3422 }
3423
3424 if (playing->flags & IT_PLAYING_DEAD) { state->sample = 0; return; }
3425
3426 state->channel = playing->channel - sr->channel;
3427 state->sample = playing->sampnum;
3428 state->volume = calculate_volume(sr, playing, 1.0f);
3429
3430 t = apply_pan_envelope(playing);
3431 state->pan = (unsigned char)((t + 128) >> IT_ENVELOPE_SHIFT);
3432 state->subpan = (signed char)t;
3433
3434 delta = playing->delta * 65536.0f;
3435 t = playing->filter_cutoff << IT_ENVELOPE_SHIFT;
3436 apply_pitch_modifications(sr->sigdata, playing, &delta, &t);
3437 state->freq = (int)delta;
3438 if (t == 127 << IT_ENVELOPE_SHIFT && playing->filter_resonance == 0) {
3439 state->filter_resonance = playing->true_filter_resonance;
3440 t = playing->true_filter_cutoff;
3441 } else
3442 state->filter_resonance = playing->filter_resonance;
3443 state->filter_cutoff = (unsigned char)(t >> 8);
3444 state->filter_subcutoff = (unsigned char)t;
3445}
3446
3447
3448
3449int dumb_it_callback_terminate(void *data)
3450{
3451 (void)data;
3452 return 1;
3453}
3454
3455
3456
3457int dumb_it_callback_midi_block(void *data, int channel, unsigned char byte)
3458{
3459 (void)data;
3460 (void)channel;
3461 (void)byte;
3462 return 1;
3463}
3464
3465
3466
3467#define IT_CHECKPOINT_INTERVAL (30 * 65536) /* Half a minute */
3468
3469
3470
3471/* Returns the length of the module, up until it first loops. */
3472long _dumb_it_build_checkpoints(DUMB_IT_SIGDATA *sigdata)
3473{
3474 IT_CHECKPOINT *checkpoint = malloc(sizeof(*checkpoint));
3475 if (!checkpoint) return 0;
3476 checkpoint->time = 0;
3477 checkpoint->sigrenderer = dumb_it_init_sigrenderer(sigdata, 0, 0);
3478 if (!checkpoint->sigrenderer) {
3479 free(checkpoint);
3480 return 0;
3481 }
3482 checkpoint->sigrenderer->callbacks->loop = &dumb_it_callback_terminate;
3483 checkpoint->sigrenderer->callbacks->xm_speed_zero = &dumb_it_callback_terminate;
3484 sigdata->checkpoint = checkpoint;
3485
3486 for (;;) {
3487 long l;
3488 DUMB_IT_SIGRENDERER *sigrenderer = dup_sigrenderer(checkpoint->sigrenderer, 0, checkpoint->sigrenderer->callbacks);
3489 checkpoint->sigrenderer->callbacks = NULL;
3490 if (!sigrenderer) {
3491 checkpoint->next = NULL;
3492 return checkpoint->time;
3493 }
3494
3495 l = it_sigrenderer_get_samples(sigrenderer, 0, 1.0f, IT_CHECKPOINT_INTERVAL, NULL);
3496 if (l < IT_CHECKPOINT_INTERVAL) {
3497 _dumb_it_end_sigrenderer(sigrenderer);
3498 checkpoint->next = NULL;
3499 return checkpoint->time + l;
3500 }
3501
3502 checkpoint->next = malloc(sizeof(*checkpoint->next));
3503 if (!checkpoint->next) {
3504 _dumb_it_end_sigrenderer(sigrenderer);
3505 return checkpoint->time + IT_CHECKPOINT_INTERVAL;
3506 }
3507
3508 checkpoint->next->time = checkpoint->time + IT_CHECKPOINT_INTERVAL;
3509 checkpoint = checkpoint->next;
3510 checkpoint->sigrenderer = sigrenderer;
3511 }
3512}