Ejemplo n.º 1
0
static void sound_cmd_load(char *wavfile)
/* load a sound */
{
    FILE *f;
    int size, bits;
    struct sound *ss;
    int big = 1;

    if (_daemon_seq == MAX_ORIGINAL_SOUNDS)
        return;

    _daemon_seq++;

    if ((f = qdgdfa_load_wav(wavfile, &size, &bits)) == NULL)
        return;

    if (!_qdgdfa_16_bit && bits == 16)
        size /= 2;
    if (_qdgdfa_16_bit && bits == 8)
        size *= 2;

    ss = &_sounds[_daemon_seq];

    /* test if file must not be considered big */
    if (size < _qdgdfa_big_sound_threshold)
        big = 0;
    else
        /* big files must be at least two buffers big */
    if (size < 2 * _qdgdfa_big_sound_buffer_size)
        big = 0;

    ss->total_size = size;

    if (big) {
        ss->file = f;
        ss->fpos = ftell(f);

        ss->partial_size = _qdgdfa_big_sound_buffer_size;
        ss->data[0] = (unsigned char *) malloc(ss->partial_size);
        ss->data[1] = (unsigned char *) malloc(ss->partial_size);
    }
    else {
        ss->partial_size = size;
        ss->data[0] = (unsigned char *) malloc(ss->partial_size);
    }

    /* sound size is stored as # of samples, not bytes */
    if (_qdgdfa_16_bit) {
        ss->total_size /= 2;
        ss->partial_size /= 2;
    }

    ss->playing = 0;
    ss->pan = 0;
    ss->att = 63;
    ss->duped = 0;
    ss->bits = bits;
    ss->big = big;
    ss->bi = 0;

    ss->partial_left = ss->partial_size;
    ss->total_left = ss->total_size;

    ss->data_8 = (unsigned char *) ss->data[0];
    ss->data_16 = (short int *) ss->data[0];

    _sound_read_block(f, ss->partial_size, _sounds[_daemon_seq].data[0],
                      bits);

    if (big)
        _sound_read_block(f, ss->partial_size,
                          _sounds[_daemon_seq].data[1], bits);
    else
        fclose(f);
}
Ejemplo n.º 2
0
static int _qdgdfa_load_sound(char *wavfile)
{
    PCMWAVEFORMAT pcmwf;
    DSBUFFERDESC dsbd;
    FILE *f;
    int n, size, bits;
    char *w1;
    char *w2;
    unsigned long s1;
    unsigned long s2;

    if (!sound_test())
        return 1;

    if ((f = qdgdfa_load_wav(wavfile, &size, &bits)) == NULL)
        return -1;

    memset(&pcmwf, '\0', sizeof(pcmwf));

    pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
    pcmwf.wf.nChannels = 1;
    pcmwf.wf.nSamplesPerSec = 11025;
    pcmwf.wf.nBlockAlign = bits / 8;
    pcmwf.wf.nAvgBytesPerSec =
        pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
    pcmwf.wBitsPerSample = bits;

    memset(&dsbd, '\0', sizeof(dsbd));

    dsbd.dwSize = sizeof(DSBUFFERDESC);
    dsbd.dwFlags =
        DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN;
    dsbd.dwBufferBytes = size;
    dsbd.lpwfxFormat = (LPWAVEFORMATEX) & pcmwf;

    n = _sound_seq;

    if (dsound->lpVtbl->
        CreateSoundBuffer(dsound, &dsbd, &_sounds[n], NULL) != DS_OK) {
        fclose(f);
        return -2;
    }

    if (_sounds[n]->lpVtbl->Lock(_sounds[n], 0, size,
                                 (void **) &w1, &s1, (void **) &w2, &s2,
                                 0) != DS_OK) {
        fclose(f);
        return -3;
    }

    if (w1 != NULL)
        fread(w1, 1, s1, f);

    if (w2 != NULL)
        fread(w2, 1, s2, f);

    fclose(f);

    if (_sounds[n]->lpVtbl->Unlock(_sounds[n], w1, s1, w2, s2) != DS_OK)
        return -4;

    _sound_seq++;
    return n;
}