1 | // jar_xm.h - v0.01 - public domain - Joshua Reisenauer, MAR 2016 |
2 | // |
3 | // HISTORY: |
4 | // |
5 | // v0.01 2016-02-22 Setup |
6 | // |
7 | // |
8 | // USAGE: |
9 | // |
10 | // In ONE source file, put: |
11 | // |
12 | // #define JAR_XM_IMPLEMENTATION |
13 | // #include "jar_xm.h" |
14 | // |
15 | // Other source files should just include jar_xm.h |
16 | // |
17 | // SAMPLE CODE: |
18 | // |
19 | // jar_xm_context_t *musicptr; |
20 | // float musicBuffer[48000 / 60]; |
21 | // int intro_load(void) |
22 | // { |
23 | // jar_xm_create_context_from_file(&musicptr, 48000, "Song.XM"); |
24 | // return 1; |
25 | // } |
26 | // int intro_unload(void) |
27 | // { |
28 | // jar_xm_free_context(musicptr); |
29 | // return 1; |
30 | // } |
31 | // int intro_tick(long counter) |
32 | // { |
33 | // jar_xm_generate_samples(musicptr, musicBuffer, (48000 / 60) / 2); |
34 | // if(IsKeyDown(KEY_ENTER)) |
35 | // return 1; |
36 | // return 0; |
37 | // } |
38 | // |
39 | // |
40 | // LISCENSE - FOR LIBXM: |
41 | // |
42 | // Author: Romain "Artefact2" Dalmaso <artefact2@gmail.com> |
43 | // Contributor: Dan Spencer <dan@atomicpotato.net> |
44 | // Repackaged into jar_xm.h By: Joshua Adam Reisenauer <kd7tck@gmail.com> |
45 | // This program is free software. It comes without any warranty, to the |
46 | // extent permitted by applicable law. You can redistribute it and/or |
47 | // modify it under the terms of the Do What The Fuck You Want To Public |
48 | // License, Version 2, as published by Sam Hocevar. See |
49 | // http://sam.zoy.org/wtfpl/COPYING for more details. |
50 | |
51 | #ifndef INCLUDE_JAR_XM_H |
52 | #define INCLUDE_JAR_XM_H |
53 | |
54 | #include <stdint.h> |
55 | |
56 | #define JAR_XM_DEBUG 0 |
57 | #define JAR_XM_LINEAR_INTERPOLATION 1 // speed increase with decrease in quality |
58 | #define JAR_XM_DEFENSIVE 1 |
59 | #define JAR_XM_RAMPING 1 |
60 | |
61 | // Allow custom memory allocators |
62 | #ifndef JARXM_MALLOC |
63 | #define JARXM_MALLOC(sz) malloc(sz) |
64 | #endif |
65 | #ifndef JARXM_FREE |
66 | #define JARXM_FREE(p) free(p) |
67 | #endif |
68 | |
69 | //------------------------------------------------------------------------------- |
70 | struct jar_xm_context_s; |
71 | typedef struct jar_xm_context_s jar_xm_context_t; |
72 | |
73 | #ifdef __cplusplus |
74 | extern "C" { |
75 | #endif |
76 | |
77 | /** Create a XM context. |
78 | * |
79 | * @param moddata the contents of the module |
80 | * @param rate play rate in Hz, recommended value of 48000 |
81 | * |
82 | * @returns 0 on success |
83 | * @returns 1 if module data is not sane |
84 | * @returns 2 if memory allocation failed |
85 | * @returns 3 unable to open input file |
86 | * @returns 4 fseek() failed |
87 | * @returns 5 fread() failed |
88 | * @returns 6 unkown error |
89 | * |
90 | * @deprecated This function is unsafe! |
91 | * @see jar_xm_create_context_safe() |
92 | */ |
93 | int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename); |
94 | |
95 | /** Create a XM context. |
96 | * |
97 | * @param moddata the contents of the module |
98 | * @param rate play rate in Hz, recommended value of 48000 |
99 | * |
100 | * @returns 0 on success |
101 | * @returns 1 if module data is not sane |
102 | * @returns 2 if memory allocation failed |
103 | * |
104 | * @deprecated This function is unsafe! |
105 | * @see jar_xm_create_context_safe() |
106 | */ |
107 | int jar_xm_create_context(jar_xm_context_t** ctx, const char* moddata, uint32_t rate); |
108 | |
109 | /** Create a XM context. |
110 | * |
111 | * @param moddata the contents of the module |
112 | * @param moddata_length the length of the contents of the module, in bytes |
113 | * @param rate play rate in Hz, recommended value of 48000 |
114 | * |
115 | * @returns 0 on success |
116 | * @returns 1 if module data is not sane |
117 | * @returns 2 if memory allocation failed |
118 | */ |
119 | int jar_xm_create_context_safe(jar_xm_context_t** ctx, const char* moddata, size_t moddata_length, uint32_t rate); |
120 | |
121 | /** Free a XM context created by jar_xm_create_context(). */ |
122 | void jar_xm_free_context(jar_xm_context_t* ctx); |
123 | |
124 | /** Play the module and put the sound samples in an output buffer. |
125 | * |
126 | * @param output buffer of 2*numsamples elements (A left and right value for each sample) |
127 | * @param numsamples number of samples to generate |
128 | */ |
129 | void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsamples); |
130 | |
131 | /** Play the module, resample from 32 bit to 16 bit, and put the sound samples in an output buffer. |
132 | * |
133 | * @param output buffer of 2*numsamples elements (A left and right value for each sample) |
134 | * @param numsamples number of samples to generate |
135 | */ |
136 | void jar_xm_generate_samples_16bit(jar_xm_context_t* ctx, short* output, size_t numsamples) |
137 | { |
138 | float* musicBuffer = JARXM_MALLOC((2*numsamples)*sizeof(float)); |
139 | jar_xm_generate_samples(ctx, musicBuffer, numsamples); |
140 | |
141 | if(output){ |
142 | int x; |
143 | for(x=0;x<2*numsamples;x++) |
144 | output[x] = musicBuffer[x] * SHRT_MAX; |
145 | } |
146 | |
147 | JARXM_FREE(musicBuffer); |
148 | } |
149 | |
150 | /** Play the module, resample from 32 bit to 8 bit, and put the sound samples in an output buffer. |
151 | * |
152 | * @param output buffer of 2*numsamples elements (A left and right value for each sample) |
153 | * @param numsamples number of samples to generate |
154 | */ |
155 | void jar_xm_generate_samples_8bit(jar_xm_context_t* ctx, char* output, size_t numsamples) |
156 | { |
157 | float* musicBuffer = JARXM_MALLOC((2*numsamples)*sizeof(float)); |
158 | jar_xm_generate_samples(ctx, musicBuffer, numsamples); |
159 | |
160 | if(output){ |
161 | int x; |
162 | for(x=0;x<2*numsamples;x++) |
163 | output[x] = musicBuffer[x] * CHAR_MAX; |
164 | } |
165 | |
166 | JARXM_FREE(musicBuffer); |
167 | } |
168 | |
169 | |
170 | |
171 | /** Set the maximum number of times a module can loop. After the |
172 | * specified number of loops, calls to jar_xm_generate_samples will only |
173 | * generate silence. You can control the current number of loops with |
174 | * jar_xm_get_loop_count(). |
175 | * |
176 | * @param loopcnt maximum number of loops. Use 0 to loop |
177 | * indefinitely. */ |
178 | void jar_xm_set_max_loop_count(jar_xm_context_t* ctx, uint8_t loopcnt); |
179 | |
180 | /** Get the loop count of the currently playing module. This value is |
181 | * 0 when the module is still playing, 1 when the module has looped |
182 | * once, etc. */ |
183 | uint8_t jar_xm_get_loop_count(jar_xm_context_t* ctx); |
184 | |
185 | |
186 | |
187 | /** Mute or unmute a channel. |
188 | * |
189 | * @note Channel numbers go from 1 to jar_xm_get_number_of_channels(...). |
190 | * |
191 | * @return whether the channel was muted. |
192 | */ |
193 | bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t, bool); |
194 | |
195 | /** Mute or unmute an instrument. |
196 | * |
197 | * @note Instrument numbers go from 1 to |
198 | * jar_xm_get_number_of_instruments(...). |
199 | * |
200 | * @return whether the instrument was muted. |
201 | */ |
202 | bool jar_xm_mute_instrument(jar_xm_context_t* ctx, uint16_t, bool); |
203 | |
204 | |
205 | |
206 | /** Get the module name as a NUL-terminated string. */ |
207 | const char* jar_xm_get_module_name(jar_xm_context_t* ctx); |
208 | |
209 | /** Get the tracker name as a NUL-terminated string. */ |
210 | const char* jar_xm_get_tracker_name(jar_xm_context_t* ctx); |
211 | |
212 | |
213 | |
214 | /** Get the number of channels. */ |
215 | uint16_t jar_xm_get_number_of_channels(jar_xm_context_t* ctx); |
216 | |
217 | /** Get the module length (in patterns). */ |
218 | uint16_t jar_xm_get_module_length(jar_xm_context_t*); |
219 | |
220 | /** Get the number of patterns. */ |
221 | uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t* ctx); |
222 | |
223 | /** Get the number of rows of a pattern. |
224 | * |
225 | * @note Pattern numbers go from 0 to |
226 | * jar_xm_get_number_of_patterns(...)-1. |
227 | */ |
228 | uint16_t jar_xm_get_number_of_rows(jar_xm_context_t* ctx, uint16_t); |
229 | |
230 | /** Get the number of instruments. */ |
231 | uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t* ctx); |
232 | |
233 | /** Get the number of samples of an instrument. |
234 | * |
235 | * @note Instrument numbers go from 1 to |
236 | * jar_xm_get_number_of_instruments(...). |
237 | */ |
238 | uint16_t jar_xm_get_number_of_samples(jar_xm_context_t* ctx, uint16_t); |
239 | |
240 | |
241 | |
242 | /** Get the current module speed. |
243 | * |
244 | * @param bpm will receive the current BPM |
245 | * @param tempo will receive the current tempo (ticks per line) |
246 | */ |
247 | void jar_xm_get_playing_speed(jar_xm_context_t* ctx, uint16_t* bpm, uint16_t* tempo); |
248 | |
249 | /** Get the current position in the module being played. |
250 | * |
251 | * @param pattern_index if not NULL, will receive the current pattern |
252 | * index in the POT (pattern order table) |
253 | * |
254 | * @param pattern if not NULL, will receive the current pattern number |
255 | * |
256 | * @param row if not NULL, will receive the current row |
257 | * |
258 | * @param samples if not NULL, will receive the total number of |
259 | * generated samples (divide by sample rate to get seconds of |
260 | * generated audio) |
261 | */ |
262 | void jar_xm_get_position(jar_xm_context_t* ctx, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples); |
263 | |
264 | /** Get the latest time (in number of generated samples) when a |
265 | * particular instrument was triggered in any channel. |
266 | * |
267 | * @note Instrument numbers go from 1 to |
268 | * jar_xm_get_number_of_instruments(...). |
269 | */ |
270 | uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t* ctx, uint16_t); |
271 | |
272 | /** Get the latest time (in number of generated samples) when a |
273 | * particular sample was triggered in any channel. |
274 | * |
275 | * @note Instrument numbers go from 1 to |
276 | * jar_xm_get_number_of_instruments(...). |
277 | * |
278 | * @note Sample numbers go from 0 to |
279 | * jar_xm_get_nubmer_of_samples(...,instr)-1. |
280 | */ |
281 | uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t* ctx, uint16_t instr, uint16_t sample); |
282 | |
283 | /** Get the latest time (in number of generated samples) when any |
284 | * instrument was triggered in a given channel. |
285 | * |
286 | * @note Channel numbers go from 1 to jar_xm_get_number_of_channels(...). |
287 | */ |
288 | uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t* ctx, uint16_t); |
289 | |
290 | /** Get the number of remaining samples. Divide by 2 to get the number of individual LR data samples. |
291 | * |
292 | * @note This is the remaining number of samples before the loop starts module again, or halts if on last pass. |
293 | * @note This function is very slow and should only be run once, if at all. |
294 | */ |
295 | uint64_t jar_xm_get_remaining_samples(jar_xm_context_t* ctx); |
296 | |
297 | #ifdef __cplusplus |
298 | } |
299 | #endif |
300 | //------------------------------------------------------------------------------- |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | //Function Definitions----------------------------------------------------------- |
308 | #ifdef JAR_XM_IMPLEMENTATION |
309 | |
310 | #include <math.h> |
311 | #include <stdio.h> |
312 | #include <stdlib.h> |
313 | #include <limits.h> |
314 | #include <string.h> |
315 | |
316 | #if JAR_XM_DEBUG //JAR_XM_DEBUG defined as 0 |
317 | #include <stdio.h> |
318 | #define DEBUG(fmt, ...) do { \ |
319 | fprintf(stderr, "%s(): " fmt "\n", __func__, __VA_ARGS__); \ |
320 | fflush(stderr); \ |
321 | } while(0) |
322 | #else |
323 | #define DEBUG(...) |
324 | #endif |
325 | |
326 | #if jar_xm_BIG_ENDIAN |
327 | #error "Big endian platforms are not yet supported, sorry" |
328 | /* Make sure the compiler stops, even if #error is ignored */ |
329 | extern int __fail[-1]; |
330 | #endif |
331 | |
332 | /* ----- XM constants ----- */ |
333 | |
334 | #define SAMPLE_NAME_LENGTH 22 |
335 | #define INSTRUMENT_NAME_LENGTH 22 |
336 | #define MODULE_NAME_LENGTH 20 |
337 | #define TRACKER_NAME_LENGTH 20 |
338 | #define PATTERN_ORDER_TABLE_LENGTH 256 |
339 | #define NUM_NOTES 96 |
340 | #define NUM_ENVELOPE_POINTS 12 |
341 | #define MAX_NUM_ROWS 256 |
342 | |
343 | #if JAR_XM_RAMPING |
344 | #define jar_xm_SAMPLE_RAMPING_POINTS 0x20 |
345 | #endif |
346 | |
347 | /* ----- Data types ----- */ |
348 | |
349 | enum jar_xm_waveform_type_e { |
350 | jar_xm_SINE_WAVEFORM = 0, |
351 | jar_xm_RAMP_DOWN_WAVEFORM = 1, |
352 | jar_xm_SQUARE_WAVEFORM = 2, |
353 | jar_xm_RANDOM_WAVEFORM = 3, |
354 | jar_xm_RAMP_UP_WAVEFORM = 4, |
355 | }; |
356 | typedef enum jar_xm_waveform_type_e jar_xm_waveform_type_t; |
357 | |
358 | enum jar_xm_loop_type_e { |
359 | jar_xm_NO_LOOP, |
360 | jar_xm_FORWARD_LOOP, |
361 | jar_xm_PING_PONG_LOOP, |
362 | }; |
363 | typedef enum jar_xm_loop_type_e jar_xm_loop_type_t; |
364 | |
365 | enum jar_xm_frequency_type_e { |
366 | jar_xm_LINEAR_FREQUENCIES, |
367 | jar_xm_AMIGA_FREQUENCIES, |
368 | }; |
369 | typedef enum jar_xm_frequency_type_e jar_xm_frequency_type_t; |
370 | |
371 | struct jar_xm_envelope_point_s { |
372 | uint16_t frame; |
373 | uint16_t value; |
374 | }; |
375 | typedef struct jar_xm_envelope_point_s jar_xm_envelope_point_t; |
376 | |
377 | struct jar_xm_envelope_s { |
378 | jar_xm_envelope_point_t points[NUM_ENVELOPE_POINTS]; |
379 | uint8_t num_points; |
380 | uint8_t sustain_point; |
381 | uint8_t loop_start_point; |
382 | uint8_t loop_end_point; |
383 | bool enabled; |
384 | bool sustain_enabled; |
385 | bool loop_enabled; |
386 | }; |
387 | typedef struct jar_xm_envelope_s jar_xm_envelope_t; |
388 | |
389 | struct jar_xm_sample_s { |
390 | char name[SAMPLE_NAME_LENGTH + 1]; |
391 | int8_t bits; /* Either 8 or 16 */ |
392 | |
393 | uint32_t length; |
394 | uint32_t loop_start; |
395 | uint32_t loop_length; |
396 | uint32_t loop_end; |
397 | float volume; |
398 | int8_t finetune; |
399 | jar_xm_loop_type_t loop_type; |
400 | float panning; |
401 | int8_t relative_note; |
402 | uint64_t latest_trigger; |
403 | |
404 | float* data; |
405 | }; |
406 | typedef struct jar_xm_sample_s jar_xm_sample_t; |
407 | |
408 | struct jar_xm_instrument_s { |
409 | char name[INSTRUMENT_NAME_LENGTH + 1]; |
410 | uint16_t num_samples; |
411 | uint8_t sample_of_notes[NUM_NOTES]; |
412 | jar_xm_envelope_t volume_envelope; |
413 | jar_xm_envelope_t panning_envelope; |
414 | jar_xm_waveform_type_t vibrato_type; |
415 | uint8_t vibrato_sweep; |
416 | uint8_t vibrato_depth; |
417 | uint8_t vibrato_rate; |
418 | uint16_t volume_fadeout; |
419 | uint64_t latest_trigger; |
420 | bool muted; |
421 | |
422 | jar_xm_sample_t* samples; |
423 | }; |
424 | typedef struct jar_xm_instrument_s jar_xm_instrument_t; |
425 | |
426 | struct jar_xm_pattern_slot_s { |
427 | uint8_t note; /* 1-96, 97 = Key Off note */ |
428 | uint8_t instrument; /* 1-128 */ |
429 | uint8_t volume_column; |
430 | uint8_t effect_type; |
431 | uint8_t effect_param; |
432 | }; |
433 | typedef struct jar_xm_pattern_slot_s jar_xm_pattern_slot_t; |
434 | |
435 | struct jar_xm_pattern_s { |
436 | uint16_t num_rows; |
437 | jar_xm_pattern_slot_t* slots; /* Array of size num_rows * num_channels */ |
438 | }; |
439 | typedef struct jar_xm_pattern_s jar_xm_pattern_t; |
440 | |
441 | struct jar_xm_module_s { |
442 | char name[MODULE_NAME_LENGTH + 1]; |
443 | char trackername[TRACKER_NAME_LENGTH + 1]; |
444 | uint16_t length; |
445 | uint16_t restart_position; |
446 | uint16_t num_channels; |
447 | uint16_t num_patterns; |
448 | uint16_t num_instruments; |
449 | jar_xm_frequency_type_t frequency_type; |
450 | uint8_t pattern_table[PATTERN_ORDER_TABLE_LENGTH]; |
451 | |
452 | jar_xm_pattern_t* patterns; |
453 | jar_xm_instrument_t* instruments; /* Instrument 1 has index 0, |
454 | * instrument 2 has index 1, etc. */ |
455 | }; |
456 | typedef struct jar_xm_module_s jar_xm_module_t; |
457 | |
458 | struct jar_xm_channel_context_s { |
459 | float note; |
460 | float orig_note; /* The original note before effect modifications, as read in the pattern. */ |
461 | jar_xm_instrument_t* instrument; /* Could be NULL */ |
462 | jar_xm_sample_t* sample; /* Could be NULL */ |
463 | jar_xm_pattern_slot_t* current; |
464 | |
465 | float sample_position; |
466 | float period; |
467 | float frequency; |
468 | float step; |
469 | bool ping; /* For ping-pong samples: true is -->, false is <-- */ |
470 | |
471 | float volume; /* Ideally between 0 (muted) and 1 (loudest) */ |
472 | float panning; /* Between 0 (left) and 1 (right); 0.5 is centered */ |
473 | |
474 | uint16_t autovibrato_ticks; |
475 | |
476 | bool sustained; |
477 | float fadeout_volume; |
478 | float volume_envelope_volume; |
479 | float panning_envelope_panning; |
480 | uint16_t volume_envelope_frame_count; |
481 | uint16_t panning_envelope_frame_count; |
482 | |
483 | float autovibrato_note_offset; |
484 | |
485 | bool arp_in_progress; |
486 | uint8_t arp_note_offset; |
487 | uint8_t volume_slide_param; |
488 | uint8_t fine_volume_slide_param; |
489 | uint8_t global_volume_slide_param; |
490 | uint8_t panning_slide_param; |
491 | uint8_t portamento_up_param; |
492 | uint8_t portamento_down_param; |
493 | uint8_t fine_portamento_up_param; |
494 | uint8_t fine_portamento_down_param; |
495 | uint8_t ; |
496 | uint8_t ; |
497 | uint8_t tone_portamento_param; |
498 | float tone_portamento_target_period; |
499 | uint8_t multi_retrig_param; |
500 | uint8_t note_delay_param; |
501 | uint8_t pattern_loop_origin; /* Where to restart a E6y loop */ |
502 | uint8_t pattern_loop_count; /* How many loop passes have been done */ |
503 | bool vibrato_in_progress; |
504 | jar_xm_waveform_type_t vibrato_waveform; |
505 | bool vibrato_waveform_retrigger; /* True if a new note retriggers the waveform */ |
506 | uint8_t vibrato_param; |
507 | uint16_t vibrato_ticks; /* Position in the waveform */ |
508 | float vibrato_note_offset; |
509 | jar_xm_waveform_type_t tremolo_waveform; |
510 | bool tremolo_waveform_retrigger; |
511 | uint8_t tremolo_param; |
512 | uint8_t tremolo_ticks; |
513 | float tremolo_volume; |
514 | uint8_t tremor_param; |
515 | bool tremor_on; |
516 | |
517 | uint64_t latest_trigger; |
518 | bool muted; |
519 | |
520 | #if JAR_XM_RAMPING |
521 | /* These values are updated at the end of each tick, to save |
522 | * a couple of float operations on every generated sample. */ |
523 | float target_panning; |
524 | float target_volume; |
525 | |
526 | unsigned long frame_count; |
527 | float end_of_previous_sample[jar_xm_SAMPLE_RAMPING_POINTS]; |
528 | #endif |
529 | |
530 | float actual_panning; |
531 | float actual_volume; |
532 | }; |
533 | typedef struct jar_xm_channel_context_s jar_xm_channel_context_t; |
534 | |
535 | struct jar_xm_context_s { |
536 | void* allocated_memory; |
537 | jar_xm_module_t module; |
538 | uint32_t rate; |
539 | |
540 | uint16_t tempo; |
541 | uint16_t bpm; |
542 | float global_volume; |
543 | float amplification; |
544 | |
545 | #if JAR_XM_RAMPING |
546 | /* How much is a channel final volume allowed to change per |
547 | * sample; this is used to avoid abrubt volume changes which |
548 | * manifest as "clicks" in the generated sound. */ |
549 | float volume_ramp; |
550 | float panning_ramp; /* Same for panning. */ |
551 | #endif |
552 | |
553 | uint8_t current_table_index; |
554 | uint8_t current_row; |
555 | uint16_t current_tick; /* Can go below 255, with high tempo and a pattern delay */ |
556 | float remaining_samples_in_tick; |
557 | uint64_t generated_samples; |
558 | |
559 | bool position_jump; |
560 | bool pattern_break; |
561 | uint8_t jump_dest; |
562 | uint8_t jump_row; |
563 | |
564 | /* Extra ticks to be played before going to the next row - |
565 | * Used for EEy effect */ |
566 | uint16_t ; |
567 | |
568 | uint8_t* row_loop_count; /* Array of size MAX_NUM_ROWS * module_length */ |
569 | uint8_t loop_count; |
570 | uint8_t max_loop_count; |
571 | |
572 | jar_xm_channel_context_t* channels; |
573 | }; |
574 | |
575 | /* ----- Internal API ----- */ |
576 | |
577 | #if JAR_XM_DEFENSIVE |
578 | |
579 | /** Check the module data for errors/inconsistencies. |
580 | * |
581 | * @returns 0 if everything looks OK. Module should be safe to load. |
582 | */ |
583 | int jar_xm_check_sanity_preload(const char*, size_t); |
584 | |
585 | /** Check a loaded module for errors/inconsistencies. |
586 | * |
587 | * @returns 0 if everything looks OK. |
588 | */ |
589 | int jar_xm_check_sanity_postload(jar_xm_context_t*); |
590 | |
591 | #endif |
592 | |
593 | /** Get the number of bytes needed to store the module data in a |
594 | * dynamically allocated blank context. |
595 | * |
596 | * Things that are dynamically allocated: |
597 | * - sample data |
598 | * - sample structures in instruments |
599 | * - pattern data |
600 | * - row loop count arrays |
601 | * - pattern structures in module |
602 | * - instrument structures in module |
603 | * - channel contexts |
604 | * - context structure itself |
605 | |
606 | * @returns 0 if everything looks OK. |
607 | */ |
608 | size_t jar_xm_get_memory_needed_for_context(const char*, size_t); |
609 | |
610 | /** Populate the context from module data. |
611 | * |
612 | * @returns pointer to the memory pool |
613 | */ |
614 | char* jar_xm_load_module(jar_xm_context_t*, const char*, size_t, char*); |
615 | |
616 | int jar_xm_create_context(jar_xm_context_t** ctxp, const char* moddata, uint32_t rate) { |
617 | return jar_xm_create_context_safe(ctxp, moddata, SIZE_MAX, rate); |
618 | } |
619 | |
620 | #define ALIGN(x, b) (((x) + ((b) - 1)) & ~((b) - 1)) |
621 | #define ALIGN_PTR(x, b) (void*)(((uintptr_t)(x) + ((b) - 1)) & ~((b) - 1)) |
622 | int jar_xm_create_context_safe(jar_xm_context_t** ctxp, const char* moddata, size_t moddata_length, uint32_t rate) { |
623 | #if JAR_XM_DEFENSIVE |
624 | int ret; |
625 | #endif |
626 | size_t bytes_needed; |
627 | char* mempool; |
628 | jar_xm_context_t* ctx; |
629 | |
630 | #if JAR_XM_DEFENSIVE |
631 | if((ret = jar_xm_check_sanity_preload(moddata, moddata_length))) { |
632 | DEBUG("jar_xm_check_sanity_preload() returned %i, module is not safe to load" , ret); |
633 | return 1; |
634 | } |
635 | #endif |
636 | |
637 | bytes_needed = jar_xm_get_memory_needed_for_context(moddata, moddata_length); |
638 | mempool = JARXM_MALLOC(bytes_needed); |
639 | if(mempool == NULL && bytes_needed > 0) { |
640 | /* JARXM_MALLOC() failed, trouble ahead */ |
641 | DEBUG("call to JARXM_MALLOC() failed, returned %p" , (void*)mempool); |
642 | return 2; |
643 | } |
644 | |
645 | /* Initialize most of the fields to 0, 0.f, NULL or false depending on type */ |
646 | memset(mempool, 0, bytes_needed); |
647 | |
648 | ctx = (*ctxp = (jar_xm_context_t *)mempool); |
649 | ctx->allocated_memory = mempool; /* Keep original pointer for JARXM_FREE() */ |
650 | mempool += sizeof(jar_xm_context_t); |
651 | |
652 | ctx->rate = rate; |
653 | mempool = jar_xm_load_module(ctx, moddata, moddata_length, mempool); |
654 | mempool = ALIGN_PTR(mempool, 16); |
655 | |
656 | ctx->channels = (jar_xm_channel_context_t*)mempool; |
657 | mempool += ctx->module.num_channels * sizeof(jar_xm_channel_context_t); |
658 | mempool = ALIGN_PTR(mempool, 16); |
659 | |
660 | ctx->global_volume = 1.f; |
661 | ctx->amplification = .25f; /* XXX: some bad modules may still clip. Find out something better. */ |
662 | |
663 | #if JAR_XM_RAMPING |
664 | ctx->volume_ramp = (1.f / 128.f); |
665 | ctx->panning_ramp = (1.f / 128.f); |
666 | #endif |
667 | |
668 | for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { |
669 | jar_xm_channel_context_t* ch = ctx->channels + i; |
670 | |
671 | ch->ping = true; |
672 | ch->vibrato_waveform = jar_xm_SINE_WAVEFORM; |
673 | ch->vibrato_waveform_retrigger = true; |
674 | ch->tremolo_waveform = jar_xm_SINE_WAVEFORM; |
675 | ch->tremolo_waveform_retrigger = true; |
676 | |
677 | ch->volume = ch->volume_envelope_volume = ch->fadeout_volume = 1.0f; |
678 | ch->panning = ch->panning_envelope_panning = .5f; |
679 | ch->actual_volume = .0f; |
680 | ch->actual_panning = .5f; |
681 | } |
682 | |
683 | mempool = ALIGN_PTR(mempool, 16); |
684 | ctx->row_loop_count = (uint8_t*)mempool; |
685 | mempool += MAX_NUM_ROWS * sizeof(uint8_t); |
686 | |
687 | #if JAR_XM_DEFENSIVE |
688 | if((ret = jar_xm_check_sanity_postload(ctx))) { |
689 | DEBUG("jar_xm_check_sanity_postload() returned %i, module is not safe to play" , ret); |
690 | jar_xm_free_context(ctx); |
691 | return 1; |
692 | } |
693 | #endif |
694 | |
695 | return 0; |
696 | } |
697 | |
698 | void jar_xm_free_context(jar_xm_context_t* ctx) { |
699 | JARXM_FREE(ctx->allocated_memory); |
700 | } |
701 | |
702 | void jar_xm_set_max_loop_count(jar_xm_context_t* ctx, uint8_t loopcnt) { |
703 | ctx->max_loop_count = loopcnt; |
704 | } |
705 | |
706 | uint8_t jar_xm_get_loop_count(jar_xm_context_t* ctx) { |
707 | return ctx->loop_count; |
708 | } |
709 | |
710 | bool jar_xm_mute_channel(jar_xm_context_t* ctx, uint16_t channel, bool mute) { |
711 | bool old = ctx->channels[channel - 1].muted; |
712 | ctx->channels[channel - 1].muted = mute; |
713 | return old; |
714 | } |
715 | |
716 | bool jar_xm_mute_instrument(jar_xm_context_t* ctx, uint16_t instr, bool mute) { |
717 | bool old = ctx->module.instruments[instr - 1].muted; |
718 | ctx->module.instruments[instr - 1].muted = mute; |
719 | return old; |
720 | } |
721 | |
722 | |
723 | |
724 | const char* jar_xm_get_module_name(jar_xm_context_t* ctx) { |
725 | return ctx->module.name; |
726 | } |
727 | |
728 | const char* jar_xm_get_tracker_name(jar_xm_context_t* ctx) { |
729 | return ctx->module.trackername; |
730 | } |
731 | |
732 | |
733 | |
734 | uint16_t jar_xm_get_number_of_channels(jar_xm_context_t* ctx) { |
735 | return ctx->module.num_channels; |
736 | } |
737 | |
738 | uint16_t jar_xm_get_module_length(jar_xm_context_t* ctx) { |
739 | return ctx->module.length; |
740 | } |
741 | |
742 | uint16_t jar_xm_get_number_of_patterns(jar_xm_context_t* ctx) { |
743 | return ctx->module.num_patterns; |
744 | } |
745 | |
746 | uint16_t jar_xm_get_number_of_rows(jar_xm_context_t* ctx, uint16_t pattern) { |
747 | return ctx->module.patterns[pattern].num_rows; |
748 | } |
749 | |
750 | uint16_t jar_xm_get_number_of_instruments(jar_xm_context_t* ctx) { |
751 | return ctx->module.num_instruments; |
752 | } |
753 | |
754 | uint16_t jar_xm_get_number_of_samples(jar_xm_context_t* ctx, uint16_t instrument) { |
755 | return ctx->module.instruments[instrument - 1].num_samples; |
756 | } |
757 | |
758 | |
759 | |
760 | void jar_xm_get_playing_speed(jar_xm_context_t* ctx, uint16_t* bpm, uint16_t* tempo) { |
761 | if(bpm) *bpm = ctx->bpm; |
762 | if(tempo) *tempo = ctx->tempo; |
763 | } |
764 | |
765 | void jar_xm_get_position(jar_xm_context_t* ctx, uint8_t* pattern_index, uint8_t* pattern, uint8_t* row, uint64_t* samples) { |
766 | if(pattern_index) *pattern_index = ctx->current_table_index; |
767 | if(pattern) *pattern = ctx->module.pattern_table[ctx->current_table_index]; |
768 | if(row) *row = ctx->current_row; |
769 | if(samples) *samples = ctx->generated_samples; |
770 | } |
771 | |
772 | uint64_t jar_xm_get_latest_trigger_of_instrument(jar_xm_context_t* ctx, uint16_t instr) { |
773 | return ctx->module.instruments[instr - 1].latest_trigger; |
774 | } |
775 | |
776 | uint64_t jar_xm_get_latest_trigger_of_sample(jar_xm_context_t* ctx, uint16_t instr, uint16_t sample) { |
777 | return ctx->module.instruments[instr - 1].samples[sample].latest_trigger; |
778 | } |
779 | |
780 | uint64_t jar_xm_get_latest_trigger_of_channel(jar_xm_context_t* ctx, uint16_t chn) { |
781 | return ctx->channels[chn - 1].latest_trigger; |
782 | } |
783 | |
784 | /* .xm files are little-endian. (XXX: Are they really?) */ |
785 | |
786 | /* Bounded reader macros. |
787 | * If we attempt to read the buffer out-of-bounds, pretend that the buffer is |
788 | * infinitely padded with zeroes. |
789 | */ |
790 | #define READ_U8(offset) (((offset) < moddata_length) ? (*(uint8_t*)(moddata + (offset))) : 0) |
791 | #define READ_U16(offset) ((uint16_t)READ_U8(offset) | ((uint16_t)READ_U8((offset) + 1) << 8)) |
792 | #define READ_U32(offset) ((uint32_t)READ_U16(offset) | ((uint32_t)READ_U16((offset) + 2) << 16)) |
793 | #define READ_MEMCPY(ptr, offset, length) memcpy_pad(ptr, length, moddata, moddata_length, offset) |
794 | |
795 | static void memcpy_pad(void* dst, size_t dst_len, const void* src, size_t src_len, size_t offset) { |
796 | uint8_t* dst_c = dst; |
797 | const uint8_t* src_c = src; |
798 | |
799 | /* how many bytes can be copied without overrunning `src` */ |
800 | size_t copy_bytes = (src_len >= offset) ? (src_len - offset) : 0; |
801 | copy_bytes = copy_bytes > dst_len ? dst_len : copy_bytes; |
802 | |
803 | memcpy(dst_c, src_c + offset, copy_bytes); |
804 | /* padded bytes */ |
805 | memset(dst_c + copy_bytes, 0, dst_len - copy_bytes); |
806 | } |
807 | |
808 | #if JAR_XM_DEFENSIVE |
809 | |
810 | int jar_xm_check_sanity_preload(const char* module, size_t module_length) { |
811 | if(module_length < 60) { |
812 | return 4; |
813 | } |
814 | |
815 | if(memcmp("Extended Module: " , module, 17) != 0) { |
816 | return 1; |
817 | } |
818 | |
819 | if(module[37] != 0x1A) { |
820 | return 2; |
821 | } |
822 | |
823 | if(module[59] != 0x01 || module[58] != 0x04) { |
824 | /* Not XM 1.04 */ |
825 | return 3; |
826 | } |
827 | |
828 | return 0; |
829 | } |
830 | |
831 | int jar_xm_check_sanity_postload(jar_xm_context_t* ctx) { |
832 | /* @todo: plenty of stuff to do here… */ |
833 | |
834 | /* Check the POT */ |
835 | for(uint8_t i = 0; i < ctx->module.length; ++i) { |
836 | if(ctx->module.pattern_table[i] >= ctx->module.num_patterns) { |
837 | if(i+1 == ctx->module.length && ctx->module.length > 1) { |
838 | /* Cheap fix */ |
839 | --ctx->module.length; |
840 | DEBUG("trimming invalid POT at pos %X" , i); |
841 | } else { |
842 | DEBUG("module has invalid POT, pos %X references nonexistent pattern %X" , |
843 | i, |
844 | ctx->module.pattern_table[i]); |
845 | return 1; |
846 | } |
847 | } |
848 | } |
849 | |
850 | return 0; |
851 | } |
852 | |
853 | #endif |
854 | |
855 | size_t jar_xm_get_memory_needed_for_context(const char* moddata, size_t moddata_length) { |
856 | size_t memory_needed = 0; |
857 | size_t offset = 60; /* Skip the first header */ |
858 | uint16_t num_channels; |
859 | uint16_t num_patterns; |
860 | uint16_t num_instruments; |
861 | |
862 | /* Read the module header */ |
863 | num_channels = READ_U16(offset + 8); |
864 | |
865 | num_patterns = READ_U16(offset + 10); |
866 | memory_needed += num_patterns * sizeof(jar_xm_pattern_t); |
867 | memory_needed = ALIGN(memory_needed, 16); |
868 | |
869 | num_instruments = READ_U16(offset + 12); |
870 | memory_needed += num_instruments * sizeof(jar_xm_instrument_t); |
871 | memory_needed = ALIGN(memory_needed, 16); |
872 | |
873 | memory_needed += MAX_NUM_ROWS * READ_U16(offset + 4) * sizeof(uint8_t); /* Module length */ |
874 | |
875 | /* Header size */ |
876 | offset += READ_U32(offset); |
877 | |
878 | /* Read pattern headers */ |
879 | for(uint16_t i = 0; i < num_patterns; ++i) { |
880 | uint16_t num_rows; |
881 | |
882 | num_rows = READ_U16(offset + 5); |
883 | memory_needed += num_rows * num_channels * sizeof(jar_xm_pattern_slot_t); |
884 | |
885 | /* Pattern header length + packed pattern data size */ |
886 | offset += READ_U32(offset) + READ_U16(offset + 7); |
887 | } |
888 | memory_needed = ALIGN(memory_needed, 16); |
889 | |
890 | /* Read instrument headers */ |
891 | for(uint16_t i = 0; i < num_instruments; ++i) { |
892 | uint16_t num_samples; |
893 | uint32_t = 0; |
894 | uint32_t sample_size_aggregate = 0; |
895 | |
896 | num_samples = READ_U16(offset + 27); |
897 | memory_needed += num_samples * sizeof(jar_xm_sample_t); |
898 | |
899 | if(num_samples > 0) { |
900 | sample_header_size = READ_U32(offset + 29); |
901 | } |
902 | |
903 | /* Instrument header size */ |
904 | offset += READ_U32(offset); |
905 | |
906 | for(uint16_t j = 0; j < num_samples; ++j) { |
907 | uint32_t sample_size; |
908 | uint8_t flags; |
909 | |
910 | sample_size = READ_U32(offset); |
911 | flags = READ_U8(offset + 14); |
912 | sample_size_aggregate += sample_size; |
913 | |
914 | if(flags & (1 << 4)) { |
915 | /* 16 bit sample */ |
916 | memory_needed += sample_size * (sizeof(float) >> 1); |
917 | } else { |
918 | /* 8 bit sample */ |
919 | memory_needed += sample_size * sizeof(float); |
920 | } |
921 | |
922 | offset += sample_header_size; |
923 | } |
924 | |
925 | offset += sample_size_aggregate; |
926 | } |
927 | |
928 | memory_needed += num_channels * sizeof(jar_xm_channel_context_t); |
929 | memory_needed += sizeof(jar_xm_context_t); |
930 | |
931 | return memory_needed; |
932 | } |
933 | |
934 | char* jar_xm_load_module(jar_xm_context_t* ctx, const char* moddata, size_t moddata_length, char* mempool) { |
935 | size_t offset = 0; |
936 | jar_xm_module_t* mod = &(ctx->module); |
937 | |
938 | /* Read XM header */ |
939 | READ_MEMCPY(mod->name, offset + 17, MODULE_NAME_LENGTH); |
940 | READ_MEMCPY(mod->trackername, offset + 38, TRACKER_NAME_LENGTH); |
941 | offset += 60; |
942 | |
943 | /* Read module header */ |
944 | uint32_t = READ_U32(offset); |
945 | |
946 | mod->length = READ_U16(offset + 4); |
947 | mod->restart_position = READ_U16(offset + 6); |
948 | mod->num_channels = READ_U16(offset + 8); |
949 | mod->num_patterns = READ_U16(offset + 10); |
950 | mod->num_instruments = READ_U16(offset + 12); |
951 | |
952 | mod->patterns = (jar_xm_pattern_t*)mempool; |
953 | mempool += mod->num_patterns * sizeof(jar_xm_pattern_t); |
954 | mempool = ALIGN_PTR(mempool, 16); |
955 | |
956 | mod->instruments = (jar_xm_instrument_t*)mempool; |
957 | mempool += mod->num_instruments * sizeof(jar_xm_instrument_t); |
958 | mempool = ALIGN_PTR(mempool, 16); |
959 | |
960 | uint16_t flags = READ_U32(offset + 14); |
961 | mod->frequency_type = (flags & (1 << 0)) ? jar_xm_LINEAR_FREQUENCIES : jar_xm_AMIGA_FREQUENCIES; |
962 | |
963 | ctx->tempo = READ_U16(offset + 16); |
964 | ctx->bpm = READ_U16(offset + 18); |
965 | |
966 | READ_MEMCPY(mod->pattern_table, offset + 20, PATTERN_ORDER_TABLE_LENGTH); |
967 | offset += header_size; |
968 | |
969 | /* Read patterns */ |
970 | for(uint16_t i = 0; i < mod->num_patterns; ++i) { |
971 | uint16_t packed_patterndata_size = READ_U16(offset + 7); |
972 | jar_xm_pattern_t* pat = mod->patterns + i; |
973 | |
974 | pat->num_rows = READ_U16(offset + 5); |
975 | |
976 | pat->slots = (jar_xm_pattern_slot_t*)mempool; |
977 | mempool += mod->num_channels * pat->num_rows * sizeof(jar_xm_pattern_slot_t); |
978 | |
979 | /* Pattern header length */ |
980 | offset += READ_U32(offset); |
981 | |
982 | if(packed_patterndata_size == 0) { |
983 | /* No pattern data is present */ |
984 | memset(pat->slots, 0, sizeof(jar_xm_pattern_slot_t) * pat->num_rows * mod->num_channels); |
985 | } else { |
986 | /* This isn't your typical for loop */ |
987 | for(uint16_t j = 0, k = 0; j < packed_patterndata_size; ++k) { |
988 | uint8_t note = READ_U8(offset + j); |
989 | jar_xm_pattern_slot_t* slot = pat->slots + k; |
990 | |
991 | if(note & (1 << 7)) { |
992 | /* MSB is set, this is a compressed packet */ |
993 | ++j; |
994 | |
995 | if(note & (1 << 0)) { |
996 | /* Note follows */ |
997 | slot->note = READ_U8(offset + j); |
998 | ++j; |
999 | } else { |
1000 | slot->note = 0; |
1001 | } |
1002 | |
1003 | if(note & (1 << 1)) { |
1004 | /* Instrument follows */ |
1005 | slot->instrument = READ_U8(offset + j); |
1006 | ++j; |
1007 | } else { |
1008 | slot->instrument = 0; |
1009 | } |
1010 | |
1011 | if(note & (1 << 2)) { |
1012 | /* Volume column follows */ |
1013 | slot->volume_column = READ_U8(offset + j); |
1014 | ++j; |
1015 | } else { |
1016 | slot->volume_column = 0; |
1017 | } |
1018 | |
1019 | if(note & (1 << 3)) { |
1020 | /* Effect follows */ |
1021 | slot->effect_type = READ_U8(offset + j); |
1022 | ++j; |
1023 | } else { |
1024 | slot->effect_type = 0; |
1025 | } |
1026 | |
1027 | if(note & (1 << 4)) { |
1028 | /* Effect parameter follows */ |
1029 | slot->effect_param = READ_U8(offset + j); |
1030 | ++j; |
1031 | } else { |
1032 | slot->effect_param = 0; |
1033 | } |
1034 | } else { |
1035 | /* Uncompressed packet */ |
1036 | slot->note = note; |
1037 | slot->instrument = READ_U8(offset + j + 1); |
1038 | slot->volume_column = READ_U8(offset + j + 2); |
1039 | slot->effect_type = READ_U8(offset + j + 3); |
1040 | slot->effect_param = READ_U8(offset + j + 4); |
1041 | j += 5; |
1042 | } |
1043 | } |
1044 | } |
1045 | |
1046 | offset += packed_patterndata_size; |
1047 | } |
1048 | mempool = ALIGN_PTR(mempool, 16); |
1049 | |
1050 | /* Read instruments */ |
1051 | for(uint16_t i = 0; i < ctx->module.num_instruments; ++i) { |
1052 | uint32_t = 0; |
1053 | jar_xm_instrument_t* instr = mod->instruments + i; |
1054 | |
1055 | READ_MEMCPY(instr->name, offset + 4, INSTRUMENT_NAME_LENGTH); |
1056 | instr->num_samples = READ_U16(offset + 27); |
1057 | |
1058 | if(instr->num_samples > 0) { |
1059 | /* Read extra header properties */ |
1060 | sample_header_size = READ_U32(offset + 29); |
1061 | READ_MEMCPY(instr->sample_of_notes, offset + 33, NUM_NOTES); |
1062 | |
1063 | instr->volume_envelope.num_points = READ_U8(offset + 225); |
1064 | instr->panning_envelope.num_points = READ_U8(offset + 226); |
1065 | |
1066 | for(uint8_t j = 0; j < instr->volume_envelope.num_points; ++j) { |
1067 | instr->volume_envelope.points[j].frame = READ_U16(offset + 129 + 4 * j); |
1068 | instr->volume_envelope.points[j].value = READ_U16(offset + 129 + 4 * j + 2); |
1069 | } |
1070 | |
1071 | for(uint8_t j = 0; j < instr->panning_envelope.num_points; ++j) { |
1072 | instr->panning_envelope.points[j].frame = READ_U16(offset + 177 + 4 * j); |
1073 | instr->panning_envelope.points[j].value = READ_U16(offset + 177 + 4 * j + 2); |
1074 | } |
1075 | |
1076 | instr->volume_envelope.sustain_point = READ_U8(offset + 227); |
1077 | instr->volume_envelope.loop_start_point = READ_U8(offset + 228); |
1078 | instr->volume_envelope.loop_end_point = READ_U8(offset + 229); |
1079 | |
1080 | instr->panning_envelope.sustain_point = READ_U8(offset + 230); |
1081 | instr->panning_envelope.loop_start_point = READ_U8(offset + 231); |
1082 | instr->panning_envelope.loop_end_point = READ_U8(offset + 232); |
1083 | |
1084 | uint8_t flags = READ_U8(offset + 233); |
1085 | instr->volume_envelope.enabled = flags & (1 << 0); |
1086 | instr->volume_envelope.sustain_enabled = flags & (1 << 1); |
1087 | instr->volume_envelope.loop_enabled = flags & (1 << 2); |
1088 | |
1089 | flags = READ_U8(offset + 234); |
1090 | instr->panning_envelope.enabled = flags & (1 << 0); |
1091 | instr->panning_envelope.sustain_enabled = flags & (1 << 1); |
1092 | instr->panning_envelope.loop_enabled = flags & (1 << 2); |
1093 | |
1094 | instr->vibrato_type = READ_U8(offset + 235); |
1095 | if(instr->vibrato_type == 2) { |
1096 | instr->vibrato_type = 1; |
1097 | } else if(instr->vibrato_type == 1) { |
1098 | instr->vibrato_type = 2; |
1099 | } |
1100 | instr->vibrato_sweep = READ_U8(offset + 236); |
1101 | instr->vibrato_depth = READ_U8(offset + 237); |
1102 | instr->vibrato_rate = READ_U8(offset + 238); |
1103 | instr->volume_fadeout = READ_U16(offset + 239); |
1104 | |
1105 | instr->samples = (jar_xm_sample_t*)mempool; |
1106 | mempool += instr->num_samples * sizeof(jar_xm_sample_t); |
1107 | } else { |
1108 | instr->samples = NULL; |
1109 | } |
1110 | |
1111 | /* Instrument header size */ |
1112 | offset += READ_U32(offset); |
1113 | |
1114 | for(uint16_t j = 0; j < instr->num_samples; ++j) { |
1115 | /* Read sample header */ |
1116 | jar_xm_sample_t* sample = instr->samples + j; |
1117 | |
1118 | sample->length = READ_U32(offset); |
1119 | sample->loop_start = READ_U32(offset + 4); |
1120 | sample->loop_length = READ_U32(offset + 8); |
1121 | sample->loop_end = sample->loop_start + sample->loop_length; |
1122 | sample->volume = (float)READ_U8(offset + 12) / (float)0x40; |
1123 | sample->finetune = (int8_t)READ_U8(offset + 13); |
1124 | |
1125 | uint8_t flags = READ_U8(offset + 14); |
1126 | if((flags & 3) == 0) { |
1127 | sample->loop_type = jar_xm_NO_LOOP; |
1128 | } else if((flags & 3) == 1) { |
1129 | sample->loop_type = jar_xm_FORWARD_LOOP; |
1130 | } else { |
1131 | sample->loop_type = jar_xm_PING_PONG_LOOP; |
1132 | } |
1133 | |
1134 | sample->bits = (flags & (1 << 4)) ? 16 : 8; |
1135 | |
1136 | sample->panning = (float)READ_U8(offset + 15) / (float)0xFF; |
1137 | sample->relative_note = (int8_t)READ_U8(offset + 16); |
1138 | READ_MEMCPY(sample->name, 18, SAMPLE_NAME_LENGTH); |
1139 | sample->data = (float*)mempool; |
1140 | |
1141 | if(sample->bits == 16) { |
1142 | /* 16 bit sample */ |
1143 | mempool += sample->length * (sizeof(float) >> 1); |
1144 | sample->loop_start >>= 1; |
1145 | sample->loop_length >>= 1; |
1146 | sample->loop_end >>= 1; |
1147 | sample->length >>= 1; |
1148 | } else { |
1149 | /* 8 bit sample */ |
1150 | mempool += sample->length * sizeof(float); |
1151 | } |
1152 | |
1153 | offset += sample_header_size; |
1154 | } |
1155 | |
1156 | for(uint16_t j = 0; j < instr->num_samples; ++j) { |
1157 | /* Read sample data */ |
1158 | jar_xm_sample_t* sample = instr->samples + j; |
1159 | uint32_t length = sample->length; |
1160 | |
1161 | if(sample->bits == 16) { |
1162 | int16_t v = 0; |
1163 | for(uint32_t k = 0; k < length; ++k) { |
1164 | v = v + (int16_t)READ_U16(offset + (k << 1)); |
1165 | sample->data[k] = (float)v / (float)(1 << 15); |
1166 | } |
1167 | offset += sample->length << 1; |
1168 | } else { |
1169 | int8_t v = 0; |
1170 | for(uint32_t k = 0; k < length; ++k) { |
1171 | v = v + (int8_t)READ_U8(offset + k); |
1172 | sample->data[k] = (float)v / (float)(1 << 7); |
1173 | } |
1174 | offset += sample->length; |
1175 | } |
1176 | } |
1177 | } |
1178 | |
1179 | return mempool; |
1180 | } |
1181 | |
1182 | //------------------------------------------------------------------------------- |
1183 | //THE FOLLOWING IS FOR PLAYING |
1184 | //------------------------------------------------------------------------------- |
1185 | |
1186 | /* ----- Static functions ----- */ |
1187 | |
1188 | static float jar_xm_waveform(jar_xm_waveform_type_t, uint8_t); |
1189 | static void jar_xm_autovibrato(jar_xm_context_t*, jar_xm_channel_context_t*); |
1190 | static void jar_xm_vibrato(jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t); |
1191 | static void jar_xm_tremolo(jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t); |
1192 | static void jar_xm_arpeggio(jar_xm_context_t*, jar_xm_channel_context_t*, uint8_t, uint16_t); |
1193 | static void jar_xm_tone_portamento(jar_xm_context_t*, jar_xm_channel_context_t*); |
1194 | static void jar_xm_pitch_slide(jar_xm_context_t*, jar_xm_channel_context_t*, float); |
1195 | static void jar_xm_panning_slide(jar_xm_channel_context_t*, uint8_t); |
1196 | static void jar_xm_volume_slide(jar_xm_channel_context_t*, uint8_t); |
1197 | |
1198 | static float jar_xm_envelope_lerp(jar_xm_envelope_point_t*, jar_xm_envelope_point_t*, uint16_t); |
1199 | static void jar_xm_envelope_tick(jar_xm_channel_context_t*, jar_xm_envelope_t*, uint16_t*, float*); |
1200 | static void jar_xm_envelopes(jar_xm_channel_context_t*); |
1201 | |
1202 | static float jar_xm_linear_period(float); |
1203 | static float jar_xm_linear_frequency(float); |
1204 | static float jar_xm_amiga_period(float); |
1205 | static float jar_xm_amiga_frequency(float); |
1206 | static float jar_xm_period(jar_xm_context_t*, float); |
1207 | static float jar_xm_frequency(jar_xm_context_t*, float, float); |
1208 | static void jar_xm_update_frequency(jar_xm_context_t*, jar_xm_channel_context_t*); |
1209 | |
1210 | static void jar_xm_handle_note_and_instrument(jar_xm_context_t*, jar_xm_channel_context_t*, jar_xm_pattern_slot_t*); |
1211 | static void jar_xm_trigger_note(jar_xm_context_t*, jar_xm_channel_context_t*, unsigned int flags); |
1212 | static void jar_xm_cut_note(jar_xm_channel_context_t*); |
1213 | static void jar_xm_key_off(jar_xm_channel_context_t*); |
1214 | |
1215 | static void jar_xm_post_pattern_change(jar_xm_context_t*); |
1216 | static void jar_xm_row(jar_xm_context_t*); |
1217 | static void jar_xm_tick(jar_xm_context_t*); |
1218 | |
1219 | static float jar_xm_next_of_sample(jar_xm_channel_context_t*); |
1220 | static void jar_xm_sample(jar_xm_context_t*, float*, float*); |
1221 | |
1222 | /* ----- Other oddities ----- */ |
1223 | |
1224 | #define jar_xm_TRIGGER_KEEP_VOLUME (1 << 0) |
1225 | #define jar_xm_TRIGGER_KEEP_PERIOD (1 << 1) |
1226 | #define jar_xm_TRIGGER_KEEP_SAMPLE_POSITION (1 << 2) |
1227 | |
1228 | static const uint16_t amiga_frequencies[] = { |
1229 | 1712, 1616, 1525, 1440, /* C-2, C#2, D-2, D#2 */ |
1230 | 1357, 1281, 1209, 1141, /* E-2, F-2, F#2, G-2 */ |
1231 | 1077, 1017, 961, 907, /* G#2, A-2, A#2, B-2 */ |
1232 | 856, /* C-3 */ |
1233 | }; |
1234 | |
1235 | static const float multi_retrig_add[] = { |
1236 | 0.f, -1.f, -2.f, -4.f, /* 0, 1, 2, 3 */ |
1237 | -8.f, -16.f, 0.f, 0.f, /* 4, 5, 6, 7 */ |
1238 | 0.f, 1.f, 2.f, 4.f, /* 8, 9, A, B */ |
1239 | 8.f, 16.f, 0.f, 0.f /* C, D, E, F */ |
1240 | }; |
1241 | |
1242 | static const float multi_retrig_multiply[] = { |
1243 | 1.f, 1.f, 1.f, 1.f, /* 0, 1, 2, 3 */ |
1244 | 1.f, 1.f, .6666667f, .5f, /* 4, 5, 6, 7 */ |
1245 | 1.f, 1.f, 1.f, 1.f, /* 8, 9, A, B */ |
1246 | 1.f, 1.f, 1.5f, 2.f /* C, D, E, F */ |
1247 | }; |
1248 | |
1249 | #define jar_xm_CLAMP_UP1F(vol, limit) do { \ |
1250 | if((vol) > (limit)) (vol) = (limit); \ |
1251 | } while(0) |
1252 | #define jar_xm_CLAMP_UP(vol) jar_xm_CLAMP_UP1F((vol), 1.f) |
1253 | |
1254 | #define jar_xm_CLAMP_DOWN1F(vol, limit) do { \ |
1255 | if((vol) < (limit)) (vol) = (limit); \ |
1256 | } while(0) |
1257 | #define jar_xm_CLAMP_DOWN(vol) jar_xm_CLAMP_DOWN1F((vol), .0f) |
1258 | |
1259 | #define jar_xm_CLAMP2F(vol, up, down) do { \ |
1260 | if((vol) > (up)) (vol) = (up); \ |
1261 | else if((vol) < (down)) (vol) = (down); \ |
1262 | } while(0) |
1263 | #define jar_xm_CLAMP(vol) jar_xm_CLAMP2F((vol), 1.f, .0f) |
1264 | |
1265 | #define jar_xm_SLIDE_TOWARDS(val, goal, incr) do { \ |
1266 | if((val) > (goal)) { \ |
1267 | (val) -= (incr); \ |
1268 | jar_xm_CLAMP_DOWN1F((val), (goal)); \ |
1269 | } else if((val) < (goal)) { \ |
1270 | (val) += (incr); \ |
1271 | jar_xm_CLAMP_UP1F((val), (goal)); \ |
1272 | } \ |
1273 | } while(0) |
1274 | |
1275 | #define jar_xm_LERP(u, v, t) ((u) + (t) * ((v) - (u))) |
1276 | #define jar_xm_INVERSE_LERP(u, v, lerp) (((lerp) - (u)) / ((v) - (u))) |
1277 | |
1278 | #define HAS_TONE_PORTAMENTO(s) ((s)->effect_type == 3 \ |
1279 | || (s)->effect_type == 5 \ |
1280 | || ((s)->volume_column >> 4) == 0xF) |
1281 | #define HAS_ARPEGGIO(s) ((s)->effect_type == 0 \ |
1282 | && (s)->effect_param != 0) |
1283 | #define HAS_VIBRATO(s) ((s)->effect_type == 4 \ |
1284 | || (s)->effect_param == 6 \ |
1285 | || ((s)->volume_column >> 4) == 0xB) |
1286 | #define NOTE_IS_VALID(n) ((n) > 0 && (n) < 97) |
1287 | |
1288 | /* ----- Function definitions ----- */ |
1289 | |
1290 | static float jar_xm_waveform(jar_xm_waveform_type_t waveform, uint8_t step) { |
1291 | static unsigned int next_rand = 24492; |
1292 | step %= 0x40; |
1293 | |
1294 | switch(waveform) { |
1295 | |
1296 | case jar_xm_SINE_WAVEFORM: |
1297 | /* Why not use a table? For saving space, and because there's |
1298 | * very very little actual performance gain. */ |
1299 | return -sinf(2.f * 3.141592f * (float)step / (float)0x40); |
1300 | |
1301 | case jar_xm_RAMP_DOWN_WAVEFORM: |
1302 | /* Ramp down: 1.0f when step = 0; -1.0f when step = 0x40 */ |
1303 | return (float)(0x20 - step) / 0x20; |
1304 | |
1305 | case jar_xm_SQUARE_WAVEFORM: |
1306 | /* Square with a 50% duty */ |
1307 | return (step >= 0x20) ? 1.f : -1.f; |
1308 | |
1309 | case jar_xm_RANDOM_WAVEFORM: |
1310 | /* Use the POSIX.1-2001 example, just to be deterministic |
1311 | * across different machines */ |
1312 | next_rand = next_rand * 1103515245 + 12345; |
1313 | return (float)((next_rand >> 16) & 0x7FFF) / (float)0x4000 - 1.f; |
1314 | |
1315 | case jar_xm_RAMP_UP_WAVEFORM: |
1316 | /* Ramp up: -1.f when step = 0; 1.f when step = 0x40 */ |
1317 | return (float)(step - 0x20) / 0x20; |
1318 | |
1319 | default: |
1320 | break; |
1321 | |
1322 | } |
1323 | |
1324 | return .0f; |
1325 | } |
1326 | |
1327 | static void jar_xm_autovibrato(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) { |
1328 | if(ch->instrument == NULL || ch->instrument->vibrato_depth == 0) return; |
1329 | jar_xm_instrument_t* instr = ch->instrument; |
1330 | float sweep = 1.f; |
1331 | |
1332 | if(ch->autovibrato_ticks < instr->vibrato_sweep) { |
1333 | /* No idea if this is correct, but it sounds close enough… */ |
1334 | sweep = jar_xm_LERP(0.f, 1.f, (float)ch->autovibrato_ticks / (float)instr->vibrato_sweep); |
1335 | } |
1336 | |
1337 | unsigned int step = ((ch->autovibrato_ticks++) * instr->vibrato_rate) >> 2; |
1338 | ch->autovibrato_note_offset = .25f * jar_xm_waveform(instr->vibrato_type, step) |
1339 | * (float)instr->vibrato_depth / (float)0xF * sweep; |
1340 | jar_xm_update_frequency(ctx, ch); |
1341 | } |
1342 | |
1343 | static void jar_xm_vibrato(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t pos) { |
1344 | unsigned int step = pos * (param >> 4); |
1345 | ch->vibrato_note_offset = |
1346 | 2.f |
1347 | * jar_xm_waveform(ch->vibrato_waveform, step) |
1348 | * (float)(param & 0x0F) / (float)0xF; |
1349 | jar_xm_update_frequency(ctx, ch); |
1350 | } |
1351 | |
1352 | static void jar_xm_tremolo(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t pos) { |
1353 | unsigned int step = pos * (param >> 4); |
1354 | /* Not so sure about this, it sounds correct by ear compared with |
1355 | * MilkyTracker, but it could come from other bugs */ |
1356 | ch->tremolo_volume = -1.f * jar_xm_waveform(ch->tremolo_waveform, step) |
1357 | * (float)(param & 0x0F) / (float)0xF; |
1358 | } |
1359 | |
1360 | static void jar_xm_arpeggio(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, uint8_t param, uint16_t tick) { |
1361 | switch(tick % 3) { |
1362 | case 0: |
1363 | ch->arp_in_progress = false; |
1364 | ch->arp_note_offset = 0; |
1365 | break; |
1366 | case 2: |
1367 | ch->arp_in_progress = true; |
1368 | ch->arp_note_offset = param >> 4; |
1369 | break; |
1370 | case 1: |
1371 | ch->arp_in_progress = true; |
1372 | ch->arp_note_offset = param & 0x0F; |
1373 | break; |
1374 | } |
1375 | |
1376 | jar_xm_update_frequency(ctx, ch); |
1377 | } |
1378 | |
1379 | static void jar_xm_tone_portamento(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) { |
1380 | /* 3xx called without a note, wait until we get an actual |
1381 | * target note. */ |
1382 | if(ch->tone_portamento_target_period == 0.f) return; |
1383 | |
1384 | if(ch->period != ch->tone_portamento_target_period) { |
1385 | jar_xm_SLIDE_TOWARDS(ch->period, |
1386 | ch->tone_portamento_target_period, |
1387 | (ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES ? |
1388 | 4.f : 1.f) * ch->tone_portamento_param |
1389 | ); |
1390 | jar_xm_update_frequency(ctx, ch); |
1391 | } |
1392 | } |
1393 | |
1394 | static void jar_xm_pitch_slide(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, float period_offset) { |
1395 | /* Don't ask about the 4.f coefficient. I found mention of it |
1396 | * nowhere. Found by earâ„¢. */ |
1397 | if(ctx->module.frequency_type == jar_xm_LINEAR_FREQUENCIES) { |
1398 | period_offset *= 4.f; |
1399 | } |
1400 | |
1401 | ch->period += period_offset; |
1402 | jar_xm_CLAMP_DOWN(ch->period); |
1403 | /* XXX: upper bound of period ? */ |
1404 | |
1405 | jar_xm_update_frequency(ctx, ch); |
1406 | } |
1407 | |
1408 | static void jar_xm_panning_slide(jar_xm_channel_context_t* ch, uint8_t rawval) { |
1409 | float f; |
1410 | |
1411 | if((rawval & 0xF0) && (rawval & 0x0F)) { |
1412 | /* Illegal state */ |
1413 | return; |
1414 | } |
1415 | |
1416 | if(rawval & 0xF0) { |
1417 | /* Slide right */ |
1418 | f = (float)(rawval >> 4) / (float)0xFF; |
1419 | ch->panning += f; |
1420 | jar_xm_CLAMP_UP(ch->panning); |
1421 | } else { |
1422 | /* Slide left */ |
1423 | f = (float)(rawval & 0x0F) / (float)0xFF; |
1424 | ch->panning -= f; |
1425 | jar_xm_CLAMP_DOWN(ch->panning); |
1426 | } |
1427 | } |
1428 | |
1429 | static void jar_xm_volume_slide(jar_xm_channel_context_t* ch, uint8_t rawval) { |
1430 | float f; |
1431 | |
1432 | if((rawval & 0xF0) && (rawval & 0x0F)) { |
1433 | /* Illegal state */ |
1434 | return; |
1435 | } |
1436 | |
1437 | if(rawval & 0xF0) { |
1438 | /* Slide up */ |
1439 | f = (float)(rawval >> 4) / (float)0x40; |
1440 | ch->volume += f; |
1441 | jar_xm_CLAMP_UP(ch->volume); |
1442 | } else { |
1443 | /* Slide down */ |
1444 | f = (float)(rawval & 0x0F) / (float)0x40; |
1445 | ch->volume -= f; |
1446 | jar_xm_CLAMP_DOWN(ch->volume); |
1447 | } |
1448 | } |
1449 | |
1450 | static float jar_xm_envelope_lerp(jar_xm_envelope_point_t* a, jar_xm_envelope_point_t* b, uint16_t pos) { |
1451 | /* Linear interpolation between two envelope points */ |
1452 | if(pos <= a->frame) return a->value; |
1453 | else if(pos >= b->frame) return b->value; |
1454 | else { |
1455 | float p = (float)(pos - a->frame) / (float)(b->frame - a->frame); |
1456 | return a->value * (1 - p) + b->value * p; |
1457 | } |
1458 | } |
1459 | |
1460 | static void jar_xm_post_pattern_change(jar_xm_context_t* ctx) { |
1461 | /* Loop if necessary */ |
1462 | if(ctx->current_table_index >= ctx->module.length) { |
1463 | ctx->current_table_index = ctx->module.restart_position; |
1464 | } |
1465 | } |
1466 | |
1467 | static float jar_xm_linear_period(float note) { |
1468 | return 7680.f - note * 64.f; |
1469 | } |
1470 | |
1471 | static float jar_xm_linear_frequency(float period) { |
1472 | return 8363.f * powf(2.f, (4608.f - period) / 768.f); |
1473 | } |
1474 | |
1475 | static float jar_xm_amiga_period(float note) { |
1476 | unsigned int intnote = note; |
1477 | uint8_t a = intnote % 12; |
1478 | int8_t octave = note / 12.f - 2; |
1479 | uint16_t p1 = amiga_frequencies[a], p2 = amiga_frequencies[a + 1]; |
1480 | |
1481 | if(octave > 0) { |
1482 | p1 >>= octave; |
1483 | p2 >>= octave; |
1484 | } else if(octave < 0) { |
1485 | p1 <<= (-octave); |
1486 | p2 <<= (-octave); |
1487 | } |
1488 | |
1489 | return jar_xm_LERP(p1, p2, note - intnote); |
1490 | } |
1491 | |
1492 | static float jar_xm_amiga_frequency(float period) { |
1493 | if(period == .0f) return .0f; |
1494 | |
1495 | /* This is the PAL value. No reason to choose this one over the |
1496 | * NTSC value. */ |
1497 | return 7093789.2f / (period * 2.f); |
1498 | } |
1499 | |
1500 | static float jar_xm_period(jar_xm_context_t* ctx, float note) { |
1501 | switch(ctx->module.frequency_type) { |
1502 | case jar_xm_LINEAR_FREQUENCIES: |
1503 | return jar_xm_linear_period(note); |
1504 | case jar_xm_AMIGA_FREQUENCIES: |
1505 | return jar_xm_amiga_period(note); |
1506 | } |
1507 | return .0f; |
1508 | } |
1509 | |
1510 | static float jar_xm_frequency(jar_xm_context_t* ctx, float period, float note_offset) { |
1511 | uint8_t a; |
1512 | int8_t octave; |
1513 | float note; |
1514 | uint16_t p1, p2; |
1515 | |
1516 | switch(ctx->module.frequency_type) { |
1517 | |
1518 | case jar_xm_LINEAR_FREQUENCIES: |
1519 | return jar_xm_linear_frequency(period - 64.f * note_offset); |
1520 | |
1521 | case jar_xm_AMIGA_FREQUENCIES: |
1522 | if(note_offset == 0) { |
1523 | /* A chance to escape from insanity */ |
1524 | return jar_xm_amiga_frequency(period); |
1525 | } |
1526 | |
1527 | /* FIXME: this is very crappy at best */ |
1528 | a = octave = 0; |
1529 | |
1530 | /* Find the octave of the current period */ |
1531 | if(period > amiga_frequencies[0]) { |
1532 | --octave; |
1533 | while(period > (amiga_frequencies[0] << (-octave))) --octave; |
1534 | } else if(period < amiga_frequencies[12]) { |
1535 | ++octave; |
1536 | while(period < (amiga_frequencies[12] >> octave)) ++octave; |
1537 | } |
1538 | |
1539 | /* Find the smallest note closest to the current period */ |
1540 | for(uint8_t i = 0; i < 12; ++i) { |
1541 | p1 = amiga_frequencies[i], p2 = amiga_frequencies[i + 1]; |
1542 | |
1543 | if(octave > 0) { |
1544 | p1 >>= octave; |
1545 | p2 >>= octave; |
1546 | } else if(octave < 0) { |
1547 | p1 <<= (-octave); |
1548 | p2 <<= (-octave); |
1549 | } |
1550 | |
1551 | if(p2 <= period && period <= p1) { |
1552 | a = i; |
1553 | break; |
1554 | } |
1555 | } |
1556 | |
1557 | if(JAR_XM_DEBUG && (p1 < period || p2 > period)) { |
1558 | DEBUG("%i <= %f <= %i should hold but doesn't, this is a bug" , p2, period, p1); |
1559 | } |
1560 | |
1561 | note = 12.f * (octave + 2) + a + jar_xm_INVERSE_LERP(p1, p2, period); |
1562 | |
1563 | return jar_xm_amiga_frequency(jar_xm_amiga_period(note + note_offset)); |
1564 | |
1565 | } |
1566 | |
1567 | return .0f; |
1568 | } |
1569 | |
1570 | static void jar_xm_update_frequency(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch) { |
1571 | ch->frequency = jar_xm_frequency( |
1572 | ctx, ch->period, |
1573 | (ch->arp_note_offset > 0 ? ch->arp_note_offset : ( |
1574 | ch->vibrato_note_offset + ch->autovibrato_note_offset |
1575 | )) |
1576 | ); |
1577 | ch->step = ch->frequency / ctx->rate; |
1578 | } |
1579 | |
1580 | static void jar_xm_handle_note_and_instrument(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, |
1581 | jar_xm_pattern_slot_t* s) { |
1582 | if(s->instrument > 0) { |
1583 | if(HAS_TONE_PORTAMENTO(ch->current) && ch->instrument != NULL && ch->sample != NULL) { |
1584 | /* Tone portamento in effect, unclear stuff happens */ |
1585 | jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_PERIOD | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION); |
1586 | } else if(s->instrument > ctx->module.num_instruments) { |
1587 | /* Invalid instrument, Cut current note */ |
1588 | jar_xm_cut_note(ch); |
1589 | ch->instrument = NULL; |
1590 | ch->sample = NULL; |
1591 | } else { |
1592 | ch->instrument = ctx->module.instruments + (s->instrument - 1); |
1593 | if(s->note == 0 && ch->sample != NULL) { |
1594 | /* Ghost instrument, trigger note */ |
1595 | /* Sample position is kept, but envelopes are reset */ |
1596 | jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_SAMPLE_POSITION); |
1597 | } |
1598 | } |
1599 | } |
1600 | |
1601 | if(NOTE_IS_VALID(s->note)) { |
1602 | /* Yes, the real note number is s->note -1. Try finding |
1603 | * THAT in any of the specs! :-) */ |
1604 | |
1605 | jar_xm_instrument_t* instr = ch->instrument; |
1606 | |
1607 | if(HAS_TONE_PORTAMENTO(ch->current) && instr != NULL && ch->sample != NULL) { |
1608 | /* Tone portamento in effect */ |
1609 | ch->note = s->note + ch->sample->relative_note + ch->sample->finetune / 128.f - 1.f; |
1610 | ch->tone_portamento_target_period = jar_xm_period(ctx, ch->note); |
1611 | } else if(instr == NULL || ch->instrument->num_samples == 0) { |
1612 | /* Bad instrument */ |
1613 | jar_xm_cut_note(ch); |
1614 | } else { |
1615 | if(instr->sample_of_notes[s->note - 1] < instr->num_samples) { |
1616 | #if JAR_XM_RAMPING |
1617 | for(unsigned int z = 0; z < jar_xm_SAMPLE_RAMPING_POINTS; ++z) { |
1618 | ch->end_of_previous_sample[z] = jar_xm_next_of_sample(ch); |
1619 | } |
1620 | ch->frame_count = 0; |
1621 | #endif |
1622 | ch->sample = instr->samples + instr->sample_of_notes[s->note - 1]; |
1623 | ch->orig_note = ch->note = s->note + ch->sample->relative_note |
1624 | + ch->sample->finetune / 128.f - 1.f; |
1625 | if(s->instrument > 0) { |
1626 | jar_xm_trigger_note(ctx, ch, 0); |
1627 | } else { |
1628 | /* Ghost note: keep old volume */ |
1629 | jar_xm_trigger_note(ctx, ch, jar_xm_TRIGGER_KEEP_VOLUME); |
1630 | } |
1631 | } else { |
1632 | /* Bad sample */ |
1633 | jar_xm_cut_note(ch); |
1634 | } |
1635 | } |
1636 | } else if(s->note == 97) { |
1637 | /* Key Off */ |
1638 | jar_xm_key_off(ch); |
1639 | } |
1640 | |
1641 | switch(s->volume_column >> 4) { |
1642 | |
1643 | case 0x5: |
1644 | if(s->volume_column > 0x50) break; |
1645 | case 0x1: |
1646 | case 0x2: |
1647 | case 0x3: |
1648 | case 0x4: |
1649 | /* Set volume */ |
1650 | ch->volume = (float)(s->volume_column - 0x10) / (float)0x40; |
1651 | break; |
1652 | |
1653 | case 0x8: /* Fine volume slide down */ |
1654 | jar_xm_volume_slide(ch, s->volume_column & 0x0F); |
1655 | break; |
1656 | |
1657 | case 0x9: /* Fine volume slide up */ |
1658 | jar_xm_volume_slide(ch, s->volume_column << 4); |
1659 | break; |
1660 | |
1661 | case 0xA: /* Set vibrato speed */ |
1662 | ch->vibrato_param = (ch->vibrato_param & 0x0F) | ((s->volume_column & 0x0F) << 4); |
1663 | break; |
1664 | |
1665 | case 0xC: /* Set panning */ |
1666 | ch->panning = (float)( |
1667 | ((s->volume_column & 0x0F) << 4) | (s->volume_column & 0x0F) |
1668 | ) / (float)0xFF; |
1669 | break; |
1670 | |
1671 | case 0xF: /* Tone portamento */ |
1672 | if(s->volume_column & 0x0F) { |
1673 | ch->tone_portamento_param = ((s->volume_column & 0x0F) << 4) |
1674 | | (s->volume_column & 0x0F); |
1675 | } |
1676 | break; |
1677 | |
1678 | default: |
1679 | break; |
1680 | |
1681 | } |
1682 | |
1683 | switch(s->effect_type) { |
1684 | |
1685 | case 1: /* 1xx: Portamento up */ |
1686 | if(s->effect_param > 0) { |
1687 | ch->portamento_up_param = s->effect_param; |
1688 | } |
1689 | break; |
1690 | |
1691 | case 2: /* 2xx: Portamento down */ |
1692 | if(s->effect_param > 0) { |
1693 | ch->portamento_down_param = s->effect_param; |
1694 | } |
1695 | break; |
1696 | |
1697 | case 3: /* 3xx: Tone portamento */ |
1698 | if(s->effect_param > 0) { |
1699 | ch->tone_portamento_param = s->effect_param; |
1700 | } |
1701 | break; |
1702 | |
1703 | case 4: /* 4xy: Vibrato */ |
1704 | if(s->effect_param & 0x0F) { |
1705 | /* Set vibrato depth */ |
1706 | ch->vibrato_param = (ch->vibrato_param & 0xF0) | (s->effect_param & 0x0F); |
1707 | } |
1708 | if(s->effect_param >> 4) { |
1709 | /* Set vibrato speed */ |
1710 | ch->vibrato_param = (s->effect_param & 0xF0) | (ch->vibrato_param & 0x0F); |
1711 | } |
1712 | break; |
1713 | |
1714 | case 5: /* 5xy: Tone portamento + Volume slide */ |
1715 | if(s->effect_param > 0) { |
1716 | ch->volume_slide_param = s->effect_param; |
1717 | } |
1718 | break; |
1719 | |
1720 | case 6: /* 6xy: Vibrato + Volume slide */ |
1721 | if(s->effect_param > 0) { |
1722 | ch->volume_slide_param = s->effect_param; |
1723 | } |
1724 | break; |
1725 | |
1726 | case 7: /* 7xy: Tremolo */ |
1727 | if(s->effect_param & 0x0F) { |
1728 | /* Set tremolo depth */ |
1729 | ch->tremolo_param = (ch->tremolo_param & 0xF0) | (s->effect_param & 0x0F); |
1730 | } |
1731 | if(s->effect_param >> 4) { |
1732 | /* Set tremolo speed */ |
1733 | ch->tremolo_param = (s->effect_param & 0xF0) | (ch->tremolo_param & 0x0F); |
1734 | } |
1735 | break; |
1736 | |
1737 | case 8: /* 8xx: Set panning */ |
1738 | ch->panning = (float)s->effect_param / (float)0xFF; |
1739 | break; |
1740 | |
1741 | case 9: /* 9xx: Sample offset */ |
1742 | if(ch->sample != NULL && NOTE_IS_VALID(s->note)) { |
1743 | uint32_t final_offset = s->effect_param << (ch->sample->bits == 16 ? 7 : 8); |
1744 | if(final_offset >= ch->sample->length) { |
1745 | /* Pretend the sample dosen't loop and is done playing */ |
1746 | ch->sample_position = -1; |
1747 | break; |
1748 | } |
1749 | ch->sample_position = final_offset; |
1750 | } |
1751 | break; |
1752 | |
1753 | case 0xA: /* Axy: Volume slide */ |
1754 | if(s->effect_param > 0) { |
1755 | ch->volume_slide_param = s->effect_param; |
1756 | } |
1757 | break; |
1758 | |
1759 | case 0xB: /* Bxx: Position jump */ |
1760 | if(s->effect_param < ctx->module.length) { |
1761 | ctx->position_jump = true; |
1762 | ctx->jump_dest = s->effect_param; |
1763 | } |
1764 | break; |
1765 | |
1766 | case 0xC: /* Cxx: Set volume */ |
1767 | ch->volume = (float)((s->effect_param > 0x40) |
1768 | ? 0x40 : s->effect_param) / (float)0x40; |
1769 | break; |
1770 | |
1771 | case 0xD: /* Dxx: Pattern break */ |
1772 | /* Jump after playing this line */ |
1773 | ctx->pattern_break = true; |
1774 | ctx->jump_row = (s->effect_param >> 4) * 10 + (s->effect_param & 0x0F); |
1775 | break; |
1776 | |
1777 | case 0xE: /* EXy: Extended command */ |
1778 | switch(s->effect_param >> 4) { |
1779 | |
1780 | case 1: /* E1y: Fine portamento up */ |
1781 | if(s->effect_param & 0x0F) { |
1782 | ch->fine_portamento_up_param = s->effect_param & 0x0F; |
1783 | } |
1784 | jar_xm_pitch_slide(ctx, ch, -ch->fine_portamento_up_param); |
1785 | break; |
1786 | |
1787 | case 2: /* E2y: Fine portamento down */ |
1788 | if(s->effect_param & 0x0F) { |
1789 | ch->fine_portamento_down_param = s->effect_param & 0x0F; |
1790 | } |
1791 | jar_xm_pitch_slide(ctx, ch, ch->fine_portamento_down_param); |
1792 | break; |
1793 | |
1794 | case 4: /* E4y: Set vibrato control */ |
1795 | ch->vibrato_waveform = s->effect_param & 3; |
1796 | ch->vibrato_waveform_retrigger = !((s->effect_param >> 2) & 1); |
1797 | break; |
1798 | |
1799 | case 5: /* E5y: Set finetune */ |
1800 | if(NOTE_IS_VALID(ch->current->note) && ch->sample != NULL) { |
1801 | ch->note = ch->current->note + ch->sample->relative_note + |
1802 | (float)(((s->effect_param & 0x0F) - 8) << 4) / 128.f - 1.f; |
1803 | ch->period = jar_xm_period(ctx, ch->note); |
1804 | jar_xm_update_frequency(ctx, ch); |
1805 | } |
1806 | break; |
1807 | |
1808 | case 6: /* E6y: Pattern loop */ |
1809 | if(s->effect_param & 0x0F) { |
1810 | if((s->effect_param & 0x0F) == ch->pattern_loop_count) { |
1811 | /* Loop is over */ |
1812 | ch->pattern_loop_count = 0; |
1813 | break; |
1814 | } |
1815 | |
1816 | /* Jump to the beginning of the loop */ |
1817 | ch->pattern_loop_count++; |
1818 | ctx->position_jump = true; |
1819 | ctx->jump_row = ch->pattern_loop_origin; |
1820 | ctx->jump_dest = ctx->current_table_index; |
1821 | } else { |
1822 | /* Set loop start point */ |
1823 | ch->pattern_loop_origin = ctx->current_row; |
1824 | /* Replicate FT2 E60 bug */ |
1825 | ctx->jump_row = ch->pattern_loop_origin; |
1826 | } |
1827 | break; |
1828 | |
1829 | case 7: /* E7y: Set tremolo control */ |
1830 | ch->tremolo_waveform = s->effect_param & 3; |
1831 | ch->tremolo_waveform_retrigger = !((s->effect_param >> 2) & 1); |
1832 | break; |
1833 | |
1834 | case 0xA: /* EAy: Fine volume slide up */ |
1835 | if(s->effect_param & 0x0F) { |
1836 | ch->fine_volume_slide_param = s->effect_param & 0x0F; |
1837 | } |
1838 | jar_xm_volume_slide(ch, ch->fine_volume_slide_param << 4); |
1839 | break; |
1840 | |
1841 | case 0xB: /* EBy: Fine volume slide down */ |
1842 | if(s->effect_param & 0x0F) { |
1843 | ch->fine_volume_slide_param = s->effect_param & 0x0F; |
1844 | } |
1845 | jar_xm_volume_slide(ch, ch->fine_volume_slide_param); |
1846 | break; |
1847 | |
1848 | case 0xD: /* EDy: Note delay */ |
1849 | /* XXX: figure this out better. EDx triggers |
1850 | * the note even when there no note and no |
1851 | * instrument. But ED0 acts like like a ghost |
1852 | * note, EDx (x ≠0) does not. */ |
1853 | if(s->note == 0 && s->instrument == 0) { |
1854 | unsigned int flags = jar_xm_TRIGGER_KEEP_VOLUME; |
1855 | |
1856 | if(ch->current->effect_param & 0x0F) { |
1857 | ch->note = ch->orig_note; |
1858 | jar_xm_trigger_note(ctx, ch, flags); |
1859 | } else { |
1860 | jar_xm_trigger_note( |
1861 | ctx, ch, |
1862 | flags |
1863 | | jar_xm_TRIGGER_KEEP_PERIOD |
1864 | | jar_xm_TRIGGER_KEEP_SAMPLE_POSITION |
1865 | ); |
1866 | } |
1867 | } |
1868 | break; |
1869 | |
1870 | case 0xE: /* EEy: Pattern delay */ |
1871 | ctx->extra_ticks = (ch->current->effect_param & 0x0F) * ctx->tempo; |
1872 | break; |
1873 | |
1874 | default: |
1875 | break; |
1876 | |
1877 | } |
1878 | break; |
1879 | |
1880 | case 0xF: /* Fxx: Set tempo/BPM */ |
1881 | if(s->effect_param > 0) { |
1882 | if(s->effect_param <= 0x1F) { |
1883 | ctx->tempo = s->effect_param; |
1884 | } else { |
1885 | ctx->bpm = s->effect_param; |
1886 | } |
1887 | } |
1888 | break; |
1889 | |
1890 | case 16: /* Gxx: Set global volume */ |
1891 | ctx->global_volume = (float)((s->effect_param > 0x40) |
1892 | ? 0x40 : s->effect_param) / (float)0x40; |
1893 | break; |
1894 | |
1895 | case 17: /* Hxy: Global volume slide */ |
1896 | if(s->effect_param > 0) { |
1897 | ch->global_volume_slide_param = s->effect_param; |
1898 | } |
1899 | break; |
1900 | |
1901 | case 21: /* Lxx: Set envelope position */ |
1902 | ch->volume_envelope_frame_count = s->effect_param; |
1903 | ch->panning_envelope_frame_count = s->effect_param; |
1904 | break; |
1905 | |
1906 | case 25: /* Pxy: Panning slide */ |
1907 | if(s->effect_param > 0) { |
1908 | ch->panning_slide_param = s->effect_param; |
1909 | } |
1910 | break; |
1911 | |
1912 | case 27: /* Rxy: Multi retrig note */ |
1913 | if(s->effect_param > 0) { |
1914 | if((s->effect_param >> 4) == 0) { |
1915 | /* Keep previous x value */ |
1916 | ch->multi_retrig_param = (ch->multi_retrig_param & 0xF0) | (s->effect_param & 0x0F); |
1917 | } else { |
1918 | ch->multi_retrig_param = s->effect_param; |
1919 | } |
1920 | } |
1921 | break; |
1922 | |
1923 | case 29: /* Txy: Tremor */ |
1924 | if(s->effect_param > 0) { |
1925 | /* Tremor x and y params do not appear to be separately |
1926 | * kept in memory, unlike Rxy */ |
1927 | ch->tremor_param = s->effect_param; |
1928 | } |
1929 | break; |
1930 | |
1931 | case 33: /* Xxy: Extra stuff */ |
1932 | switch(s->effect_param >> 4) { |
1933 | case 1: /* X1y: Extra fine portamento up */ |
1934 | if(s->effect_param & 0x0F) { |
1935 | ch->extra_fine_portamento_up_param = s->effect_param & 0x0F; |
1936 | } |
1937 | jar_xm_pitch_slide(ctx, ch, -1.0f * ch->extra_fine_portamento_up_param); |
1938 | break; |
1939 | |
1940 | case 2: /* X2y: Extra fine portamento down */ |
1941 | if(s->effect_param & 0x0F) { |
1942 | ch->extra_fine_portamento_down_param = s->effect_param & 0x0F; |
1943 | } |
1944 | jar_xm_pitch_slide(ctx, ch, ch->extra_fine_portamento_down_param); |
1945 | break; |
1946 | |
1947 | default: |
1948 | break; |
1949 | |
1950 | } |
1951 | break; |
1952 | |
1953 | default: |
1954 | break; |
1955 | |
1956 | } |
1957 | } |
1958 | |
1959 | static void jar_xm_trigger_note(jar_xm_context_t* ctx, jar_xm_channel_context_t* ch, unsigned int flags) { |
1960 | if(!(flags & jar_xm_TRIGGER_KEEP_SAMPLE_POSITION)) { |
1961 | ch->sample_position = 0.f; |
1962 | ch->ping = true; |
1963 | } |
1964 | |
1965 | if(ch->sample != NULL) { |
1966 | if(!(flags & jar_xm_TRIGGER_KEEP_VOLUME)) { |
1967 | ch->volume = ch->sample->volume; |
1968 | } |
1969 | |
1970 | ch->panning = ch->sample->panning; |
1971 | } |
1972 | |
1973 | ch->sustained = true; |
1974 | ch->fadeout_volume = ch->volume_envelope_volume = 1.0f; |
1975 | ch->panning_envelope_panning = .5f; |
1976 | ch->volume_envelope_frame_count = ch->panning_envelope_frame_count = 0; |
1977 | ch->vibrato_note_offset = 0.f; |
1978 | ch->tremolo_volume = 0.f; |
1979 | ch->tremor_on = false; |
1980 | |
1981 | ch->autovibrato_ticks = 0; |
1982 | |
1983 | if(ch->vibrato_waveform_retrigger) { |
1984 | ch->vibrato_ticks = 0; /* XXX: should the waveform itself also |
1985 | * be reset to sine? */ |
1986 | } |
1987 | if(ch->tremolo_waveform_retrigger) { |
1988 | ch->tremolo_ticks = 0; |
1989 | } |
1990 | |
1991 | if(!(flags & jar_xm_TRIGGER_KEEP_PERIOD)) { |
1992 | ch->period = jar_xm_period(ctx, ch->note); |
1993 | jar_xm_update_frequency(ctx, ch); |
1994 | } |
1995 | |
1996 | ch->latest_trigger = ctx->generated_samples; |
1997 | if(ch->instrument != NULL) { |
1998 | ch->instrument->latest_trigger = ctx->generated_samples; |
1999 | } |
2000 | if(ch->sample != NULL) { |
2001 | ch->sample->latest_trigger = ctx->generated_samples; |
2002 | } |
2003 | } |
2004 | |
2005 | static void jar_xm_cut_note(jar_xm_channel_context_t* ch) { |
2006 | /* NB: this is not the same as Key Off */ |
2007 | ch->volume = .0f; |
2008 | } |
2009 | |
2010 | static void jar_xm_key_off(jar_xm_channel_context_t* ch) { |
2011 | /* Key Off */ |
2012 | ch->sustained = false; |
2013 | |
2014 | /* If no volume envelope is used, also cut the note */ |
2015 | if(ch->instrument == NULL || !ch->instrument->volume_envelope.enabled) { |
2016 | jar_xm_cut_note(ch); |
2017 | } |
2018 | } |
2019 | |
2020 | static void jar_xm_row(jar_xm_context_t* ctx) { |
2021 | if(ctx->position_jump) { |
2022 | ctx->current_table_index = ctx->jump_dest; |
2023 | ctx->current_row = ctx->jump_row; |
2024 | ctx->position_jump = false; |
2025 | ctx->pattern_break = false; |
2026 | ctx->jump_row = 0; |
2027 | jar_xm_post_pattern_change(ctx); |
2028 | } else if(ctx->pattern_break) { |
2029 | ctx->current_table_index++; |
2030 | ctx->current_row = ctx->jump_row; |
2031 | ctx->pattern_break = false; |
2032 | ctx->jump_row = 0; |
2033 | jar_xm_post_pattern_change(ctx); |
2034 | } |
2035 | |
2036 | jar_xm_pattern_t* cur = ctx->module.patterns + ctx->module.pattern_table[ctx->current_table_index]; |
2037 | bool in_a_loop = false; |
2038 | |
2039 | /* Read notes… */ |
2040 | for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { |
2041 | jar_xm_pattern_slot_t* s = cur->slots + ctx->current_row * ctx->module.num_channels + i; |
2042 | jar_xm_channel_context_t* ch = ctx->channels + i; |
2043 | |
2044 | ch->current = s; |
2045 | |
2046 | if(s->effect_type != 0xE || s->effect_param >> 4 != 0xD) { |
2047 | jar_xm_handle_note_and_instrument(ctx, ch, s); |
2048 | } else { |
2049 | ch->note_delay_param = s->effect_param & 0x0F; |
2050 | } |
2051 | |
2052 | if(!in_a_loop && ch->pattern_loop_count > 0) { |
2053 | in_a_loop = true; |
2054 | } |
2055 | } |
2056 | |
2057 | if(!in_a_loop) { |
2058 | /* No E6y loop is in effect (or we are in the first pass) */ |
2059 | ctx->loop_count = (ctx->row_loop_count[MAX_NUM_ROWS * ctx->current_table_index + ctx->current_row]++); |
2060 | } |
2061 | |
2062 | ctx->current_row++; /* Since this is an uint8, this line can |
2063 | * increment from 255 to 0, in which case it |
2064 | * is still necessary to go the next |
2065 | * pattern. */ |
2066 | if(!ctx->position_jump && !ctx->pattern_break && |
2067 | (ctx->current_row >= cur->num_rows || ctx->current_row == 0)) { |
2068 | ctx->current_table_index++; |
2069 | ctx->current_row = ctx->jump_row; /* This will be 0 most of |
2070 | * the time, except when E60 |
2071 | * is used */ |
2072 | ctx->jump_row = 0; |
2073 | jar_xm_post_pattern_change(ctx); |
2074 | } |
2075 | } |
2076 | |
2077 | static void jar_xm_envelope_tick(jar_xm_channel_context_t* ch, |
2078 | jar_xm_envelope_t* env, |
2079 | uint16_t* counter, |
2080 | float* outval) { |
2081 | if(env->num_points < 2) { |
2082 | /* Don't really know what to do… */ |
2083 | if(env->num_points == 1) { |
2084 | /* XXX I am pulling this out of my ass */ |
2085 | *outval = (float)env->points[0].value / (float)0x40; |
2086 | if(*outval > 1) { |
2087 | *outval = 1; |
2088 | } |
2089 | } |
2090 | |
2091 | return; |
2092 | } else { |
2093 | uint8_t j; |
2094 | |
2095 | if(env->loop_enabled) { |
2096 | uint16_t loop_start = env->points[env->loop_start_point].frame; |
2097 | uint16_t loop_end = env->points[env->loop_end_point].frame; |
2098 | uint16_t loop_length = loop_end - loop_start; |
2099 | |
2100 | if(*counter >= loop_end) { |
2101 | *counter -= loop_length; |
2102 | } |
2103 | } |
2104 | |
2105 | for(j = 0; j < (env->num_points - 2); ++j) { |
2106 | if(env->points[j].frame <= *counter && |
2107 | env->points[j+1].frame >= *counter) { |
2108 | break; |
2109 | } |
2110 | } |
2111 | |
2112 | *outval = jar_xm_envelope_lerp(env->points + j, env->points + j + 1, *counter) / (float)0x40; |
2113 | |
2114 | /* Make sure it is safe to increment frame count */ |
2115 | if(!ch->sustained || !env->sustain_enabled || |
2116 | *counter != env->points[env->sustain_point].frame) { |
2117 | (*counter)++; |
2118 | } |
2119 | } |
2120 | } |
2121 | |
2122 | static void jar_xm_envelopes(jar_xm_channel_context_t* ch) { |
2123 | if(ch->instrument != NULL) { |
2124 | if(ch->instrument->volume_envelope.enabled) { |
2125 | if(!ch->sustained) { |
2126 | ch->fadeout_volume -= (float)ch->instrument->volume_fadeout / 65536.f; |
2127 | jar_xm_CLAMP_DOWN(ch->fadeout_volume); |
2128 | } |
2129 | |
2130 | jar_xm_envelope_tick(ch, |
2131 | &(ch->instrument->volume_envelope), |
2132 | &(ch->volume_envelope_frame_count), |
2133 | &(ch->volume_envelope_volume)); |
2134 | } |
2135 | |
2136 | if(ch->instrument->panning_envelope.enabled) { |
2137 | jar_xm_envelope_tick(ch, |
2138 | &(ch->instrument->panning_envelope), |
2139 | &(ch->panning_envelope_frame_count), |
2140 | &(ch->panning_envelope_panning)); |
2141 | } |
2142 | } |
2143 | } |
2144 | |
2145 | static void jar_xm_tick(jar_xm_context_t* ctx) { |
2146 | if(ctx->current_tick == 0) { |
2147 | jar_xm_row(ctx); |
2148 | } |
2149 | |
2150 | for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { |
2151 | jar_xm_channel_context_t* ch = ctx->channels + i; |
2152 | |
2153 | jar_xm_envelopes(ch); |
2154 | jar_xm_autovibrato(ctx, ch); |
2155 | |
2156 | if(ch->arp_in_progress && !HAS_ARPEGGIO(ch->current)) { |
2157 | ch->arp_in_progress = false; |
2158 | ch->arp_note_offset = 0; |
2159 | jar_xm_update_frequency(ctx, ch); |
2160 | } |
2161 | if(ch->vibrato_in_progress && !HAS_VIBRATO(ch->current)) { |
2162 | ch->vibrato_in_progress = false; |
2163 | ch->vibrato_note_offset = 0.f; |
2164 | jar_xm_update_frequency(ctx, ch); |
2165 | } |
2166 | |
2167 | switch(ch->current->volume_column >> 4) { |
2168 | |
2169 | case 0x6: /* Volume slide down */ |
2170 | if(ctx->current_tick == 0) break; |
2171 | jar_xm_volume_slide(ch, ch->current->volume_column & 0x0F); |
2172 | break; |
2173 | |
2174 | case 0x7: /* Volume slide up */ |
2175 | if(ctx->current_tick == 0) break; |
2176 | jar_xm_volume_slide(ch, ch->current->volume_column << 4); |
2177 | break; |
2178 | |
2179 | case 0xB: /* Vibrato */ |
2180 | if(ctx->current_tick == 0) break; |
2181 | ch->vibrato_in_progress = false; |
2182 | jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++); |
2183 | break; |
2184 | |
2185 | case 0xD: /* Panning slide left */ |
2186 | if(ctx->current_tick == 0) break; |
2187 | jar_xm_panning_slide(ch, ch->current->volume_column & 0x0F); |
2188 | break; |
2189 | |
2190 | case 0xE: /* Panning slide right */ |
2191 | if(ctx->current_tick == 0) break; |
2192 | jar_xm_panning_slide(ch, ch->current->volume_column << 4); |
2193 | break; |
2194 | |
2195 | case 0xF: /* Tone portamento */ |
2196 | if(ctx->current_tick == 0) break; |
2197 | jar_xm_tone_portamento(ctx, ch); |
2198 | break; |
2199 | |
2200 | default: |
2201 | break; |
2202 | |
2203 | } |
2204 | |
2205 | switch(ch->current->effect_type) { |
2206 | |
2207 | case 0: /* 0xy: Arpeggio */ |
2208 | if(ch->current->effect_param > 0) { |
2209 | char arp_offset = ctx->tempo % 3; |
2210 | switch(arp_offset) { |
2211 | case 2: /* 0 -> x -> 0 -> y -> x -> … */ |
2212 | if(ctx->current_tick == 1) { |
2213 | ch->arp_in_progress = true; |
2214 | ch->arp_note_offset = ch->current->effect_param >> 4; |
2215 | jar_xm_update_frequency(ctx, ch); |
2216 | break; |
2217 | } |
2218 | /* No break here, this is intended */ |
2219 | case 1: /* 0 -> 0 -> y -> x -> … */ |
2220 | if(ctx->current_tick == 0) { |
2221 | ch->arp_in_progress = false; |
2222 | ch->arp_note_offset = 0; |
2223 | jar_xm_update_frequency(ctx, ch); |
2224 | break; |
2225 | } |
2226 | /* No break here, this is intended */ |
2227 | case 0: /* 0 -> y -> x -> … */ |
2228 | jar_xm_arpeggio(ctx, ch, ch->current->effect_param, ctx->current_tick - arp_offset); |
2229 | default: |
2230 | break; |
2231 | } |
2232 | } |
2233 | break; |
2234 | |
2235 | case 1: /* 1xx: Portamento up */ |
2236 | if(ctx->current_tick == 0) break; |
2237 | jar_xm_pitch_slide(ctx, ch, -ch->portamento_up_param); |
2238 | break; |
2239 | |
2240 | case 2: /* 2xx: Portamento down */ |
2241 | if(ctx->current_tick == 0) break; |
2242 | jar_xm_pitch_slide(ctx, ch, ch->portamento_down_param); |
2243 | break; |
2244 | |
2245 | case 3: /* 3xx: Tone portamento */ |
2246 | if(ctx->current_tick == 0) break; |
2247 | jar_xm_tone_portamento(ctx, ch); |
2248 | break; |
2249 | |
2250 | case 4: /* 4xy: Vibrato */ |
2251 | if(ctx->current_tick == 0) break; |
2252 | ch->vibrato_in_progress = true; |
2253 | jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++); |
2254 | break; |
2255 | |
2256 | case 5: /* 5xy: Tone portamento + Volume slide */ |
2257 | if(ctx->current_tick == 0) break; |
2258 | jar_xm_tone_portamento(ctx, ch); |
2259 | jar_xm_volume_slide(ch, ch->volume_slide_param); |
2260 | break; |
2261 | |
2262 | case 6: /* 6xy: Vibrato + Volume slide */ |
2263 | if(ctx->current_tick == 0) break; |
2264 | ch->vibrato_in_progress = true; |
2265 | jar_xm_vibrato(ctx, ch, ch->vibrato_param, ch->vibrato_ticks++); |
2266 | jar_xm_volume_slide(ch, ch->volume_slide_param); |
2267 | break; |
2268 | |
2269 | case 7: /* 7xy: Tremolo */ |
2270 | if(ctx->current_tick == 0) break; |
2271 | jar_xm_tremolo(ctx, ch, ch->tremolo_param, ch->tremolo_ticks++); |
2272 | break; |
2273 | |
2274 | case 0xA: /* Axy: Volume slide */ |
2275 | if(ctx->current_tick == 0) break; |
2276 | jar_xm_volume_slide(ch, ch->volume_slide_param); |
2277 | break; |
2278 | |
2279 | case 0xE: /* EXy: Extended command */ |
2280 | switch(ch->current->effect_param >> 4) { |
2281 | |
2282 | case 0x9: /* E9y: Retrigger note */ |
2283 | if(ctx->current_tick != 0 && ch->current->effect_param & 0x0F) { |
2284 | if(!(ctx->current_tick % (ch->current->effect_param & 0x0F))) { |
2285 | jar_xm_trigger_note(ctx, ch, 0); |
2286 | jar_xm_envelopes(ch); |
2287 | } |
2288 | } |
2289 | break; |
2290 | |
2291 | case 0xC: /* ECy: Note cut */ |
2292 | if((ch->current->effect_param & 0x0F) == ctx->current_tick) { |
2293 | jar_xm_cut_note(ch); |
2294 | } |
2295 | break; |
2296 | |
2297 | case 0xD: /* EDy: Note delay */ |
2298 | if(ch->note_delay_param == ctx->current_tick) { |
2299 | jar_xm_handle_note_and_instrument(ctx, ch, ch->current); |
2300 | jar_xm_envelopes(ch); |
2301 | } |
2302 | break; |
2303 | |
2304 | default: |
2305 | break; |
2306 | |
2307 | } |
2308 | break; |
2309 | |
2310 | case 17: /* Hxy: Global volume slide */ |
2311 | if(ctx->current_tick == 0) break; |
2312 | if((ch->global_volume_slide_param & 0xF0) && |
2313 | (ch->global_volume_slide_param & 0x0F)) { |
2314 | /* Illegal state */ |
2315 | break; |
2316 | } |
2317 | if(ch->global_volume_slide_param & 0xF0) { |
2318 | /* Global slide up */ |
2319 | float f = (float)(ch->global_volume_slide_param >> 4) / (float)0x40; |
2320 | ctx->global_volume += f; |
2321 | jar_xm_CLAMP_UP(ctx->global_volume); |
2322 | } else { |
2323 | /* Global slide down */ |
2324 | float f = (float)(ch->global_volume_slide_param & 0x0F) / (float)0x40; |
2325 | ctx->global_volume -= f; |
2326 | jar_xm_CLAMP_DOWN(ctx->global_volume); |
2327 | } |
2328 | break; |
2329 | |
2330 | case 20: /* Kxx: Key off */ |
2331 | /* Most documentations will tell you the parameter has no |
2332 | * use. Don't be fooled. */ |
2333 | if(ctx->current_tick == ch->current->effect_param) { |
2334 | jar_xm_key_off(ch); |
2335 | } |
2336 | break; |
2337 | |
2338 | case 25: /* Pxy: Panning slide */ |
2339 | if(ctx->current_tick == 0) break; |
2340 | jar_xm_panning_slide(ch, ch->panning_slide_param); |
2341 | break; |
2342 | |
2343 | case 27: /* Rxy: Multi retrig note */ |
2344 | if(ctx->current_tick == 0) break; |
2345 | if(((ch->multi_retrig_param) & 0x0F) == 0) break; |
2346 | if((ctx->current_tick % (ch->multi_retrig_param & 0x0F)) == 0) { |
2347 | float v = ch->volume * multi_retrig_multiply[ch->multi_retrig_param >> 4] |
2348 | + multi_retrig_add[ch->multi_retrig_param >> 4]; |
2349 | jar_xm_CLAMP(v); |
2350 | jar_xm_trigger_note(ctx, ch, 0); |
2351 | ch->volume = v; |
2352 | } |
2353 | break; |
2354 | |
2355 | case 29: /* Txy: Tremor */ |
2356 | if(ctx->current_tick == 0) break; |
2357 | ch->tremor_on = ( |
2358 | (ctx->current_tick - 1) % ((ch->tremor_param >> 4) + (ch->tremor_param & 0x0F) + 2) |
2359 | > |
2360 | (ch->tremor_param >> 4) |
2361 | ); |
2362 | break; |
2363 | |
2364 | default: |
2365 | break; |
2366 | |
2367 | } |
2368 | |
2369 | float panning, volume; |
2370 | |
2371 | panning = ch->panning + |
2372 | (ch->panning_envelope_panning - .5f) * (.5f - fabs(ch->panning - .5f)) * 2.0f; |
2373 | |
2374 | if(ch->tremor_on) { |
2375 | volume = .0f; |
2376 | } else { |
2377 | volume = ch->volume + ch->tremolo_volume; |
2378 | jar_xm_CLAMP(volume); |
2379 | volume *= ch->fadeout_volume * ch->volume_envelope_volume; |
2380 | } |
2381 | |
2382 | #if JAR_XM_RAMPING |
2383 | ch->target_panning = panning; |
2384 | ch->target_volume = volume; |
2385 | #else |
2386 | ch->actual_panning = panning; |
2387 | ch->actual_volume = volume; |
2388 | #endif |
2389 | } |
2390 | |
2391 | ctx->current_tick++; |
2392 | if(ctx->current_tick >= ctx->tempo + ctx->extra_ticks) { |
2393 | ctx->current_tick = 0; |
2394 | ctx->extra_ticks = 0; |
2395 | } |
2396 | |
2397 | /* FT2 manual says number of ticks / second = BPM * 0.4 */ |
2398 | ctx->remaining_samples_in_tick += (float)ctx->rate / ((float)ctx->bpm * 0.4f); |
2399 | } |
2400 | |
2401 | static float jar_xm_next_of_sample(jar_xm_channel_context_t* ch) { |
2402 | if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) { |
2403 | #if JAR_XM_RAMPING |
2404 | if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) { |
2405 | return jar_xm_LERP(ch->end_of_previous_sample[ch->frame_count], .0f, |
2406 | (float)ch->frame_count / (float)jar_xm_SAMPLE_RAMPING_POINTS); |
2407 | } |
2408 | #endif |
2409 | return .0f; |
2410 | } |
2411 | if(ch->sample->length == 0) { |
2412 | return .0f; |
2413 | } |
2414 | |
2415 | float u, v, t; |
2416 | uint32_t a, b; |
2417 | a = (uint32_t)ch->sample_position; /* This cast is fine, |
2418 | * sample_position will not |
2419 | * go above integer |
2420 | * ranges */ |
2421 | if(JAR_XM_LINEAR_INTERPOLATION) { |
2422 | b = a + 1; |
2423 | t = ch->sample_position - a; /* Cheaper than fmodf(., 1.f) */ |
2424 | } |
2425 | u = ch->sample->data[a]; |
2426 | |
2427 | switch(ch->sample->loop_type) { |
2428 | |
2429 | case jar_xm_NO_LOOP: |
2430 | if(JAR_XM_LINEAR_INTERPOLATION) { |
2431 | v = (b < ch->sample->length) ? ch->sample->data[b] : .0f; |
2432 | } |
2433 | ch->sample_position += ch->step; |
2434 | if(ch->sample_position >= ch->sample->length) { |
2435 | ch->sample_position = -1; |
2436 | } |
2437 | break; |
2438 | |
2439 | case jar_xm_FORWARD_LOOP: |
2440 | if(JAR_XM_LINEAR_INTERPOLATION) { |
2441 | v = ch->sample->data[ |
2442 | (b == ch->sample->loop_end) ? ch->sample->loop_start : b |
2443 | ]; |
2444 | } |
2445 | ch->sample_position += ch->step; |
2446 | while(ch->sample_position >= ch->sample->loop_end) { |
2447 | ch->sample_position -= ch->sample->loop_length; |
2448 | } |
2449 | break; |
2450 | |
2451 | case jar_xm_PING_PONG_LOOP: |
2452 | if(ch->ping) { |
2453 | ch->sample_position += ch->step; |
2454 | } else { |
2455 | ch->sample_position -= ch->step; |
2456 | } |
2457 | /* XXX: this may not work for very tight ping-pong loops |
2458 | * (ie switches direction more than once per sample */ |
2459 | if(ch->ping) { |
2460 | if(JAR_XM_LINEAR_INTERPOLATION) { |
2461 | v = (b >= ch->sample->loop_end) ? ch->sample->data[a] : ch->sample->data[b]; |
2462 | } |
2463 | if(ch->sample_position >= ch->sample->loop_end) { |
2464 | ch->ping = false; |
2465 | ch->sample_position = (ch->sample->loop_end << 1) - ch->sample_position; |
2466 | } |
2467 | /* sanity checking */ |
2468 | if(ch->sample_position >= ch->sample->length) { |
2469 | ch->ping = false; |
2470 | ch->sample_position -= ch->sample->length - 1; |
2471 | } |
2472 | } else { |
2473 | if(JAR_XM_LINEAR_INTERPOLATION) { |
2474 | v = u; |
2475 | u = (b == 1 || b - 2 <= ch->sample->loop_start) ? ch->sample->data[a] : ch->sample->data[b - 2]; |
2476 | } |
2477 | if(ch->sample_position <= ch->sample->loop_start) { |
2478 | ch->ping = true; |
2479 | ch->sample_position = (ch->sample->loop_start << 1) - ch->sample_position; |
2480 | } |
2481 | /* sanity checking */ |
2482 | if(ch->sample_position <= .0f) { |
2483 | ch->ping = true; |
2484 | ch->sample_position = .0f; |
2485 | } |
2486 | } |
2487 | break; |
2488 | |
2489 | default: |
2490 | v = .0f; |
2491 | break; |
2492 | } |
2493 | |
2494 | float endval = JAR_XM_LINEAR_INTERPOLATION ? jar_xm_LERP(u, v, t) : u; |
2495 | |
2496 | #if JAR_XM_RAMPING |
2497 | if(ch->frame_count < jar_xm_SAMPLE_RAMPING_POINTS) { |
2498 | /* Smoothly transition between old and new sample. */ |
2499 | return jar_xm_LERP(ch->end_of_previous_sample[ch->frame_count], endval, |
2500 | (float)ch->frame_count / (float)jar_xm_SAMPLE_RAMPING_POINTS); |
2501 | } |
2502 | #endif |
2503 | |
2504 | return endval; |
2505 | } |
2506 | |
2507 | static void jar_xm_sample(jar_xm_context_t* ctx, float* left, float* right) { |
2508 | if(ctx->remaining_samples_in_tick <= 0) { |
2509 | jar_xm_tick(ctx); |
2510 | } |
2511 | ctx->remaining_samples_in_tick--; |
2512 | |
2513 | *left = 0.f; |
2514 | *right = 0.f; |
2515 | |
2516 | if(ctx->max_loop_count > 0 && ctx->loop_count >= ctx->max_loop_count) { |
2517 | return; |
2518 | } |
2519 | |
2520 | for(uint8_t i = 0; i < ctx->module.num_channels; ++i) { |
2521 | jar_xm_channel_context_t* ch = ctx->channels + i; |
2522 | |
2523 | if(ch->instrument == NULL || ch->sample == NULL || ch->sample_position < 0) { |
2524 | continue; |
2525 | } |
2526 | |
2527 | const float fval = jar_xm_next_of_sample(ch); |
2528 | |
2529 | if(!ch->muted && !ch->instrument->muted) { |
2530 | *left += fval * ch->actual_volume * (1.f - ch->actual_panning); |
2531 | *right += fval * ch->actual_volume * ch->actual_panning; |
2532 | } |
2533 | |
2534 | #if JAR_XM_RAMPING |
2535 | ch->frame_count++; |
2536 | jar_xm_SLIDE_TOWARDS(ch->actual_volume, ch->target_volume, ctx->volume_ramp); |
2537 | jar_xm_SLIDE_TOWARDS(ch->actual_panning, ch->target_panning, ctx->panning_ramp); |
2538 | #endif |
2539 | } |
2540 | |
2541 | const float fgvol = ctx->global_volume * ctx->amplification; |
2542 | *left *= fgvol; |
2543 | *right *= fgvol; |
2544 | |
2545 | #if JAR_XM_DEBUG |
2546 | if(fabs(*left) > 1 || fabs(*right) > 1) { |
2547 | DEBUG("clipping frame: %f %f, this is a bad module or a libxm bug" , *left, *right); |
2548 | } |
2549 | #endif |
2550 | } |
2551 | |
2552 | void jar_xm_generate_samples(jar_xm_context_t* ctx, float* output, size_t numsamples) { |
2553 | if(ctx && output) { |
2554 | ctx->generated_samples += numsamples; |
2555 | for(size_t i = 0; i < numsamples; i++) { |
2556 | jar_xm_sample(ctx, output + (2 * i), output + (2 * i + 1)); |
2557 | } |
2558 | } |
2559 | } |
2560 | |
2561 | uint64_t jar_xm_get_remaining_samples(jar_xm_context_t* ctx) |
2562 | { |
2563 | uint64_t total = 0; |
2564 | uint8_t currentLoopCount = jar_xm_get_loop_count(ctx); |
2565 | jar_xm_set_max_loop_count(ctx, 0); |
2566 | |
2567 | while(jar_xm_get_loop_count(ctx) == currentLoopCount) |
2568 | { |
2569 | total += ctx->remaining_samples_in_tick; |
2570 | ctx->remaining_samples_in_tick = 0; |
2571 | jar_xm_tick(ctx); |
2572 | } |
2573 | |
2574 | ctx->loop_count = currentLoopCount; |
2575 | return total; |
2576 | } |
2577 | |
2578 | //-------------------------------------------- |
2579 | //FILE LOADER - TODO - NEEDS TO BE CLEANED UP |
2580 | //-------------------------------------------- |
2581 | |
2582 | #undef DEBUG |
2583 | #define DEBUG(...) do { \ |
2584 | fprintf(stderr, __VA_ARGS__); \ |
2585 | fflush(stderr); \ |
2586 | } while(0) |
2587 | |
2588 | #define DEBUG_ERR(...) do { \ |
2589 | fprintf(stderr, __VA_ARGS__); \ |
2590 | fflush(stderr); \ |
2591 | } while(0) |
2592 | |
2593 | #define FATAL(...) do { \ |
2594 | fprintf(stderr, __VA_ARGS__); \ |
2595 | fflush(stderr); \ |
2596 | exit(1); \ |
2597 | } while(0) |
2598 | |
2599 | #define FATAL_ERR(...) do { \ |
2600 | fprintf(stderr, __VA_ARGS__); \ |
2601 | fflush(stderr); \ |
2602 | exit(1); \ |
2603 | } while(0) |
2604 | |
2605 | |
2606 | int jar_xm_create_context_from_file(jar_xm_context_t** ctx, uint32_t rate, const char* filename) { |
2607 | FILE* xmf; |
2608 | int size; |
2609 | int ret; |
2610 | |
2611 | xmf = fopen(filename, "rb" ); |
2612 | if(xmf == NULL) { |
2613 | DEBUG_ERR("Could not open input file" ); |
2614 | *ctx = NULL; |
2615 | return 3; |
2616 | } |
2617 | |
2618 | fseek(xmf, 0, SEEK_END); |
2619 | size = ftell(xmf); |
2620 | rewind(xmf); |
2621 | if(size == -1) { |
2622 | fclose(xmf); |
2623 | DEBUG_ERR("fseek() failed" ); |
2624 | *ctx = NULL; |
2625 | return 4; |
2626 | } |
2627 | |
2628 | char* data = JARXM_MALLOC(size + 1); |
2629 | if(!data || fread(data, 1, size, xmf) < size) { |
2630 | fclose(xmf); |
2631 | DEBUG_ERR(data ? "fread() failed" : "JARXM_MALLOC() failed" ); |
2632 | JARXM_FREE(data); |
2633 | *ctx = NULL; |
2634 | return 5; |
2635 | } |
2636 | |
2637 | fclose(xmf); |
2638 | |
2639 | ret = jar_xm_create_context_safe(ctx, data, size, rate); |
2640 | JARXM_FREE(data); |
2641 | |
2642 | switch(ret) { |
2643 | case 0: |
2644 | break; |
2645 | |
2646 | case 1: |
2647 | DEBUG("could not create context: module is not sane\n" ); |
2648 | *ctx = NULL; |
2649 | return 1; |
2650 | break; |
2651 | |
2652 | case 2: |
2653 | FATAL("could not create context: malloc failed\n" ); |
2654 | return 2; |
2655 | break; |
2656 | |
2657 | default: |
2658 | FATAL("could not create context: unknown error\n" ); |
2659 | return 6; |
2660 | break; |
2661 | |
2662 | } |
2663 | |
2664 | return 0; |
2665 | } |
2666 | |
2667 | // not part of the original library |
2668 | void jar_xm_reset(jar_xm_context_t* ctx) |
2669 | { |
2670 | // I don't know what I am doing |
2671 | // this is probably very broken |
2672 | // but it kinda works |
2673 | for (uint16_t i = 0; i < jar_xm_get_number_of_channels(ctx); i++) |
2674 | { |
2675 | jar_xm_cut_note(&ctx->channels[i]); |
2676 | } |
2677 | ctx->current_row = 0; |
2678 | ctx->current_table_index = 0; |
2679 | ctx->current_tick = 0; |
2680 | } |
2681 | |
2682 | |
2683 | #endif//end of JAR_XM_IMPLEMENTATION |
2684 | //------------------------------------------------------------------------------- |
2685 | |
2686 | #endif//end of INCLUDE_JAR_XM_H |
2687 | |