2 * Copyright (c) 2014 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
33 * @file Tracker module handling library.
42 #include "protracker.h"
51 /** Standard definitions set in stone */
53 /** Base sample clock */
54 base_clock
= 8363 * 428,
55 /** Maximum sample volume */
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
*);
93 * @param sample Sample
95 static void trackmod_sample_destroy(trackmod_sample_t
*sample
)
100 /** Destroy instrument.
102 * @param instr Intrument
104 static void trackmod_instr_destroy(trackmod_instr_t
*instr
)
108 for (i
= 0; i
< instr
->samples
; i
++)
109 trackmod_sample_destroy(&instr
->sample
[i
]);
114 * @param pattern Pattern
116 static void trackmod_pattern_destroy(trackmod_pattern_t
*pattern
)
121 /** Create new empty module structure.
125 trackmod_module_t
*trackmod_module_new(void)
127 return calloc(1, sizeof(trackmod_module_t
));
132 * @param module Module
134 void trackmod_module_destroy(trackmod_module_t
*module
)
138 /* Destroy samples */
139 if (module
->instr
!= NULL
) {
140 for (i
= 0; i
< module
->instrs
; i
++)
141 trackmod_instr_destroy(&module
->instr
[i
]);
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
);
156 errno_t
trackmod_module_load(char *fname
, trackmod_module_t
**rmodule
)
160 rc
= trackmod_xm_load(fname
, rmodule
);
164 rc
= trackmod_protracker_load(fname
, rmodule
);
169 /** Return current pattern.
171 * @param modplay Module playback
174 static trackmod_pattern_t
*trackmod_cur_pattern(trackmod_modplay_t
*modplay
)
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,
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
)
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
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
];
236 if (chan
->sample
== NULL
)
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
);
245 period
= period_table
[opitch
] * 8 / (1 << octave
);
247 period
= period_table
[opitch
] * 8 * (1 << (-octave
));
249 period
= cell
->period
;
251 finetune_factor
[chan
->sample
->finetune
+ 8] / 10000;
252 if (period
> period_max
)
254 if (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
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
;
275 if (cell
->instr
== 0)
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
];
285 chan
->volume
= modplay
->chan
[i
].sample
->def_vol
;
288 /** Process keyoff note
290 * @param modplay Module playback
291 * @param i Channel number
294 static void trackmod_process_keyoff_note(trackmod_modplay_t
*modplay
, size_t i
)
296 trackmod_chan_t
*chan
= &modplay
->chan
[i
];
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
,
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
)
326 trackmod_pattern_t
*next_pat
;
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
,
348 if (param
> 0 && param
< 32)
349 modplay
->tpr
= param
;
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
)
365 nv
= modplay
->chan
[chan
].volume
- param
;
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
)
382 nv
= modplay
->chan
[chan
].volume
+ param
;
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;
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
)
438 np
= modplay
->chan
[chan
].period
+ param
;
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
)
455 np
= modplay
->chan
[chan
].period
- param
;
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
509 static void trackmod_process_volume(trackmod_modplay_t
*modplay
, size_t chan
,
510 trackmod_cell_t
*cell
)
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) {
521 trackmod_effect_vol_slide_down(modplay
, chan
, param4
);
524 trackmod_effect_vol_slide_up(modplay
, chan
, param4
);
527 trackmod_effect_fine_vol_slide_down(modplay
, chan
, param4
);
530 trackmod_effect_fine_vol_slide_up(modplay
, chan
, param4
);
533 trackmod_effect_tone_porta(modplay
, chan
, param4
<< 4);
542 * @param modplay Module playback
543 * @param chan Channel number
546 static void trackmod_process_effect(trackmod_modplay_t
*modplay
, size_t chan
,
547 trackmod_cell_t
*cell
)
552 param8
= cell
->effect
& 0xff;
554 switch (cell
->effect
& 0xf00) {
556 trackmod_effect_porta_up(modplay
, chan
, param8
);
559 trackmod_effect_porta_down(modplay
, chan
, param8
);
562 trackmod_effect_tone_porta(modplay
, chan
, param8
);
565 trackmod_effect_vol_slide(modplay
, chan
, param8
);
568 trackmod_effect_set_volume(modplay
, chan
, param8
);
571 trackmod_effect_pattern_break(modplay
, chan
, param8
);
574 trackmod_effect_set_speed(modplay
, chan
, param8
);
580 param4
= cell
->effect
& 0xf;
582 switch (cell
->effect
& 0xff0) {
584 trackmod_effect_fine_porta_up(modplay
, chan
, param4
);
587 trackmod_effect_fine_porta_down(modplay
, chan
, param4
);
590 trackmod_effect_fine_vol_slide_up(modplay
, chan
, param4
);
593 trackmod_effect_fine_vol_slide_down(modplay
, chan
, param4
);
598 /** Process pattern cell.
600 * @param modplay Module playback
601 * @param chan Channel number
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
;
634 pattern
= trackmod_cur_pattern(modplay
);
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
);
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
)
664 ord_idx
= modplay
->ord_idx
+ 1;
665 if (ord_idx
>= modplay
->module
->ord_list_len
)
666 ord_idx
= modplay
->module
->restart_pos
;
671 /** Advance to next pattern.
673 * @param modplay Module playback
675 static void trackmod_next_pattern(trackmod_modplay_t
*modplay
)
678 printf("Next pattern\n");
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
)
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
;
709 for (i
= 0; i
< modplay
->module
->channels
; i
++) {
710 chan
= &modplay
->chan
[i
];
713 nv
= (int)chan
->volume
+ chan
->vol_slide
;
722 if (chan
->period_tgt
== 0) {
723 /* Up or down portamento */
724 np
= (int)chan
->period
- chan
->portamento
;
726 /* Tone portamento */
727 if (chan
->period_tgt
< chan
->period
)
728 np
= max((int)chan
->period_tgt
, (int)chan
->period
- chan
->portamento
);
730 np
= min((int)chan
->period_tgt
, (int)chan
->period
+ chan
->portamento
);
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
);
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
)
774 if (modplay
->tick
>= modplay
->tpr
)
775 trackmod_next_row(modplay
);
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
));
795 modplay
->module
= module
;
796 modplay
->smp_freq
= smp_freq
;
797 modplay
->frame_size
= sizeof(int16_t);
798 modplay
->ord_idx
= 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
)
811 trackmod_process_tick(modplay
);
812 trackmod_process_row(modplay
);
823 /** Destroy module playback object.
825 * @param modplay Module playback
827 void trackmod_modplay_destroy(trackmod_modplay_t
*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
)
867 if (sample
->bytes_smp
== 1) {
868 i8p
= (int8_t *)sample
->data
;
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
);
886 switch (chan
->sample
->loop_type
) {
887 case tl_pingpong_loop
:
888 /** XXX Pingpong loop */
891 if (chan
->smp_pos
>= chan
->sample
->length
) {
896 case tl_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
,
918 trackmod_chan_t
*chan
= &modplay
->chan
[cidx
];
920 if (chan
->sample
== NULL
|| chan
->period
== 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
);
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
)
960 nsamples
= bufsize
/ modplay
->frame_size
;
962 for (smpidx
= 0; smpidx
< nsamples
; smpidx
++) {
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
)
987 nsamples
= bufsize
/ modplay
->frame_size
;
988 while (nsamples
> 0) {
989 rsmp
= samples_remain_tick(modplay
);
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
);
999 buffer
+= now
* modplay
->frame_size
;