• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/io_wav.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Matthias Ihrke   *
00003  *   ihrke@nld.ds.mpg.de
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "io_wav.h"
00022 #include <stdlib.h>
00023 
00028 void wavfile_print( FILE *out, WavFile *w ){
00029   fprintf( out, "WAV '%p'\n"
00030               " numchannels     = %d\n"
00031               " samplerate      = %d\n"
00032               " byterate        = %d\n"
00033               " blockalign      = %d\n"
00034               " bits_per_sample = %d\n"
00035               " data_size       = %d\n"
00036               " data            = %p\n",
00037               w, w->numchannels, w->samplerate, w->byterate, 
00038               w->blockalign, w->bits_per_sample, w->data_size,
00039               w->data );
00040 }
00041 
00045 void     wavfile_free ( WavFile *w ){
00046   if( w ){
00047      if( w->data )
00048         free( w->data );
00049      free( w );
00050   }
00051 }
00052 
00053 
00054 /* ------------- READER --------------------- */
00055 
00063 WavFile* wavfile_read( FILE *f ){
00064   uint32_t buf;
00065   WavFile *w;
00066   int stopflag=0;
00067 
00068   if( ffread( &buf, 1, 4, f ) &&  buf!=0x46464952 ){ /* "RIFF" */
00069      stopflag=1;
00070   }
00071   ffread( &buf, 1, 4, f ); /* trash */
00072   if( ffread( &buf, 1, 4, f ) &&  buf!=0x45564157 ){ /* 'WAVE' */
00073      stopflag=1;
00074   }
00075   if( ffread( &buf, 1, 4, f ) &&  buf!=0x20746d66 ){ /* 'fmt ' */
00076      stopflag=1;
00077   }
00078   
00079   if( stopflag ){
00080      fprintf( stderr, "No WAV-File\n" );
00081      return NULL;
00082   }
00083   if( ffread( &buf, 1, 4, f ) && buf!=16 ){ /* no compression */
00084      fprintf( stderr, "Cannot handle compression...\n" );
00085      return NULL;
00086   }
00087 
00088   w = (WavFile*) malloc( sizeof(WavFile) );
00089   ffread( w, 1, 16, f );
00090 
00091   if( ffread( &buf, 1, 4, f ) && buf!=0x61746164 ){ /* 'data' */
00092      fprintf( stderr, "No data\n" );
00093      return NULL;
00094   }
00095 
00096   if( !ffread( &(w->data_size), 1, 4, f ) )
00097      return NULL;
00098   
00099   w->data = (void*) malloc( w->data_size );
00100   ffread( w->data, 1, w->data_size, f );
00101 
00102   return w;
00103 }
00104 
00105 
00106 
00107 /* ------------- WRITER --------------------- */
00108 
00117 int      wavfile_write( WavFile *w, const char *fname ){
00118   FILE *f;
00119   uint32_t buf[5];
00120   uint16_t buf2;
00121   if( !(f = fopen( fname, "w" )) ){
00122      fprintf( stderr, "Could not open file '%s'\n", fname );
00123      return -1;
00124   }
00125 
00126   buf[0] = 0x46464952; /* RIFF */
00127   buf[1] = (uint32_t)(36+w->data_size);
00128   buf[2] = 0x45564157; /* WAVE */
00129   buf[3] = 0x20746d66; /* fmt */
00130   buf[4] = 16;         /* (PCM) */
00131   fwrite( buf, 1, 5*4, f );
00132   buf2   = 1;
00133   fwrite( &buf2, 1,   2, f );
00134   fwrite( &(w->numchannels), 1,  2, f ); 
00135   fwrite( &(w->samplerate) , 1,  4, f ); 
00136   fwrite( &(w->byterate),    1,  4, f ); 
00137   fwrite( &(w->blockalign),  1,  2, f ); 
00138   fwrite( &(w->bits_per_sample), 1,  2, f ); 
00139   buf[0] = 0x61746164; /* data */
00140   fwrite( buf, 1,   4, f );
00141   fwrite( &(w->data_size), 1, 4, f );
00142   fwrite( w->data, 1, w->data_size, f );
00143   fclose( f );
00144 
00145   return 0;
00146 }

Generated on Fri Jun 25 2010 14:10:19 for libeegtools by  doxygen 1.7.0