00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef LIBSNDFILEWRAPPER_H_
00024 #define LIBSNDFILEWRAPPER_H_
00025
00026
00027
00028
00029
00030
00031 #include <sndfile.h>
00032 #include <string.h>
00033
00034
00035 #ifdef DEBUG_LSFW
00036 #include <iostream>
00037 #endif
00038
00039
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
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
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
00082 void closeSndFile(SNDFILE* file){
00083 sf_close(file);
00084 }
00085
00086
00087
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
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
00102 void writeWav(const char *name, int cnt, short int *output, int freq, int channelCnt, int bitCnt){
00103
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
00119 #ifdef DEBUG_LSFW
00120 printf("written : %d\n",written);
00121 #endif
00122 closeSndFile(file);
00123 }
00124
00125
00126
00127
00128 short int* readWav(const char *name, int *cnt, int *freq, int *channels){
00129
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