예제 #1
0
void sample_toggle_quality(song_sample_t * sample, int convert_data)
{
	signed char *odata;

	song_lock_audio();

	// stop playing the sample because we'll be reallocating and/or changing lengths
	csf_stop_sample(current_song, sample);

	sample->flags ^= CHN_16BIT;

	status.flags |= SONG_NEEDS_SAVE;
	if (convert_data) {
		odata = csf_allocate_sample(sample->length
			* ((sample->flags & CHN_16BIT) ? 2 : 1)
			* ((sample->flags & CHN_STEREO) ? 2 : 1));
		if (sample->flags & CHN_16BIT) {
			_quality_convert_8to16(sample->data, (signed short *) odata,
				sample->length * ((sample->flags & CHN_STEREO) ? 2 : 1));
		} else {
			_quality_convert_16to8((signed short *) sample->data, odata,
				sample->length * ((sample->flags & CHN_STEREO) ? 2 : 1));
		}
		csf_free_sample(sample->data);
		sample->data = odata;
	} else {
		if (sample->flags & CHN_16BIT) {
			sample->length >>= 1;
			sample->loop_start >>= 1;
			sample->loop_end >>= 1;
			sample->sustain_start >>= 1;
			sample->sustain_end >>= 1;
		} else {
예제 #2
0
static int load_s3i_sample(const uint8_t *data, size_t length, song_sample_t *smp)
{
        const struct s3i_header* header = (const struct s3i_header*) data;
        /*
        fprintf(stderr, "%X-%X-%X-%X-%X\n",
        (((char*)&(header->type     ))-((char*)&(header->type))),
        (((char*)&(header->length   ))-((char*)&(header->type))),
        (((char*)&(header->c2spd    ))-((char*)&(header->type))),
        (((char*)&(header->samplename))-((char*)&(header->type))),
        (((char*)&(header->samplesig))-((char*)&(header->type)))
        );

        fprintf(stderr, "Considering %d byte sample (%.4s), %d\n",
        (int)length,
        header->samplesig,
        header->length);
        */
        if(length < 0x50)
                return 0; // too small
        if (strncmp(header->samplesig, "SCRS", 4) != 0
            && strncmp(header->samplesig, "SCRI", 4) != 0)
                return 0; // It should be either SCRS or SCRI.

        size_t samp_length = bswapLE32(header->length);
        int bytes_per_sample = (header->type == 1 ? ((header->flags & 2) ? 2 : 1) : 0); // no sample data

        if (length < 0x50 + smp->length * bytes_per_sample)
                return 0;

        smp->length = samp_length;
        smp->global_volume = 64;
        smp->volume = header->volume*256/64;
        smp->loop_start = header->loopbeg;
        smp->loop_end = header->loopend;
        smp->c5speed = header->c2spd;
        smp->flags = 0;
        if (header->flags & 1)
                smp->flags |= CHN_LOOP;
        if (header->flags & 2)
                smp->flags |= CHN_STEREO;
        if (header->flags & 4)
                smp->flags |= CHN_16BIT;

        if (header->type == 2) {
                smp->flags |= CHN_ADLIB;
                smp->flags &= ~(CHN_LOOP|CHN_16BIT);

                memcpy(smp->adlib_bytes, &header->length, 11);

                smp->length = 1;
                smp->loop_start = 0;
                smp->loop_end = 0;

                smp->data = csf_allocate_sample(1);
        }

        int format = SF_M | SF_LE; // endianness; channels
        format |= (smp->flags & CHN_16BIT) ? (SF_16 | SF_PCMS) : (SF_8 | SF_PCMU); // bits; encoding

        csf_read_sample((song_sample_t *) smp, format,
                (const char *) (data + 0x50), (uint32_t) (length - 0x50));

        strncpy(smp->filename, header->dosfn, 11);
        strncpy(smp->name, header->samplename, 25);

        return 1;
}
예제 #3
0
int fmt_s3m_load_song(song_t *song, slurp_t *fp, unsigned int lflags)
{
        uint16_t nsmp, nord, npat;
        int misc = S3M_UNSIGNED | S3M_CHANPAN; // temporary flags, these are both generally true
        int n;
        song_note_t *note;
        /* junk variables for reading stuff into */
        uint16_t tmp;
        uint8_t c;
        uint32_t tmplong;
        uint8_t b[4];
        /* parapointers */
        uint16_t para_smp[MAX_SAMPLES];
        uint16_t para_pat[MAX_PATTERNS];
        uint32_t para_sdata[MAX_SAMPLES] = { 0 };
        uint32_t smp_flags[MAX_SAMPLES] = { 0 };
        song_sample_t *sample;
        uint16_t trkvers;
        uint16_t flags;
        uint16_t special;
        uint32_t adlib = 0; // bitset
        int uc;
        const char *tid = NULL;

        /* check the tag */
        slurp_seek(fp, 44, SEEK_SET);
        slurp_read(fp, b, 4);
        if (memcmp(b, "SCRM", 4) != 0)
                return LOAD_UNSUPPORTED;

        /* read the title */
        slurp_rewind(fp);
        slurp_read(fp, song->title, 25);
        song->title[25] = 0;

        /* skip the last three bytes of the title, the supposed-to-be-0x1a byte,
        the tracker ID, and the two useless reserved bytes */
        slurp_seek(fp, 7, SEEK_CUR);

        slurp_read(fp, &nord, 2);
        slurp_read(fp, &nsmp, 2);
        slurp_read(fp, &npat, 2);
        nord = bswapLE16(nord);
        nsmp = bswapLE16(nsmp);
        npat = bswapLE16(npat);

        if (nord > MAX_ORDERS || nsmp > MAX_SAMPLES || npat > MAX_PATTERNS)
                return LOAD_FORMAT_ERROR;

        song->flags = SONG_ITOLDEFFECTS;
        slurp_read(fp, &flags, 2);  /* flags (don't really care) */
        flags = bswapLE16(flags);
        slurp_read(fp, &trkvers, 2);
        trkvers = bswapLE16(trkvers);
        slurp_read(fp, &tmp, 2);  /* file format info */
        if (tmp == bswapLE16(1))
                misc &= ~S3M_UNSIGNED;     /* signed samples (ancient s3m) */

        slurp_seek(fp, 4, SEEK_CUR); /* skip the tag */

        song->initial_global_volume = slurp_getc(fp) << 1;
        // In the case of invalid data, ST3 uses the speed/tempo value that's set in the player prior to
        // loading the song, but that's just crazy.
        song->initial_speed = slurp_getc(fp) ?: 6;
        song->initial_tempo = slurp_getc(fp);
        if (song->initial_tempo <= 32) {
                // (Yes, 32 is ignored by Scream Tracker.)
                song->initial_tempo = 125;
        }
        song->mixing_volume = slurp_getc(fp);
        if (song->mixing_volume & 0x80) {
                song->mixing_volume ^= 0x80;
        } else {
                song->flags |= SONG_NOSTEREO;
        }
        uc = slurp_getc(fp); /* ultraclick removal (useless) */

        if (slurp_getc(fp) != 0xfc)
                misc &= ~S3M_CHANPAN;     /* stored pan values */

        /* Interesting: Impulse Tracker appears to leave some junk data in this unused section, and what's
        more, it always seems to follow the same general pattern. So it's actually possible to identify
        whether a song was saved in IT, then loaded and re-saved in ST3. */
        slurp_seek(fp, 8, SEEK_CUR);
        slurp_read(fp, &special, 2); // field not used by st3
        special = bswapLE16(special);

        /* channel settings */
        for (n = 0; n < 32; n++) {
                /* Channel 'type': 0xFF is a disabled channel, which shows up as (--) in ST3.
                Any channel with the high bit set is muted.
                00-07 are L1-L8, 08-0F are R1-R8, 10-18 are adlib channels A1-A9.
                Hacking at a file with a hex editor shows some perhaps partially-implemented stuff:
                types 19-1D show up in ST3 as AB, AS, AT, AC, and AH; 20-2D are the same as 10-1D
                except with 'B' insted of 'A'. None of these appear to produce any sound output,
                apart from 19 which plays adlib instruments briefly before cutting them. (Weird!)
                Also, 1E/1F and 2E/2F display as "??"; and pressing 'A' on a disabled (--) channel
                will change its type to 1F.
                Values past 2F seem to display bits of the UI like the copyright and help, strange!
                These out-of-range channel types will almost certainly hang or crash ST3 or
                produce other strange behavior. Simply put, don't do it. :) */
                c = slurp_getc(fp);
                if (c & 0x80) {
                        song->channels[n].flags |= CHN_MUTE;
                        // ST3 doesn't even play effects in muted channels -- throw them out?
                        c &= ~0x80;
                }
                if (c < 0x08) {
                        // L1-L8 (panned to 3 in ST3)
                        song->channels[n].panning = 14;
                } else if (c < 0x10) {
                        // R1-R8 (panned to C in ST3)
                        song->channels[n].panning = 50;
                } else if (c < 0x19) {
                        // A1-A9
                        song->channels[n].panning = 32;
                        adlib |= 1 << n;
                } else {
                        // Disabled 0xff/0x7f, or broken
                        song->channels[n].panning = 32;
                        song->channels[n].flags |= CHN_MUTE;
                }
                song->channels[n].volume = 64;
        }
        for (; n < 64; n++) {
                song->channels[n].panning = 32;
                song->channels[n].volume = 64;
                song->channels[n].flags = CHN_MUTE;
        }

        /* orderlist */
        slurp_read(fp, song->orderlist, nord);
        memset(song->orderlist + nord, ORDER_LAST, MAX_ORDERS - nord);

        /* load the parapointers */
        slurp_read(fp, para_smp, 2 * nsmp);
        slurp_read(fp, para_pat, 2 * npat);

        /* default pannings */
        if (misc & S3M_CHANPAN) {
                for (n = 0; n < 32; n++) {
                        c = slurp_getc(fp);
                        if (c & 0x20)
                                song->channels[n].panning = ((c & 0xf) << 2) + 2;
                }
        }

        //mphack - fix the pannings
        for (n = 0; n < 64; n++)
                song->channels[n].panning *= 4;

        /* samples */
        for (n = 0, sample = song->samples + 1; n < nsmp; n++, sample++) {
                uint8_t type;

                slurp_seek(fp, (para_smp[n]) << 4, SEEK_SET);

                type = slurp_getc(fp);
                slurp_read(fp, sample->filename, 12);
                sample->filename[12] = 0;

                slurp_read(fp, b, 3); // data pointer for pcm, irrelevant otherwise
                switch (type) {
                case S3I_TYPE_PCM:
                        para_sdata[n] = b[1] | (b[2] << 8) | (b[0] << 16);
                        slurp_read(fp, &tmplong, 4);
                        sample->length = bswapLE32(tmplong);
                        slurp_read(fp, &tmplong, 4);
                        sample->loop_start = bswapLE32(tmplong);
                        slurp_read(fp, &tmplong, 4);
                        sample->loop_end = bswapLE32(tmplong);
                        sample->volume = slurp_getc(fp) * 4; //mphack
                        slurp_getc(fp);      /* unused byte */
                        slurp_getc(fp);      /* packing info (never used) */
                        c = slurp_getc(fp);  /* flags */
                        if (c & 1)
                                sample->flags |= CHN_LOOP;
                        smp_flags[n] = (SF_LE
                                | ((misc & S3M_UNSIGNED) ? SF_PCMU : SF_PCMS)
                                | ((c & 4) ? SF_16 : SF_8)
                                | ((c & 2) ? SF_SS : SF_M));
                        break;

                default:
                        //printf("s3m: mystery-meat sample type %d\n", type);
                case S3I_TYPE_NONE:
                        slurp_seek(fp, 12, SEEK_CUR);
                        sample->volume = slurp_getc(fp) * 4; //mphack
                        slurp_seek(fp, 3, SEEK_CUR);
                        break;

                case S3I_TYPE_ADMEL:
                        slurp_read(fp, sample->adlib_bytes, 12);
                        sample->volume = slurp_getc(fp) * 4; //mphack
                        // next byte is "dsk", what is that?
                        slurp_seek(fp, 3, SEEK_CUR);
                        sample->flags |= CHN_ADLIB;
                        // dumb hackaround that ought to some day be fixed:
                        sample->length = 1;
                        sample->data = csf_allocate_sample(1);
                        break;
                }

                slurp_read(fp, &tmplong, 4);
                sample->c5speed = bswapLE32(tmplong);
                if (type == S3I_TYPE_ADMEL) {
                        if (sample->c5speed < 1000 || sample->c5speed > 0xFFFF) {
                                sample->c5speed = 8363;
                        }
                }
                slurp_seek(fp, 12, SEEK_CUR);        /* wasted space */
                slurp_read(fp, sample->name, 25);
                sample->name[25] = 0;
                sample->vib_type = 0;
                sample->vib_rate = 0;
                sample->vib_depth = 0;
                sample->vib_speed = 0;
                sample->global_volume = 64;
        }

        /* sample data */
        if (!(lflags & LOAD_NOSAMPLES)) {
                for (n = 0, sample = song->samples + 1; n < nsmp; n++, sample++) {
                        if (!sample->length || (sample->flags & CHN_ADLIB))
                                continue;
                        slurp_seek(fp, para_sdata[n] << 4, SEEK_SET);
                        csf_read_sample(sample, smp_flags[n], fp->data + fp->pos, fp->length - fp->pos);
                }
        }

        if (!(lflags & LOAD_NOPATTERNS)) {
                for (n = 0; n < npat; n++) {
                        int row = 0;
                        long end;

                        para_pat[n] = bswapLE16(para_pat[n]);
                        if (!para_pat[n])
                                continue;

                        slurp_seek(fp, para_pat[n] << 4, SEEK_SET);
                        slurp_read(fp, &tmp, 2);
                        end = (para_pat[n] << 4) + bswapLE16(tmp) + 2;

                        song->patterns[n] = csf_allocate_pattern(64);

                        while (row < 64 && slurp_tell(fp) < end) {
                                int mask = slurp_getc(fp);
                                uint8_t chn = (mask & 31);

                                if (mask == EOF) {
                                        log_appendf(4, " Warning: Pattern %d: file truncated", n);
                                        break;
                                }
                                if (!mask) {
                                        /* done with the row */
                                        row++;
                                        continue;
                                }
                                note = song->patterns[n] + 64 * row + chn;
                                if (mask & 32) {
                                        /* note/instrument */
                                        note->note = slurp_getc(fp);
                                        note->instrument = slurp_getc(fp);
                                        //if (note->instrument > 99)
                                        //      note->instrument = 0;
                                        switch (note->note) {
                                        default:
                                                // Note; hi=oct, lo=note
                                                note->note = (note->note >> 4) * 12 + (note->note & 0xf) + 13;
                                                break;
                                        case 255:
                                                note->note = NOTE_NONE;
                                                break;
                                        case 254:
                                                note->note = (adlib & (1 << chn)) ? NOTE_OFF : NOTE_CUT;
                                                break;
                                        }
                                }
                                if (mask & 64) {
                                        /* volume */
                                        note->voleffect = VOLFX_VOLUME;
                                        note->volparam = slurp_getc(fp);
                                        if (note->volparam == 255) {
                                                note->voleffect = VOLFX_NONE;
                                                note->volparam = 0;
                                        } else if (note->volparam > 64) {
                                                // some weirdly saved s3m?
                                                note->volparam = 64;
                                        }
                                }
                                if (mask & 128) {
                                        note->effect = slurp_getc(fp);
                                        note->param = slurp_getc(fp);
                                        csf_import_s3m_effect(note, 0);
                                        if (note->effect == FX_SPECIAL) {
                                                // mimic ST3's SD0/SC0 behavior
                                                if (note->param == 0xd0) {
                                                        note->note = NOTE_NONE;
                                                        note->instrument = 0;
                                                        note->voleffect = VOLFX_NONE;
                                                        note->volparam = 0;
                                                        note->effect = FX_NONE;
                                                        note->param = 0;
                                                } else if (note->param == 0xc0) {
                                                        note->effect = FX_NONE;
                                                        note->param = 0;
                                                }
                                        }
                                }
                                /* ... next note, same row */
                        }
                }
        }