예제 #1
0
void midi_send_flush(void)
{
        struct midi_port *ptr = NULL;
        int need_explicit_flush = 0;

        if (!midi_record_mutex || !midi_play_mutex) return;

        while (midi_port_foreach(NULL, &ptr)) {
                if ((ptr->io & MIDI_OUTPUT)) {
                        if (ptr->drain) ptr->drain(ptr);
                        else if (ptr->send_now) need_explicit_flush=1;
                }
        }

        /* no need for midi sync huzzah; driver does it for us... */
        if (!need_explicit_flush) return;

        if (!midi_queue_thread) {
                midi_queue_thread = SDL_CreateThread(_midi_queue_run, NULL);
                if (midi_queue_thread) {
                        log_appendf(3, "Started MIDI queue thread");
                } else {
                        log_appendf(2, "ACK: Couldn't start MIDI thread; things are likely going to go boom!");
                }
        }

        SDL_mutexP(midi_play_mutex);
        SDL_CondSignal(midi_play_cond);
        SDL_mutexV(midi_play_mutex);
}
예제 #2
0
/* NOTE: ptr should be dynamically allocated, or NULL */
static void do_save_song(char *ptr)
{
	int ret, export = (status.current_page == PAGE_EXPORT_MODULE);
	const char *filename = ptr ?: song_get_filename();
	const char *seltype = NULL;
	struct widget *widget;

	set_page(PAGE_LOG);

	// 4 is the index of the first file-type button
	for (widget = (export ? widgets_exportmodule : widgets_savemodule) + 4;
	     widget->type == WIDGET_TOGGLEBUTTON; widget++) {
		if (widget->d.togglebutton.state) {
			// Aha!
			seltype = widget->d.togglebutton.text;
			break;
		}
	}

	if (!seltype) {
		// No button was selected? (should never happen)
		log_appendf(4, "No file format selected?");
		ret = SAVE_INTERNAL_ERROR;
	} else if (export) {
		ret = song_export(filename, seltype);
	} else {
예제 #3
0
int fmt_aiff_export_body(disko_t *fp, const uint8_t *data, size_t length)
{
        struct aiff_writedata *awd = fp->userdata;

        if (length % awd->bps) {
                log_appendf(4, "AIFF export: received uneven length");
                return DW_ERROR;
        }

        awd->numbytes += length;

        if (awd->swap) {
                const int16_t *ptr = (const int16_t *) data;
                uint16_t v;

                length /= 2;
                while (length--) {
                        v = *ptr;
                        v = bswapBE16(v);
                        disko_write(fp, &v, 2);
                        ptr++;
                }
        } else {
                disko_write(fp, data, length);
        }

        return DW_OK;
}
예제 #4
0
int fmt_aiff_save_sample(disko_t *fp, song_sample_t *smp)
{
        int bps;
        uint32_t ul;
        uint32_t flags = SF_BE | SF_PCMS;
        flags |= (smp->flags & CHN_16BIT) ? SF_16 : SF_8;
        flags |= (smp->flags & CHN_STEREO) ? SF_SI : SF_M;

        bps = aiff_header(fp, (smp->flags & CHN_16BIT) ? 16 : 8, (smp->flags & CHN_STEREO) ? 2 : 1,
                smp->c5speed, smp->name, smp->length, NULL);

        if (csf_write_sample(fp, smp, flags) != smp->length * bps) {
                log_appendf(4, "AIFF: unexpected data size written");
                return SAVE_INTERNAL_ERROR;
        }

        /* TODO: loop data */

        /* fix the length in the file header */
        ul = disko_tell(fp) - 8;
        ul = bswapBE32(ul);
        disko_seek(fp, 4, SEEK_SET);
        disko_write(fp, &ul, 4);

        return SAVE_SUCCESS;
}
예제 #5
0
void log_underline(int chars)
{
        char buf[75];

        chars = CLAMP(chars, 0, (int) sizeof(buf) - 1);
        buf[chars--] = '\0';
        do
                buf[chars] = 0x81;
        while (chars--);
        log_appendf(2, "%s", buf);
}
예제 #6
0
int slurp_win32(slurp_t *useme, const char *filename, size_t st)
{
        HANDLE h, m, *bp;
        LPVOID addr;

        bp = (HANDLE*)mem_alloc(sizeof(HANDLE)*2);

        h = CreateFile(filename, GENERIC_READ,
                        FILE_SHARE_READ, NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (!h) {
                log_appendf(4, "CreateFile(%s) failed with %lu", filename,
                                        GetLastError());
                free(bp);
                return 0;
        }

        m = CreateFileMapping(h, NULL, PAGE_READONLY, 0, 0, NULL);
        if (!h) {
                log_appendf(4, "CreateFileMapping failed with %lu", GetLastError());
                CloseHandle(h);
                free(bp);
                return -1;
        }
        addr = MapViewOfFile(m, FILE_MAP_READ, 0, 0, 0);
        if (!addr) {
                log_appendf(4, "MapViewOfFile failed with %lu", GetLastError());
                CloseHandle(m);
                CloseHandle(h);
                free(bp);
                return -1;
        }
        useme->data = addr;
        useme->length = st;
        useme->closure = _win32_unmap;

        bp[0] = m; bp[1] = h;
        useme->bextra = bp;
        return 1;
}
예제 #7
0
/* returns 1 if a character was actually added */
static int message_add_char(int newchar, int position)
{
        int len = strlen(current_song->message);

        if (len == MAX_MESSAGE) {
                dialog_create(DIALOG_OK, "  Song message too long!  ", NULL, NULL, 0, NULL);
                return 0;
        }
        if (position < 0 || position > len) {
                log_appendf(4, "message_add_char: position=%d, len=%d - shouldn't happen!", position, len);
                return 0;
        }

        memmove(current_song->message + position + 1, current_song->message + position, len - position);
        current_song->message[len + 1] = 0;
        current_song->message[position] = (unsigned char)newchar;
        return 1;
}
예제 #8
0
void log_perror(const char *prefix)
{
        char *e = strerror(errno);
        perror(prefix);
        log_appendf(4, "%s: %s", prefix, e);
}
예제 #9
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 */
                        }
                }
        }
예제 #10
0
static int _read_iff(dmoz_file_t *file, song_sample_t *smp, const uint8_t *data, size_t length)
{
        chunk_t chunk;
        size_t pos = 0;
        chunk_t vhdr, body, name, comm, auth, anno, ssnd; // butt

        if (!iff_chunk_read(&chunk, data, length, &pos))
                return 0;
        if (chunk.id != ID_FORM)
                return 0;

        // jump "into" the FORM chunk
        // if (pos < length), there's more data after the FORM chunk -- but I don't care about this scenario
        pos = 0;
        length = MIN(length, chunk.size);
        data = chunk.data->FORM.data;

        /* the header is already byteswapped, but anything in 'chunk' will need to be swapped as needed
        because the structure is a const pointing into the data itself */
        switch (bswapBE32(chunk.data->FORM.filetype)) {
        case ID_8SVX:
                // shut up, gcc
                ZEROIZE(vhdr);
                ZEROIZE(body);
                ZEROIZE(name);
                ZEROIZE(auth);
                ZEROIZE(anno);

                while (iff_chunk_read(&chunk, data, length, &pos)) {
                        switch (chunk.id) {
                                case ID_VHDR: vhdr = chunk; break;
                                case ID_BODY: body = chunk; break;
                                case ID_NAME: name = chunk; break;
                                case ID_AUTH: auth = chunk; break;
                                case ID_ANNO: anno = chunk; break;
                                default: break;
                        }
                }
                if (!(vhdr.id && body.id))
                        return 0;

                if (vhdr.data->VHDR.compression) {
                        log_appendf(4, "error: compressed 8SVX files are unsupported");
                        return 0;
                }
                if (vhdr.data->VHDR.num_octaves != 1) {
                        log_appendf(4, "warning: 8SVX file contains %d octaves",
                                vhdr.data->VHDR.num_octaves);
                }

                if (file) {
                        file->description = "8SVX sample";
                        file->type = TYPE_SAMPLE_PLAIN;
                }
                if (!name.id) name = auth;
                if (!name.id) name = anno;
                if (name.id) {
                        if (file) {
                                file->title = calloc(1, name.size + 1);
                                memcpy(file->title, name.data->bytes, name.size);
                                file->title[name.size] = '\0';
                        }
                        if (smp) {
                                int len = MIN(25, name.size);
                                memcpy(smp->name, name.data->bytes, len);
                                smp->name[len] = 0;
                        }
                }

                if (smp) {
                        smp->c5speed = bswapBE16(vhdr.data->VHDR.smp_per_sec);
                        smp->length = body.size;

                        csf_read_sample(smp, SF_BE | SF_PCMS | SF_8 | SF_M, body.data->bytes, body.size);

                        smp->volume = 64*4;
                        smp->global_volume = 64;

                        // this is done kinda weird
                        smp->loop_end = bswapBE32(vhdr.data->VHDR.smp_highoct_repeat);
                        if (smp->loop_end) {
                                smp->loop_start = bswapBE32(vhdr.data->VHDR.smp_highoct_1shot);
                                smp->loop_end += smp->loop_start;
                                if (smp->loop_start > smp->length)
                                        smp->loop_start = 0;
                                if (smp->loop_end > smp->length)
                                        smp->loop_end = smp->length;
                                if (smp->loop_start + 2 < smp->loop_end)
                                        smp->flags |= CHN_LOOP;
                        }
                        // TODO vhdr.data->VHDR.volume ?
                }

                return 1;

        case ID_AIFF:
                ZEROIZE(comm);
                ZEROIZE(ssnd);
                ZEROIZE(name);
                ZEROIZE(auth);
                ZEROIZE(anno);

                while (iff_chunk_read(&chunk, data, length, &pos)) {
                        switch (chunk.id) {
                                case ID_COMM: comm = chunk; break;
                                case ID_SSND: ssnd = chunk; break;
                                case ID_NAME: name = chunk; break;
                                default: break;
                        }
                }
                if (!(comm.id && ssnd.id))
                        return 0;

                if (file) {
                        file->description = "Audio IFF sample";
                        file->type = TYPE_SAMPLE_PLAIN;
                }
                if (!name.id) name = auth;
                if (!name.id) name = anno;
                if (name.id) {
                        if (file) {
                                file->title = calloc(1, name.size + 1);
                                memcpy(file->title, name.data->bytes, name.size);
                                file->title[name.size] = '\0';
                        }
                        if (smp) {
                                int len = MIN(25, name.size);
                                memcpy(smp->name, name.data->bytes, len);
                                smp->name[len] = 0;
                        }
                }

                /* TODO loop points */

                if (smp) {
                        uint32_t flags = SF_BE | SF_PCMS;

                        switch (bswapBE16(comm.data->COMM.num_channels)) {
                        default:
                                log_appendf(4, "warning: multichannel AIFF is unsupported");
                        case 1:
                                flags |= SF_M;
                                break;
                        case 2:
                                flags |= SF_SI;
                                break;
                        }

                        switch ((bswapBE16(comm.data->COMM.sample_size) + 7) & ~7) {
                        default:
                                log_appendf(4, "warning: AIFF has unsupported bit-width");
                        case 8:
                                flags |= SF_8;
                                break;
                        case 16:
                                flags |= SF_16;
                                break;
                        }

                        // TODO: data checking; make sure sample count and byte size agree
                        // (and if not, cut to shorter of the two)

                        smp->c5speed = ConvertFromIeeeExtended(comm.data->COMM.sample_rate);
                        smp->length = bswapBE32(comm.data->COMM.num_frames);
                        smp->volume = 64*4;
                        smp->global_volume = 64;

                        // the audio data starts 8 bytes into the chunk
                        // (don't care about the block alignment stuff)
                        csf_read_sample(smp, flags, ssnd.data->bytes + 8, ssnd.size - 8);
                }

                return 1;
        }

        return 0;
}