Exemple #1
0
void *
sol_util_load_file_raw(const int fd, size_t *size)
{
    struct stat st;
    int saved_errno;
    char *tmp = NULL;
    char *buffer = NULL;
    size_t buffer_size = 0;
    ssize_t ret;

    if (fd < 0 || !size)
        return NULL;

    *size = 0;
    if (fstat(fd, &st) >= 0 && st.st_size) {
        buffer_size = st.st_size;
        buffer = malloc(buffer_size);
        if (!buffer)
            goto err;

        ret = _fill_buffer(fd, buffer, buffer_size, size);
        if (ret <= 0)
            goto end;
    }

    do {
        buffer_size += CHUNK_SIZE;
        tmp = realloc(buffer, buffer_size);
        if (!tmp)
            goto err;
        buffer = tmp;

        ret = _fill_buffer(fd, buffer + *size, CHUNK_SIZE, size);
    } while (ret > 0);

end:
    if (ret < 0 || !*size)
        goto err;

    if (*size < buffer_size) {
        tmp = realloc(buffer, *size);
        if (tmp)
            buffer = tmp;
    }

    return buffer;

err:
    saved_errno = errno;
    free(buffer);
    errno = saved_errno;
    *size = 0;
    return NULL;
}
    );
    alBufferData(
        buffer,
        AL_FORMAT_MONO16,
        (void*)&buffer_data[0],
        sizeof(int16_t) * effect->_buffer_size,
        44100
    );
    return left;
}

inline void
sound_server::_start_sound(playing_sound &ps)
{
    BOOST_FOREACH (ALuint buffer, ps.effect->_buffers) {
        ps.buffers_left = _fill_buffer(ps.effect, buffer);
        alSourceQueueBuffers(ps.source, 1, &buffer);
        if (!ps.buffers_left) break;
    }
}

inline void
sound_server::_stop_sound(playing_sound &ps)
{
    alSourceStop(ps.source);
    ALint processed;
    alGetSourcei(ps.source, AL_BUFFERS_PROCESSED, &processed);
    while (processed-- > 0) {
        ALuint buffer;
        alSourceUnqueueBuffers(ps.source, 1, &buffer);
    }