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 967075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:15:32+00:00 2026-05-16T02:15:32+00:00

I am seeking an example of using Audio Queue Services. I would like to

  • 0

I am seeking an example of using Audio Queue Services.

I would like to create a sound using a mathematical equation and then hear it.

  • 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-05-16T02:15:33+00:00Added an answer on May 16, 2026 at 2:15 am

    Here’s my code for generating sound from a function. I’m assuming you know how to use AudioQueue services, set up an AudioSession, and properly start and stop an audio output queue.

    Here’s a snippet for setting up and starting an output AudioQueue:

    // Get the preferred sample rate (8,000 Hz on iPhone, 44,100 Hz on iPod touch)
    size = sizeof(sampleRate);
    err = AudioSessionGetProperty (kAudioSessionProperty_CurrentHardwareSampleRate, &size, &sampleRate);
    if (err != noErr) NSLog(@"AudioSessionGetProperty(kAudioSessionProperty_CurrentHardwareSampleRate) error: %d", err); 
    //NSLog (@"Current hardware sample rate: %1.0f", sampleRate);
    
    BOOL isHighSampleRate = (sampleRate > 16000);
    int bufferByteSize;
    AudioQueueBufferRef buffer;
    
    // Set up stream format fields
    AudioStreamBasicDescription streamFormat;
    streamFormat.mSampleRate = sampleRate;
    streamFormat.mFormatID = kAudioFormatLinearPCM;
    streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
    streamFormat.mBitsPerChannel = 16;
    streamFormat.mChannelsPerFrame = 1;
    streamFormat.mBytesPerPacket = 2 * streamFormat.mChannelsPerFrame;
    streamFormat.mBytesPerFrame = 2 * streamFormat.mChannelsPerFrame;
    streamFormat.mFramesPerPacket = 1;
    streamFormat.mReserved = 0;
    
    // New output queue ---- PLAYBACK ----
    if (isPlaying == NO) {
        err = AudioQueueNewOutput (&streamFormat, AudioEngineOutputBufferCallback, self, nil, nil, 0, &outputQueue);
        if (err != noErr) NSLog(@"AudioQueueNewOutput() error: %d", err);
    
        // Enqueue buffers
        //outputFrequency = 0.0;
        outputBuffersToRewrite = 3;
        bufferByteSize = (sampleRate > 16000)? 2176 : 512; // 40.5 Hz : 31.25 Hz 
        for (i=0; i<3; i++) {
            err = AudioQueueAllocateBuffer (outputQueue, bufferByteSize, &buffer); 
            if (err == noErr) {
                [self generateTone: buffer];
                err = AudioQueueEnqueueBuffer (outputQueue, buffer, 0, nil);
                if (err != noErr) NSLog(@"AudioQueueEnqueueBuffer() error: %d", err);
            } else {
                NSLog(@"AudioQueueAllocateBuffer() error: %d", err); 
                return;
            }
        }
    
        // Start playback
        isPlaying = YES;
        err = AudioQueueStart(outputQueue, nil);
        if (err != noErr) { NSLog(@"AudioQueueStart() error: %d", err); isPlaying= NO; return; }
    } else {
        NSLog (@"Error: audio is already playing back.");
    }
    

    Here’s the part that generates the tone:

    // AudioQueue output queue callback.
    void AudioEngineOutputBufferCallback (void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
        AudioEngine *engine = (AudioEngine*) inUserData;
        [engine processOutputBuffer:inBuffer queue:inAQ];
    }
    
    - (void) processOutputBuffer: (AudioQueueBufferRef) buffer queue:(AudioQueueRef) queue {
        OSStatus err;
        if (isPlaying == YES) {
            [outputLock lock];
            if (outputBuffersToRewrite > 0) {
                outputBuffersToRewrite--;
                [self generateTone:buffer];
            }
            err = AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);
            if (err == 560030580) { // Queue is not active due to Music being started or other reasons
                isPlaying = NO;
            } else if (err != noErr) {
                NSLog(@"AudioQueueEnqueueBuffer() error %d", err);
            }
            [outputLock unlock];
        } else {
            err = AudioQueueStop (queue, NO);
            if (err != noErr) NSLog(@"AudioQueueStop() error: %d", err);
        }
    }
    
    -(void) generateTone: (AudioQueueBufferRef) buffer {
        if (outputFrequency == 0.0) {
            memset(buffer->mAudioData, 0, buffer->mAudioDataBytesCapacity);
            buffer->mAudioDataByteSize = buffer->mAudioDataBytesCapacity;
        } else {
            // Make the buffer length a multiple of the wavelength for the output frequency.
            int sampleCount = buffer->mAudioDataBytesCapacity / sizeof (SInt16);
            double bufferLength = sampleCount;
            double wavelength = sampleRate / outputFrequency;
            double repetitions = floor (bufferLength / wavelength);
            if (repetitions > 0.0) {
                sampleCount = round (wavelength * repetitions);
            }
    
            double      x, y;
            double      sd = 1.0 / sampleRate;
            double      amp = 0.9;
            double      max16bit = SHRT_MAX;
            int i;
            SInt16 *p = buffer->mAudioData;
    
            for (i = 0; i < sampleCount; i++) {
                x = i * sd * outputFrequency;
                switch (outputWaveform) {
                    case kSine: 
                        y = sin (x * 2.0 * M_PI);
                        break;
                    case kTriangle:
                        x = fmod (x, 1.0);
                        if (x < 0.25)
                            y = x * 4.0; // up 0.0 to 1.0
                        else if (x < 0.75)
                            y = (1.0 - x) * 4.0 - 2.0; // down 1.0 to -1.0
                        else 
                            y = (x - 1.0) * 4.0; // up -1.0 to 0.0
                        break;
                    case kSawtooth:
                        y  = 0.8 - fmod (x, 1.0) * 1.8;
                        break;
                    case kSquare:
                        y = (fmod(x, 1.0) < 0.5)? 0.7: -0.7;
                        break;
                    default: y = 0; break;
                }
                p[i] = y * max16bit * amp;
            }
    
            buffer->mAudioDataByteSize = sampleCount * sizeof (SInt16);
        }
    }
    

    Something to watch out for is that your callback will be called on a non-main thread, so you have to practice thread safety with locks, mutexs, or other techniques.

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

Sidebar

Related Questions

Seeking example using ADSI API to programmatically create a Windows Group. AD is Windows
Seeking to create an event handler that registers a callback and iterates based on
I am seeking for the advantages of having Spring deployed on Tomcat rather then
I'm currently seeking a solution to logging into Wordpress using PHP/Javascript, I have found
I have to insert a string after a certain character using Ruby. For example,
I am seeking a sample implementation of Authorize.Net DPM using Webforms, but I can't
I'm seeing a strange behavior in my code, here's an analogous example using apples
I definitely remember seeing somewhere an example of doing so using reflection or something.
If you use the satellite GMapType using this Google-provided example in v3 of the
I remember seeing an example of this recently, but for the life of me

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.