diff modules/bluespec/Pygar/common/SndfileWavUtil.cpp @ 8:74716e9a81cc pygar svn.9

[svn r9] Pygar now has the proper directory structure to play nicely with awb. Also, the apm file for audio-core willcompile successfully.
author rlm
date Fri, 23 Apr 2010 02:32:05 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/bluespec/Pygar/common/SndfileWavUtil.cpp	Fri Apr 23 02:32:05 2010 -0400
     1.3 @@ -0,0 +1,125 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <string.h>
     1.6 +#include <errno.h>
     1.7 +#include <math.h>
     1.8 +#include <sndfile.h>
     1.9 +#include "SndfileWavUtil.h"
    1.10 +
    1.11 +void
    1.12 +generate_wav(const char * pcmfilename, const char * samplewavfilename, const char * outputwavfilename)
    1.13 +{
    1.14 +  char outfilename[2048];
    1.15 +  SNDFILE * outfile ;
    1.16 +  SNDFILE * wavfile ;
    1.17 +  SNDFILE * pcmfile ;
    1.18 +  SF_INFO wavinfo ;
    1.19 +  SF_INFO pcminfo ;
    1.20 +  int buff;
    1.21 +  SF_INSTRUMENT inst ;
    1.22 +  
    1.23 +  memset (&wavinfo, 0, sizeof (wavinfo)) ;
    1.24 +
    1.25 +  
    1.26 +  wavfile = sf_open(samplewavfilename, SFM_READ, &wavinfo);
    1.27 +
    1.28 +  if (wavfile == NULL){      
    1.29 +    printf ("\nERROR : Not able to open wav file named '%s' : %s/\n", samplewavfilename, sf_strerror (NULL)) ;
    1.30 +    exit (1) ;
    1.31 +  } ;
    1.32 +
    1.33 +  printf("WAV format: %x\n", wavinfo.format);
    1.34 +
    1.35 +  if (!((wavinfo.format & SF_FORMAT_PCM_16) && (wavinfo.channels == 1) && 
    1.36 +        (wavinfo.format & SF_FORMAT_WAV))){
    1.37 +    printf("\nERROR : .wav file must be SF_FORMAT_PCM_16 in mono\n");
    1.38 +  }
    1.39 +
    1.40 +  pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
    1.41 +  pcminfo.samplerate = wavinfo.samplerate;
    1.42 +  pcminfo.channels = wavinfo.channels;
    1.43 +
    1.44 +  pcmfile = sf_open(pcmfilename, SFM_READ, &pcminfo);
    1.45 +
    1.46 +  if (pcmfile == NULL){      
    1.47 +    printf ("\nERROR : Not able to open pcm file named '%s' : %s/\n", pcmfilename, sf_strerror (NULL)) ;
    1.48 +    exit (1) ;
    1.49 +  } ;
    1.50 +
    1.51 +
    1.52 +
    1.53 +  outfile = sf_open(outputwavfilename, SFM_WRITE, &wavinfo);
    1.54 +
    1.55 +  memset (&inst, 0, sizeof (inst)) ;
    1.56 +
    1.57 +  for(int i = SF_STR_FIRST; i <= SF_STR_LAST; i = i + 1) { 
    1.58 +    const char * str =  sf_get_string(wavfile,i);
    1.59 +    if(str != NULL) {
    1.60 +      sf_set_string(outfile,i,str);
    1.61 +    }
    1.62 +  }
    1.63 +  
    1.64 +  if (outfile == NULL){      
    1.65 +    printf ("\nERROR : Not able to create wav file named '%s' : %s/\n", outfilename, sf_strerror (NULL)) ;
    1.66 +    exit (1) ;
    1.67 +  } ;
    1.68 +
    1.69 +  while(sf_read_int(pcmfile, &buff, 1) == 1){
    1.70 +    if(sf_write_int(outfile, &buff, 1) != 1){
    1.71 +      printf("\nERROR : unable to write to '%s' : %s/\n", outfilename, sf_strerror(NULL));
    1.72 +    }
    1.73 +  }
    1.74 +  
    1.75 +  sf_close (wavfile) ;
    1.76 +  sf_close (outfile) ;
    1.77 +  sf_close (pcmfile) ;
    1.78 +  
    1.79 +}
    1.80 +
    1.81 +
    1.82 +void
    1.83 +generate_pcm (const char * wavfilename, const char * pcmfilename)
    1.84 +{
    1.85 +	SNDFILE * wavfile ;
    1.86 +	SNDFILE * pcmfile ;
    1.87 +	SF_INFO wavinfo ;
    1.88 +	SF_INFO pcminfo ;
    1.89 +	int buff;
    1.90 +
    1.91 +	memset (&wavinfo, 0, sizeof (wavinfo)) ;
    1.92 +	memset (&pcminfo, 0, sizeof (pcminfo)) ;
    1.93 +
    1.94 +	wavfile = sf_open (wavfilename, SFM_READ, &wavinfo) ;
    1.95 +
    1.96 +	if (wavfile == NULL){      
    1.97 +	  printf ("\nERROR : Not able to open wav file named '%s' : %s/\n", wavfilename, sf_strerror (NULL)) ;
    1.98 +	  exit (1) ;
    1.99 +	} ;
   1.100 +
   1.101 +	pcminfo.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16;
   1.102 +	pcminfo.samplerate = wavinfo.samplerate;
   1.103 +	pcminfo.channels = wavinfo.channels;
   1.104 +
   1.105 +	if ((!wavinfo.format & SF_FORMAT_PCM_16) || (!wavinfo.channels == 1)){
   1.106 +	  printf("\nERROR : .wav file must be SF_FORMAT_PCM_16 and mono\n");
   1.107 +	}
   1.108 +
   1.109 +	pcmfile = sf_open (pcmfilename, SFM_WRITE, &pcminfo) ;
   1.110 +
   1.111 +	if (pcmfile == NULL){      
   1.112 +	  printf ("\nERROR : Not able to create pcm file named '%s' : %s/\n", pcmfilename, sf_strerror (NULL)) ;
   1.113 +	  exit (1) ;
   1.114 +	} ;
   1.115 +	
   1.116 +	while(sf_read_int(wavfile, &buff, 1) == 1){
   1.117 +	  if(sf_write_int(pcmfile, &buff, 1) != 1){
   1.118 +	    printf("\nERROR : unable to write to '%s' : %s/\n", pcmfilename, sf_strerror(NULL));
   1.119 +	  }
   1.120 +	}
   1.121 +
   1.122 +	sf_close (wavfile) ;
   1.123 +	sf_close (pcmfile) ;
   1.124 +} 
   1.125 +
   1.126 +
   1.127 +
   1.128 +