void test_get_frame_short_interleaved(FILE *g, char *filename) { short sbuffer[8000]; int n, error; stb_vorbis *v = stb_vorbis_open_filename(filename, &error, NULL); if (!v) stb_fatal("Couldn't open {%s}", filename); show_info(v); while (0 != (n=stb_vorbis_get_frame_short_interleaved(v, 2, sbuffer, 8000))) { fwrite(sbuffer, 2, n*2, g); } stb_vorbis_close(v); }
static unsigned char *loadSound_ogg( String path,int *length,int *channels,int *format,int *hertz ){ FILE *f=fopenFile( path,"rb" ); if( !f ) return 0; int error; stb_vorbis *v=stb_vorbis_open_file( f,0,&error,0 ); if( !v ){ fclose( f ); return 0; } stb_vorbis_info info=stb_vorbis_get_info( v ); int limit=info.channels*4096; int offset=0,data_len=0,total=limit; short *data=(short*)malloc( total*sizeof(short) ); for(;;){ int n=stb_vorbis_get_frame_short_interleaved( v,info.channels,data+offset,total-offset ); if( !n ) break; data_len+=n; offset+=n*info.channels; if( offset+limit>total ){ total*=2; data=(short*)realloc( data,total*sizeof(short) ); } } *length=data_len; *channels=info.channels; *format=2; *hertz=info.sample_rate; stb_vorbis_close(v); fclose( f ); return (unsigned char*)data; }
int stb_vorbis_decode_memory(unsigned char *input_data, int input_len, int *channels, short **output) { int data_len, offset, total, limit, error; short *data; stb_vorbis *v = stb_vorbis_open_memory(input_data, input_len, &error, NULL); if (v == NULL) return -1; limit = v->channels * 4096; *channels = v->channels; offset = data_len = 0; total = limit; data = malloc(total * sizeof(*data)); if (data == NULL) { stb_vorbis_close(v); return -2; } for (;;) { int n = stb_vorbis_get_frame_short_interleaved(v, v->channels, data+offset, total-offset); if (n == 0) break; data_len += n; offset += n * v->channels; if (offset + limit > total) { short *data2; total *= 2; data2 = realloc(data, total * sizeof(*data)); if (data2 == NULL) { free(data); stb_vorbis_close(v); return -2; } data = data2; } } *output = data; return data_len; }