Main Page   File List  

libSndFileWrapper.H

00001 /* Copyright 2001 Matt Flax <flatmax@ieee.org>
00002    This file is part of MFFM Time Scale Modification for Audio.
00003 
00004    MFFM Time Scale Modification for Audio is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 2 of the License, or
00007    (at your option) any later version.
00008    
00009    MFFM Time Scale Modification for Audio is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013    
00014    A copy of the GPL license is available here :
00015    http://www.gnu.org/licenses/licenses.html#GPL
00016    http://www.gnu.org/licenses/gpl.html
00017  */
00018 
00019 /*
00020 Version 1.1
00021 */
00022 
00023 #ifndef LIBSNDFILEWRAPPER_H_
00024 #define LIBSNDFILEWRAPPER_H_
00025 /*
00026   C++ Wrapper for the libsndfile library
00027   http://www.zip.com.au/~erikd/libsndfile/
00028   Author: Matt Flax <flatmax@ieee.org>
00029 */
00030 
00031 #include <sndfile.h>
00032 #include <string.h>
00033 
00034 /*#define DEBUG_LSFW*/
00035 #ifdef DEBUG_LSFW
00036 #include <iostream>
00037 #endif
00038 
00039 /** Opens a WAV file for writing, on failure returns NULL */
00040 SNDFILE* openWavWrite(SF_INFO *sfInfo, const char *name, int freq, int channelCnt, int bitCnt){
00041   printf("libSndFileWrapper::openWavWrite : opening %s\n %dHz\t%d channel(s)\t%d bits per word\n", name, freq, channelCnt, bitCnt);
00042     sfInfo->samplerate=freq;
00043     sfInfo->channels=channelCnt;
00044     /*    sfInfo->pcmbitwidth=bitCnt; from version 0*/
00045     switch (bitCnt){
00046     case 8:
00047       sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_S8);
00048     case 16:
00049       sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_16);
00050     case 24:
00051       sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_24);
00052     case 32:
00053       sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_32);
00054     default:
00055       sfInfo->format=(SF_FORMAT_WAV | SF_FORMAT_PCM_16);     
00056     }
00057 
00058     return sf_open(name, SFM_WRITE, sfInfo);
00059 }
00060 
00061 /** Opens an AIFF sound file for writing, on failure returns NULL */
00062 SNDFILE* openAiffWrite(SF_INFO *sfInfo, const char *name, int freq, int channelCnt, int bitCnt){
00063   printf("libSndFileWrapper::openAiffWrite : opening %s\n %dHz\t%d channel(s)\t%d bits per word\n", name, freq, channelCnt, bitCnt);
00064   sfInfo->samplerate=freq;
00065     sfInfo->channels=channelCnt;
00066     switch (bitCnt){
00067     case 8:
00068       sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_S8);
00069     case 16:
00070       sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_16);
00071     case 24:
00072       sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_24);
00073     case 32:
00074       sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_32);
00075     default:
00076       sfInfo->format=(SF_FORMAT_AIFF | SF_FORMAT_PCM_16);     
00077     }
00078     return sf_open(name, SFM_WRITE, sfInfo);
00079 }
00080 
00081 /**Closes a sound file*/
00082 void closeSndFile(SNDFILE* file){
00083   sf_close(file);
00084 }
00085 
00086 /** writes N samples to file
00087    returns number written */
00088 int writeNSndFile(SNDFILE *file, int N, short int *output){
00089   int written;
00090 #ifdef DEBUG_LSFW
00091   printf("about to write : %d\n",N);
00092 #endif
00093   /*sfInfo->samples+=N;*/
00094   written=sf_write_short(file, output, N);
00095 #ifdef DEBUG_LSFW
00096   printf("written : %d\n",written);
00097 #endif
00098   return written;
00099 }
00100 
00101 /** writes an entire wav file */
00102 void writeWav(const char *name, int cnt, short int *output, int freq, int channelCnt, int bitCnt){
00103   /*Write out to file ...*/
00104   int written;
00105   SF_INFO sfInfo;
00106   memset(&sfInfo, 0, sizeof (sfInfo)) ;
00107   SNDFILE *file=openWavWrite(&sfInfo, name, freq, channelCnt, bitCnt);
00108   if (file==NULL){
00109     perror("couldn't open output file\n");
00110     sf_perror(file);
00111     exit(-1);
00112   }
00113   sfInfo.frames=cnt;
00114 #ifdef DEBUG_LSFW
00115   printf("about to write : %d\n",cnt);
00116 #endif
00117   written=sf_write_short(file, output, channelCnt*cnt);
00118   /*  printf("written : %d\n",written);*/
00119 #ifdef DEBUG_LSFW
00120   printf("written : %d\n",written);
00121 #endif
00122   closeSndFile(file);
00123 }
00124 
00125 /** Read an entire WAV file, allocating the memory array returned.
00126     You must de-allocate yourself.
00127 */
00128 short int* readWav(const char *name, int *cnt, int *freq, int *channels){
00129   /*Read from file ...*/
00130   short int *input;
00131   int readn;
00132   SF_INFO sfinfo;
00133   sfinfo.format=0;
00134   SNDFILE *file=sf_open(name, SFM_READ,&sfinfo);
00135   if (file==NULL){
00136     perror("couldn't open input file\n");
00137     sf_perror(file);
00138     exit(-1);
00139   }
00140 
00141   *cnt=sfinfo.frames;
00142   *freq=sfinfo.samplerate;
00143   *channels=sfinfo.channels;
00144 
00145   input=(short int *)malloc(sizeof(short int)*(*cnt)*(*channels));
00146   if (!input){
00147     perror("readWav:: input malloc error - out of memory ?");
00148     printf("\t requested %d bytes",sizeof(short int)*(*cnt)*(*channels));
00149     exit(-1);
00150   }
00151 
00152 #ifdef DEBUG_LSFW
00153   printf("about to read : %d\n",*cnt);
00154 #endif
00155   readn=sf_read_short(file, input, (*cnt)*(*channels)) ;
00156 #ifdef DEBUG_LSFW
00157   printf("read : %d\n",readn);
00158 #endif
00159   closeSndFile(file);
00160   return input;
00161 }
00162 #endif /*LIBSNDFILEWRAPPER_H_*/

Generated on Fri Aug 22 10:28:37 2003 for MFFM libsndfile wrapper by doxygen1.2.18