annotate modules/bluespec/Pygar/common/SndfileWavUtil.cpp @ 43:4d87fa55a776 pygar svn.44

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