Esempio n. 1
0
VGMSTREAM * init_vgmstream_str_snds(STREAMFILE *streamFile) {
    VGMSTREAM * vgmstream = NULL;
    char filename[1024];

    int channel_count;
    int loop_flag = 0;
    off_t SHDR_offset = -1;
    int FoundSHDR = 0;
    int CTRL_size = -1;

    size_t file_size;

    /* check extension, case insensitive */
    streamFile->get_name(streamFile,filename,sizeof(filename));
    if (strcasecmp("str",filename_extension(filename))) goto fail;

    /* check for opening CTRL or SNDS chunk */
    if (read_32bitBE(0x0,streamFile) != 0x4354524c &&   /* CTRL */
        read_32bitBE(0x0,streamFile) != 0x534e4453 &&   // SNDS
		read_32bitBE(0x0,streamFile) != 0x53484452)     // SHDR    
        goto fail;

    file_size = get_streamfile_size(streamFile);

    /* scan chunks until we find a SNDS containing a SHDR */
    {
        off_t current_chunk;

        current_chunk = 0;

        while (!FoundSHDR && current_chunk < file_size) {
            if (current_chunk < 0) goto fail;

            if (current_chunk+read_32bitBE(current_chunk+4,streamFile) >=
                    file_size) goto fail;

            switch (read_32bitBE(current_chunk,streamFile)) 
			{
                case 0x4354524C: /* CTRL */
                    /* to distinguish between styles */
                    CTRL_size = read_32bitBE(current_chunk+4,streamFile);					
					break;
                case 0x534e4453: /* SNDS */
                    switch (read_32bitBE(current_chunk+16,streamFile)) 
					{
						case 0x53484452: /* SHDR */
							FoundSHDR = 1;
							SHDR_offset = current_chunk+16;
						break;
						
						default:
							break;
                    }
                    break;
                case 0x53484452: /* SHDR */
                    switch (read_32bitBE(current_chunk+0x7C, streamFile)) 
					{
						case 0x4354524C: /* CTRL */
							// to distinguish between styles 
							CTRL_size = read_32bitBE(current_chunk + 0x80, streamFile);							
							break;
						
						default:
							break;
                    }
					break;
				default:
                    /* ignore others for now */
                    break;
            }

            current_chunk += read_32bitBE(current_chunk+4,streamFile);
        }
    }

    if (!FoundSHDR) goto fail;

    /* details */
    channel_count = read_32bitBE(SHDR_offset+0x20,streamFile);
    loop_flag = 0;

    /* build the VGMSTREAM */

    vgmstream = allocate_vgmstream(channel_count,loop_flag);
    if (!vgmstream) goto fail;

    /* fill in the vital statistics */
    if ((CTRL_size == 0x1C) ||
		(CTRL_size == 0x0B) ||
		(CTRL_size == -1))
	{
        vgmstream->num_samples =
            read_32bitBE(SHDR_offset+0x2c,streamFile)-1; /* sample count? */
    } 
	else {
        vgmstream->num_samples =
            read_32bitBE(SHDR_offset+0x2c,streamFile)   /* frame count? */
            * 0x10;
    }

	vgmstream->num_samples/=vgmstream->channels;

    vgmstream->sample_rate = read_32bitBE(SHDR_offset+0x1c,streamFile);
    switch (read_32bitBE(SHDR_offset+0x24,streamFile)) {
        case 0x53445832:    /* SDX2 */
            if (channel_count > 1) {
                vgmstream->coding_type = coding_SDX2_int;
                vgmstream->interleave_block_size = 1;
            } else
                vgmstream->coding_type = coding_SDX2;
            break;
        default:
            goto fail;
    }
    vgmstream->layout_type = layout_str_snds_blocked;
    vgmstream->meta_type = meta_STR_SNDS;

    /* channels and loop flag are set by allocate_vgmstream */
    if (loop_flag) {
        /* just guessin', no way to set loop flag anyway */
        vgmstream->loop_start_sample = 0;
        vgmstream->loop_end_sample = vgmstream->num_samples;
    }

    /* open the file for reading by each channel */
    {
        int i;
        vgmstream->ch[0].streamfile = streamFile->open(streamFile,filename,
                STREAMFILE_DEFAULT_BUFFER_SIZE);
        if (!vgmstream->ch[0].streamfile) goto fail;
        for (i=0;i<channel_count;i++) {
            vgmstream->ch[i].streamfile = vgmstream->ch[0].streamfile;
        }
    }

    /* start me up */
    str_snds_block_update(0,vgmstream);

    return vgmstream;

    /* clean up anything we may have opened */
fail:
    if (vgmstream) close_vgmstream(vgmstream);
    return NULL;
}
Esempio n. 2
0
void render_vgmstream_blocked(sample * buffer, int32_t sample_count, VGMSTREAM * vgmstream) {
    int samples_written=0;

    int frame_size = get_vgmstream_frame_size(vgmstream);
    int samples_per_frame = get_vgmstream_samples_per_frame(vgmstream);
    int samples_this_block;

    if (frame_size == 0) {
        /* assume 4 bit */
        /* TODO: get_vgmstream_frame_size() really should return bits... */
        samples_this_block = vgmstream->current_block_size * 2 * samples_per_frame;
    } else {
        samples_this_block = vgmstream->current_block_size / frame_size * samples_per_frame;
    }

    while (samples_written<sample_count) {
        int samples_to_do;

        if (vgmstream->loop_flag && vgmstream_do_loop(vgmstream)) {
            if (frame_size == 0) {
                samples_this_block = vgmstream->current_block_size * 2 * samples_per_frame;
            } else {
                samples_this_block = vgmstream->current_block_size / frame_size * samples_per_frame;
            }
            continue;
        }

        samples_to_do = vgmstream_samples_to_do(samples_this_block, samples_per_frame, vgmstream);

        if (samples_written+samples_to_do > sample_count)
            samples_to_do=sample_count-samples_written;

        if (vgmstream->current_block_offset>=0)
            decode_vgmstream(vgmstream, samples_written, samples_to_do, buffer);
        else {
            int i;
            /* we've run off the end! */
            for (i=samples_written*vgmstream->channels;
                    i<(samples_written+samples_to_do)*vgmstream->channels; i++)
                buffer[i]=0;
        }

        samples_written += samples_to_do;
        vgmstream->current_sample += samples_to_do;
        vgmstream->samples_into_block+=samples_to_do;

        if (vgmstream->samples_into_block==samples_this_block) {
            switch (vgmstream->layout_type) {
            case layout_ast_blocked:
                ast_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_halpst_blocked:
                if (vgmstream->next_block_offset>=0)
                    halpst_block_update(vgmstream->next_block_offset,vgmstream);
                else
                    vgmstream->current_block_offset=-1;
                break;
            case layout_xa_blocked:
                xa_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_ea_blocked:
                ea_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_eacs_blocked:
                eacs_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_caf_blocked:
                caf_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_wsi_blocked:
                wsi_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_str_snds_blocked:
                str_snds_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_ws_aud_blocked:
                ws_aud_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_matx_blocked:
                matx_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_de2_blocked:
                de2_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_emff_ps2_blocked:
                emff_ps2_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_emff_ngc_blocked:
                emff_ngc_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_gsb_blocked:
                gsb_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_vs_blocked:
                vs_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_xvas_blocked:
                xvas_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_thp_blocked:
                thp_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            case layout_filp_blocked:
                filp_block_update(vgmstream->next_block_offset,vgmstream);
                break;
            default:
                break;
            }

            /* for VBR these may change */
            frame_size = get_vgmstream_frame_size(vgmstream);
            samples_per_frame = get_vgmstream_samples_per_frame(vgmstream);

            if (frame_size == 0) {
                samples_this_block = vgmstream->current_block_size * 2 * samples_per_frame;
            } else {
                samples_this_block = vgmstream->current_block_size / frame_size * samples_per_frame;
            }
            vgmstream->samples_into_block=0;
        }

    }
}