Split the async framework into several parts.
[helenos.git] / uspace / lib / trackmod / trackmod.c
blobf6a66725a6fe801115c96f1df07a093a0b23199d
1 /*
2 * Copyright (c) 2014 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup trackmod
30 * @{
32 /**
33 * @file Tracker module handling library.
36 #include <assert.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
41 #include "macros.h"
42 #include "protracker.h"
43 #include "trackmod.h"
44 #include "xm.h"
46 /** Tunables */
47 enum {
48 amp_factor = 16
51 /** Standard definitions set in stone */
52 enum {
53 /** Base sample clock */
54 base_clock = 8363 * 428,
55 /** Maximum sample volume */
56 vol_max = 64,
57 /** Minimum period */
58 period_min = 113,
59 /** Maxium period */
60 period_max = 856
63 /** Table for finetune computation.
65 * Finetune is a number ft in [-8 .. 7]. The pitch should be adjusted by
66 * ft/8 semitones. To adjust pitch by 1/8 semitone down we can mutiply the
67 * period by 2^(1/12/8) =. 1.0072, one semitone up: 2^-(1/12/8) =. 0.9928,
68 * to adjust by ft/8 semitones, multiply by 2^(-ft/12/8).
70 * finetune_factor[ft] := 10000 * 2^(-ft/12/8)
71 * res_period = clip(period * fineture_factor[ft+8] / 10000)
73 static unsigned finetune_factor[16] = {
74 10595, 10518, 10443, 10368, 10293, 10219, 10145, 10072,
75 10000, 9928, 9857, 9786, 9715, 9645, 9576, 9507
78 static unsigned period_table[12 * 8] = {
79 907, 900, 894, 887, 881, 875, 868, 862, 856, 850, 844, 838,
80 832, 826, 820, 814, 808, 802, 796, 791, 785, 779, 774, 768,
81 762, 757, 752, 746, 741, 736, 730, 725, 720, 715, 709, 704,
82 699, 694, 689, 684, 678, 675, 670, 665, 660, 655, 651, 646,
83 640, 636, 632, 628, 623, 619, 614, 610, 604, 601, 597, 592,
84 588, 584, 580, 575, 570, 567, 563, 559, 555, 551, 547, 543,
85 538, 535, 532, 528, 524, 520, 516, 513, 508, 505, 502, 498,
86 494, 491, 487, 484, 480, 477, 474, 470, 467, 463, 460, 457
89 static size_t trackmod_get_next_ord_idx(trackmod_modplay_t *);
91 /** Destroy sample.
93 * @param sample Sample
95 static void trackmod_sample_destroy(trackmod_sample_t *sample)
97 free(sample->data);
100 /** Destroy instrument.
102 * @param instr Intrument
104 static void trackmod_instr_destroy(trackmod_instr_t *instr)
106 size_t i;
108 for (i = 0; i < instr->samples; i++)
109 trackmod_sample_destroy(&instr->sample[i]);
112 /** Destroy pattern.
114 * @param pattern Pattern
116 static void trackmod_pattern_destroy(trackmod_pattern_t *pattern)
118 free(pattern->data);
121 /** Create new empty module structure.
123 * @return New module
125 trackmod_module_t *trackmod_module_new(void)
127 return calloc(1, sizeof(trackmod_module_t));
130 /** Destroy module.
132 * @param module Module
134 void trackmod_module_destroy(trackmod_module_t *module)
136 size_t i;
138 /* Destroy samples */
139 if (module->instr != NULL) {
140 for (i = 0; i < module->instrs; i++)
141 trackmod_instr_destroy(&module->instr[i]);
142 free(module->instr);
145 /* Destroy patterns */
146 if (module->pattern != NULL) {
147 for (i = 0; i < module->patterns; i++)
148 trackmod_pattern_destroy(&module->pattern[i]);
149 free(module->pattern);
152 free(module->ord_list);
153 free(module);
156 errno_t trackmod_module_load(char *fname, trackmod_module_t **rmodule)
158 errno_t rc;
160 rc = trackmod_xm_load(fname, rmodule);
161 if (rc == EOK)
162 return EOK;
164 rc = trackmod_protracker_load(fname, rmodule);
165 return rc;
169 /** Return current pattern.
171 * @param modplay Module playback
172 * @return Pattern
174 static trackmod_pattern_t *trackmod_cur_pattern(trackmod_modplay_t *modplay)
176 unsigned pat_idx;
178 pat_idx = modplay->module->ord_list[modplay->ord_idx];
179 return &modplay->module->pattern[pat_idx];
182 /** Decode pattern cell.
184 * @param pattern Pattern
185 * @param row Row number
186 * @param channel Channel number
187 * @param cell Place to store decoded cell
189 static void trackmod_pattern_get_cell(trackmod_pattern_t *pattern,
190 size_t row, size_t channel, trackmod_cell_t *cell)
192 *cell = pattern->data[row * pattern->channels + channel];
195 /** Compute floor(a / b), and the remainder.
197 * Unlike standard integer division this rounds towars negative infinity,
198 * not towards zero.
200 * @param a Dividend
201 * @param b Divisor
202 * @param quot Place to store 'quotient' (floor (a/b))
203 * @param rem Place to store 'remainder' (a - floor(a/b) * b)
205 static void divmod_floor(int a, int b, int *quot, int *rem)
207 if (b < 0) {
208 a = -a;
209 b = -b;
212 if (a >= 0) {
213 *quot = a / b;
214 *rem = a % b;
215 } else {
216 *quot = -(-a + (b - 1)) / b;
217 *rem = a - (*quot * b);
221 /** Process note (period)
223 * @param modplay Module playback
224 * @param i Channel number
225 * @param cell Cell
227 static void trackmod_process_note(trackmod_modplay_t *modplay, size_t i,
228 trackmod_cell_t *cell)
230 trackmod_chan_t *chan = &modplay->chan[i];
231 int period;
232 int pitch;
233 int octave;
234 int opitch;
236 if (chan->sample == NULL)
237 return;
239 if (cell->period == 0) {
240 pitch = 8 * (cell->note + chan->sample->rel_note) +
241 chan->sample->finetune;
242 divmod_floor(pitch, 8 * 12, &octave, &opitch);
244 if (octave >= 0)
245 period = period_table[opitch] * 8 / (1 << octave);
246 else
247 period = period_table[opitch] * 8 * (1 << (-octave));
248 } else {
249 period = cell->period;
250 period = period *
251 finetune_factor[chan->sample->finetune + 8] / 10000;
252 if (period > period_max)
253 period = period_max;
254 if (period < period_min)
255 period = period_min;
258 chan->period_new = period;
261 /** Process instrument number (this is what triggers the note playback)
263 * @param modplay Module playback
264 * @param i Channel number
265 * @param cell Cell
267 static void trackmod_process_instr(trackmod_modplay_t *modplay, size_t i,
268 trackmod_cell_t *cell)
270 trackmod_chan_t *chan = &modplay->chan[i];
271 trackmod_instr_t *instr;
272 size_t iidx;
273 size_t sidx;
275 if (cell->instr == 0)
276 return;
278 iidx = (cell->instr - 1) % modplay->module->instrs;
279 instr = &modplay->module->instr[iidx];
280 sidx = instr->key_smp[cell->note] % instr->samples;
281 chan->sample = &instr->sample[sidx];
282 chan->smp_pos = 0;
283 chan->lsmp = 0;
285 chan->volume = modplay->chan[i].sample->def_vol;
288 /** Process keyoff note
290 * @param modplay Module playback
291 * @param i Channel number
292 * @param cell Cell
294 static void trackmod_process_keyoff_note(trackmod_modplay_t *modplay, size_t i)
296 trackmod_chan_t *chan = &modplay->chan[i];
298 chan->sample = NULL;
299 chan->period = 0;
300 chan->smp_pos = 0;
301 chan->lsmp = 0;
304 /** Process Set volume effect.
306 * @param modplay Module playback
307 * @param chan Channel number
308 * @param param Effect parameter
310 static void trackmod_effect_set_volume(trackmod_modplay_t *modplay, size_t chan,
311 uint8_t param)
313 modplay->chan[chan].volume = param % (vol_max + 1);
316 /** Process Pattern break effect.
318 * @param modplay Module playback
319 * @param chan Channel number
320 * @param param Effect parameter
322 static void trackmod_effect_pattern_break(trackmod_modplay_t *modplay,
323 size_t chan, uint8_t param)
325 size_t next_idx;
326 trackmod_pattern_t *next_pat;
327 unsigned row;
329 /* Strangely the parameter is BCD */
330 row = (param >> 4) * 10 + (param & 0xf);
332 next_idx = trackmod_get_next_ord_idx(modplay);
333 next_pat = &modplay->module->pattern[next_idx];
335 modplay->pat_break = true;
336 modplay->pat_break_row = row % next_pat->rows;
339 /** Process Set speed effect.
341 * @param modplay Module playback
342 * @param chan Channel number
343 * @param param Effect parameter
345 static void trackmod_effect_set_speed(trackmod_modplay_t *modplay, size_t chan,
346 uint8_t param)
348 if (param > 0 && param < 32)
349 modplay->tpr = param;
350 else if (param > 0)
351 modplay->bpm = param;
354 /** Process Fine volume slide down effect.
356 * @param modplay Module playback
357 * @param chan Channel number
358 * @param param Effect parameter
360 static void trackmod_effect_fine_vol_slide_down(trackmod_modplay_t *modplay,
361 size_t chan, uint8_t param)
363 int nv;
365 nv = modplay->chan[chan].volume - param;
366 if (nv < 0)
367 nv = 0;
368 modplay->chan[chan].volume = nv;
371 /** Process Fine volume slide up effect.
373 * @param modplay Module playback
374 * @param chan Channel number
375 * @param param Effect parameter
377 static void trackmod_effect_fine_vol_slide_up(trackmod_modplay_t *modplay,
378 size_t chan, uint8_t param)
380 int nv;
382 nv = modplay->chan[chan].volume + param;
383 if (nv > vol_max)
384 nv = vol_max;
385 modplay->chan[chan].volume = nv;
388 /** Process Volume slide effect.
390 * @param modplay Module playback
391 * @param chan Channel number
392 * @param param Effect parameter
394 static void trackmod_effect_vol_slide(trackmod_modplay_t *modplay,
395 size_t chan, uint8_t param)
397 if ((param & 0xf0) != 0)
398 modplay->chan[chan].vol_slide = param >> 4;
399 else
400 modplay->chan[chan].vol_slide = -(int)(param & 0xf);
403 /** Process Volume slide down effect.
405 * @param modplay Module playback
406 * @param chan Channel number
407 * @param param Effect parameter
409 static void trackmod_effect_vol_slide_down(trackmod_modplay_t *modplay,
410 size_t chan, uint8_t param4)
412 modplay->chan[chan].vol_slide = -(int)param4;
415 /** Process Volume slide up effect.
417 * @param modplay Module playback
418 * @param chan Channel number
419 * @param param Effect parameter
421 static void trackmod_effect_vol_slide_up(trackmod_modplay_t *modplay,
422 size_t chan, uint8_t param4)
424 modplay->chan[chan].vol_slide = param4;
427 /** Process Fine portamento down effect.
429 * @param modplay Module playback
430 * @param chan Channel number
431 * @param param Effect parameter
433 static void trackmod_effect_fine_porta_down(trackmod_modplay_t *modplay,
434 size_t chan, uint8_t param)
436 int np;
438 np = modplay->chan[chan].period + param;
439 if (np > period_max)
440 np = period_max;
441 modplay->chan[chan].period = np;
444 /** Process Fine portamento up effect.
446 * @param modplay Module playback
447 * @param chan Channel number
448 * @param param Effect parameter
450 static void trackmod_effect_fine_porta_up(trackmod_modplay_t *modplay,
451 size_t chan, uint8_t param)
453 int np;
455 np = modplay->chan[chan].period - param;
456 if (np < period_min)
457 np = period_min;
458 modplay->chan[chan].period = np;
461 /** Process Portamento down effect.
463 * @param modplay Module playback
464 * @param chan Channel number
465 * @param param Effect parameter
467 static void trackmod_effect_porta_down(trackmod_modplay_t *modplay,
468 size_t chan, uint8_t param)
470 modplay->chan[chan].portamento = -(int)param;
473 /** Process Portamento up effect.
475 * @param modplay Module playback
476 * @param chan Channel number
477 * @param param Effect parameter
479 static void trackmod_effect_porta_up(trackmod_modplay_t *modplay,
480 size_t chan, uint8_t param)
482 modplay->chan[chan].portamento = param;
485 /** Process Tone portamento effect.
487 * @param modplay Module playback
488 * @param chan Channel number
489 * @param param Effect parameter
491 static void trackmod_effect_tone_porta(trackmod_modplay_t *modplay,
492 size_t chan, uint8_t param)
494 /* Set up tone portamento effect */
495 modplay->chan[chan].portamento = param;
496 if (modplay->chan[chan].period_new != 0)
497 modplay->chan[chan].period_tgt = modplay->chan[chan].period_new;
499 /* Prevent going directly to new period */
500 modplay->chan[chan].period_new = 0;
503 /** Process volume column.
505 * @param modplay Module playback
506 * @param chan Channel number
507 * @param cell Cell
509 static void trackmod_process_volume(trackmod_modplay_t *modplay, size_t chan,
510 trackmod_cell_t *cell)
512 uint8_t param4;
514 if (cell->volume >= 0x10 && cell->volume <= 0x10 + vol_max)
515 trackmod_effect_set_volume(modplay, chan, cell->volume - 0x10);
517 param4 = cell->volume & 0xf;
519 switch (cell->volume & 0xf0) {
520 case 0x60:
521 trackmod_effect_vol_slide_down(modplay, chan, param4);
522 break;
523 case 0x70:
524 trackmod_effect_vol_slide_up(modplay, chan, param4);
525 break;
526 case 0x80:
527 trackmod_effect_fine_vol_slide_down(modplay, chan, param4);
528 break;
529 case 0x90:
530 trackmod_effect_fine_vol_slide_up(modplay, chan, param4);
531 break;
532 case 0xf0:
533 trackmod_effect_tone_porta(modplay, chan, param4 << 4);
534 break;
535 default:
536 break;
540 /** Process effect.
542 * @param modplay Module playback
543 * @param chan Channel number
544 * @param cell Cell
546 static void trackmod_process_effect(trackmod_modplay_t *modplay, size_t chan,
547 trackmod_cell_t *cell)
549 uint8_t param8;
550 uint8_t param4;
552 param8 = cell->effect & 0xff;
554 switch (cell->effect & 0xf00) {
555 case 0x100:
556 trackmod_effect_porta_up(modplay, chan, param8);
557 break;
558 case 0x200:
559 trackmod_effect_porta_down(modplay, chan, param8);
560 break;
561 case 0x300:
562 trackmod_effect_tone_porta(modplay, chan, param8);
563 break;
564 case 0xa00:
565 trackmod_effect_vol_slide(modplay, chan, param8);
566 break;
567 case 0xc00:
568 trackmod_effect_set_volume(modplay, chan, param8);
569 break;
570 case 0xd00:
571 trackmod_effect_pattern_break(modplay, chan, param8);
572 break;
573 case 0xf00:
574 trackmod_effect_set_speed(modplay, chan, param8);
575 break;
576 default:
577 break;
580 param4 = cell->effect & 0xf;
582 switch (cell->effect & 0xff0) {
583 case 0xe10:
584 trackmod_effect_fine_porta_up(modplay, chan, param4);
585 break;
586 case 0xe20:
587 trackmod_effect_fine_porta_down(modplay, chan, param4);
588 break;
589 case 0xea0:
590 trackmod_effect_fine_vol_slide_up(modplay, chan, param4);
591 break;
592 case 0xeb0:
593 trackmod_effect_fine_vol_slide_down(modplay, chan, param4);
594 break;
598 /** Process pattern cell.
600 * @param modplay Module playback
601 * @param chan Channel number
602 * @param cell Cell
604 static void trackmod_process_cell(trackmod_modplay_t *modplay, size_t chan,
605 trackmod_cell_t *cell)
607 modplay->chan[chan].period_new = 0;
609 trackmod_process_instr(modplay, chan, cell);
611 if (cell->period != 0 || (cell->note != 0 && cell->note != keyoff_note)) {
612 trackmod_process_note(modplay, chan, cell);
613 } else if (cell->note == keyoff_note && cell->instr == 0) {
614 trackmod_process_keyoff_note(modplay, chan);
617 trackmod_process_volume(modplay, chan, cell);
618 trackmod_process_effect(modplay, chan, cell);
620 if (modplay->chan[chan].period_new != 0)
621 modplay->chan[chan].period = modplay->chan[chan].period_new;
624 /** Process pattern row.
626 * @param modplay Module playback
628 static void trackmod_process_row(trackmod_modplay_t *modplay)
630 trackmod_pattern_t *pattern;
631 trackmod_cell_t cell;
632 size_t i;
634 pattern = trackmod_cur_pattern(modplay);
636 if (modplay->debug)
637 printf("%02zx: ", modplay->row);
639 for (i = 0; i < modplay->module->channels; i++) {
640 trackmod_pattern_get_cell(pattern, modplay->row, i, &cell);
642 if (modplay->debug) {
643 printf("%4d %02x %02x %03x |", cell.period ?
644 cell.period : cell.note, cell.instr,
645 cell.volume, cell.effect);
648 trackmod_process_cell(modplay, i, &cell);
651 if (modplay->debug)
652 printf("\n");
655 /** Get next order list index.
657 * @param modplay Module playback
658 * @return Next order list index
660 static size_t trackmod_get_next_ord_idx(trackmod_modplay_t *modplay)
662 size_t ord_idx;
664 ord_idx = modplay->ord_idx + 1;
665 if (ord_idx >= modplay->module->ord_list_len)
666 ord_idx = modplay->module->restart_pos;
668 return ord_idx;
671 /** Advance to next pattern.
673 * @param modplay Module playback
675 static void trackmod_next_pattern(trackmod_modplay_t *modplay)
677 if (modplay->debug)
678 printf("Next pattern\n");
680 modplay->row = 0;
681 modplay->ord_idx = trackmod_get_next_ord_idx(modplay);
683 /* If we are doing a pattern break */
684 if (modplay->pat_break) {
685 modplay->row = modplay->pat_break_row;
686 modplay->pat_break = false;
690 /** Clear effects at end of row. */
691 static void trackmod_clear_effects(trackmod_modplay_t *modplay)
693 size_t i;
695 for (i = 0; i < modplay->module->channels; i++) {
696 modplay->chan[i].vol_slide = 0;
697 modplay->chan[i].portamento = 0;
701 /** Process effects at beginning of tick. */
702 static void trackmod_process_tick(trackmod_modplay_t *modplay)
704 trackmod_chan_t *chan;
705 size_t i;
706 int nv;
707 int np;
709 for (i = 0; i < modplay->module->channels; i++) {
710 chan = &modplay->chan[i];
712 /* Volume slides */
713 nv = (int)chan->volume + chan->vol_slide;
714 if (nv < 0)
715 nv = 0;
716 if (nv > vol_max)
717 nv = vol_max;
719 chan->volume = nv;
721 /* Portamentos */
722 if (chan->period_tgt == 0) {
723 /* Up or down portamento */
724 np = (int)chan->period - chan->portamento;
725 } else {
726 /* Tone portamento */
727 if (chan->period_tgt < chan->period)
728 np = max((int)chan->period_tgt, (int)chan->period - chan->portamento);
729 else
730 np = min((int)chan->period_tgt, (int)chan->period + chan->portamento);
733 #if 0
734 /* XXX */
735 if (np < period_min)
736 np = period_min;
737 if (np > period_max)
738 np = period_max;
739 #endif
740 modplay->chan[i].period = np;
744 /** Advance to next row.
746 * @param modplay Module playback
748 static void trackmod_next_row(trackmod_modplay_t *modplay)
750 trackmod_pattern_t *pattern;
752 /* Clear effect state at end of row */
753 trackmod_clear_effects(modplay);
755 pattern = trackmod_cur_pattern(modplay);
757 modplay->tick = 0;
758 ++modplay->row;
759 if (modplay->row >= pattern->rows || modplay->pat_break)
760 trackmod_next_pattern(modplay);
762 trackmod_process_tick(modplay);
763 trackmod_process_row(modplay);
766 /** Advance to next tick.
768 * @param modplay Module playback
770 static void trackmod_next_tick(trackmod_modplay_t *modplay)
772 modplay->smp = 0;
773 ++modplay->tick;
774 if (modplay->tick >= modplay->tpr)
775 trackmod_next_row(modplay);
776 else
777 trackmod_process_tick(modplay);
780 /** Create module playback object.
782 * @param module Module
783 * @param smp_freq Sampling frequency
784 * @param rmodplay Place to store pointer to module playback object
786 errno_t trackmod_modplay_create(trackmod_module_t *module,
787 unsigned smp_freq, trackmod_modplay_t **rmodplay)
789 trackmod_modplay_t *modplay = NULL;
791 modplay = calloc(1, sizeof(trackmod_modplay_t));
792 if (modplay == NULL)
793 goto error;
795 modplay->module = module;
796 modplay->smp_freq = smp_freq;
797 modplay->frame_size = sizeof(int16_t);
798 modplay->ord_idx = 0;
799 modplay->row = 0;
800 modplay->tick = 0;
801 modplay->smp = 0;
803 modplay->tpr = module->def_tpr;
804 modplay->bpm = module->def_bpm;
806 modplay->chan = calloc(module->channels,
807 sizeof(trackmod_chan_t));
808 if (modplay->chan == NULL)
809 goto error;
811 trackmod_process_tick(modplay);
812 trackmod_process_row(modplay);
814 *rmodplay = modplay;
815 return EOK;
816 error:
817 if (modplay != NULL)
818 free(modplay->chan);
819 free(modplay);
820 return ENOMEM;
823 /** Destroy module playback object.
825 * @param modplay Module playback
827 void trackmod_modplay_destroy(trackmod_modplay_t *modplay)
829 free(modplay->chan);
830 free(modplay);
833 /** Get number of samples per tick.
835 * @param modplay Module playback
836 * @return Number of samples per tick
838 static size_t samples_per_tick(trackmod_modplay_t *modplay)
840 return modplay->smp_freq * 10 / 4 / modplay->bpm;
843 /** Get number of samples remaining in current tick.
845 * @param modplay Module playback
846 * @return Number of remaining samples in tick
848 static size_t samples_remain_tick(trackmod_modplay_t *modplay)
850 /* XXX Integer samples per tick is a simplification */
851 return samples_per_tick(modplay) - modplay->smp;
854 /** Get sample frame.
856 * Get frame at the specified sample position.
858 * @param sample Sample
859 * @param pos Position (frame index)
860 * @return Frame value
862 int trackmod_sample_get_frame(trackmod_sample_t *sample, size_t pos)
864 int8_t *i8p;
865 int16_t *i16p;
867 if (sample->bytes_smp == 1) {
868 i8p = (int8_t *)sample->data;
869 return i8p[pos];
870 } else {
871 /* chan->sample->bytes_smp == 2 */
872 i16p = (int16_t *)sample->data;
873 return i16p[pos] / 256; /* XXX Retain full precision */
877 /** Advance sample position to next frame.
879 * @param chan Channel playback
881 static void chan_smp_next_frame(trackmod_chan_t *chan)
883 chan->lsmp = trackmod_sample_get_frame(chan->sample, chan->smp_pos);
884 ++chan->smp_pos;
886 switch (chan->sample->loop_type) {
887 case tl_pingpong_loop:
888 /** XXX Pingpong loop */
889 case tl_no_loop:
890 /* No loop */
891 if (chan->smp_pos >= chan->sample->length) {
892 chan->sample = NULL;
893 chan->smp_pos = 0;
895 break;
896 case tl_forward_loop:
897 /** Forward loop */
898 if (chan->smp_pos >= chan->sample->loop_start +
899 chan->sample->loop_len) {
900 chan->smp_pos = chan->sample->loop_start;
905 /** Render next sample on channel.
907 * @param modplay Module playback
908 * @param cidx Channel number
909 * @return Sample value
911 static int trackmod_chan_next_sample(trackmod_modplay_t *modplay,
912 size_t cidx)
914 int sl, sn;
915 int period, clk;
916 int s;
918 trackmod_chan_t *chan = &modplay->chan[cidx];
920 if (chan->sample == NULL || chan->period == 0)
921 return 0;
924 * Linear interpolation. Note this is slightly simplified:
925 * We ignore the half-sample offset and the boundary condition
926 * at the end of the sample (we should extend with zero).
928 sl = (int)chan->lsmp * amp_factor * chan->volume / vol_max;
929 sn = (int)trackmod_sample_get_frame(chan->sample, chan->smp_pos) *
930 amp_factor * chan->volume / vol_max;
932 period = (int)chan->period;
933 clk = (int)chan->smp_clk;
935 s = (sl * (period - clk) + sn * clk) / period;
937 chan->smp_clk += base_clock / modplay->smp_freq;
938 while (chan->sample != NULL && chan->smp_clk >= chan->period) {
939 chan->smp_clk -= chan->period;
940 chan_smp_next_frame(chan);
943 return s;
946 /** Render a segment of samples contained entirely within a tick.
948 * @param modplay Module playback
949 * @param buffer Buffer for storing audio data
950 * @param bufsize Size of @a buffer in bytes
952 static void get_samples_within_tick(trackmod_modplay_t *modplay,
953 void *buffer, size_t bufsize)
955 size_t nsamples;
956 size_t smpidx;
957 size_t chan;
958 int s;
960 nsamples = bufsize / modplay->frame_size;
962 for (smpidx = 0; smpidx < nsamples; smpidx++) {
963 s = 0;
964 for (chan = 0; chan < modplay->module->channels; chan++) {
965 s += trackmod_chan_next_sample(modplay, chan);
968 ((int16_t *)buffer)[smpidx] = s;
971 modplay->smp += nsamples;
974 /** Render a segment of samples.
976 * @param modplay Module playback
977 * @param buffer Buffer for storing audio data
978 * @param bufsize Size of @a buffer in bytes
980 void trackmod_modplay_get_samples(trackmod_modplay_t *modplay,
981 void *buffer, size_t bufsize)
983 size_t nsamples;
984 size_t rsmp;
985 size_t now;
987 nsamples = bufsize / modplay->frame_size;
988 while (nsamples > 0) {
989 rsmp = samples_remain_tick(modplay);
990 if (rsmp == 0)
991 trackmod_next_tick(modplay);
993 rsmp = samples_remain_tick(modplay);
994 now = min(rsmp, nsamples);
996 get_samples_within_tick(modplay, buffer,
997 now * modplay->frame_size);
998 nsamples -= now;
999 buffer += now * modplay->frame_size;
1003 /** @}