A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 598 lines 16 kB view raw
1/* MikMod sound library 2 (c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file 3 AUTHORS for complete list. 4 5 This library is free software; you can redistribute it and/or modify 6 it under the terms of the GNU Library General Public License as 7 published by the Free Software Foundation; either version 2 of 8 the License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU Library General Public License for more details. 14 15 You should have received a copy of the GNU Library General Public 16 License along with this library; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 18 02111-1307, USA. 19*/ 20 21/*============================================================================== 22 23 $Id$ 24 25 Generic MOD loader (Protracker, StarTracker, FastTracker, etc) 26 27==============================================================================*/ 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#ifdef HAVE_UNISTD_H 34#include <unistd.h> 35#endif 36 37#include <ctype.h> 38#include <stdio.h> 39#ifdef HAVE_MEMORY_H 40#include <memory.h> 41#endif 42#include <string.h> 43 44#include "mikmod_internals.h" 45 46#ifdef SUNOS 47extern int fprintf(FILE *, const char *, ...); 48#endif 49 50/*========== Module structure */ 51 52typedef struct MSAMPINFO { 53 CHAR samplename[23]; /* 22 in module, 23 in memory */ 54 UWORD length; 55 UBYTE finetune; 56 UBYTE volume; 57 UWORD reppos; 58 UWORD replen; 59} MSAMPINFO; 60 61typedef struct MODULEHEADER { 62 CHAR songname[21]; /* the songname.. 20 in module, 21 in memory */ 63 MSAMPINFO samples[31]; /* all sampleinfo */ 64 UBYTE songlength; /* number of patterns used */ 65 UBYTE magic1; /* should be 127 */ 66 UBYTE positions[128]; /* which pattern to play at pos */ 67 UBYTE magic2[4]; /* string "M.K." or "FLT4" or "FLT8" */ 68} MODULEHEADER; 69 70typedef struct MODTYPE { 71 CHAR id[5]; 72 UBYTE channels; 73 CHAR *name; 74} MODTYPE; 75 76typedef struct MODNOTE { 77 UBYTE a, b, c, d; 78} MODNOTE; 79 80/*========== Loader variables */ 81 82#define MODULEHEADERSIZE 0x438 83 84static CHAR protracker[] = "Protracker"; 85static CHAR startrekker[] = "Startrekker"; 86static CHAR fasttracker[] = "Fasttracker"; 87static CHAR octalyser[] = "Octalyser"; 88static CHAR oktalyzer[] = "Oktalyzer"; 89static CHAR taketracker[] = "TakeTracker"; 90static CHAR digitaltracker[] = "Digital Tracker MOD"; 91static CHAR orpheus[] = "Imago Orpheus (MOD format)"; 92static CHAR modsgrave[] = "Mod's Grave"; 93static CHAR unknown[] = "Unknown tracker MOD"; 94 95static MODULEHEADER *mh = NULL; 96static MODNOTE *patbuf = NULL; 97static int modtype, trekker; 98 99/*========== Loader code */ 100 101/* given the module ID, determine the number of channels and the tracker 102 description ; also alters modtype */ 103static int MOD_CheckType(UBYTE *id, UBYTE *numchn, CHAR **descr) 104{ 105 modtype = trekker = 0; 106 107 /* Protracker and variants */ 108 if ((!memcmp(id, "M.K.", 4)) || (!memcmp(id, "M!K!", 4)) || (!memcmp(id, "M&K!", 4))) { 109 *descr = protracker; 110 modtype = 0; 111 *numchn = 4; 112 return 1; 113 } 114 115 /* Star Tracker */ 116 if (((!memcmp(id, "FLT", 3)) || (!memcmp(id, "EXO", 3))) && 117 (isdigit(id[3]))) { 118 *descr = startrekker; 119 modtype = trekker = 1; 120 *numchn = id[3] - '0'; 121 if (*numchn == 4 || *numchn == 8) 122 return 1; 123#ifdef MIKMOD_DEBUG 124 else 125 fprintf(stderr, "\rUnknown FLT%d module type\n", *numchn); 126#endif 127 return 0; 128 } 129 130 /* Oktalyzer (Amiga) */ 131 if (!memcmp(id, "OKTA", 4)) { 132 *descr = oktalyzer; 133 modtype = 1; 134 *numchn = 8; 135 return 1; 136 } 137 138 /* Octalyser (Atari) */ 139 if (!memcmp(id, "CD81", 4) || !memcmp(id, "CD61", 4)) { 140 *descr = octalyser; 141 modtype = 1; 142 *numchn = id[2] - '0'; 143 return 1; 144 } 145 146 /* Fasttracker */ 147 if ((!memcmp(id + 1, "CHN", 3)) && (isdigit(id[0]))) { 148 *descr = fasttracker; 149 modtype = 1; 150 *numchn = id[0] - '0'; 151 return 1; 152 } 153 /* Fasttracker or Taketracker */ 154 if (((!memcmp(id + 2, "CH", 2)) || (!memcmp(id + 2, "CN", 2))) 155 && (isdigit(id[0])) && (isdigit(id[1]))) { 156 if (id[3] == 'H') { 157 *descr = fasttracker; 158 modtype = 2; /* this can also be Imago Orpheus */ 159 } else { 160 *descr = taketracker; 161 modtype = 1; 162 } 163 *numchn = (id[0] - '0') * 10 + (id[1] - '0'); 164 return 1; 165 } 166 /* Taketracker */ 167 if (!memcmp(id, "TDZ", 3) && (id[3] >= '1' && id[3] <= '3')) { 168 *descr = taketracker; 169 *numchn = (id[3] - '0'); 170 return 1; 171 } 172 173 /* Digital Tracker */ 174 if (!memcmp(id, "FA0", 3) && (id[3] == '4' || id[3] == '6' || id[3] == '8')) { 175 *descr = digitaltracker; 176 *numchn = (id[3] - '0'); 177 return 1; 178 } 179 180 /* Standard 4-channel MODs with unusual IDs. */ 181 if (!memcmp(id, "LARD", 4) /* judgement_day_gvine.mod */ 182 || !memcmp(id, "NSMS", 4)) { /* kingdomofpleasure.mod */ 183 *descr = unknown; 184 *numchn = 4; 185 return 1; 186 } 187 188 return 0; 189} 190 191static int MOD_Test(void) 192{ 193 UBYTE id[4], numchn; 194 CHAR *descr; 195 196 _mm_fseek(modreader, MODULEHEADERSIZE, SEEK_SET); 197 if (!_mm_read_UBYTES(id, 4, modreader)) 198 return 0; 199 200 if (MOD_CheckType(id, &numchn, &descr)) 201 return 1; 202 203 return 0; 204} 205 206static int MOD_Init(void) 207{ 208 if (!(mh = (MODULEHEADER *)MikMod_malloc(sizeof(MODULEHEADER)))) 209 return 0; 210 return 1; 211} 212 213static void MOD_Cleanup(void) 214{ 215 MikMod_free(mh); 216 MikMod_free(patbuf); 217 mh=NULL; 218 patbuf=NULL; 219} 220 221/* 222Old (amiga) noteinfo: 223 224_____byte 1_____ byte2_ _____byte 3_____ byte4_ 225/ \ / \ / \ / \ 2260000 0000-00000000 0000 0000-00000000 227 228Upper four 12 bits for Lower four Effect command. 229bits of sam- note period. bits of sam- 230ple number. ple number. 231 232*/ 233 234static UBYTE ConvertNote(MODNOTE *n, UBYTE lasteffect) 235{ 236 UBYTE instrument, effect, effdat, note; 237 UWORD period; 238 UBYTE lastnote = 0; 239 240 /* extract the various information from the 4 bytes that make up a note */ 241 instrument = (n->a & 0x10) | (n->c >> 4); 242 period = (((UWORD)n->a & 0xf) << 8) + n->b; 243 effect = n->c & 0xf; 244 effdat = n->d; 245 246 /* Convert the period to a note number */ 247 note = 0; 248 if (period) { 249 for (note = 0; note < 7 * OCTAVE; note++) 250 if (period >= npertab[note]) 251 break; 252 if (note == 7 * OCTAVE) 253 note = 0; 254 else 255 note++; 256 } 257 258 if (instrument) { 259 /* if instrument does not exist, note cut */ 260 if ((instrument > 31) || (!mh->samples[instrument - 1].length)) { 261 UniPTEffect(0xc, 0); 262 if (effect == 0xc) 263 effect = effdat = 0; 264 } else { 265 /* Protracker handling */ 266 if (!modtype) { 267 /* if we had a note, then change instrument... */ 268 if (note) 269 UniInstrument(instrument - 1); 270 /* ...otherwise, only adjust volume... */ 271 else { 272 /* ...unless an effect was specified, which forces a new 273 note to be played */ 274 if (effect || effdat) { 275 UniInstrument(instrument - 1); 276 note = lastnote; 277 } else 278 UniPTEffect(0xc, 279 mh->samples[instrument - 280 1].volume & 0x7f); 281 } 282 } else { 283 /* Fasttracker handling */ 284 UniInstrument(instrument - 1); 285 if (!note) 286 note = lastnote; 287 } 288 } 289 } 290 if (note) { 291 UniNote(note + 2 * OCTAVE - 1); 292 lastnote = note; 293 } 294 295 /* Convert pattern jump from Dec to Hex */ 296 if (effect == 0xd) 297 effdat = (((effdat & 0xf0) >> 4) * 10) + (effdat & 0xf); 298 299 /* Volume slide, up has priority */ 300 if ((effect == 0xa) && (effdat & 0xf) && (effdat & 0xf0)) 301 effdat &= 0xf0; 302 303 /* Handle ``heavy'' volumes correctly */ 304 if ((effect == 0xc) && (effdat > 0x40)) 305 effdat = 0x40; 306 307 /* An isolated 100, 200 or 300 effect should be ignored (no 308 "standalone" porta memory in mod files). However, a sequence such 309 as 1XX, 100, 100, 100 is fine. */ 310 if ((!effdat) && ((effect == 1)||(effect == 2)||(effect ==3)) && 311 (lasteffect < 0x10) && (effect != lasteffect)) 312 effect = 0; 313 314 UniPTEffect(effect, effdat); 315 if (effect == 8) 316 of.flags |= UF_PANNING; 317 318 return effect; 319} 320 321static UBYTE *ConvertTrack(MODNOTE *n, int numchn) 322{ 323 int t; 324 UBYTE lasteffect = 0x10; /* non existant effect */ 325 326 UniReset(); 327 for (t = 0; t < 64; t++) { 328 lasteffect = ConvertNote(n,lasteffect); 329 UniNewline(); 330 n += numchn; 331 } 332 return UniDup(); 333} 334 335/* Loads all patterns of a modfile and converts them into the 3 byte format. */ 336static int ML_LoadPatterns(void) 337{ 338 unsigned int t, s, tracks = 0; 339 340 if (!AllocPatterns()) 341 return 0; 342 if (!AllocTracks()) 343 return 0; 344 345 /* Allocate temporary buffer for loading and converting the patterns */ 346 if (!(patbuf = (MODNOTE *)MikMod_calloc(64U * of.numchn, sizeof(MODNOTE)))) 347 return 0; 348 349 if (trekker && of.numchn == 8) { 350 /* Startrekker module dual pattern */ 351 for (t = 0; t < of.numpat; t++) { 352 for (s = 0; s < (64U * 4); s++) { 353 patbuf[s].a = _mm_read_UBYTE(modreader); 354 patbuf[s].b = _mm_read_UBYTE(modreader); 355 patbuf[s].c = _mm_read_UBYTE(modreader); 356 patbuf[s].d = _mm_read_UBYTE(modreader); 357 } 358 for (s = 0; s < 4; s++) 359 if (!(of.tracks[tracks++] = ConvertTrack(patbuf + s, 4))) 360 return 0; 361 for (s = 0; s < (64U * 4); s++) { 362 patbuf[s].a = _mm_read_UBYTE(modreader); 363 patbuf[s].b = _mm_read_UBYTE(modreader); 364 patbuf[s].c = _mm_read_UBYTE(modreader); 365 patbuf[s].d = _mm_read_UBYTE(modreader); 366 } 367 for (s = 0; s < 4; s++) 368 if (!(of.tracks[tracks++] = ConvertTrack(patbuf + s, 4))) 369 return 0; 370 } 371 } else { 372 /* Generic module pattern */ 373 for (t = 0; t < of.numpat; t++) { 374 /* Load the pattern into the temp buffer and convert it */ 375 for (s = 0; s < (64U * of.numchn); s++) { 376 patbuf[s].a = _mm_read_UBYTE(modreader); 377 patbuf[s].b = _mm_read_UBYTE(modreader); 378 patbuf[s].c = _mm_read_UBYTE(modreader); 379 patbuf[s].d = _mm_read_UBYTE(modreader); 380 } 381 for (s = 0; s < of.numchn; s++) 382 if (!(of.tracks[tracks++] = ConvertTrack(patbuf + s, of.numchn))) 383 return 0; 384 } 385 } 386 return 1; 387} 388 389static int MOD_Load(int curious) 390{ 391 int t, scan; 392 SAMPLE *q; 393 MSAMPINFO *s; 394 CHAR *descr; 395 int maybewow = 1; 396 ULONG samplelength = 0; 397 ULONG filelength; 398 ULONG pos; 399 char adpcm[5]; 400 401 pos = _mm_ftell(modreader); 402 _mm_fseek(modreader, 0, SEEK_END); 403 filelength = _mm_ftell(modreader); 404 _mm_fseek(modreader, pos, SEEK_SET); 405 406 /* try to read module header */ 407 _mm_read_string((CHAR *)mh->songname, 20, modreader); 408 mh->songname[20] = 0; /* just in case */ 409 410 for (t = 0; t < 31; t++) { 411 s = &mh->samples[t]; 412 _mm_read_string(s->samplename, 22, modreader); 413 s->samplename[22] = 0; /* just in case */ 414 s->length = _mm_read_M_UWORD(modreader); 415 s->finetune = _mm_read_UBYTE(modreader); 416 s->volume = _mm_read_UBYTE(modreader); 417 s->reppos = _mm_read_M_UWORD(modreader); 418 s->replen = _mm_read_M_UWORD(modreader); 419 /* Mod's Grave .WOW files are converted from .669 and thus 420 do not have sample finetune or volume. */ 421 samplelength += (ULONG)s->length << 1; 422 if (s->length && (s->finetune != 0x00 || s->volume != 0x40)) 423 maybewow = 0; 424 } 425 426 mh->songlength = _mm_read_UBYTE(modreader); 427 428 /* this fixes mods which declare more than 128 positions. 429 * eg: beatwave.mod */ 430 if (mh->songlength > 128) { mh->songlength = 128; } 431 432 mh->magic1 = _mm_read_UBYTE(modreader); 433 _mm_read_UBYTES(mh->positions, 128, modreader); 434 _mm_read_UBYTES(mh->magic2, 4, modreader); 435 436 /* Mod's Grave .WOW files always use 0x00 for the "restart" byte. */ 437 if (mh->magic1 != 0x00) 438 maybewow = 0; 439 440 if (_mm_eof(modreader)) { 441 _mm_errno = MMERR_LOADING_HEADER; 442 return 0; 443 } 444 445 /* set module variables */ 446 of.initspeed = 6; 447 of.inittempo = 125; 448 if (!(MOD_CheckType(mh->magic2, &of.numchn, &descr))) { 449 _mm_errno = MMERR_NOT_A_MODULE; 450 return 0; 451 } 452 if (descr == digitaltracker) { 453 /* Digital Tracker FA0x modules add four extra bytes after the 454 * magic. These don't seem to have ever been used for their 455 * intended purpose (rows per pattern and sample bits/rate). */ 456 _mm_read_M_ULONG(modreader); 457 } 458 if (trekker && of.numchn == 8) 459 for (t = 0; t < 128; t++) 460 /* if module pretends to be FLT8, yet the order table 461 contains odd numbers, chances are it's a lying FLT4... */ 462 if (mh->positions[t] & 1) { 463 of.numchn = 4; 464 break; 465 } 466 if (trekker && of.numchn == 8) 467 for (t = 0; t < 128; t++) 468 mh->positions[t] >>= 1; 469 470 of.songname = DupStr(mh->songname, 21, 1); 471 of.numpos = mh->songlength; 472 of.reppos = 0; 473 474 /* Count the number of patterns */ 475 of.numpat = 0; 476 for (t = 0; t < of.numpos; t++) 477 if (mh->positions[t] > of.numpat) 478 of.numpat = mh->positions[t]; 479 480 /* since some old modules embed extra patterns, we have to check the 481 whole list to get the samples' file offsets right - however we can find 482 garbage here, so check carefully */ 483 scan = 1; 484 for (t = of.numpos; t < 128; t++) 485 if (mh->positions[t] >= 0x80) 486 scan = 0; 487 if (scan) 488 for (t = of.numpos; t < 128; t++) { 489 if (mh->positions[t] > of.numpat) 490 of.numpat = mh->positions[t]; 491 if ((curious) && (mh->positions[t])) 492 of.numpos = t + 1; 493 } 494 of.numpat++; 495 496 /* Mod's Grave .WOW files have an M.K. signature but they're actually 8 channel. 497 The only way to distinguish them from a 4-channel M.K. file is to check the 498 length of the .MOD against the expected length of a .WOW file with the same 499 number of patterns as this file. To make things harder, Mod's Grave occasionally 500 adds an extra byte to .WOW files and sometimes .MOD authors pad their files. 501 Prior checks for WOW behavior should help eliminate false positives here. 502 503 Also note the length check relies on counting samples with a length word=1 to work. */ 504 if (modtype == 0 && maybewow == 1) { 505 ULONG wowlength = MODULEHEADERSIZE + 4 + samplelength + of.numpat * (64 * 4 * 8); 506 if ((filelength & ~1) == wowlength) { 507 modtype = 1; 508 descr = modsgrave; 509 of.numchn = 8; 510 } 511 } 512 513 of.numtrk = of.numpat * of.numchn; 514 515 if (!AllocPositions(of.numpos)) 516 return 0; 517 for (t = 0; t < of.numpos; t++) 518 of.positions[t] = mh->positions[t]; 519 520 if (!ML_LoadPatterns()) 521 return 0; 522 523 /* Finally, init the sampleinfo structures */ 524 of.numins = of.numsmp = 31; 525 if (!AllocSamples()) 526 return 0; 527 s = mh->samples; 528 q = of.samples; 529 pos = _mm_ftell(modreader); 530 for (t = 0; t < of.numins; t++) { 531 /* convert the samplename */ 532 q->samplename = DupStr(s->samplename, 23, 1); 533 /* init the sampleinfo variables and convert the size pointers */ 534 q->speed = finetune[s->finetune & 0xf]; 535 q->volume = s->volume & 0x7f; 536 q->loopstart = (ULONG)s->reppos << 1; 537 q->loopend = q->loopstart + ((ULONG)s->replen << 1); 538 q->length = (ULONG)s->length << 1; 539 q->flags = SF_SIGNED; 540 /* Imago Orpheus creates MODs with 16 bit samples, check */ 541 if ((modtype == 2) && (s->volume & 0x80)) { 542 q->flags |= SF_16BITS; 543 descr = orpheus; 544 } 545 if (s->replen > 2) 546 q->flags |= SF_LOOP; 547 548 q->seekpos = pos; 549 /* Test for MODPlugin ADPCM. These are indicated by "ADPCM" 550 * embedded at the start of each sample's data. :( */ 551 memset(adpcm, 0, sizeof(adpcm)); 552 _mm_read_UBYTES(adpcm, 5, modreader); 553 if (!memcmp(adpcm, "ADPCM", 5)) { 554 q->flags |= SF_ADPCM4; 555 q->seekpos += 5; 556 /* Stored half-length, plus a 16 byte table. */ 557 pos += s->length + 16 + 5; 558 _mm_fseek(modreader, s->length + 16, SEEK_CUR); 559 } else { 560 pos += q->length; 561 _mm_fseek(modreader, q->length - 5, SEEK_CUR); 562 } 563 564 s++; 565 q++; 566 } 567 568 of.modtype = MikMod_strdup(descr); 569 570 return 1; 571} 572 573static CHAR *MOD_LoadTitle(void) 574{ 575 CHAR s[21]; 576 577 _mm_fseek(modreader, 0, SEEK_SET); 578 if (!_mm_read_UBYTES(s, 20, modreader)) 579 return NULL; 580 s[20] = 0; /* just in case */ 581 582 return (DupStr(s, 21, 1)); 583} 584 585/*========== Loader information */ 586 587MIKMODAPI MLOADER load_mod = { 588 NULL, 589 "Standard module", 590 "MOD (31 instruments)", 591 MOD_Init, 592 MOD_Test, 593 MOD_Load, 594 MOD_Cleanup, 595 MOD_LoadTitle 596}; 597 598/* ex:set ts=4: */