Pergunta

I'm completely incompetent in C programming and I pieced this together by looking at different programs and working by analogy (i.e. I don't have much coding experience at all).

The program (for reading an aiff file (using libaiff library) and then making a copy of it) below works when executed. But I get errors if I make the following two "meaningless" changes (i.e. I simply define a pointer that I don't use):

After

char *filepathwrite = "Filepath/filename (Copy).aiff";

if I define

char *filepath = "LongFilepath";

I get segmentation fault 11. But if I define

char *filepath = "ShortFilepath";

or

int check = 1;

the program still works fine.

I can't tell what I'm doing wrong.

Thanks for any help you can extend. I'm working on Mac OS X Mavericks using the gcc compiler.

#define LIBAIFF_NOCOMPAT 1 // do not use LibAiff 2 API compatibility
#include <libaiff/libaiff.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
char *filepathread = "Filepath/filename.aiff";
char *filepathwrite = "Filepath/filename (Copy).aiff";

/* Initialize files for importing */
AIFF_Ref fileref1;

/* Intialize files for getting information about AIFF file */
uint64_t seconds, nSamples;
int channels;
double samplingRate;
int bitsPerSample;
int segmentSize;
int dummy;
int nsamplepts;
int32_t *samples, *samplesVec;
int count, maincount = 0, k, p;

/* Import Routine */
fileref1 = AIFF_OpenFile(filepathread, F_RDONLY) ;
if(fileref1)
{
    printf("File opened successfully.\n");
    dummy = AIFF_GetAudioFormat(fileref1,&nSamples,&channels,&samplingRate,&bitsPerSample,&segmentSize);
    if (dummy < 1)
    {
        printf("Error getting audio format.\n");
        AIFF_CloseFile(fileref1); return 0;
    }
    nsamplepts = ((int) nSamples)*channels;
    samplesVec = malloc(nsamplepts * sizeof(int32_t));
    for (count = 0; count < nsamplepts; count++)
    {
        dummy = AIFF_ReadSamples32Bit(fileref1, samples, channels);
        if (dummy == 0) {
            break;
        }
        p = 0;
        for (k = channels*maincount; k < ((channels*maincount)+channels); k++) {
            samplesVec[k] = *(samples+p);
            p = p + 1;
        }
        maincount = maincount + 1;
    }
    printf("Number of maincounts (should equal sample frames): %d\n",maincount);
    printf("Number of sample frames: %llu\n",nSamples);
    printf("Number of sample points: %d\n",nsamplepts);
    printf("Segment size: %d\n",segmentSize);
    printf("Number of channels: %d\n",channels);
    printf("Number of Sampling Rate: %f\n",samplingRate);
    AIFF_CloseFile(fileref1);
}

seconds = nSamples/samplingRate;

/* Print out the seconds in H:MM:SS format */
printf("Length: %lu:%02lu:%02lu \n", (unsigned long) seconds/3600, (unsigned long) (seconds/60)%60, (unsigned long) seconds%60);

/* Export Routine */
fileref1 = AIFF_OpenFile(filepathwrite, F_WRONLY);
if(fileref1)
{
    dummy = AIFF_SetAudioFormat(fileref1, channels, samplingRate, bitsPerSample);
    dummy = AIFF_StartWritingSamples(fileref1);
    dummy = AIFF_WriteSamples32Bit(fileref1, samplesVec, nsamplepts);
    dummy = AIFF_EndWritingSamples(fileref1);
    AIFF_CloseFile(fileref1);
}
free(samplesVec);
return 0;
}

Nenhuma solução correta

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top