annotate tools/audio_processor_test/null/checker/SndfileWavUtil.cpp @ 55:d0db18cdeacc pygar svn.56

[svn r56] added submodule
author punk
date Sun, 09 May 2010 17:47:21 -0400
parents 90197e3375e2
children
rev   line source
rlm@23 1 #include <stdlib.h>
rlm@23 2 #include <string.h>
rlm@23 3 #include <errno.h>
rlm@23 4 #include <math.h>
rlm@23 5 #include <sndfile.h>
rlm@23 6 #include "SndfileWavUtil.h"
rlm@23 7
rlm@23 8 void
rlm@23 9 generate_wav(const char * pcmfilename, const char * samplewavfilename, const char * outputwavfilename)
rlm@23 10 {
rlm@23 11 char outfilename[2048];
rlm@23 12 SNDFILE * outfile ;
rlm@23 13 SNDFILE * wavfile ;
rlm@23 14 SNDFILE * pcmfile ;
rlm@23 15 SF_INFO wavinfo ;
rlm@23 16 SF_INFO pcminfo ;
rlm@23 17 int buff;
rlm@23 18 SF_INSTRUMENT inst ;
rlm@23 19
rlm@23 20 memset (&wavinfo, 0, sizeof (wavinfo)) ;
rlm@23 21
rlm@23 22
rlm@23 23 wavfile = sf_open(samplewavfilename, SFM_READ, &wavinfo);
rlm@23 24
rlm@23 25 if (wavfile == NULL){
rlm@23 26 printf ("\nERROR : Not able to open wav file named '%s' : %s/\n", samplewavfilename, sf_strerror (NULL)) ;
rlm@23 27 exit (1) ;
rlm@23 28 } ;
rlm@23 29
rlm@23 30 printf("WAV format: %x\n", wavinfo.format);
rlm@23 31
rlm@23 32 if (!((wavinfo.format & SF_FORMAT_PCM_16) && (wavinfo.channels == 1) &&
rlm@23 33 (wavinfo.format & SF_FORMAT_WAV))){
rlm@23 34 printf("\nERROR : .wav file must be SF_FORMAT_PCM_16 in mono\n");
rlm@23 35 }
rlm@23 36
rlm@23 37 pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
rlm@23 38 pcminfo.samplerate = wavinfo.samplerate;
rlm@23 39 pcminfo.channels = wavinfo.channels;
rlm@23 40
rlm@23 41 pcmfile = sf_open(pcmfilename, SFM_READ, &pcminfo);
rlm@23 42
rlm@23 43 if (pcmfile == NULL){
rlm@23 44 printf ("\nERROR : Not able to open pcm file named '%s' : %s/\n", pcmfilename, sf_strerror (NULL)) ;
rlm@23 45 exit (1) ;
rlm@23 46 } ;
rlm@23 47
rlm@23 48
rlm@23 49
rlm@23 50 outfile = sf_open(outputwavfilename, SFM_WRITE, &wavinfo);
rlm@23 51
rlm@23 52 memset (&inst, 0, sizeof (inst)) ;
rlm@23 53
rlm@23 54 for(int i = SF_STR_FIRST; i <= SF_STR_LAST; i = i + 1) {
rlm@23 55 const char * str = sf_get_string(wavfile,i);
rlm@23 56 if(str != NULL) {
rlm@23 57 sf_set_string(outfile,i,str);
rlm@23 58 }
rlm@23 59 }
rlm@23 60
rlm@23 61 if (outfile == NULL){
rlm@23 62 printf ("\nERROR : Not able to create wav file named '%s' : %s/\n", outfilename, sf_strerror (NULL)) ;
rlm@23 63 exit (1) ;
rlm@23 64 } ;
rlm@23 65
rlm@23 66 while(sf_read_int(pcmfile, &buff, 1) == 1){
rlm@23 67 if(sf_write_int(outfile, &buff, 1) != 1){
rlm@23 68 printf("\nERROR : unable to write to '%s' : %s/\n", outfilename, sf_strerror(NULL));
rlm@23 69 }
rlm@23 70 }
rlm@23 71
rlm@23 72 sf_close (wavfile) ;
rlm@23 73 sf_close (outfile) ;
rlm@23 74 sf_close (pcmfile) ;
rlm@23 75
rlm@23 76 }
rlm@23 77
rlm@23 78
rlm@23 79 void
rlm@23 80 generate_pcm (const char * wavfilename, const char * pcmfilename)
rlm@23 81 {
rlm@23 82 SNDFILE * wavfile ;
rlm@23 83 SNDFILE * pcmfile ;
rlm@23 84 SF_INFO wavinfo ;
rlm@23 85 SF_INFO pcminfo ;
rlm@23 86 int buff;
rlm@23 87
rlm@23 88 memset (&wavinfo, 0, sizeof (wavinfo)) ;
rlm@23 89 memset (&pcminfo, 0, sizeof (pcminfo)) ;
rlm@23 90
rlm@23 91 wavfile = sf_open (wavfilename, SFM_READ, &wavinfo) ;
rlm@23 92
rlm@23 93 if (wavfile == NULL){
rlm@23 94 printf ("\nERROR : Not able to open wav file named '%s' : %s/\n", wavfilename, sf_strerror (NULL)) ;
rlm@23 95 exit (1) ;
rlm@23 96 } ;
rlm@23 97
rlm@23 98 pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
rlm@23 99 pcminfo.samplerate = wavinfo.samplerate;
rlm@23 100 pcminfo.channels = wavinfo.channels;
rlm@23 101
rlm@23 102 if ((!wavinfo.format & SF_FORMAT_PCM_16) || (!wavinfo.channels == 1)){
rlm@23 103 printf("\nERROR : .wav file must be SF_FORMAT_PCM_16 and mono\n");
rlm@23 104 }
rlm@23 105
rlm@23 106 pcmfile = sf_open (pcmfilename, SFM_WRITE, &pcminfo) ;
rlm@23 107
rlm@23 108 if (pcmfile == NULL){
rlm@23 109 printf ("\nERROR : Not able to create pcm file named '%s' : %s/\n", pcmfilename, sf_strerror (NULL)) ;
rlm@23 110 exit (1) ;
rlm@23 111 } ;
rlm@23 112
rlm@23 113 while(sf_read_int(wavfile, &buff, 1) == 1){
rlm@23 114 if(sf_write_int(pcmfile, &buff, 1) != 1){
rlm@23 115 printf("\nERROR : unable to write to '%s' : %s/\n", pcmfilename, sf_strerror(NULL));
rlm@23 116 }
rlm@23 117 }
rlm@23 118
rlm@23 119 sf_close (wavfile) ;
rlm@23 120 sf_close (pcmfile) ;
rlm@23 121 }
rlm@23 122
rlm@23 123
rlm@23 124
rlm@23 125