Resolved Audio Units

Discussion in 'iOS Programming' started by xArtx, Apr 26, 2013.

  1. xArtx, Apr 26, 2013
    Last edited: Apr 28, 2013

    xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #1
    Hi,
    I think I really need to learn about audio units.
    This really doesn't cut it:
    Code:
    [avbeep play];
    
    It seems iOS can just forget about the sound if it hasn't been played for
    a while, and needs to hold everything to reload it if you play it again.
    Code:
    [avbeep prepareToPlay];
    
    This can appear to fix it, but still... something going on, like it only works
    temporarily or something.

    AVFoundation doesn't play the same sound in harmony, so I did this:
    Code:
        beepcnt++;
        if (beepcnt > 3) {beepcnt = 0;}
        if (beepcnt == 0) {[avbeep play];}
        if (beepcnt == 1) {[avbeepx play];}
        if (beepcnt == 2) {[avbeepy play];}
        if (beepcnt == 3) {[avbeepz play];}
    
    So that an identical sound can be playing, more than one instance simultaneously
    (i.e. user presses a button twice very fast).
    By the time the counter is reset to zero, the first sound has actually
    finished, and ready to play again, so the user can go crazy with the button.

    If a button is not pushed for a while, all of the sounds are forgotten, and
    the App paused a short period while they are reloaded.

    Everything is fine as long as the sound has been played fairly recently,
    so I think it can be fixed with a shovel:
    Code:
    // somewhere execution occurs every second or so…
    
        beepcnt++;
        if (beepcnt > 3) {beepcnt = 0;}
        if (beepcnt == 0) {avbeep.volume = 0.0; [avbeep play]; avbeep.volume = 1.0;}
        if (beepcnt == 1) {avbeepx.volume = 0.0; [avbeepx play]; avbeepx.volume = 1.0;}
        if (beepcnt == 2) {avbeepy.volume = 0.0; [avbeepy play]; avbeepy.volume = 1.0;}
        if (beepcnt == 3) {avbeepz.volume = 0.0; [avbeepz play]; avbeepz.volume = 1.0;}
    
    Just keep playing them.... very desperate. When the button routine goes to play one,
    it should have been used recently, but not currently in use.

    Where's a good place to start with Audio Units, or a very bare bones example?
    I used something similar (I think) on the PSP, where you had an
    audio thread where a 16 bit buffer needed to be filled when it was ready for the next sample.
    The sample data was a .wav file with stripped header for raw PCM file.
    I converted that to C arrays, and still have a lot of sound sample data in that format.
    Cheers, Art.
     
  2. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #2
    I suspect it's just the same as a tone generator sample
    and you just load your PCM data where the tone is generated
    Code:
    // Generate the samples
    for (UInt32 frame = 0; frame < inNumberFrames; frame++) {
    aubuffer[frame] = stheta * amplitude;
    stheta -= theta_increment;
    }
    
     
  3. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #3
    Got it!

    Convert 16 bit array, and sned to buffer:
    Code:
            short workf = (beep[clickindex+1]*256) + (beep[clickindex]);
            Float32 sample = workf / (Float32)SHRT_MAX;
            aubuffer[frame] = sample *volume;
            if (volume < 0.14) {volume = volume+0.001;}
            clickindex++; clickindex++;
            if (clickindex > beep_length) {clickindex = 0; samlock = 5;}
    
    This one wastes some time fading in volume manually.
     

Share This Page