| 1 | /* | 
|---|
| 2 | * Copyright (c) 2014 Jiri Svoboda | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 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. | 
|---|
| 16 | * | 
|---|
| 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. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup trackmod | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** | 
|---|
| 33 | * @file Tracker module handling library. | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | #include <assert.h> | 
|---|
| 37 | #include <errno.h> | 
|---|
| 38 | #include <stdio.h> | 
|---|
| 39 | #include <stdlib.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include "macros.h" | 
|---|
| 42 | #include "protracker.h" | 
|---|
| 43 | #include "trackmod.h" | 
|---|
| 44 | #include "xm.h" | 
|---|
| 45 |  | 
|---|
| 46 | /** Tunables */ | 
|---|
| 47 | enum { | 
|---|
| 48 | amp_factor = 16 | 
|---|
| 49 | }; | 
|---|
| 50 |  | 
|---|
| 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 | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | /** Table for finetune computation. | 
|---|
| 64 | * | 
|---|
| 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). | 
|---|
| 69 | * | 
|---|
| 70 | * finetune_factor[ft] := 10000 * 2^(-ft/12/8) | 
|---|
| 71 | * res_period = clip(period * fineture_factor[ft+8] / 10000) | 
|---|
| 72 | */ | 
|---|
| 73 | static unsigned finetune_factor[16] = { | 
|---|
| 74 | 10595, 10518, 10443, 10368, 10293, 10219, 10145, 10072, | 
|---|
| 75 | 10000,  9928,  9857,  9786,  9715,  9645,  9576,  9507 | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 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 | 
|---|
| 87 | }; | 
|---|
| 88 |  | 
|---|
| 89 | static size_t trackmod_get_next_ord_idx(trackmod_modplay_t *); | 
|---|
| 90 |  | 
|---|
| 91 | /** Destroy sample. | 
|---|
| 92 | * | 
|---|
| 93 | * @param sample Sample | 
|---|
| 94 | */ | 
|---|
| 95 | static void trackmod_sample_destroy(trackmod_sample_t *sample) | 
|---|
| 96 | { | 
|---|
| 97 | free(sample->data); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /** Destroy instrument. | 
|---|
| 101 | * | 
|---|
| 102 | * @param instr Intrument | 
|---|
| 103 | */ | 
|---|
| 104 | static void trackmod_instr_destroy(trackmod_instr_t *instr) | 
|---|
| 105 | { | 
|---|
| 106 | size_t i; | 
|---|
| 107 |  | 
|---|
| 108 | for (i = 0; i < instr->samples; i++) | 
|---|
| 109 | trackmod_sample_destroy(&instr->sample[i]); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | /** Destroy pattern. | 
|---|
| 113 | * | 
|---|
| 114 | * @param pattern Pattern | 
|---|
| 115 | */ | 
|---|
| 116 | static void trackmod_pattern_destroy(trackmod_pattern_t *pattern) | 
|---|
| 117 | { | 
|---|
| 118 | free(pattern->data); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | /** Create new empty module structure. | 
|---|
| 122 | * | 
|---|
| 123 | * @return New module | 
|---|
| 124 | */ | 
|---|
| 125 | trackmod_module_t *trackmod_module_new(void) | 
|---|
| 126 | { | 
|---|
| 127 | return calloc(1, sizeof(trackmod_module_t)); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | /** Destroy module. | 
|---|
| 131 | * | 
|---|
| 132 | * @param module Module | 
|---|
| 133 | */ | 
|---|
| 134 | void trackmod_module_destroy(trackmod_module_t *module) | 
|---|
| 135 | { | 
|---|
| 136 | size_t i; | 
|---|
| 137 |  | 
|---|
| 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); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 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); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | free(module->ord_list); | 
|---|
| 153 | free(module); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | errno_t trackmod_module_load(char *fname, trackmod_module_t **rmodule) | 
|---|
| 157 | { | 
|---|
| 158 | errno_t rc; | 
|---|
| 159 |  | 
|---|
| 160 | rc = trackmod_xm_load(fname, rmodule); | 
|---|
| 161 | if (rc == EOK) | 
|---|
| 162 | return EOK; | 
|---|
| 163 |  | 
|---|
| 164 | rc = trackmod_protracker_load(fname, rmodule); | 
|---|
| 165 | return rc; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | /** Return current pattern. | 
|---|
| 170 | * | 
|---|
| 171 | * @param modplay Module playback | 
|---|
| 172 | * @return        Pattern | 
|---|
| 173 | */ | 
|---|
| 174 | static trackmod_pattern_t *trackmod_cur_pattern(trackmod_modplay_t *modplay) | 
|---|
| 175 | { | 
|---|
| 176 | unsigned pat_idx; | 
|---|
| 177 |  | 
|---|
| 178 | pat_idx = modplay->module->ord_list[modplay->ord_idx]; | 
|---|
| 179 | return &modplay->module->pattern[pat_idx]; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | /** Decode pattern cell. | 
|---|
| 183 | * | 
|---|
| 184 | * @param pattern Pattern | 
|---|
| 185 | * @param row     Row number | 
|---|
| 186 | * @param channel Channel number | 
|---|
| 187 | * @param cell    Place to store decoded cell | 
|---|
| 188 | */ | 
|---|
| 189 | static void trackmod_pattern_get_cell(trackmod_pattern_t *pattern, | 
|---|
| 190 | size_t row, size_t channel, trackmod_cell_t *cell) | 
|---|
| 191 | { | 
|---|
| 192 | *cell = pattern->data[row * pattern->channels + channel]; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | /** Compute floor(a / b), and the remainder. | 
|---|
| 196 | * | 
|---|
| 197 | * Unlike standard integer division this rounds towars negative infinity, | 
|---|
| 198 | * not towards zero. | 
|---|
| 199 | * | 
|---|
| 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) | 
|---|
| 204 | */ | 
|---|
| 205 | static void divmod_floor(int a, int b, int *quot, int *rem) | 
|---|
| 206 | { | 
|---|
| 207 | if (b < 0) { | 
|---|
| 208 | a = -a; | 
|---|
| 209 | b = -b; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 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); | 
|---|
| 218 | } | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | /** Process note (period) | 
|---|
| 222 | * | 
|---|
| 223 | * @param modplay Module playback | 
|---|
| 224 | * @param i       Channel number | 
|---|
| 225 | * @param cell    Cell | 
|---|
| 226 | */ | 
|---|
| 227 | static void trackmod_process_note(trackmod_modplay_t *modplay, size_t i, | 
|---|
| 228 | trackmod_cell_t *cell) | 
|---|
| 229 | { | 
|---|
| 230 | trackmod_chan_t *chan = &modplay->chan[i]; | 
|---|
| 231 | int period; | 
|---|
| 232 | int pitch; | 
|---|
| 233 | int octave; | 
|---|
| 234 | int opitch; | 
|---|
| 235 |  | 
|---|
| 236 | if (chan->sample == NULL) | 
|---|
| 237 | return; | 
|---|
| 238 |  | 
|---|
| 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); | 
|---|
| 243 |  | 
|---|
| 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; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | chan->period_new = period; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | /** Process instrument number (this is what triggers the note playback) | 
|---|
| 262 | * | 
|---|
| 263 | * @param modplay Module playback | 
|---|
| 264 | * @param i       Channel number | 
|---|
| 265 | * @param cell    Cell | 
|---|
| 266 | */ | 
|---|
| 267 | static void trackmod_process_instr(trackmod_modplay_t *modplay, size_t i, | 
|---|
| 268 | trackmod_cell_t *cell) | 
|---|
| 269 | { | 
|---|
| 270 | trackmod_chan_t *chan = &modplay->chan[i]; | 
|---|
| 271 | trackmod_instr_t *instr; | 
|---|
| 272 | size_t iidx; | 
|---|
| 273 | size_t sidx; | 
|---|
| 274 |  | 
|---|
| 275 | if (cell->instr == 0) | 
|---|
| 276 | return; | 
|---|
| 277 |  | 
|---|
| 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; | 
|---|
| 284 |  | 
|---|
| 285 | chan->volume = modplay->chan[i].sample->def_vol; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | /** Process keyoff note | 
|---|
| 289 | * | 
|---|
| 290 | * @param modplay Module playback | 
|---|
| 291 | * @param i       Channel number | 
|---|
| 292 | * @param cell    Cell | 
|---|
| 293 | */ | 
|---|
| 294 | static void trackmod_process_keyoff_note(trackmod_modplay_t *modplay, size_t i) | 
|---|
| 295 | { | 
|---|
| 296 | trackmod_chan_t *chan = &modplay->chan[i]; | 
|---|
| 297 |  | 
|---|
| 298 | chan->sample = NULL; | 
|---|
| 299 | chan->period = 0; | 
|---|
| 300 | chan->smp_pos = 0; | 
|---|
| 301 | chan->lsmp = 0; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | /** Process Set volume effect. | 
|---|
| 305 | * | 
|---|
| 306 | * @param modplay Module playback | 
|---|
| 307 | * @param chan    Channel number | 
|---|
| 308 | * @param param   Effect parameter | 
|---|
| 309 | */ | 
|---|
| 310 | static void trackmod_effect_set_volume(trackmod_modplay_t *modplay, size_t chan, | 
|---|
| 311 | uint8_t param) | 
|---|
| 312 | { | 
|---|
| 313 | modplay->chan[chan].volume = param % (vol_max + 1); | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | /** Process Pattern break effect. | 
|---|
| 317 | * | 
|---|
| 318 | * @param modplay Module playback | 
|---|
| 319 | * @param chan    Channel number | 
|---|
| 320 | * @param param   Effect parameter | 
|---|
| 321 | */ | 
|---|
| 322 | static void trackmod_effect_pattern_break(trackmod_modplay_t *modplay, | 
|---|
| 323 | size_t chan, uint8_t param) | 
|---|
| 324 | { | 
|---|
| 325 | size_t next_idx; | 
|---|
| 326 | trackmod_pattern_t *next_pat; | 
|---|
| 327 | unsigned row; | 
|---|
| 328 |  | 
|---|
| 329 | /* Strangely the parameter is BCD */ | 
|---|
| 330 | row = (param >> 4) * 10 + (param & 0xf); | 
|---|
| 331 |  | 
|---|
| 332 | next_idx = trackmod_get_next_ord_idx(modplay); | 
|---|
| 333 | next_pat = &modplay->module->pattern[next_idx]; | 
|---|
| 334 |  | 
|---|
| 335 | modplay->pat_break = true; | 
|---|
| 336 | modplay->pat_break_row = row % next_pat->rows; | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | /** Process Set speed effect. | 
|---|
| 340 | * | 
|---|
| 341 | * @param modplay Module playback | 
|---|
| 342 | * @param chan    Channel number | 
|---|
| 343 | * @param param   Effect parameter | 
|---|
| 344 | */ | 
|---|
| 345 | static void trackmod_effect_set_speed(trackmod_modplay_t *modplay, size_t chan, | 
|---|
| 346 | uint8_t param) | 
|---|
| 347 | { | 
|---|
| 348 | if (param > 0 && param < 32) | 
|---|
| 349 | modplay->tpr = param; | 
|---|
| 350 | else if (param > 0) | 
|---|
| 351 | modplay->bpm = param; | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | /** Process Fine volume slide down effect. | 
|---|
| 355 | * | 
|---|
| 356 | * @param modplay Module playback | 
|---|
| 357 | * @param chan    Channel number | 
|---|
| 358 | * @param param   Effect parameter | 
|---|
| 359 | */ | 
|---|
| 360 | static void trackmod_effect_fine_vol_slide_down(trackmod_modplay_t *modplay, | 
|---|
| 361 | size_t chan, uint8_t param) | 
|---|
| 362 | { | 
|---|
| 363 | int nv; | 
|---|
| 364 |  | 
|---|
| 365 | nv = modplay->chan[chan].volume - param; | 
|---|
| 366 | if (nv < 0) | 
|---|
| 367 | nv = 0; | 
|---|
| 368 | modplay->chan[chan].volume = nv; | 
|---|
| 369 | } | 
|---|
| 370 |  | 
|---|
| 371 | /** Process Fine volume slide up effect. | 
|---|
| 372 | * | 
|---|
| 373 | * @param modplay Module playback | 
|---|
| 374 | * @param chan    Channel number | 
|---|
| 375 | * @param param   Effect parameter | 
|---|
| 376 | */ | 
|---|
| 377 | static void trackmod_effect_fine_vol_slide_up(trackmod_modplay_t *modplay, | 
|---|
| 378 | size_t chan, uint8_t param) | 
|---|
| 379 | { | 
|---|
| 380 | int nv; | 
|---|
| 381 |  | 
|---|
| 382 | nv = modplay->chan[chan].volume + param; | 
|---|
| 383 | if (nv > vol_max) | 
|---|
| 384 | nv = vol_max; | 
|---|
| 385 | modplay->chan[chan].volume = nv; | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | /** Process Volume slide effect. | 
|---|
| 389 | * | 
|---|
| 390 | * @param modplay Module playback | 
|---|
| 391 | * @param chan    Channel number | 
|---|
| 392 | * @param param   Effect parameter | 
|---|
| 393 | */ | 
|---|
| 394 | static void trackmod_effect_vol_slide(trackmod_modplay_t *modplay, | 
|---|
| 395 | size_t chan, uint8_t param) | 
|---|
| 396 | { | 
|---|
| 397 | if ((param & 0xf0) != 0) | 
|---|
| 398 | modplay->chan[chan].vol_slide = param >> 4; | 
|---|
| 399 | else | 
|---|
| 400 | modplay->chan[chan].vol_slide = -(int)(param & 0xf); | 
|---|
| 401 | } | 
|---|
| 402 |  | 
|---|
| 403 | /** Process Volume slide down effect. | 
|---|
| 404 | * | 
|---|
| 405 | * @param modplay Module playback | 
|---|
| 406 | * @param chan    Channel number | 
|---|
| 407 | * @param param   Effect parameter | 
|---|
| 408 | */ | 
|---|
| 409 | static void trackmod_effect_vol_slide_down(trackmod_modplay_t *modplay, | 
|---|
| 410 | size_t chan, uint8_t param4) | 
|---|
| 411 | { | 
|---|
| 412 | modplay->chan[chan].vol_slide = -(int)param4; | 
|---|
| 413 | } | 
|---|
| 414 |  | 
|---|
| 415 | /** Process Volume slide up effect. | 
|---|
| 416 | * | 
|---|
| 417 | * @param modplay Module playback | 
|---|
| 418 | * @param chan    Channel number | 
|---|
| 419 | * @param param   Effect parameter | 
|---|
| 420 | */ | 
|---|
| 421 | static void trackmod_effect_vol_slide_up(trackmod_modplay_t *modplay, | 
|---|
| 422 | size_t chan, uint8_t param4) | 
|---|
| 423 | { | 
|---|
| 424 | modplay->chan[chan].vol_slide = param4; | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | /** Process Fine portamento down effect. | 
|---|
| 428 | * | 
|---|
| 429 | * @param modplay Module playback | 
|---|
| 430 | * @param chan    Channel number | 
|---|
| 431 | * @param param   Effect parameter | 
|---|
| 432 | */ | 
|---|
| 433 | static void trackmod_effect_fine_porta_down(trackmod_modplay_t *modplay, | 
|---|
| 434 | size_t chan, uint8_t param) | 
|---|
| 435 | { | 
|---|
| 436 | int np; | 
|---|
| 437 |  | 
|---|
| 438 | np = modplay->chan[chan].period + param; | 
|---|
| 439 | if (np > period_max) | 
|---|
| 440 | np = period_max; | 
|---|
| 441 | modplay->chan[chan].period = np; | 
|---|
| 442 | } | 
|---|
| 443 |  | 
|---|
| 444 | /** Process Fine portamento up effect. | 
|---|
| 445 | * | 
|---|
| 446 | * @param modplay Module playback | 
|---|
| 447 | * @param chan    Channel number | 
|---|
| 448 | * @param param   Effect parameter | 
|---|
| 449 | */ | 
|---|
| 450 | static void trackmod_effect_fine_porta_up(trackmod_modplay_t *modplay, | 
|---|
| 451 | size_t chan, uint8_t param) | 
|---|
| 452 | { | 
|---|
| 453 | int np; | 
|---|
| 454 |  | 
|---|
| 455 | np = modplay->chan[chan].period - param; | 
|---|
| 456 | if (np < period_min) | 
|---|
| 457 | np = period_min; | 
|---|
| 458 | modplay->chan[chan].period = np; | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | /** Process Portamento down effect. | 
|---|
| 462 | * | 
|---|
| 463 | * @param modplay Module playback | 
|---|
| 464 | * @param chan    Channel number | 
|---|
| 465 | * @param param   Effect parameter | 
|---|
| 466 | */ | 
|---|
| 467 | static void trackmod_effect_porta_down(trackmod_modplay_t *modplay, | 
|---|
| 468 | size_t chan, uint8_t param) | 
|---|
| 469 | { | 
|---|
| 470 | modplay->chan[chan].portamento = -(int)param; | 
|---|
| 471 | } | 
|---|
| 472 |  | 
|---|
| 473 | /** Process Portamento up effect. | 
|---|
| 474 | * | 
|---|
| 475 | * @param modplay Module playback | 
|---|
| 476 | * @param chan    Channel number | 
|---|
| 477 | * @param param   Effect parameter | 
|---|
| 478 | */ | 
|---|
| 479 | static void trackmod_effect_porta_up(trackmod_modplay_t *modplay, | 
|---|
| 480 | size_t chan, uint8_t param) | 
|---|
| 481 | { | 
|---|
| 482 | modplay->chan[chan].portamento = param; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | /** Process Tone portamento effect. | 
|---|
| 486 | * | 
|---|
| 487 | * @param modplay Module playback | 
|---|
| 488 | * @param chan    Channel number | 
|---|
| 489 | * @param param   Effect parameter | 
|---|
| 490 | */ | 
|---|
| 491 | static void trackmod_effect_tone_porta(trackmod_modplay_t *modplay, | 
|---|
| 492 | size_t chan, uint8_t param) | 
|---|
| 493 | { | 
|---|
| 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; | 
|---|
| 498 |  | 
|---|
| 499 | /* Prevent going directly to new period */ | 
|---|
| 500 | modplay->chan[chan].period_new = 0; | 
|---|
| 501 | } | 
|---|
| 502 |  | 
|---|
| 503 | /** Process volume column. | 
|---|
| 504 | * | 
|---|
| 505 | * @param modplay Module playback | 
|---|
| 506 | * @param chan    Channel number | 
|---|
| 507 | * @param cell    Cell | 
|---|
| 508 | */ | 
|---|
| 509 | static void trackmod_process_volume(trackmod_modplay_t *modplay, size_t chan, | 
|---|
| 510 | trackmod_cell_t *cell) | 
|---|
| 511 | { | 
|---|
| 512 | uint8_t param4; | 
|---|
| 513 |  | 
|---|
| 514 | if (cell->volume >= 0x10 && cell->volume <= 0x10 + vol_max) | 
|---|
| 515 | trackmod_effect_set_volume(modplay, chan, cell->volume - 0x10); | 
|---|
| 516 |  | 
|---|
| 517 | param4 = cell->volume & 0xf; | 
|---|
| 518 |  | 
|---|
| 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; | 
|---|
| 537 | } | 
|---|
| 538 | } | 
|---|
| 539 |  | 
|---|
| 540 | /** Process effect. | 
|---|
| 541 | * | 
|---|
| 542 | * @param modplay Module playback | 
|---|
| 543 | * @param chan    Channel number | 
|---|
| 544 | * @param cell    Cell | 
|---|
| 545 | */ | 
|---|
| 546 | static void trackmod_process_effect(trackmod_modplay_t *modplay, size_t chan, | 
|---|
| 547 | trackmod_cell_t *cell) | 
|---|
| 548 | { | 
|---|
| 549 | uint8_t param8; | 
|---|
| 550 | uint8_t param4; | 
|---|
| 551 |  | 
|---|
| 552 | param8 = cell->effect & 0xff; | 
|---|
| 553 |  | 
|---|
| 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; | 
|---|
| 578 | } | 
|---|
| 579 |  | 
|---|
| 580 | param4 = cell->effect & 0xf; | 
|---|
| 581 |  | 
|---|
| 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; | 
|---|
| 595 | } | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | /** Process pattern cell. | 
|---|
| 599 | * | 
|---|
| 600 | * @param modplay Module playback | 
|---|
| 601 | * @param chan    Channel number | 
|---|
| 602 | * @param cell    Cell | 
|---|
| 603 | */ | 
|---|
| 604 | static void trackmod_process_cell(trackmod_modplay_t *modplay, size_t chan, | 
|---|
| 605 | trackmod_cell_t *cell) | 
|---|
| 606 | { | 
|---|
| 607 | modplay->chan[chan].period_new = 0; | 
|---|
| 608 |  | 
|---|
| 609 | trackmod_process_instr(modplay, chan, cell); | 
|---|
| 610 |  | 
|---|
| 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); | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | trackmod_process_volume(modplay, chan, cell); | 
|---|
| 618 | trackmod_process_effect(modplay, chan, cell); | 
|---|
| 619 |  | 
|---|
| 620 | if (modplay->chan[chan].period_new != 0) | 
|---|
| 621 | modplay->chan[chan].period = modplay->chan[chan].period_new; | 
|---|
| 622 | } | 
|---|
| 623 |  | 
|---|
| 624 | /** Process pattern row. | 
|---|
| 625 | * | 
|---|
| 626 | * @param modplay Module playback | 
|---|
| 627 | */ | 
|---|
| 628 | static void trackmod_process_row(trackmod_modplay_t *modplay) | 
|---|
| 629 | { | 
|---|
| 630 | trackmod_pattern_t *pattern; | 
|---|
| 631 | trackmod_cell_t cell; | 
|---|
| 632 | size_t i; | 
|---|
| 633 |  | 
|---|
| 634 | pattern = trackmod_cur_pattern(modplay); | 
|---|
| 635 |  | 
|---|
| 636 | if (modplay->debug) | 
|---|
| 637 | printf("%02zx: ", modplay->row); | 
|---|
| 638 |  | 
|---|
| 639 | for (i = 0; i < modplay->module->channels; i++) { | 
|---|
| 640 | trackmod_pattern_get_cell(pattern, modplay->row, i, &cell); | 
|---|
| 641 |  | 
|---|
| 642 | if (modplay->debug) { | 
|---|
| 643 | printf("%4d %02x %02x %03x |", cell.period ? | 
|---|
| 644 | cell.period : cell.note, cell.instr, | 
|---|
| 645 | cell.volume, cell.effect); | 
|---|
| 646 | } | 
|---|
| 647 |  | 
|---|
| 648 | trackmod_process_cell(modplay, i, &cell); | 
|---|
| 649 | } | 
|---|
| 650 |  | 
|---|
| 651 | if (modplay->debug) | 
|---|
| 652 | printf("\n"); | 
|---|
| 653 | } | 
|---|
| 654 |  | 
|---|
| 655 | /** Get next order list index. | 
|---|
| 656 | * | 
|---|
| 657 | * @param modplay Module playback | 
|---|
| 658 | * @return        Next order list index | 
|---|
| 659 | */ | 
|---|
| 660 | static size_t trackmod_get_next_ord_idx(trackmod_modplay_t *modplay) | 
|---|
| 661 | { | 
|---|
| 662 | size_t ord_idx; | 
|---|
| 663 |  | 
|---|
| 664 | ord_idx = modplay->ord_idx + 1; | 
|---|
| 665 | if (ord_idx >= modplay->module->ord_list_len) | 
|---|
| 666 | ord_idx = modplay->module->restart_pos; | 
|---|
| 667 |  | 
|---|
| 668 | return ord_idx; | 
|---|
| 669 | } | 
|---|
| 670 |  | 
|---|
| 671 | /** Advance to next pattern. | 
|---|
| 672 | * | 
|---|
| 673 | * @param modplay Module playback | 
|---|
| 674 | */ | 
|---|
| 675 | static void trackmod_next_pattern(trackmod_modplay_t *modplay) | 
|---|
| 676 | { | 
|---|
| 677 | if (modplay->debug) | 
|---|
| 678 | printf("Next pattern\n"); | 
|---|
| 679 |  | 
|---|
| 680 | modplay->row = 0; | 
|---|
| 681 | modplay->ord_idx = trackmod_get_next_ord_idx(modplay); | 
|---|
| 682 |  | 
|---|
| 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; | 
|---|
| 687 | } | 
|---|
| 688 | } | 
|---|
| 689 |  | 
|---|
| 690 | /** Clear effects at end of row. */ | 
|---|
| 691 | static void trackmod_clear_effects(trackmod_modplay_t *modplay) | 
|---|
| 692 | { | 
|---|
| 693 | size_t i; | 
|---|
| 694 |  | 
|---|
| 695 | for (i = 0; i < modplay->module->channels; i++) { | 
|---|
| 696 | modplay->chan[i].vol_slide = 0; | 
|---|
| 697 | modplay->chan[i].portamento = 0; | 
|---|
| 698 | } | 
|---|
| 699 | } | 
|---|
| 700 |  | 
|---|
| 701 | /** Process effects at beginning of tick. */ | 
|---|
| 702 | static void trackmod_process_tick(trackmod_modplay_t *modplay) | 
|---|
| 703 | { | 
|---|
| 704 | trackmod_chan_t *chan; | 
|---|
| 705 | size_t i; | 
|---|
| 706 | int nv; | 
|---|
| 707 | int np; | 
|---|
| 708 |  | 
|---|
| 709 | for (i = 0; i < modplay->module->channels; i++) { | 
|---|
| 710 | chan = &modplay->chan[i]; | 
|---|
| 711 |  | 
|---|
| 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; | 
|---|
| 718 |  | 
|---|
| 719 | chan->volume = nv; | 
|---|
| 720 |  | 
|---|
| 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); | 
|---|
| 731 | } | 
|---|
| 732 |  | 
|---|
| 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; | 
|---|
| 741 | } | 
|---|
| 742 | } | 
|---|
| 743 |  | 
|---|
| 744 | /** Advance to next row. | 
|---|
| 745 | * | 
|---|
| 746 | * @param modplay Module playback | 
|---|
| 747 | */ | 
|---|
| 748 | static void trackmod_next_row(trackmod_modplay_t *modplay) | 
|---|
| 749 | { | 
|---|
| 750 | trackmod_pattern_t *pattern; | 
|---|
| 751 |  | 
|---|
| 752 | /* Clear effect state at end of row */ | 
|---|
| 753 | trackmod_clear_effects(modplay); | 
|---|
| 754 |  | 
|---|
| 755 | pattern = trackmod_cur_pattern(modplay); | 
|---|
| 756 |  | 
|---|
| 757 | modplay->tick = 0; | 
|---|
| 758 | ++modplay->row; | 
|---|
| 759 | if (modplay->row >= pattern->rows || modplay->pat_break) | 
|---|
| 760 | trackmod_next_pattern(modplay); | 
|---|
| 761 |  | 
|---|
| 762 | trackmod_process_tick(modplay); | 
|---|
| 763 | trackmod_process_row(modplay); | 
|---|
| 764 | } | 
|---|
| 765 |  | 
|---|
| 766 | /** Advance to next tick. | 
|---|
| 767 | * | 
|---|
| 768 | * @param modplay Module playback | 
|---|
| 769 | */ | 
|---|
| 770 | static void trackmod_next_tick(trackmod_modplay_t *modplay) | 
|---|
| 771 | { | 
|---|
| 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); | 
|---|
| 778 | } | 
|---|
| 779 |  | 
|---|
| 780 | /** Create module playback object. | 
|---|
| 781 | * | 
|---|
| 782 | * @param module   Module | 
|---|
| 783 | * @param smp_freq Sampling frequency | 
|---|
| 784 | * @param rmodplay Place to store pointer to module playback object | 
|---|
| 785 | */ | 
|---|
| 786 | errno_t trackmod_modplay_create(trackmod_module_t *module, | 
|---|
| 787 | unsigned smp_freq, trackmod_modplay_t **rmodplay) | 
|---|
| 788 | { | 
|---|
| 789 | trackmod_modplay_t *modplay = NULL; | 
|---|
| 790 |  | 
|---|
| 791 | modplay = calloc(1, sizeof(trackmod_modplay_t)); | 
|---|
| 792 | if (modplay == NULL) | 
|---|
| 793 | goto error; | 
|---|
| 794 |  | 
|---|
| 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; | 
|---|
| 802 |  | 
|---|
| 803 | modplay->tpr = module->def_tpr; | 
|---|
| 804 | modplay->bpm = module->def_bpm; | 
|---|
| 805 |  | 
|---|
| 806 | modplay->chan = calloc(module->channels, | 
|---|
| 807 | sizeof(trackmod_chan_t)); | 
|---|
| 808 | if (modplay->chan == NULL) | 
|---|
| 809 | goto error; | 
|---|
| 810 |  | 
|---|
| 811 | trackmod_process_tick(modplay); | 
|---|
| 812 | trackmod_process_row(modplay); | 
|---|
| 813 |  | 
|---|
| 814 | *rmodplay = modplay; | 
|---|
| 815 | return EOK; | 
|---|
| 816 | error: | 
|---|
| 817 | if (modplay != NULL) | 
|---|
| 818 | free(modplay->chan); | 
|---|
| 819 | free(modplay); | 
|---|
| 820 | return ENOMEM; | 
|---|
| 821 | } | 
|---|
| 822 |  | 
|---|
| 823 | /** Destroy module playback object. | 
|---|
| 824 | * | 
|---|
| 825 | * @param modplay Module playback | 
|---|
| 826 | */ | 
|---|
| 827 | void trackmod_modplay_destroy(trackmod_modplay_t *modplay) | 
|---|
| 828 | { | 
|---|
| 829 | free(modplay->chan); | 
|---|
| 830 | free(modplay); | 
|---|
| 831 | } | 
|---|
| 832 |  | 
|---|
| 833 | /** Get number of samples per tick. | 
|---|
| 834 | * | 
|---|
| 835 | * @param modplay Module playback | 
|---|
| 836 | * @return        Number of samples per tick | 
|---|
| 837 | */ | 
|---|
| 838 | static size_t samples_per_tick(trackmod_modplay_t *modplay) | 
|---|
| 839 | { | 
|---|
| 840 | return modplay->smp_freq * 10 / 4 / modplay->bpm; | 
|---|
| 841 | } | 
|---|
| 842 |  | 
|---|
| 843 | /** Get number of samples remaining in current tick. | 
|---|
| 844 | * | 
|---|
| 845 | * @param modplay Module playback | 
|---|
| 846 | * @return        Number of remaining samples in tick | 
|---|
| 847 | */ | 
|---|
| 848 | static size_t samples_remain_tick(trackmod_modplay_t *modplay) | 
|---|
| 849 | { | 
|---|
| 850 | /* XXX Integer samples per tick is a simplification */ | 
|---|
| 851 | return samples_per_tick(modplay) - modplay->smp; | 
|---|
| 852 | } | 
|---|
| 853 |  | 
|---|
| 854 | /** Get sample frame. | 
|---|
| 855 | * | 
|---|
| 856 | * Get frame at the specified sample position. | 
|---|
| 857 | * | 
|---|
| 858 | * @param sample Sample | 
|---|
| 859 | * @param pos    Position (frame index) | 
|---|
| 860 | * @return       Frame value | 
|---|
| 861 | */ | 
|---|
| 862 | int trackmod_sample_get_frame(trackmod_sample_t *sample, size_t pos) | 
|---|
| 863 | { | 
|---|
| 864 | int8_t *i8p; | 
|---|
| 865 | int16_t *i16p; | 
|---|
| 866 |  | 
|---|
| 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 */ | 
|---|
| 874 | } | 
|---|
| 875 | } | 
|---|
| 876 |  | 
|---|
| 877 | /** Advance sample position to next frame. | 
|---|
| 878 | * | 
|---|
| 879 | * @param chan Channel playback | 
|---|
| 880 | */ | 
|---|
| 881 | static void chan_smp_next_frame(trackmod_chan_t *chan) | 
|---|
| 882 | { | 
|---|
| 883 | chan->lsmp = trackmod_sample_get_frame(chan->sample, chan->smp_pos); | 
|---|
| 884 | ++chan->smp_pos; | 
|---|
| 885 |  | 
|---|
| 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; | 
|---|
| 894 | } | 
|---|
| 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; | 
|---|
| 901 | } | 
|---|
| 902 | } | 
|---|
| 903 | } | 
|---|
| 904 |  | 
|---|
| 905 | /** Render next sample on channel. | 
|---|
| 906 | * | 
|---|
| 907 | * @param modplay Module playback | 
|---|
| 908 | * @param cidx    Channel number | 
|---|
| 909 | * @return        Sample value | 
|---|
| 910 | */ | 
|---|
| 911 | static int trackmod_chan_next_sample(trackmod_modplay_t *modplay, | 
|---|
| 912 | size_t cidx) | 
|---|
| 913 | { | 
|---|
| 914 | int sl, sn; | 
|---|
| 915 | int period, clk; | 
|---|
| 916 | int s; | 
|---|
| 917 |  | 
|---|
| 918 | trackmod_chan_t *chan = &modplay->chan[cidx]; | 
|---|
| 919 |  | 
|---|
| 920 | if (chan->sample == NULL || chan->period == 0) | 
|---|
| 921 | return 0; | 
|---|
| 922 |  | 
|---|
| 923 | /* | 
|---|
| 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). | 
|---|
| 927 | */ | 
|---|
| 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; | 
|---|
| 931 |  | 
|---|
| 932 | period = (int)chan->period; | 
|---|
| 933 | clk = (int)chan->smp_clk; | 
|---|
| 934 |  | 
|---|
| 935 | s = (sl * (period - clk) + sn * clk) / period; | 
|---|
| 936 |  | 
|---|
| 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); | 
|---|
| 941 | } | 
|---|
| 942 |  | 
|---|
| 943 | return s; | 
|---|
| 944 | } | 
|---|
| 945 |  | 
|---|
| 946 | /** Render a segment of samples contained entirely within a tick. | 
|---|
| 947 | * | 
|---|
| 948 | * @param modplay Module playback | 
|---|
| 949 | * @param buffer  Buffer for storing audio data | 
|---|
| 950 | * @param bufsize Size of @a buffer in bytes | 
|---|
| 951 | */ | 
|---|
| 952 | static void get_samples_within_tick(trackmod_modplay_t *modplay, | 
|---|
| 953 | void *buffer, size_t bufsize) | 
|---|
| 954 | { | 
|---|
| 955 | size_t nsamples; | 
|---|
| 956 | size_t smpidx; | 
|---|
| 957 | size_t chan; | 
|---|
| 958 | int s; | 
|---|
| 959 |  | 
|---|
| 960 | nsamples = bufsize / modplay->frame_size; | 
|---|
| 961 |  | 
|---|
| 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); | 
|---|
| 966 | } | 
|---|
| 967 |  | 
|---|
| 968 | ((int16_t *)buffer)[smpidx] = s; | 
|---|
| 969 | } | 
|---|
| 970 |  | 
|---|
| 971 | modplay->smp += nsamples; | 
|---|
| 972 | } | 
|---|
| 973 |  | 
|---|
| 974 | /** Render a segment of samples. | 
|---|
| 975 | * | 
|---|
| 976 | * @param modplay Module playback | 
|---|
| 977 | * @param buffer  Buffer for storing audio data | 
|---|
| 978 | * @param bufsize Size of @a buffer in bytes | 
|---|
| 979 | */ | 
|---|
| 980 | void trackmod_modplay_get_samples(trackmod_modplay_t *modplay, | 
|---|
| 981 | void *buffer, size_t bufsize) | 
|---|
| 982 | { | 
|---|
| 983 | size_t nsamples; | 
|---|
| 984 | size_t rsmp; | 
|---|
| 985 | size_t now; | 
|---|
| 986 |  | 
|---|
| 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); | 
|---|
| 992 |  | 
|---|
| 993 | rsmp = samples_remain_tick(modplay); | 
|---|
| 994 | now = min(rsmp, nsamples); | 
|---|
| 995 |  | 
|---|
| 996 | get_samples_within_tick(modplay, buffer, | 
|---|
| 997 | now * modplay->frame_size); | 
|---|
| 998 | nsamples -= now; | 
|---|
| 999 | buffer += now * modplay->frame_size; | 
|---|
| 1000 | } | 
|---|
| 1001 | } | 
|---|
| 1002 |  | 
|---|
| 1003 | /** @} | 
|---|
| 1004 | */ | 
|---|