0

playmp3() using libmpg123

if (isPaused==0 && mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
{
    char * resBuffer=&buffer[0]; //22100=0,5s
    buffer = resample(resBuffer,22100,22100);
    if((ao_play(dev, (char*)buffer, done)==0)){
        return 1;
}

resample() Using avcodec from ffmpeg

#define LENGTH_MS 500       // how many milliseconds of speech to store
#define RATE 44100      // the sampling rate (input)
#define FORMAT PA_SAMPLE_S16NE  // sample size: 8 or 16 bits
#define CHANNELS 2      // 1 = mono 2 = stereo

struct AVResampleContext* audio_cntx = 0;

char * resample(char in_buffer[(LENGTH_MS*RATE*16*CHANNELS)/8000],int out_rate,int nsamples)
{
    char out_buffer[ sizeof( in_buffer ) * 4];
    audio_cntx = av_resample_init( out_rate, //out rate
        RATE, //in rate
        16, //filter length
        10, //phase count
        0, //linear FIR filter
        1.0 ); //cutoff frequency
    assert( audio_cntx && "Failed to create resampling context!");
    int samples_consumed;
    int samples_output = av_resample( audio_cntx, //resample context
        (short*)out_buffer, //buffout
        (short*)in_buffer,  //buffin
        &samples_consumed,  //&consumed
        nsamples,       //nb_samples
        sizeof(out_buffer)/2,//lenout
        0);//is_last
    assert( samples_output > 0 && "Error calling av_resample()!" );
    av_resample_close( audio_cntx );
    //*resample = malloc(sizeof(out_buffer));
    return &out_buffer[0];  
}

When i run this code i get 3393 Segmentation fault (core dump created). Why?

For example, the use of pointers is correct? and 22100 are the samples that are contained in 0.5 seconds of the song?

1
  • Is this your code? Or code from somewhere else? If the latter, you may want to contact whoever wrote this code to find out if there are any known errors with this code. Jun 17, 2014 at 16:24

1 Answer 1

1

You have two issues that I can see right off the bat. These are noob questions, but everyone does this at least once, so don't worry!

Check that sizeof( in_buffer ) is giving you the size of the buffer you expect ((LENGTH_MS*RATE*16*CHANNELS)/8000) or the size of the pointer(which would be 2, 4 or 8 depending on your system.) Using sizeof on an array on the stack gives you its total size, because there is no pointer only a buffer. Sizeof an array on the parameter list gives you the size of the pointer even if you use [] on the param list, because there only is a pointer.

Also, returning a stack based buffer is undefined (i.e. will crash), since the stack gets reused on the next function call:

return &out_buffer[0]; 

Don't do that. Pass in the out buffer already allocated by the caller.

3
  • Worth noting: char in_buffer[(LENGTH_MS*RATE*16*CHANNELS)/8000] as a declared function parameter, the magnitude is meaningless. It may as well simply be declared as char buffer[], which would potentially at least hint to the first problem discussed in this answer.
    – WhozCraig
    Jun 17, 2014 at 16:45
  • Some compilers will use that size to check calls to memcmp, strcmp etc. Also it gives an indication of the size of the buffer needed. It is however something you need to watch out for when you use sizeof. Jun 17, 2014 at 20:42
  • @WC: Well its something I was told and something I seem to remember having run into. I just tried it on msvc and I got no warnings, so I'm gonna say that its a wives tale and I was drunk when I thought I got that warning. Jun 17, 2014 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.