static void *combining_load_signal(DUH *duh, DUMBFILE *file) { COMBINING_SIGNAL *signal; int n_signals; (void)duh; n_signals = dumbfile_getc(file); /* No point in combining only one signal! */ if (dumbfile_error(file) || n_signals <= 1) return NULL; signal = malloc(sizeof(*signal) + n_signals * sizeof(*signal->sig)); if (!signal) return NULL; signal->n_signals = n_signals; { int n; for (n = 0; n < signal->n_signals; n++) { signal->sig[n] = dumbfile_igetl(file); if (dumbfile_error(file)) { free(signal); return NULL; } } } return signal; }
static DUH_SIGNAL *read_signal(DUH *duh, DUMBFILE *f) { DUH_SIGNAL *signal; long type; signal = malloc(sizeof(*signal)); if (!signal) return NULL; type = dumbfile_mgetl(f); if (dumbfile_error(f)) { free(signal); return NULL; } signal->desc = _dumb_get_sigtype_desc(type); if (!signal->desc) { free(signal); return NULL; } if (signal->desc->load_sigdata) { signal->sigdata = (*signal->desc->load_sigdata)(duh, f); if (!signal->sigdata) { free(signal); return NULL; } } else signal->sigdata = NULL; return signal; }
static int it_asy_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f) { int finetune, key_offset; /** 21 22 Chars Sample 1 name. If the name is not a full 22 chars in length, it will be null terminated. If the sample name begins with a '#' character (ASCII $23 (35)) then this is assumed not to be an instrument name, and is probably a message. */ dumbfile_getnc((char *)sample->name, 22, f); sample->name[22] = 0; sample->filename[0] = 0; /** Each finetune step changes the note 1/8th of a semitone. */ finetune = (signed char)(dumbfile_getc(f) << 4) >> 4; /* signed nibble */ sample->default_volume = dumbfile_getc(f); // Should we be setting global_volume to this instead? sample->global_volume = 64; if (sample->default_volume > 64) sample->default_volume = 64; key_offset = (signed char)dumbfile_getc(f); /* base key offset */ sample->length = dumbfile_igetl(f); sample->loop_start = dumbfile_igetl(f); sample->loop_end = sample->loop_start + dumbfile_igetl(f); if (sample->length <= 0) { sample->flags = 0; return 0; } sample->flags = IT_SAMPLE_EXISTS; sample->default_pan = 0; sample->C5_speed = (int)(AMIGA_CLOCK / 214.0 * pow(DUMB_SEMITONE_BASE, key_offset)); //( long )( 16726.0 * pow( // DUMB_PITCH_BASE, finetune //* 32 ) ); sample->finetune = finetune * 32; // the above line might be wrong if ((sample->loop_end - sample->loop_start > 2) && (sample->loop_end <= sample->length)) sample->flags |= IT_SAMPLE_LOOP; sample->vibrato_speed = 0; sample->vibrato_depth = 0; sample->vibrato_rate = 0; sample->vibrato_waveform = 0; // do we have to set _all_ these? sample->max_resampling_quality = -1; return dumbfile_error(f); }
static int it_ptm_read_sample_header(IT_SAMPLE *sample, long *offset, DUMBFILE *f) { int flags; flags = dumbfile_getc(f); dumbfile_getnc((char *)sample->filename, 12, f); sample->filename[12] = 0; sample->default_volume = dumbfile_getc(f); sample->C5_speed = dumbfile_igetw(f) << 1; dumbfile_skip(f, 2); /* segment */ *offset = dumbfile_igetl(f); sample->length = dumbfile_igetl(f); sample->loop_start = dumbfile_igetl(f); sample->loop_end = dumbfile_igetl(f); /* GUSBegin, GUSLStart, GUSLEnd, GUSLoop, reserverd */ dumbfile_skip(f, 4+4+4+1+1); dumbfile_getnc((char *)sample->name, 28, f); sample->name[28] = 0; /* if (dumbfile_mgetl(f) != DUMB_ID('P','T','M','S')) return -1; */ /* BLAH! Shit likes to have broken or missing sample IDs */ dumbfile_skip(f, 4); if ((flags & 3) == 0) { /* Looks like no sample */ sample->flags &= ~IT_SAMPLE_EXISTS; return dumbfile_error(f); } sample->global_volume = 64; sample->flags = IT_SAMPLE_EXISTS; if (flags & 4) sample->flags |= IT_SAMPLE_LOOP; if (flags & 8) sample->flags |= IT_SAMPLE_PINGPONG_LOOP; if (flags & 16) { sample->flags |= IT_SAMPLE_16BIT; sample->length >>= 1; sample->loop_start >>= 1; sample->loop_end >>= 1; }
/* I (entheh) have two XM files saved by a very naughty program. After a * 16-bit sample, it saved a rogue byte. The length of the sample was indeed * an odd number, incremented to include the rogue byte. * * In this function we are converting sample lengths and loop points so they * are measured in samples. This means we forget about the extra bytes, and * they don't get skipped. So we fail trying to read the next instrument. * * To get around this, this function returns the number of rogue bytes that * won't be accounted for by reading sample->length samples. It returns a * negative number on failure. */ static int it_xm_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f) { int type; int relative_note_number; /* relative to C4 */ int finetune; int roguebytes; int roguebytesmask; int reserved; sample->length = dumbfile_igetl(f); sample->loop_start = dumbfile_igetl(f); sample->loop_end = sample->loop_start + dumbfile_igetl(f); sample->global_volume = 64; sample->default_volume = dumbfile_getc(f); finetune = (signed char)dumbfile_getc(f); /* -128..127 <=> -1 semitone .. +127/128 of a semitone */ type = dumbfile_getc(f); sample->default_pan = dumbfile_getc(f); /* 0-255 */ relative_note_number = (signed char)dumbfile_getc(f); reserved = dumbfile_getc(f); dumbfile_getnc((char *)sample->name, 22, f); sample->name[22] = 0; trim_whitespace((char *)sample->name, 22); sample->filename[0] = 0; if (dumbfile_error(f)) return -1; sample->C5_speed = (long)(16726.0*pow(DUMB_SEMITONE_BASE, relative_note_number) /**pow(DUMB_PITCH_BASE, )*/ ); sample->finetune = finetune*2; sample->flags = IT_SAMPLE_EXISTS; if (reserved == 0xAD && (!(type & (XM_SAMPLE_16BIT | XM_SAMPLE_STEREO)))) { /* F U Olivier Lapicque */ roguebytes = 4; roguebytesmask = 4 << 2; } else { roguebytes = (int)sample->length; roguebytesmask = 3; } if (type & XM_SAMPLE_16BIT) { sample->flags |= IT_SAMPLE_16BIT; sample->length >>= 1; sample->loop_start >>= 1; sample->loop_end >>= 1; } else
/* read_duh(): reads a DUH from an already open DUMBFILE, and returns its * pointer, or null on error. The file is not closed. */ DUH *read_duh(DUMBFILE *f) { DUH *duh; int i; if (dumbfile_mgetl(f) != DUH_SIGNATURE) return NULL; duh = malloc(sizeof(*duh)); if (!duh) return NULL; duh->length = dumbfile_igetl(f); if (dumbfile_error(f) || duh->length <= 0) { free(duh); return NULL; } duh->n_signals = (int)dumbfile_igetl(f); if (dumbfile_error(f) || duh->n_signals <= 0) { free(duh); return NULL; } duh->signal = malloc(sizeof(*duh->signal) * duh->n_signals); if (!duh->signal) { free(duh); return NULL; } for (i = 0; i < duh->n_signals; i++) duh->signal[i] = NULL; for (i = 0; i < duh->n_signals; i++) { if (!(duh->signal[i] = read_signal(duh, f))) { unload_duh(duh); return NULL; } } return duh; }
static int it_s3m_read_sample_data(IT_SAMPLE *sample, int ffi, DUMBFILE *f) { long n; long datasize = sample->length; if (sample->flags & IT_SAMPLE_STEREO) datasize <<= 1; sample->data = malloc(datasize * (sample->flags & IT_SAMPLE_16BIT ? 2 : 1)); if (!sample->data) return -1; if (sample->flags & IT_SAMPLE_STEREO) { if (sample->flags & IT_SAMPLE_16BIT) { for (n = 0; n < datasize; n += 2) ((short *)sample->data)[n] = dumbfile_igetw(f); for (n = 1; n < datasize; n += 2) ((short *)sample->data)[n] = dumbfile_igetw(f); } else { for (n = 0; n < datasize; n += 2) ((signed char *)sample->data)[n] = dumbfile_getc(f); for (n = 1; n < datasize; n += 2) ((signed char *)sample->data)[n] = dumbfile_getc(f); } } else if (sample->flags & IT_SAMPLE_16BIT) for (n = 0; n < sample->length; n++) ((short *)sample->data)[n] = dumbfile_igetw(f); else for (n = 0; n < sample->length; n++) ((signed char *)sample->data)[n] = dumbfile_getc(f); if (dumbfile_error(f)) return -1; if (ffi != 1) { /* Convert to signed. */ if (sample->flags & IT_SAMPLE_16BIT) for (n = 0; n < datasize; n++) ((short *)sample->data)[n] ^= 0x8000; else for (n = 0; n < datasize; n++) ((signed char *)sample->data)[n] ^= 0x80; } return 0; }
/* I (entheh) have two XM files saved by a very naughty program. After a * 16-bit sample, it saved a rogue byte. The length of the sample was indeed * an odd number, incremented to include the rogue byte. * * In this function we are converting sample lengths and loop points so they * are measured in samples. This means we forget about the extra bytes, and * they don't get skipped. So we fail trying to read the next instrument. * * To get around this, this function returns the number of rogue bytes that * won't be accounted for by reading sample->length samples. It returns a * negative number on failure. */ static int it_xm_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f) { int type; int relative_note_number; /* relative to C4 */ int finetune; int roguebytes; int roguebytesmask; sample->length = dumbfile_igetl(f); sample->loop_start = dumbfile_igetl(f); sample->loop_end = sample->loop_start + dumbfile_igetl(f); sample->global_volume = 64; sample->default_volume = dumbfile_getc(f); finetune = (signed char)dumbfile_getc(f); /* -128..127 <=> -1 semitone .. +127/128 of a semitone */ type = dumbfile_getc(f); sample->default_pan = dumbfile_getc(f); /* 0-255 */ relative_note_number = (signed char)dumbfile_getc(f); dumbfile_skip(f, 1); /* reserved */ dumbfile_getnc(sample->name, 22, f); sample->name[22] = 0; sample->filename[0] = 0; if (dumbfile_error(f)) return -1; sample->C5_speed = (long)(16726.0*pow(DUMB_SEMITONE_BASE, relative_note_number)*pow(DUMB_PITCH_BASE, finetune*2)); sample->flags = IT_SAMPLE_EXISTS; roguebytes = (int)sample->length; roguebytesmask = 3; if (type & XM_SAMPLE_16BIT) { sample->flags |= IT_SAMPLE_16BIT; sample->length >>= 1; sample->loop_start >>= 1; sample->loop_end >>= 1; } else
static void *sequence_load_signal(DUH *duh, DUMBFILE *file) { long size; unsigned char *signal; (void)duh; size = dumbfile_igetl(file); if (dumbfile_error(file) || size <= 0) return NULL; signal = malloc(size); if (!signal) return NULL; if (dumbfile_getnc((char *)signal, size, file) < size) { free(signal); return NULL; } return signal; }
static int it_asy_read_sample_data(IT_SAMPLE *sample, DUMBFILE *f) { long truncated_size; /* let's get rid of the sample data coming after the end of the loop */ if ((sample->flags & IT_SAMPLE_LOOP) && sample->loop_end < sample->length) { truncated_size = sample->length - sample->loop_end; sample->length = sample->loop_end; } else { truncated_size = 0; } sample->data = malloc(sample->length); if (!sample->data) return -1; if (sample->length) dumbfile_getnc(sample->data, sample->length, f); dumbfile_skip(f, truncated_size); return dumbfile_error(f); }
static int it_xm_read_instrument(IT_INSTRUMENT *instrument, XM_INSTRUMENT_EXTRA *extra, DUMBFILE *f) { unsigned long size, bytes_read; unsigned short vol_points[24]; unsigned short pan_points[24]; int i, type; const unsigned long max_size = 4 + 22 + 1 + 2 + 4 + 96 + 48 + 48 + 1 * 14 + 2 + 2; unsigned long skip_end = 0; /* Header size. Tends to be more than the actual size of the structure. * So unread bytes must be skipped before reading the first sample * header. */ if ( limit_xm_resize( f, 4 ) < 0 ) return -1; size = dumbfile_igetl(f); if ( size == 0 ) size = max_size; else if ( size > max_size ) { skip_end = size - max_size; size = max_size; } if ( limit_xm_resize( f, size - 4 ) < 0 ) return -1; dumbfile_getnc((char *)instrument->name, 22, f); instrument->name[22] = 0; trim_whitespace((char *)instrument->name, 22); instrument->filename[0] = 0; dumbfile_skip(f, 1); /* Instrument type. Should be 0, but seems random. */ extra->n_samples = dumbfile_igetw(f); if (dumbfile_error(f) || (unsigned int)extra->n_samples > XM_MAX_SAMPLES_PER_INSTRUMENT) return -1; bytes_read = 4 + 22 + 1 + 2; if (extra->n_samples) { /* sample header size */ /*i = dumbfile_igetl(f); if (!i || i > 0x28) i = 0x28;*/ dumbfile_skip(f, 4); i = 0x28; extra->sample_header_size = i; /* sample map */ for (i = 0; i < 96; i++) { instrument->map_sample[i] = dumbfile_getc(f) + 1; instrument->map_note[i] = i; } if (dumbfile_error(f)) return 1; /* volume/panning envelopes */ for (i = 0; i < 24; i++) vol_points[i] = dumbfile_igetw(f); for (i = 0; i < 24; i++) pan_points[i] = dumbfile_igetw(f); instrument->volume_envelope.n_nodes = dumbfile_getc(f); instrument->pan_envelope.n_nodes = dumbfile_getc(f); if (dumbfile_error(f)) return -1; instrument->volume_envelope.sus_loop_start = dumbfile_getc(f); instrument->volume_envelope.loop_start = dumbfile_getc(f); instrument->volume_envelope.loop_end = dumbfile_getc(f); instrument->pan_envelope.sus_loop_start = dumbfile_getc(f); instrument->pan_envelope.loop_start = dumbfile_getc(f); instrument->pan_envelope.loop_end = dumbfile_getc(f); /* The envelope handler for XM files won't use sus_loop_end. */ type = dumbfile_getc(f); instrument->volume_envelope.flags = 0; if ((type & XM_ENVELOPE_ON) && instrument->volume_envelope.n_nodes) instrument->volume_envelope.flags |= IT_ENVELOPE_ON; if (type & XM_ENVELOPE_LOOP) instrument->volume_envelope.flags |= IT_ENVELOPE_LOOP_ON; #if 1 if (type & XM_ENVELOPE_SUSTAIN) instrument->volume_envelope.flags |= IT_ENVELOPE_SUSTAIN_LOOP; #else // This is now handled in itrender.c /* let's avoid fading out when reaching the last envelope node */ if (!(type & XM_ENVELOPE_LOOP)) { instrument->volume_envelope.loop_start = instrument->volume_envelope.n_nodes-1; instrument->volume_envelope.loop_end = instrument->volume_envelope.n_nodes-1; } instrument->volume_envelope.flags |= IT_ENVELOPE_LOOP_ON; #endif type = dumbfile_getc(f); instrument->pan_envelope.flags = 0; if ((type & XM_ENVELOPE_ON) && instrument->pan_envelope.n_nodes) instrument->pan_envelope.flags |= IT_ENVELOPE_ON; if (type & XM_ENVELOPE_LOOP) instrument->pan_envelope.flags |= IT_ENVELOPE_LOOP_ON; // should this be here? if (type & XM_ENVELOPE_SUSTAIN) instrument->pan_envelope.flags |= IT_ENVELOPE_SUSTAIN_LOOP; if (it_xm_make_envelope(&instrument->volume_envelope, vol_points, 0) != 0) { TRACE("XM error: volume envelope\n"); if (instrument->volume_envelope.flags & IT_ENVELOPE_ON) return -1; } if (it_xm_make_envelope(&instrument->pan_envelope, pan_points, -32) != 0) { TRACE("XM error: pan envelope\n"); if (instrument->pan_envelope.flags & IT_ENVELOPE_ON) return -1; } instrument->pitch_envelope.flags = 0; extra->vibrato_type = dumbfile_getc(f); extra->vibrato_sweep = dumbfile_getc(f); extra->vibrato_depth = dumbfile_getc(f); extra->vibrato_speed = dumbfile_getc(f); if (dumbfile_error(f) || extra->vibrato_type > 4) // XXX return -1; /** WARNING: lossy approximation */ instrument->fadeout = (dumbfile_igetw(f)*128 + 64)/0xFFF; dumbfile_skip(f, 2); /* reserved */ bytes_read += 4 + 96 + 48 + 48 + 14*1 + 2 + 2; } else for (i = 0; i < 96; i++) instrument->map_sample[i] = 0; if (size > bytes_read && dumbfile_skip(f, size - bytes_read)) return -1; if (skip_end && limit_xm_skip_end(f, skip_end)) return -1; instrument->new_note_action = NNA_NOTE_CUT; instrument->dup_check_type = DCT_OFF; instrument->dup_check_action = DCA_NOTE_CUT; instrument->pp_separation = 0; instrument->pp_centre = 60; /* C-5 */ instrument->global_volume = 128; instrument->default_pan = 32; instrument->random_volume = 0; instrument->random_pan = 0; instrument->filter_cutoff = 0; instrument->filter_resonance = 0; return 0; }
static int it_xm_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, int n_channels, unsigned char *buffer, int version) { int size; int pos; int channel; int row; int effect, effectvalue; IT_ENTRY *entry; /* pattern header size */ if (dumbfile_igetl(f) != ( version == 0x0102 ? 0x08 : 0x09 ) ) { TRACE("XM error: unexpected pattern header size\n"); return -1; } /* pattern data packing type */ if (dumbfile_getc(f) != 0) { TRACE("XM error: unexpected pattern packing type\n"); return -1; } if ( version == 0x0102 ) pattern->n_rows = dumbfile_getc(f) + 1; else pattern->n_rows = dumbfile_igetw(f); /* 1..256 */ size = dumbfile_igetw(f); pattern->n_entries = 0; if (dumbfile_error(f)) return -1; if (size == 0) return 0; if (size > 1280 * n_channels) { TRACE("XM error: pattern data size > %d bytes\n", 1280 * n_channels); return -1; } if (dumbfile_getnc((char *)buffer, size, f) < size) return -1; /* compute number of entries */ pattern->n_entries = 0; pos = channel = row = 0; while (pos < size) { if (!(buffer[pos] & XM_ENTRY_PACKED) || (buffer[pos] & 31)) pattern->n_entries++; channel++; if (channel >= n_channels) { channel = 0; row++; pattern->n_entries++; } if (buffer[pos] & XM_ENTRY_PACKED) { static const char offset[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5 }; pos += 1 + offset[buffer[pos] & 31]; } else { pos += 5; } } if (row > pattern->n_rows) { TRACE("XM error: wrong number of rows in pattern data\n"); return -1; } /* Whoops, looks like some modules may be short, a few channels, maybe even rows... */ while (row < pattern->n_rows) { pattern->n_entries++; row++; } pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry)); if (!pattern->entry) return -1; /* read the entries */ entry = pattern->entry; pos = channel = row = 0; while (pos < size) { unsigned char mask; if (buffer[pos] & XM_ENTRY_PACKED) mask = buffer[pos++] & 31; else mask = 31; if (mask) { ASSERT(entry < pattern->entry + pattern->n_entries); entry->channel = channel; entry->mask = 0; if (mask & XM_ENTRY_NOTE) { int note = buffer[pos++]; /* 1-96 <=> C0-B7 */ entry->note = (note == XM_NOTE_OFF) ? (IT_NOTE_OFF) : (note-1); entry->mask |= IT_ENTRY_NOTE; } if (mask & XM_ENTRY_INSTRUMENT) { entry->instrument = buffer[pos++]; /* 1-128 */ entry->mask |= IT_ENTRY_INSTRUMENT; } if (mask & XM_ENTRY_VOLUME) it_xm_convert_volume(buffer[pos++], entry); effect = effectvalue = 0; if (mask & XM_ENTRY_EFFECT) effect = buffer[pos++]; if (mask & XM_ENTRY_EFFECTVALUE) effectvalue = buffer[pos++]; _dumb_it_xm_convert_effect(effect, effectvalue, entry, 0); entry++; } channel++; if (channel >= n_channels) { channel = 0; row++; IT_SET_END_ROW(entry); entry++; } } while (row < pattern->n_rows) { row++; IT_SET_END_ROW(entry); entry++; } return 0; }
static int it_riff_dsmf_process_pattern( IT_PATTERN * pattern, DUMBFILE * f, int len ) { int length, row; unsigned flags; long start, end; int p, q, r; IT_ENTRY * entry; length = dumbfile_igetw( f ); if ( length > len ) return -1; len = length - 2; pattern->n_rows = 64; pattern->n_entries = 64; row = 0; start = dumbfile_pos( f ); end = start + len; while ( (row < 64) && !dumbfile_error( f ) && (dumbfile_pos( f ) < end) ) { p = dumbfile_getc( f ); if ( ! p ) { ++ row; continue; } flags = p & 0xF0; if (flags) { ++ pattern->n_entries; if (flags & 0x80) dumbfile_skip( f, 1 ); if (flags & 0x40) dumbfile_skip( f, 1 ); if (flags & 0x20) dumbfile_skip( f, 1 ); if (flags & 0x10) dumbfile_skip( f, 2 ); } } if ( pattern->n_entries == 64 ) return 0; pattern->entry = malloc( pattern->n_entries * sizeof( * pattern->entry ) ); if ( ! pattern->entry ) return -1; entry = pattern->entry; row = 0; if ( dumbfile_seek( f, start, DFS_SEEK_SET ) ) return -1; while ( ( row < 64 ) && !dumbfile_error( f ) && ( dumbfile_pos( f ) < end ) ) { p = dumbfile_getc( f ); if ( ! p ) { IT_SET_END_ROW( entry ); ++ entry; ++ row; continue; } flags = p; entry->channel = flags & 0x0F; entry->mask = 0; if ( flags & 0xF0 ) { if ( flags & 0x80 ) { q = dumbfile_getc( f ); if ( q ) { entry->mask |= IT_ENTRY_NOTE; entry->note = q - 1; } } if ( flags & 0x40 ) { q = dumbfile_getc( f ); if ( q ) { entry->mask |= IT_ENTRY_INSTRUMENT; entry->instrument = q; } } if ( flags & 0x20 ) { entry->mask |= IT_ENTRY_VOLPAN; entry->volpan = dumbfile_getc( f ); } if ( flags & 0x10 ) { q = dumbfile_getc( f ); r = dumbfile_getc( f ); _dumb_it_xm_convert_effect( q, r, entry, 0 ); } if (entry->mask) entry++; } } while ( row < 64 ) { IT_SET_END_ROW( entry ); ++ entry; ++ row; } pattern->n_entries = entry - pattern->entry; if ( ! pattern->n_entries ) return -1; return 0; }
static int it_s3m_read_sample_header(IT_SAMPLE *sample, long *offset, unsigned char *pack, int cwtv, DUMBFILE *f) { unsigned char type; int flags; type = dumbfile_getc(f); dumbfile_getnc((char *)sample->filename, 12, f); sample->filename[12] = 0; if (type > 1) { /** WARNING: no adlib support */ dumbfile_skip(f, 3 + 12 + 1 + 1 + 2 + 2 + 2 + 12); dumbfile_getnc((char *)sample->name, 28, f); sample->name[28] = 0; dumbfile_skip(f, 4); sample->flags &= ~IT_SAMPLE_EXISTS; return dumbfile_error(f); } *offset = dumbfile_getc(f) << 20; *offset += dumbfile_igetw(f) << 4; sample->length = dumbfile_igetl(f); sample->loop_start = dumbfile_igetl(f); sample->loop_end = dumbfile_igetl(f); sample->default_volume = dumbfile_getc(f); dumbfile_skip(f, 1); flags = dumbfile_getc(f); if (flags < 0 || (flags != 0 && flags != 4)) /* Sample is packed apparently (or error reading from file). We don't * know how to read packed samples. */ return -1; *pack = flags; flags = dumbfile_getc(f); sample->C5_speed = dumbfile_igetl(f) << 1; /* Skip four unused bytes and three internal variables. */ dumbfile_skip(f, 4 + 2 + 2 + 4); dumbfile_getnc((char *)sample->name, 28, f); sample->name[28] = 0; if (type == 0 || sample->length <= 0) { /* Looks like no-existy. Anyway, there's for sure no 'SCRS' ... */ sample->flags &= ~IT_SAMPLE_EXISTS; return dumbfile_error(f); } if (dumbfile_mgetl(f) != DUMB_ID('S', 'C', 'R', 'S')) return -1; sample->global_volume = 64; sample->flags = IT_SAMPLE_EXISTS; if (flags & 1) sample->flags |= IT_SAMPLE_LOOP; /* The ST3 TECH.DOC is unclear on this, but IMAGO Orpheus is not. Piece of * crap. */ if (flags & 2) { sample->flags |= IT_SAMPLE_STEREO; if ((cwtv & 0xF000) == 0x2000) { sample->length >>= 1; sample->loop_start >>= 1; sample->loop_end >>= 1; }
struct riff *riff_parse(DUMBFILE *f, long offset, long size, unsigned proper) { unsigned stream_size; struct riff *stream; if (size < 8) return 0; if (dumbfile_seek(f, offset, DFS_SEEK_SET)) return 0; if (dumbfile_mgetl(f) != DUMB_ID('R', 'I', 'F', 'F')) return 0; stream_size = (int)dumbfile_igetl(f); if (stream_size + 8 > size) return 0; if (stream_size < 4) return 0; stream = (struct riff *)malloc(sizeof(struct riff)); if (!stream) return 0; stream->type = (int)dumbfile_mgetl(f); stream->chunk_count = 0; stream->chunks = 0; stream_size -= 4; while (stream_size && !dumbfile_error(f)) { struct riff_chunk *chunk; if (stream_size < 8) break; stream->chunks = (struct riff_chunk *)realloc( stream->chunks, (stream->chunk_count + 1) * sizeof(struct riff_chunk)); if (!stream->chunks) break; chunk = stream->chunks + stream->chunk_count; chunk->type = (int)dumbfile_mgetl(f); chunk->size = (int)dumbfile_igetl(f); chunk->offset = dumbfile_pos(f); stream_size -= 8; if (stream_size < chunk->size) break; if (chunk->type == DUMB_ID('R', 'I', 'F', 'F')) { chunk->nested = riff_parse(f, chunk->offset - 8, chunk->size + 8, proper); if (!chunk->nested) break; } else { chunk->nested = 0; } dumbfile_seek(f, chunk->offset + chunk->size, DFS_SEEK_SET); stream_size -= chunk->size; if (proper && (chunk->size & 1)) { dumbfile_skip(f, 1); --stream_size; } ++stream->chunk_count; } if (stream_size) { riff_free(stream); stream = 0; } return stream; }
static sigdata_t *sample_load_sigdata(DUH *duh, DUMBFILE *file) { SAMPLE_SIGDATA *sigdata; long size; long n; int flags; (void)duh; size = dumbfile_igetl(file); if (dumbfile_error(file) || size <= 0) return NULL; flags = dumbfile_getc(file); if (flags < 0) return NULL; sigdata = malloc(sizeof(*sigdata)); if (!sigdata) return NULL; sigdata->samples = malloc(size * sizeof(sample_t)); if (!sigdata->samples) { free(sigdata); return NULL; } sigdata->size = size; sigdata->flags = flags; if (sigdata->flags & (SAMPFLAG_LOOP | SAMPFLAG_XLOOP)) { sigdata->loop_start = dumbfile_igetl(file); if (dumbfile_error(file) || (unsigned long)sigdata->loop_start >= (unsigned long)size) { free(sigdata->samples); free(sigdata); return NULL; } if (sigdata->flags & SAMPFLAG_LOOP) sigdata->loop_end = size; else { sigdata->loop_end = dumbfile_igetl(file); if (dumbfile_error(file) || sigdata->loop_end <= sigdata->loop_start || sigdata->loop_end > size) { free(sigdata->samples); free(sigdata); return NULL; } } } else { sigdata->loop_start = 0; sigdata->loop_end = size; } if (sigdata->flags & SAMPFLAG_16BIT) { for (n = 0; n < size; n++) { int m = dumbfile_igetw(file); if (m < 0) { free(sigdata->samples); free(sigdata); return NULL; } sigdata->samples[n] = (int)(signed short)m << 8; } } else { for (n = 0; n < size; n++) { int m = dumbfile_getc(file); if (m < 0) { free(sigdata->samples); free(sigdata); return NULL; } sigdata->samples[n] = (int)(signed char)m << 16; } } return sigdata; }
static int it_s3m_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, unsigned char *buffer) { int buflen = 0; int bufpos = 0; IT_ENTRY *entry; unsigned char channel; /* Haha, this is hilarious! * * Well, after some experimentation, it seems that different S3M writers * define the format in different ways. The S3M docs say that the first * two bytes hold the "length of [the] packed pattern", and the packed * pattern data follow. Judging by the contents of ARMANI.S3M, packaged * with ScreamTracker itself, the measure of length _includes_ the two * bytes used to store the length; in other words, we should read * (length - 2) more bytes. However, aryx.s3m, packaged with ModPlug * Tracker, excludes these two bytes, so (length) more bytes must be * read. * * Call me crazy, but I just find it insanely funny that the format was * misunderstood in this way :D * * Now we can't just risk reading two extra bytes, because then we * overshoot, and DUMBFILEs don't support backward seeking (for a good * reason). Luckily, there is a way. We can read the data little by * little, and stop when we have 64 rows in memory. Provided we protect * against buffer overflow, this method should work with all sensibly * written S3M files. If you find one for which it does not work, please * let me know at [email protected] so I can look at it. */ /* Discard the length. */ dumbfile_skip(f, 2); if (dumbfile_error(f)) return -1; pattern->n_rows = 0; pattern->n_entries = 0; /* Read in the pattern data, little by little, and work out how many * entries we need room for. Sorry, but this is just so funny... */ for (;;) { unsigned char b = buffer[buflen++] = dumbfile_getc(f); #if 1 static const unsigned char used[8] = {0, 2, 1, 3, 2, 4, 3, 5}; channel = b & 31; b >>= 5; pattern->n_entries++; if (b) { if (buflen + used[b] >= 65536) return -1; dumbfile_getnc(buffer + buflen, used[b], f); buflen += used[b]; } else { /* End of row */ if (++pattern->n_rows == 64) break; if (buflen >= 65536) return -1; } #else if (b == 0) { /* End of row */ pattern->n_entries++; if (++pattern->n_rows == 64) break; if (buflen >= 65536) return -1; } else { static const unsigned char used[8] = {0, 2, 1, 3, 2, 4, 3, 5}; channel = b & 31; b >>= 5; if (b) { pattern->n_entries++; if (buflen + used[b] >= 65536) return -1; dumbfile_getnc(buffer + buflen, used[b], f); buflen += used[b]; } } #endif /* We have ensured that buflen < 65536 at this point, so it is safe * to iterate and read at least one more byte without checking. * However, now would be a good time to check for errors reading from * the file. */ if (dumbfile_error(f)) return -1; } pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry)); if (!pattern->entry) return -1; entry = pattern->entry; while (bufpos < buflen) { unsigned char b = buffer[bufpos++]; #if 1 if (!(b & ~31)) #else if (b == 0) #endif { /* End of row */ IT_SET_END_ROW(entry); entry++; continue; } channel = b & 31; if (b & 224) { entry->mask = 0; entry->channel = channel; if (b & 32) { unsigned char n = buffer[bufpos++]; if (n != 255) { if (n == 254) entry->note = IT_NOTE_CUT; else entry->note = (n >> 4) * 12 + (n & 15); entry->mask |= IT_ENTRY_NOTE; } entry->instrument = buffer[bufpos++]; if (entry->instrument) entry->mask |= IT_ENTRY_INSTRUMENT; } if (b & 64) { entry->volpan = buffer[bufpos++]; if (entry->volpan != 255) entry->mask |= IT_ENTRY_VOLPAN; } if (b & 128) { entry->effect = buffer[bufpos++]; entry->effectvalue = buffer[bufpos++]; if (entry->effect != 255) { entry->mask |= IT_ENTRY_EFFECT; if (entry->effect == IT_BREAK_TO_ROW) entry->effectvalue -= (entry->effectvalue >> 4) * 6; }
static int it_s3m_read_sample_header(IT_SAMPLE *sample, long *offset, DUMBFILE *f) { unsigned char type; int flags; type = dumbfile_getc(f); if (type > 1) { /** WARNING: no adlib support */ } dumbfile_getnc(sample->filename, 13, f); sample->filename[13] = 0; *offset = dumbfile_igetw(f) << 4; sample->length = dumbfile_igetl(f); sample->loop_start = dumbfile_igetl(f); sample->loop_end = dumbfile_igetl(f); sample->default_volume = dumbfile_getc(f); dumbfile_skip(f, 1); if (dumbfile_getc(f) != 0) /* Sample is packed apparently (or error reading from file). We don't * know how to read packed samples. */ return -1; flags = dumbfile_getc(f); sample->C5_speed = dumbfile_igetl(f) << 1; /* Skip four unused bytes and three internal variables. */ dumbfile_skip(f, 4+2+2+4); dumbfile_getnc(sample->name, 28, f); sample->name[28] = 0; if (type == 0) { /* Looks like no-existy. Anyway, there's for sure no 'SCRS' ... */ sample->flags &= ~IT_SAMPLE_EXISTS; return dumbfile_error(f); } if (dumbfile_mgetl(f) != DUMB_ID('S','C','R','S')) return -1; sample->global_volume = 64; sample->flags = IT_SAMPLE_EXISTS; if (flags & 1) sample->flags |= IT_SAMPLE_LOOP; if (flags & 2) sample->flags |= IT_SAMPLE_STEREO; if (flags & 4) sample->flags |= IT_SAMPLE_16BIT; sample->default_pan = 0; // 0 = don't use, or 160 = centre? if (sample->length <= 0) sample->flags &= ~IT_SAMPLE_EXISTS; else if (sample->flags & IT_SAMPLE_LOOP) { if ((unsigned int)sample->loop_end > (unsigned int)sample->length) sample->flags &= ~IT_SAMPLE_LOOP; else if ((unsigned int)sample->loop_start >= (unsigned int)sample->loop_end) sample->flags &= ~IT_SAMPLE_LOOP; else /* ScreamTracker seems not to save what comes after the loop end * point, but rather to assume it is a duplicate of what comes at * the loop start point. I am not completely sure of this though. * It is easy to evade; simply truncate the sample. */ sample->length = sample->loop_end; } //Do we need to set all these? sample->vibrato_speed = 0; sample->vibrato_depth = 0; sample->vibrato_rate = 0; sample->vibrato_waveform = IT_VIBRATO_SINE; return dumbfile_error(f); }
static IFF_CHUNKED *dumbfile_read_okt(DUMBFILE *f) { IFF_CHUNKED *mod = (IFF_CHUNKED *) malloc(sizeof(*mod)); if (!mod) return NULL; mod->chunk_count = 0; mod->chunks = 0; for (;;) { long bytes_read; IFF_CHUNK * chunk = ( IFF_CHUNK * ) realloc( mod->chunks, ( mod->chunk_count + 1 ) * sizeof( IFF_CHUNK ) ); if ( !chunk ) { if ( mod->chunks ) free( mod->chunks ); free( mod ); return NULL; } mod->chunks = chunk; chunk += mod->chunk_count; bytes_read = dumbfile_mgetl( f ); if ( bytes_read < 0 ) break; chunk->type = bytes_read; chunk->size = dumbfile_mgetl( f ); if ( dumbfile_error( f ) ) break; chunk->data = (unsigned char *) malloc( chunk->size ); if ( !chunk->data ) { free( mod->chunks ); free( mod ); return NULL; } bytes_read = dumbfile_getnc( ( char * ) chunk->data, chunk->size, f ); if ( bytes_read < (long)chunk->size ) { if ( bytes_read <= 0 ) { free( chunk->data ); break; } else { chunk->size = bytes_read; mod->chunk_count++; break; } } mod->chunk_count++; } if ( !mod->chunk_count ) { if ( mod->chunks ) free(mod->chunks); free(mod); mod = NULL; } return mod; }
static DUMB_IT_SIGDATA *it_asy_load_sigdata(DUMBFILE *f) { DUMB_IT_SIGDATA *sigdata; int i; static const char sig_part[] = "ASYLUM Music Format"; static const char sig_rest[] = " V1.0"; /* whee, string space optimization with format type below */ char signature[32]; if (dumbfile_getnc(signature, 32, f) != 32 || memcmp(signature, sig_part, 19) || memcmp(signature + 19, sig_rest, 5)) { return NULL; } sigdata = malloc(sizeof(*sigdata)); if (!sigdata) { return NULL; } sigdata->speed = dumbfile_getc(f); /* XXX seems to fit the files I have */ sigdata->tempo = dumbfile_getc(f); /* ditto */ sigdata->n_samples = dumbfile_getc(f); /* ditto */ sigdata->n_patterns = dumbfile_getc(f); sigdata->n_orders = dumbfile_getc(f); sigdata->restart_position = dumbfile_getc(f); if (dumbfile_error(f) || !sigdata->n_samples || sigdata->n_samples > 64 || !sigdata->n_patterns || !sigdata->n_orders) { free(sigdata); return NULL; } if (sigdata->restart_position > sigdata->n_orders) /* XXX */ sigdata->restart_position = 0; sigdata->order = malloc(sigdata->n_orders); if (!sigdata->order) { free(sigdata); return NULL; } if (dumbfile_getnc((char *)sigdata->order, sigdata->n_orders, f) != sigdata->n_orders || dumbfile_skip(f, 256 - sigdata->n_orders)) { free(sigdata->order); free(sigdata); return NULL; } sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample)); if (!sigdata->sample) { free(sigdata->order); free(sigdata); return NULL; } sigdata->song_message = NULL; sigdata->instrument = NULL; sigdata->pattern = NULL; sigdata->midi = NULL; sigdata->checkpoint = NULL; sigdata->n_instruments = 0; for (i = 0; i < sigdata->n_samples; ++i) sigdata->sample[i].data = NULL; for (i = 0; i < sigdata->n_samples; ++i) { if (it_asy_read_sample_header(&sigdata->sample[i], f)) { _dumb_it_unload_sigdata(sigdata); return NULL; } } if (dumbfile_skip(f, 37 * (64 - sigdata->n_samples))) { _dumb_it_unload_sigdata(sigdata); return NULL; } sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern)); if (!sigdata->pattern) { _dumb_it_unload_sigdata(sigdata); return NULL; } for (i = 0; i < sigdata->n_patterns; ++i) sigdata->pattern[i].entry = NULL; /* Read in the patterns */ { unsigned char *buffer = malloc(64 * 8 * 4); /* 64 rows * 8 channels * 4 bytes */ if (!buffer) { _dumb_it_unload_sigdata(sigdata); return NULL; } for (i = 0; i < sigdata->n_patterns; ++i) { if (it_asy_read_pattern(&sigdata->pattern[i], f, buffer) != 0) { free(buffer); _dumb_it_unload_sigdata(sigdata); return NULL; } } free(buffer); } /* And finally, the sample data */ for (i = 0; i < sigdata->n_samples; ++i) { if (it_asy_read_sample_data(&sigdata->sample[i], f)) { _dumb_it_unload_sigdata(sigdata); return NULL; } } /* Now let's initialise the remaining variables, and we're done! */ sigdata->flags = IT_WAS_AN_XM | IT_WAS_A_MOD | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_STEREO; sigdata->global_volume = 128; sigdata->mixing_volume = 48; sigdata->pan_separation = 128; sigdata->n_pchannels = 8; sigdata->name[0] = 0; memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS); for (i = 0; i < DUMB_IT_N_CHANNELS; i += 4) { int sep = 32 * dumb_it_default_panning_separation / 100; sigdata->channel_pan[i + 0] = 32 - sep; sigdata->channel_pan[i + 1] = 32 + sep; sigdata->channel_pan[i + 2] = 32 + sep; sigdata->channel_pan[i + 3] = 32 - sep; } if (_dumb_it_fix_invalid_orders(sigdata) < 0) { _dumb_it_unload_sigdata(sigdata); return NULL; } return sigdata; }