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

  • SEARCH
  • Home
  • 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 229395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:45:44+00:00 2026-05-11T19:45:44+00:00

I’m currently working on the iPhone with Audio Units and I’m playing four tracks

  • 0

I’m currently working on the iPhone with Audio Units and I’m playing four tracks simultaneously. To improve the performance of my setup, I thought it would be a good idea to minimize the number of Audio Units / threads, by mixing down the four tracks into one.

With the following code I’m processing the next buffer by adding up the samples of the four tracks, keep them in the SInt16 range and add them to a temporary buffer, which will later on be copied into the ioData.mBuffers of the Audio Unit.

Although it works, I don’t have the impression that this is the most efficient way to do this.

SInt16* buffer = bufferToWriteTo; 
int reads      = bufferSize/sizeof(SInt16);         
SInt16** files = circularBuffer->files;

float tempValue;
SInt16 values[reads];
int k,j;                
int numFiles=4;

for (k=0; k<reads; k++)
{
    tempValue=0.f; 
    for (j=0; j<numFiles; j++) 
    {
        tempValue += files[j][packetNumber];        
    }
    if      (tempValue >  32767.f) tempValue =  32767.f;
    else if (tempValue < -32768.f) tempValue =- 32768.f;

    values[k]  = (SInt16) tempValue;
    values[k] += values[k] << 16;
    packetNumber++;
    if (packetNumber >= totalPackets) packetNumber=0;
}
memcpy(buffer,values,bufferSize); 

Any ideas or pointers to speed this up? Am I right?

  • 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-11T19:45:44+00:00Added an answer on May 11, 2026 at 7:45 pm

    The biggest improvement you can get from this code would be by not using floating point arithmetic. While the arithmetic by itself is fast, the conversions which happen in the nested loops, take a long time, especially on the ARM processor in the iPhone. You can achieve exactly the same results by using ‘SInt32’ instead of ‘float’ for the ‘tempValue’ variable.

    Also, see if you can get rid of the memcpy() in the last string: perhaps you can construct the ‘buffer’ directly, without using a temporary buffer called ‘values’. That saves one copy, which would be significant improvement for such a function.

    Other notes: the last two lines of the loop probably belong outside of the loop and the body of the nested loop should use ‘k’ as a second index, instead of ‘packetNumber’, but I’m not sure about this logic.

    And the last note: you’re squashing the peaks of your resulting sound. While this seems like a good idea, it will sound pretty rough. You probably want to scale the result down instead of cropping it. Like that: instead of this code

    for (j=0; j<numFiles; j++) 
    {
        tempValue += files[j][packetNumber];            
    }
    if      (tempValue >  32767.f) tempValue =  32767.f;
    else if (tempValue < -32768.f) tempValue =- 32768.f;
    

    you probably want something like this:

    for (j=0; j<numFiles; j++) 
    {
        tempValue += files[j][packetNumber] / numFiles;            
    }
    

    Edit: and please do not forget to measure the performance before and after, to see which one of the improvements gave the biggest impact. This is the best way to learn performance: trial and measurement

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.