Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:41:53+00:00 2026-06-15T07:41:53+00:00

I was going over core audio conversion services in the Learning Core Audio and

  • 0

I was going over core audio conversion services in the Learning Core Audio and I was struck by this example in their sample code:

while(1)
{
    // wrap the destination buffer in an AudioBufferList
    AudioBufferList convertedData;
    convertedData.mNumberBuffers = 1;
    convertedData.mBuffers[0].mNumberChannels = mySettings->outputFormat.mChannelsPerFrame;
    convertedData.mBuffers[0].mDataByteSize = outputBufferSize;
    convertedData.mBuffers[0].mData = outputBuffer;

    UInt32 frameCount = packetsPerBuffer;

    // read from the extaudiofile
    CheckResult(ExtAudioFileRead(mySettings->inputFile,
                                 &frameCount,
                                 &convertedData),
                "Couldn't read from input file");

    if (frameCount == 0) {
        printf ("done reading from file");
        return;
    }

    // write the converted data to the output file
    CheckResult (AudioFileWritePackets(mySettings->outputFile,
                                       FALSE,
                                       frameCount,
                                       NULL,
                                       outputFilePacketPosition / mySettings->outputFormat.mBytesPerPacket, 
                                       &frameCount,
                                       convertedData.mBuffers[0].mData),
                 "Couldn't write packets to file");

    // advance the output file write location
    outputFilePacketPosition += (frameCount * mySettings->outputFormat.mBytesPerPacket);
}

notice how frameCount is defined as packetsPerBuffer.. packetsPerBuffer is defined here:

UInt32 outputBufferSize = 32 * 1024; // 32 KB is a good starting point
UInt32 sizePerPacket = mySettings->outputFormat.mBytesPerPacket;    
UInt32 packetsPerBuffer = outputBufferSize / sizePerPacket;

the part that stumped me is AudioFileWritePackets is called.. in the documentation AudioFileWritePackets third and fifth parameters are defined as:

inNumBytes
The number of bytes of audio data being written.

ioNumPackets
On input, a pointer to the number of packets to write. On output, a pointer to the number of packets actually written..

yet in the code both parameters are given frameCount.. how is this possible?? I know with PCM data 1 frame = 1 packet:

// define the ouput format. AudioConverter requires that one of the data formats be LPCM
audioConverterSettings.outputFormat.mSampleRate = 44100.0;
audioConverterSettings.outputFormat.mFormatID = kAudioFormatLinearPCM;
audioConverterSettings.outputFormat.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioConverterSettings.outputFormat.mBytesPerPacket = 4;
audioConverterSettings.outputFormat.mFramesPerPacket = 1;
audioConverterSettings.outputFormat.mBytesPerFrame = 4;
audioConverterSettings.outputFormat.mChannelsPerFrame = 2;
audioConverterSettings.outputFormat.mBitsPerChannel = 16;

but the same lPCM formatting also clearly states that there are 4 bytes per packet (= 4 bytes per frame)..

so how does this work? (the same applies to the other example in the same chapter that uses AudioConverterFillComplexBuffer instead of ExtAudioFileRead, and uses packets instead of frames.. but it’s the same thing)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:41:54+00:00Added an answer on June 15, 2026 at 7:41 am

    I think you’re right, according to the definition in the AudioFile.h header file, AudioFileWritePackets should take the number of bytes of audio data being written as the third parameter, and in that Learning Core Audio example the framecount variable is defined as the number of packets, not the number of bytes.

    I tried the examples out and got the exact same output with (framecount * 4), 0 and even -1 as the third parameter of the AudioFileWritePackets function call. So for me it would seem that the function doesn’t work exactly as defined in the .h file (does not require the third parameter), and that in that example the authors of the book have not noticed this error either – I might be wrong though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Going over some old code <!--- RANDOMLY DECIDE IS THIS PERSONS A WINNER 1
I was going over some old C code (listed below) with a view to
I am going over/studying the facebook code that was leaked in 2007, I notice
I've moved to a new project team and while going over the codebase, found
I have been going over this for hours and cannot find the problem. I
I've been going over and refactoring some code. I ended up changing a function
I'm going over some SQL I wrote a while ago that joins 4 tables
I am going over a sample project from Apple called Mixer Host. In the
I've been going over this in my head and trying out different approaches for
Ive been going over this for a couple days and i just cant find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.