summaryrefslogtreecommitdiff
path: root/cwav.c
diff options
context:
space:
mode:
authorGravatar waker <wakeroid@gmail.com>2009-07-03 21:44:27 +0200
committerGravatar waker <wakeroid@gmail.com>2009-07-03 21:44:27 +0200
commitea3c1d9b0351effebcae6925650a0d5060103901 (patch)
tree94756ea3e5662292c1b196cfbdc2f509396a235d /cwav.c
initial proto
Diffstat (limited to 'cwav.c')
-rw-r--r--cwav.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/cwav.c b/cwav.c
new file mode 100644
index 00000000..ae131ae7
--- /dev/null
+++ b/cwav.c
@@ -0,0 +1,80 @@
+#include <string.h>
+#include <stdio.h>
+#include "codec.h"
+#include "cwav.h"
+
+typedef struct {
+ uint32_t riffMagic;
+ uint32_t size;
+ uint32_t waveMagic;
+ uint32_t fmtMagic;
+ uint32_t chunkSize;
+ uint16_t formatTag;
+ uint16_t channels;
+ uint32_t samplesPerSecond;
+ uint32_t bytesPerSecond;
+ uint16_t blockAlignment;
+ uint16_t bitsPerSample;
+ uint32_t dataMagic;
+ uint32_t dataSize;
+} wavHeader_t;
+
+static FILE *file;
+
+int cwav_init (const char *fname) {
+
+ file = fopen (fname, "rb");
+ if (!file) {
+ return -1;
+ }
+
+ wavHeader_t header;
+
+ // read WAV header
+ if (fread (&header, sizeof (header), 1, file) != sizeof (header))
+ {
+ printf ("WARNING: WAV header not found in %s\n", fname);
+ fclose (file);
+ file = NULL;
+ return -1;
+ }
+ cwav.info.bitsPerSample = header.bitsPerSample;
+ cwav.info.channels = header.channels;
+ cwav.info.dataSize = header.dataSize;
+ cwav.info.samplesPerSecond = header.samplesPerSecond;
+
+ return 0;
+}
+
+void
+cwav_free (void)
+{
+ if (file)
+ {
+ fclose (file);
+ file = NULL;
+ }
+}
+
+int
+cwav_read (char *bytes, int size) {
+ int bytesread = 0;
+ int first = 1;
+ for (;;) {
+ int bytesread = fread (bytes, size, 1, file);
+ if (bytesread == size) {
+ break;
+ }
+ size -= bytesread;
+ bytes += bytesread;
+ fseek (file, sizeof (wavHeader_t), SEEK_SET);
+ }
+ return 0;
+}
+
+codec_t cwav = {
+ .init = cwav_init,
+ .free = cwav_free,
+ .read = cwav_read
+};
+