annotate modules/bluespec/Pygar/core/SndfileWavUtil.cpp @ 16:7e1510b47336 pygar svn.17

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