Denis Mikhaylov
Denis Mikhaylov

Reputation: 2045

Getting raw sample data of m4a file to draw waveform

I'm using AudioToolbox to access m4a audio files with following code:

UInt32 packetsToRead = 1; //Does it makes difference?

void *buffer = malloc(maxPacketSize * packetsToRead);

for (UInt64 packetIndex = 0; packetIndex < packetCount; packetIndex++)
{
   ioNumberOfPackets = packetsToRead;
   ioNumberOfBytes = maxPacketSize * ioNumberOfPackets;

   AudioFileReadPacketData(audioFile, NO, &ioNumbersOfBytes, NULL, packetIndex, &ioNumberOFPackets, buffer);
    for (UInt32 batchPacketIndex = 0; batchPacketIndex < ioNumberOfPackets; batchPacketIndex++)
    {
    //What to do here to get amplitude value? How to get sample value?
    }
    packetIndex+=ioNumberOfPackets;
}

My audio format is: AppleM4A, 8000 Hz, 16 Bit, 4096 frames per packet

Upvotes: 2

Views: 2524

Answers (2)

hotpaw2
hotpaw2

Reputation: 70673

To get waveform data, you may first need to convert your compressed audio file into raw PCM samples, such as found inside a WAV file, or other non-compressed audio format. Try AVAssetReader, et.al.

Upvotes: 0

Denis Mikhaylov
Denis Mikhaylov

Reputation: 2045

The solution was to use extended audio file services. You just have to set up transition between client format and PCM. Got the right way overthere Audio Processing: Playing with volume level.

Upvotes: 1

Related Questions