chiark / gitweb /
Disobedience: basic support for required/prohibited tags.
[disorder] / lib / wav.c
index c7d4b9397fa42f9b3ff696adf95e123989aa2a8a..fe7ffbbf0443289018bf2852dbc5c364f5f5e764 100644 (file)
--- a/lib/wav.c
+++ b/lib/wav.c
@@ -2,20 +2,18 @@
  * This file is part of DisOrder
  * Copyright (C) 2007 Richard Kettlewell
  *
- * This program is free software; you can redistribute it and/or modify
+ * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 /** @file lib/wav.c
  * @brief WAV file support
  * orderings of different channels.
  */
 
-#include <config.h>
-#include "types.h"
+#include "common.h"
 
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <string.h>
 
 #include "log.h"
 #include "wav.h"
@@ -132,9 +128,8 @@ int wav_init(struct wavfile *f, const char *path) {
   off_t where;
   
   memset(f, 0, sizeof *f);
-  f->fd = -1;
   f->data = -1;
-  if((f->fd = open(path, O_RDONLY)) < 0) goto error_errno;
+  if(hreader_init(path, f->input)) goto error_errno;
   /* Read the file header
    *
    *  offset  size  meaning
@@ -142,7 +137,7 @@ int wav_init(struct wavfile *f, const char *path) {
    *  04      4     length of rest of file
    *  08      4     'WAVE'
    * */
-  if((n = pread(f->fd, header, 12, 0)) < 0) goto error_errno;
+  if((n = hreader_pread(f->input, header, 12, 0)) < 0) goto error_errno;
   else if(n < 12) goto einval;
   if(strncmp(header, "RIFF", 4) || strncmp(header + 8, "WAVE", 4))
     goto einval;
@@ -155,7 +150,7 @@ int wav_init(struct wavfile *f, const char *path) {
      *  00      4     chunk ID
      *  04      4     length of rest of chunk
      */
-    if((n = pread(f->fd, header, 8, where)) < 0) goto error_errno;
+    if((n = hreader_pread(f->input, header, 8, where)) < 0) goto error_errno;
     else if(n < 8) goto einval;
     if(!strncmp(header,"fmt ", 4)) {
       /* This is the format chunk
@@ -172,7 +167,8 @@ int wav_init(struct wavfile *f, const char *path) {
        *  18      ?     extra undocumented rubbish
        */
       if(get32(header + 4) < 16) goto einval;
-      if((n = pread(f->fd, header + 8, 16, where + 8)) < 0) goto error_errno;
+      if((n = hreader_pread(f->input, header + 8, 16, where + 8)) < 0)
+        goto error_errno;
       else if(n < 16) goto einval;
       f->channels = get16(header + 0x0A);
       f->rate = get32(header + 0x0C);
@@ -201,13 +197,7 @@ error:
 
 /** @brief Close a WAV file */
 void wav_destroy(struct wavfile *f) {
-  if(f) {
-    const int save_errno = errno;
-
-    if(f->fd >= 0)
-      close(f->fd);
-    errno = save_errno;
-  }
+  hreader_close(f->input);
 }
 
 /** @brief Visit all the data in a WAV file
@@ -231,7 +221,7 @@ int wav_data(struct wavfile *f,
     size_t want = (off_t)sizeof buffer > left ? (size_t)left : sizeof buffer;
 
     want -= want % bytes_per_frame;
-    if((n = pread(f->fd, buffer, want, where)) < 0) return errno;
+    if((n = hreader_pread(f->input, buffer, want, where)) < 0) return errno;
     if((size_t)n < want) return EINVAL;
     if((err = callback(f, buffer, n, u))) return err;
     where += n;