test now generates a .wav output

git-svn-id: https://vgmstream.svn.sourceforge.net/svnroot/vgmstream@7 51a99a44-fe44-0410-b1ba-c3e57ba2b86b
This commit is contained in:
halleyscometsw 2008-02-04 10:34:18 +00:00
parent 52e1c97bfe
commit 8432b18f41
3 changed files with 72 additions and 2 deletions

View File

@ -1,5 +1,6 @@
#include <string.h>
#include "util.h"
#include "streamtypes.h"
int check_sample_rate(int32_t sr) {
return !(sr<1000 || sr>96000);
@ -55,3 +56,61 @@ void interleave_stereo(sample * buffer, int32_t sample_count) {
} while (tomove != sample_count);
}
*/
void put_16bitLE(uint8_t * buf, int16_t i) {
buf[0] = i;
buf[1] = i >> 8;
}
void put_32bitLE(uint8_t * buf, int32_t i) {
buf[0] = i;
buf[1] = i >> 8;
buf[2] = i >> 16;
buf[3] = i >> 24;
}
/* make a header for PCM .wav */
/* buffer must be 0x2c bytes */
void make_wav_header(uint8_t * buf, int32_t sample_count, int32_t sample_rate, int channels) {
size_t bytecount;
bytecount = sample_count*channels*sizeof(sample);
/* RIFF header */
memcpy(buf+0, "RIFF", 4);
/* size of RIFF */
put_32bitLE(buf+4, bytecount+0x2c-8);
/* WAVE header */
memcpy(buf+8, "WAVE", 4);
/* WAVE fmt chunk */
memcpy(buf+0xc, "fmt ", 4);
/* size of WAVE fmt chunk */
put_32bitLE(buf+0x10, 0x10);
/* compression code 1=PCM */
put_16bitLE(buf+0x14, 1);
/* channel count */
put_16bitLE(buf+0x16, channels);
/* sample rate */
put_32bitLE(buf+0x18, sample_rate);
/* bytes per second */
put_32bitLE(buf+0x1c, sample_rate*channels*sizeof(sample));
/* block align */
put_16bitLE(buf+0x20, channels*sizeof(sample));
/* significant bits per sample */
put_16bitLE(buf+0x22, sizeof(sample)*8);
/* PCM has no extra format bytes, so we don't even need to specify a count */
/* WAVE data chunk */
memcpy(buf+0x24, "data", 4);
/* size of WAVE data chunk */
put_32bitLE(buf+0x28, bytecount);
}

View File

@ -25,6 +25,10 @@ static inline int32_t get_32bitLE(uint8_t * p) {
return (p[0]) | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
}
void put_16bitLE(uint8_t * buf, int16_t i);
void put_32bitLE(uint8_t * buf, int32_t i);
/* signed nibbles come up a lot */
static int nibble_to_int[16] = {0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1};
@ -52,4 +56,8 @@ static inline int clamp16(int32_t val) {
return val;
}
/* make a header for PCM .wav */
/* buffer must be 0x2c bytes */
void make_wav_header(uint8_t * buf, int32_t sample_count, int32_t sample_rate, int channels);
#endif

View File

@ -8,7 +8,7 @@ int main(int argc, char ** argv) {
sample buf[BUFSIZE*2];
int32_t len;
int i;
FILE * outfile = fopen("dump.bin","wb");
FILE * outfile = fopen("dump.wav","wb");
if (argc!=2) {printf("1 arg\n"); return 1;}
@ -19,7 +19,6 @@ int main(int argc, char ** argv) {
return 1;
}
printf("samplerate %d Hz\n",s->sample_rate);
printf("channels: %d\n",s->channels);
if (s->loop_flag) {
@ -31,6 +30,10 @@ int main(int argc, char ** argv) {
len = get_vgmstream_play_samples(2.0,10.0,s);
printf("samples to play %d (%lf seconds)\n",len,(double)len/s->sample_rate);
/* slap on a .wav header */
make_wav_header((uint8_t*)buf, len, s->sample_rate, s->channels);
fwrite(buf,1,0x2c,outfile);
for (i=0;i<len;i+=BUFSIZE) {
int toget=BUFSIZE;
if (i+BUFSIZE>len) toget=len-i;