[Libav-user] How to get raw audio frames from an audio file

Finalspace finalspace at googlemail.com
Sun Oct 21 16:42:57 EEST 2018


The only thing you need to change, is to divide by INT16_MAX - not 
INT32_MAX, because in your software conversion context you use S16 as 
your target format. This should give you -1 to 1 range from a -32767 to 
+32767 amplitude. Therefore your variable "myFrame" should be int16_t 
instead.

Also the memcpy part is wrong:
memcpy(&myFrame, &localBuffer[ch * i], dataSize * sizeof(uint8_t));

Correct is:
memcpy(&myFrame, &localBuffer[i * ch * dataSize], dataSize); // * 
sizeof(uint8_t) is useless, its always and will ever be 1

Also here are a few tips:

1.) Do not use, global/static variables outside of a class for no 
reason. Just move the variables out from the global space into class 
members. If this needs to be static then use static members instead.

2.) Short variables names re bad - use long names, it does not hurt to 
write "sampleIndex" instead of "i". This way others and propably can 
understand the code much better.

3.) Fix bad naming of variables:

myFrame -> conversionSampleS16
dataSize -> sourceSampleSize
localBuffer -> conversionBuffer
buffer -> targetFloatBuffer

In addition it does not hurt to add the type to the variable names as 
well - especially when writing audio code.

4.) You have no idea if the buffer passed to getPcmFloat() fits the 
samples you wanna write. It would be to pass a size_t bufferLength as well.

Am 21.10.2018 um 11:37 schrieb Matthieu Regnauld:
> Thanks a lot for your help.
>
> That said, I still haven't managed to solve my problem, and I'm new to 
> FFMpeg.
>
> My final goal is to be able to extract raw audio frames from an OGG 
> file on the fly (and, if possible, from any other format), and convert 
> them in an array of float amplitudes (between -1 and 1).
>
> Here is a copy of the code that I'm trying to make work: 
> https://gist.github.com/mregnauld/2538d98308ad57eb75cfcd36aab5099a
>
> I initiate my player this way:
> FFMpegPlayer* ffMpegPlayer = new FFMpegPlayer();
> ffMpegPlayer->createFFmpeg("/path/to/my/file.ogg");
>
> And later on, when I need audio samples, I do this way, and I redirect 
> the buffer directly to the audio output (it for an Android app):
> float *buffer;
> ffMpegPlayer->getPcmFloat(buffer);
>
> I still have white noice so far, but I can hear a little bit the music 
> (actually I guess it more that I can hear it), which makes me think 
> that I'm close to the solution.
>
> What should I change in my code to get the proper float amplitudes?
>
> Thanks for your help.
>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20181021/2909dba2/attachment.html>


More information about the Libav-user mailing list