> // 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);
I think you mean
if (psfinfo_out->channels != 2) {
…
}
Note missing brackets, plus the logical comparison versus your assignment ("=2")
The missing brackets will make it work like this:
if (…)
buffer = …
out = ...
So, the buffer won't be allocated, since the "if" always evaluated to false…
On Jan 23, 2014, at 7:24 AM, Pablo Frank <[email protected]> wrote:
> i open psf_out2 and also replaced malloc by calloc, and still receive
> Segmentation fault 11 and an empty stereo file, from a mono file.
> the code:
>
>
>
>
> #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_out= (SF_INFO *) calloc(1,sizeof(SF_INFO));
> psfinfo_in= (SF_INFO *) calloc(1,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)) || !(psf_out2 =
> 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);
> sf_close(psf_out2);
> free(psfinfo_in);
> free(psfinfo_out);
> free(buffer);
> return 0;
> }
>
>
>
> void usage(){
> printf("\nusage: aiff2wave input.aif output.wav\n");
> }
>
>> Date: Thu, 23 Jan 2014 14:41:14 +0000
>> From: [email protected]
>> To: [email protected]
>> CC: [email protected]
>> Subject: Re: [music-dsp] Segmentation fault: 11 when running stereo to mono
>> .c file
>>
>> 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
>
>
> --
> 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