#include "meta.h" #include "../layout/layout.h" #include "../util.h" /* Resource Interchange File Format */ /* only the bare minimum needed to read PCM wavs */ VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) { VGMSTREAM * vgmstream = NULL; char filename[260]; off_t file_size = -1; int channel_count = 0; int sample_count = 0; int sample_rate = 0; int coding_type = -1; off_t start_offset = -1; int interleave = -1; int loop_flag = 0; int32_t loop_start = -1; int32_t loop_end = -1; uint32_t riff_size; uint32_t data_size = 0; int FormatChunkFound = 0; int DataChunkFound = 0; /* check extension, case insensitive */ streamFile->get_name(streamFile,filename,sizeof(filename)); if (strcasecmp("wav",filename_extension(filename))) goto fail; /* check header */ if ((uint32_t)read_32bitBE(0,streamFile)!=0x52494646) /* "RIFF" */ goto fail; /* check for WAVE form */ if ((uint32_t)read_32bitBE(8,streamFile)!=0x57415645) /* "WAVE" */ goto fail; riff_size = read_32bitLE(4,streamFile); file_size = get_streamfile_size(streamFile); /* check for tructated RIFF */ if (file_size < riff_size+8) goto fail; /* read through chunks to verify format and find metadata */ { off_t current_chunk = 0xc; /* start with first chunk */ while (current_chunk < file_size) { uint32_t chunk_type = read_32bitBE(current_chunk,streamFile); off_t chunk_size = read_32bitLE(current_chunk+4,streamFile); if (current_chunk+8+chunk_size > file_size) goto fail; switch(chunk_type) { case 0x666d7420: /* "fmt " */ /* only one per file */ if (FormatChunkFound) goto fail; FormatChunkFound = 1; sample_rate = read_32bitLE(current_chunk+0x0c,streamFile); channel_count = read_16bitLE(current_chunk+0x0a,streamFile); switch (read_16bitLE(current_chunk+0x8,streamFile)) { case 1: /* PCM */ switch (read_16bitLE(current_chunk+0x16,streamFile)) { case 16: coding_type = coding_PCM16LE; interleave = 2; break; case 8: coding_type = coding_PCM8; interleave = 1; break; default: goto fail; } break; default: goto fail; } break; case 0x64617461: /* data */ /* at most one per file */ if (DataChunkFound) goto fail; DataChunkFound = 1; start_offset = current_chunk + 8; data_size = chunk_size; break; default: /* ignorance is bliss */ break; } current_chunk += 8+chunk_size; } } if (!FormatChunkFound || !DataChunkFound) goto fail; switch (coding_type) { case coding_PCM16LE: sample_count = data_size/2/channel_count; break; case coding_PCM8: sample_count = data_size/channel_count; break; } /* build the VGMSTREAM */ vgmstream = allocate_vgmstream(channel_count,loop_flag); if (!vgmstream) goto fail; /* fill in the vital statistics */ vgmstream->num_samples = sample_count; vgmstream->sample_rate = sample_rate; vgmstream->coding_type = coding_type; if (channel_count > 1) vgmstream->layout_type = layout_interleave; else vgmstream->layout_type = layout_none; vgmstream->interleave_block_size = interleave; vgmstream->loop_start_sample = loop_start; vgmstream->loop_end_sample = loop_end; vgmstream->meta_type = meta_RIFF_WAVE; /* open the file, set up 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;ich[i].streamfile = vgmstream->ch[0].streamfile; vgmstream->ch[i].offset = vgmstream->ch[i].channel_start_offset = start_offset+i*interleave; } } return vgmstream; /* clean up anything we may have opened */ fail: if (vgmstream) close_vgmstream(vgmstream); return NULL; }