You never open psf_out2 si writing to it will be a disaster.
Also it is safer to initialise psfinfo_out to zero

psfinfo_out= (SF_INFO *) calloc(1,sizeof(SF_INFO));

Not checked as about to leave for University

==John ff
Quoting Pablo Frank <frank_pa...@hotmail.com>:

When running the file below, the message Segmentation fault: 11 is displayed and an empty stereo file is created. Can someone show the problem, or better, correct the code ? (I'm a beginner). Thanks in advance


#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>

#define BUFFRAMES 512

void usage();

int main(int argc, char** argv) {

  SNDFILE *psf_in, *psf_out,*psf_out2;
  SF_INFO *psfinfo_in, *psfinfo_out;
  float *buffer, *out;
  sf_count_t count;
  int chans;

  if(argc < 3) {
    usage();
    exit(-1);
  }

  /* memory for SF_INFO structures */
  psfinfo_in = (SF_INFO *) malloc(sizeof(SF_INFO));
  psfinfo_out = (SF_INFO *) malloc(sizeof(SF_INFO));

  /* open input  */
  if(!(psf_in = sf_open(argv[1],SFM_READ,psfinfo_in))){
   printf("error opening input file\n");
   exit(-1);
  }


    // check if file is mono or stereo
  if (!(psfinfo_out->channels = 2))


    /* allocate buffer memory  */
  buffer = (float *) malloc(sizeof(float)*BUFFRAMES);
  out = (float *) malloc(sizeof(float)*BUFFRAMES*2);

  /* Now we initialise the SF_INFO structure

     with the same sampling rate... */
  psfinfo_out->samplerate = psfinfo_in->samplerate;
  /* ... same number of channels... */
psfinfo_out->channels = 2; //<-------------------------------------Stereo FILE
  chans =  psfinfo_in->channels;

  /* and WAV format with the same precision */
psfinfo_out->format = SF_FORMAT_WAV | (psfinfo_in->format & SF_FORMAT_SUBMASK);



  /* now we open the file */
   if(!(psf_out = sf_open(argv[2], SFM_WRITE,psfinfo_out))){
   printf("error opening output file\n");
   exit(-1);
  }

   /* and we copy the data, frame by frame */
  do {
    int i;
    count = sf_readf_float(psf_in, buffer, BUFFRAMES);

    for(i=0, i < BUFFRAMES; i++;)
    out[i] = buffer[i];

    sf_writef_float(psf_out, out, count);
    sf_writef_float(psf_out2, out, count);
  }
  while(count);




    sf_close(psf_in);
    sf_close(psf_out);
    free(psfinfo_in);
    free(psfinfo_out);
    free(buffer);
    return 0;
  }



void usage(){
printf("\nusage: aiff2wave input.aif output.wav\n");
}


--
dupswapdrop -- the music-dsp mailing list and website:
subscription info, FAQ, source code archive, list archive, book reviews, dsp links
http://music.columbia.edu/cmc/music-dsp
http://music.columbia.edu/mailman/listinfo/music-dsp



--
dupswapdrop -- the music-dsp mailing list and website:
subscription info, FAQ, source code archive, list archive, book reviews, dsp 
links
http://music.columbia.edu/cmc/music-dsp
http://music.columbia.edu/mailman/listinfo/music-dsp

Reply via email to