diff options
author | waker <wakeroid@gmail.com> | 2009-07-03 21:44:27 +0200 |
---|---|---|
committer | waker <wakeroid@gmail.com> | 2009-07-03 21:44:27 +0200 |
commit | ea3c1d9b0351effebcae6925650a0d5060103901 (patch) | |
tree | 94756ea3e5662292c1b196cfbdc2f509396a235d /cvorbis.c |
initial proto
Diffstat (limited to 'cvorbis.c')
-rw-r--r-- | cvorbis.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/cvorbis.c b/cvorbis.c new file mode 100644 index 00000000..7ce772e1 --- /dev/null +++ b/cvorbis.c @@ -0,0 +1,75 @@ +#include <vorbis/codec.h> +#include <vorbis/vorbisfile.h> +#include <string.h> +#include <stdio.h> +#include "codec.h" +#include "cvorbis.h" + +static FILE *file; +static OggVorbis_File vorbis_file; +static vorbis_info *vi; +static int cur_bit_stream; + +int cvorbis_init (const char *fname) { + file = NULL; + vi = NULL; + cur_bit_stream = -1; + + file = fopen (fname, "rb"); + if (!file) { + return -1; + } + + memset (&cvorbis.info, 0, sizeof (fileinfo_t)); + ov_open (file, &vorbis_file, NULL, 0); + vi = ov_info (&vorbis_file, -1); + cvorbis.info.bitsPerSample = 16; + cvorbis.info.dataSize = ov_pcm_total (&vorbis_file, -1) * vi->channels * 2; + cvorbis.info.channels = vi->channels; + cvorbis.info.samplesPerSecond = vi->rate; + return 0; +} + +void +cvorbis_free (void) { + fclose (file); + ov_clear (&vorbis_file); + vi = NULL; +} + +int +cvorbis_read (char *bytes, int size) +{ + if (!file) + return -1; + for (;;) + { + // read ogg + long ret=ov_read (&vorbis_file, bytes, size, 0, 2, 1, &cur_bit_stream); + if (ret < 0) + { + printf ("WARNING: ogg vorbis decoder tells error %x\n", ret); + memset (bytes, 0, size); + return -1; + } + else if (ret < size) + { + if (ret == 0) { + ov_raw_seek (&vorbis_file, 0); + } + size -= ret; + bytes += ret; + } + else { + break; + } + } + return 0; +} + +codec_t cvorbis = { + .init = cvorbis_init, + .free = cvorbis_free, + .read = cvorbis_read +}; + |