📄 mloader.c
字号:
s = musiclist; speed = 0; while (s) { if ((s->sample->length) && (RealSpeed (s) > speed)) { speed = RealSpeed (s); c2smp = s; } s = s->next; } if (c2smp) SL_HalveSample (c2smp); } }#endif /* Samples dithered, now load them ! */ s = musiclist; while (s) { /* sample has to be loaded ? -> increase number of samples, allocate memory and load sample. */ if (s->sample->length) { if (s->sample->seekpos) _mm_fseek (s->reader, s->sample->seekpos, SEEK_SET); /* Call the sample load routine of the driver module. It has to return a pointer to sample dat). */ if (SL_Init (s)) { s->sample->data = SL_Load (s); SL_Exit (s); } s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt; if (s->sample->data == NULL) { FreeSampleList (musiclist); return 1; } } s = s->next; } FreeSampleList (musiclist); return 0;}void SL_Sample16to8 (SAMPLOAD * s){ s->outfmt &= ~SF_16BITS; s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;}void SL_Sample8to16 (SAMPLOAD * s){ s->outfmt |= SF_16BITS; s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;}void SL_SampleSigned (SAMPLOAD * s){ s->outfmt |= SF_SIGNED; s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;}void SL_SampleUnsigned (SAMPLOAD * s){ s->outfmt &= ~SF_SIGNED; s->sample->flags = (s->sample->flags & ~SF_FORMATMASK) | s->outfmt;}#ifdef MAX_SAMPLESPACEvoid SL_HalveSample (SAMPLOAD * s){ s->scalefactor = 2; /* this is a divisor */ s->sample->divfactor = 1; /* this is a shift count */ s->sample->length = s->length / s->scalefactor; s->sample->loopstart = s->loopstart / s->scalefactor; s->sample->loopend = s->loopend / s->scalefactor;}#endifCHAR *ML_InfoLoader (void){ int len = 0; MLOADER *l; CHAR *list = NULL; /* compute size of buffer */ for (l = firstloader; l; l = l->next) len += 1 + (l->next ? 1 : 0) + strlen (l->version); if (len) if ((list = _mm_malloc (len * sizeof (CHAR)))) { list[0] = 0; /* list all registered module loders */ for (l = firstloader; l; l = l->next) sprintf (list, (l->next) ? "%s%s\n" : "%s%s", list, l->version); } return list;}BOOL ReadComment (UWORD len){ if (len) { int i; if (!(of.comment = (CHAR *) _mm_malloc (len + 1))) return 0; _mm_read_UBYTES (of.comment, len, modreader); /* translate IT linefeeds */ for (i = 0; i < len; i++) if (of.comment[i] == '\r') of.comment[i] = '\n'; of.comment[len] = 0; /* just in case */ } if (!of.comment[0]) { free (of.comment); of.comment = NULL; } return 1;}BOOL ReadLinedComment (UWORD lines, UWORD linelen){ CHAR *tempcomment, *line, *storage; UWORD total = 0, t, len = lines * linelen; int i; if (lines) { if (!(tempcomment = (CHAR *) _mm_malloc (len + 1))) return 0; if (!(storage = (CHAR *) _mm_malloc (linelen + 1))) { free (tempcomment); return 0; } _mm_read_UBYTES (tempcomment, len, modreader); /* compute message length */ for (line = tempcomment, total = t = 0; t < lines; t++, line += linelen) { for (i = linelen; (i >= 0) && (line[i] == ' '); i--) line[i] = 0; for (i = 0; i < linelen; i++) if (!line[i]) break; total += 1 + i; } if (total > lines) { if (!(of.comment = (CHAR *) _mm_malloc (total + 1))) { free (storage); free (tempcomment); return 0; } /* convert message */ for (line = tempcomment, t = 0; t < lines; t++, line += linelen) { for (i = 0; i < linelen; i++) if (!(storage[i] = line[i])) break; storage[i] = 0; /* if (i==linelen) */ strcat (of.comment, storage); strcat (of.comment, "\r"); } free (storage); free (tempcomment); } } return 1;}BOOL AllocPositions (int total){ if (!total) { _mm_errno = MMERR_NOT_A_MODULE; return 0; } if (!(of.positions = _mm_calloc (total, sizeof (UWORD)))) return 0; return 1;}BOOL AllocPatterns (void){ int s, t, tracks = 0; if ((!of.numpat) || (!of.numchn)) { _mm_errno = MMERR_NOT_A_MODULE; return 0; } /* Allocate track sequencing array */ if (!(of.patterns = (UWORD *) _mm_calloc ((ULONG) (of.numpat + 1) * of.numchn, sizeof (UWORD)))) return 0; if (!(of.pattrows = (UWORD *) _mm_calloc (of.numpat + 1, sizeof (UWORD)))) return 0; for (t = 0; t <= of.numpat; t++) { of.pattrows[t] = 64; for (s = 0; s < of.numchn; s++) of.patterns[(t * of.numchn) + s] = tracks++; } return 1;}BOOL AllocTracks (void){ if (!of.numtrk) { _mm_errno = MMERR_NOT_A_MODULE; return 0; } if (!(of.tracks = (UBYTE **) _mm_calloc (of.numtrk, sizeof (UBYTE *)))) return 0; return 1;}BOOL AllocInstruments (void){ int t, n; if (!of.numins) { _mm_errno = MMERR_NOT_A_MODULE; return 0; } if (!(of.instruments = (INSTRUMENT *) _mm_calloc (of.numins, sizeof (INSTRUMENT)))) return 0; for (t = 0; t < of.numins; t++) { for (n = 0; n < INSTNOTES; n++) { /* Init note / sample lookup table */ of.instruments[t].samplenote[n] = n; of.instruments[t].samplenumber[n] = t; } of.instruments[t].globvol = 64; } return 1;}BOOL AllocSamples (void){ UWORD u; if (!of.numsmp) { _mm_errno = MMERR_NOT_A_MODULE; return 0; } if (!(of.samples = (SAMPLE *) _mm_calloc (of.numsmp, sizeof (SAMPLE)))) return 0; for (u = 0; u < of.numsmp; u++) { of.samples[u].panning = 128; /* center */ of.samples[u].data = NULL; of.samples[u].globvol = 64; of.samples[u].volume = 64; } return 1;}static BOOL ML_LoadSamples (void){ SAMPLE *s; int u; for (u = of.numsmp, s = of.samples; u; u--, s++) if (s->length) SL_RegisterSample (s, modreader); return 1;}/* Creates a CSTR out of a character buffer of 'len' bytes, but strips any terminating non-printing characters like 0, spaces etc. */CHAR *DupStr (CHAR * s, UWORD len, BOOL strict){ UWORD t; CHAR *d = NULL; /* Scan for last printing char in buffer [includes high ascii up to 254] */ while (len) { if (s[len - 1] > 0x20) break; len--; } /* Scan forward for possible NULL character */ if (strict) { for (t = 0; t < len; t++) if (!s[t]) break; if (t < len) len = t; } /* When the buffer wasn't completely empty, allocate a cstring and copy the buffer into that string, except for any control-chars */ if ((d = (CHAR *) _mm_malloc (sizeof (CHAR) * (len + 1)))) { for (t = 0; t < len; t++) d[t] = (s[t] < 32) ? '.' : s[t]; d[len] = 0; } return d;}static void ML_XFreeSample (SAMPLE * s){ if (s->data) free (s->data); if (s->samplename) free (s->samplename);}static void ML_XFreeInstrument (INSTRUMENT * i){ if (i->insname) free (i->insname);}void ML_Free (MODULE * mf){ UWORD t; if (!mf) return; if (mf->songname) free (mf->songname); if (mf->comment) free (mf->comment); if (mf->modtype) free (mf->modtype); if (mf->positions) free (mf->positions); if (mf->patterns) free (mf->patterns); if (mf->pattrows) free (mf->pattrows); if (mf->tracks) { for (t = 0; t < mf->numtrk; t++) if (mf->tracks[t]) free (mf->tracks[t]); free (mf->tracks); } if (mf->instruments) { for (t = 0; t < mf->numins; t++) ML_XFreeInstrument (&mf->instruments[t]); free (mf->instruments); } if (mf->samples) { for (t = 0; t < mf->numsmp; t++) if (mf->samples[t].length) ML_XFreeSample (&mf->samples[t]); free (mf->samples); } memset (mf, 0, sizeof (MODULE)); if (mf != &of) free (mf);}static MODULE *ML_AllocUniMod (void){ return (_mm_malloc (sizeof (MODULE)));}CHAR *ML_LoadTitle (URL reader){ MLOADER *l; modreader = reader; _mm_errno = 0; /* Try to find a loader that recognizes the module */ for (l = firstloader; l; l = l->next) { _mm_rewind (modreader); if (l->Test ()) break; } if (!l) { _mm_errno = MMERR_NOT_A_MODULE; return NULL; } return l->LoadTitle ();}/* Check if it is a module given a reader */BOOL ML_Test (URL reader){ MLOADER *l; modreader = reader; _mm_errno = 0; /* Try to find a loader that recognizes the module */ for (l = firstloader; l; l = l->next) { _mm_rewind (modreader); if (l->Test ()) return 1; } return 0;}/* Loads a module given a reader */MODULE *ML_Load (URL reader, int maxchan, BOOL curious){ int t; MLOADER *l; BOOL ok; MODULE *mf; modreader = reader; _mm_errno = 0; /* Try to find a loader that recognizes the module */ for (l = firstloader; l; l = l->next) { _mm_rewind (modreader); if (l->Test ()) break; } if (!l) { _mm_errno = MMERR_NOT_A_MODULE; _mm_rewind (modreader); return NULL; } /* init unitrk routines */ if (!UniInit ()) { _mm_rewind (modreader); return NULL; } /* load the song using the song's loader variable */ memset (&of, 0, sizeof (MODULE)); of.initvolume = 128; /* init panning array */ for (t = 0; t < 64; t++) of.panning[t] = ((t + 1) & 2) ? 255 : 0; for (t = 0; t < 64; t++) of.chanvol[t] = 64; /* init module loader and load the header / patterns */ if (l->Init ()) { _mm_rewind (modreader); ok = l->Load (curious); } else ok = 0; /* free loader and unitrk allocations */ l->Cleanup (); UniCleanup (); if (!ok) { ML_Free (&of); _mm_rewind (modreader); return NULL; } if (!ML_LoadSamples ()) { ML_Free (&of); _mm_rewind (modreader); return NULL; } if (!(mf = ML_AllocUniMod ())) { ML_Free (&of); return NULL; } /* Copy the static MODULE contents into the dynamic MODULE struct. */ memcpy (mf, &of, sizeof (MODULE)); if (maxchan > 0) { if (!(mf->flags & UF_NNA) && (mf->numchn < maxchan)) maxchan = mf->numchn; else if ((mf->numvoices) && (mf->numvoices < maxchan)) maxchan = mf->numvoices; if (maxchan < mf->numchn) mf->flags |= UF_NNA; } if (SL_LoadSamples ()) { ML_Free (mf); return NULL; } return mf;}void ML_RegisterAllLoaders (void){ MLOADER *last = NULL; if (firstloader) return;#define LOADER(fmt) { \ extern MLOADER fmt; \ if (!last) \ firstloader = &fmt; \ else \ last->next = &fmt; \ last = &fmt; \} /* Most likely first */ LOADER (load_xm); LOADER (load_s3m); LOADER (load_mod); LOADER (load_it); /* Then the others in alphabetic order */ LOADER (load_669); LOADER (load_amf); LOADER (load_dsm); LOADER (load_far); LOADER (load_gdm); LOADER (load_imf); LOADER (load_med); LOADER (load_mtm); LOADER (load_okt); LOADER (load_stm); LOADER (load_stx); LOADER (load_ult); LOADER (load_uni); /* must be last! */ LOADER (load_m15);}/* ex:set ts=4: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -