Playing sounds in perfect succession on the iPhone - iphone

I am developing a game for the iPhone and iPad using cocos2d, and I need to be able to play a sound exactly when another one completes.
I have a soundtrack that is chopped up in smaller pieces, and there are no room for the tinyest gap between playback when one finishes and one starts.
Btw. I cannot glue the sounds together into a single file and just play that since the order of the files will be rearranged runtime.
How can I achieve this?

With CocosDenshion you can register a delegate with
[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:self
selector:#selector(musicDidFinish)];
CDAudioManager class reference
This delegate will be called whenever the background music ends. This of course only works if you play your sound files as background music (with the playBackgroundMusic method).
If that doesn't work for you, have a look at ObjectAL. You'll have more options and greater flexibility. For example, with ALSource you can queue multiple ALBuffer objects which represent sound files. That means whenever the source's buffer count decreases to 1 you just queue the next buffer to achieve uninterrupted, sequential playback of multiple sound files (any format).
Because ObjectAL is so awesome (well, I think so :) ) it's included and ready to use in Kobold2D.

You can use a single Audio Queue or the RemoteIO Audio Unit, and just fill the callback buffers with raw/PCM audio samples from any file in any order.

Related

Is it possible to record the audio that comes out of the iPhone?

I am working on an app that allows the user to create a sort of dub. There is an audio file playing, and the user can tap at certain moments to insert sound (kind of like a censor button.) I'm wondering how to go about capturing the final product.
Capturing audio directly from the iPhone seems the easiest route, as the user already hears the finished product as it is made. However, I can't find anything on how to do this. If not possible, are there any suggestions?
The best way would probably be to be using the AV Foundation framework for mixing and then buffering the audio as well as playing it. This would allow for a high abstraction level while guaranteeing both played back and saved audio to be equal.
Apart from that: from a How can I achieve this with minimum code-perspective, without more information about your setup, the question is way too broad and/or opinion-based.
You will have to work with buffers. Don't know right now how it is done in Swift but you can implement it in Obj-C and then bridge it out.
You can refer to this answers here in StackOverflow (They are a bit old)
https://stackoverflow.com/a/11218339/2683201
https://stackoverflow.com/a/10101877/2683201
and a project also exists (but is in Obj-C)
https://github.com/alexbw/novocaine
Mainly the idea for your case would be to have 2 separated buffers and your sound effect.
Then, you will be playing from buffer A (your music) and copying played data into buffer B (final Output) unless you are playing the effect. In wich case you will be copying the effect data into your buffer B.
Other option is to do it offline:
Play your music (or audio) and keep a timer running synced with the elapsed time of your "to be censored audio".
Save the timestamp of when you start and end tapping the censor button (for example).
Overlap buffer A with your effect in those recorded (start-end) timestamps.
Save the buffer as a file (or do whatever you need to do with it)
UPDATE:
You should take a look into the Apple implementation of something like this:
https://developer.apple.com/library/ios/samplecode/AVAEMixerSample/Introduction/Intro.html

Looping audio on iOS with explicit loop-in and loop-out points

I am developing an iOS application which resembles a musical instrument.
I am trying to loop the sound samples to make them last infinitely. Simple looping is not sufficient in this case, as the samples have an "attack" section: Each sample has a part at the beginning which should not be looped. Therefore, I need some way to loop only a certain part of the sound sample.
I found a few iOS sound libraries (e.g. ObjectAL), but they all seem to support only simple looping, without an option to set loop-in and loop-out points.
Are they any iOS audio libraries which support this feature? Otherwise, what would be the best method to implement it? (Audio units? Audio queues? Some other trick with existing libraries?)
Thanks!
I can't say if it's the best solution, but for my loops that have an intro / outro... I use multiple .caf files that blend together: one for the intro, one for the loop, and one for the outro.
After the intro, the loop starts and plays until an event fires that makes the sound stop looping and finish, and then the outro immediately plays once the loop sound finishes.
I use Audacity to edit the sound files to make sure that they blend together perfectly.
Edit:
My application uses cocos2d, so I'm using the cocosDehension audio library since it is built in to cocos2d. As long as the sound file has been properly edited, it loops cleanly with no clicks or pops.
Here's a link to the technique that I used in Audacity to make sure the file looped without clicks:
http://forum.audacityteam.org/viewtopic.php?f=13&t=2820#p11073

Playing multiple sounds simultaneously with AVAudioPlayer

Could someone help me with a function that fires multiple AVAudioPlayers at the same time? Right now I am trying to get a total of twelve AVAudioPlayers to fire at once if twelve buttons are activated, but there is a delay and it sounds like someone is running their finger down a piano instead of hitting all the keys at once.
I've looked at Audio Queue Services and can't understand how to actually implement that into code, but it says it can play synchronized sounds. I'm not sure sure how to set all of it up. I'm trying to remake a Tone Grid app.
Why don't you use http://www.hollance.com/2011/02/soundbankplayer-using-openal-to-play-musical-instruments-in-your-ios-app/
There you have a polyphonic player, beautifully coded and ready to rock!
I used it in my first app a while back: http://itunes.apple.com/gb/app/chordwheel-pro/id406836326?mt=8
Are you calling the method prepareToPlay on all 12 AVAudioPlayer instances. From the docs: "Calling this method, preloads the buffers and acquires hardware, to minimize delay."
See the AVAudioPlayer class reference.

Best way to play a sound with an attack / sustain (loop) / decay with AVAudioPlayer

I am having a problem finding resources on playing an attack (start of sound) / sustain (looping sound) / decay (ending of sound) sequence with no transition breaks. Are there any good libraries for handling this, or should I roll my own with AVAudioPlayer? Is AudioQueue a better place to look? I used to use SoundEngine.cpp, but that's been long gone for a while. Is CAF still the best format to use for it?
Thanks!
From your description, it sounds as if you're trying to write a software synthesizer. The only way that you could use AVAudioPlayer for something like this would be to compose the entire duration of a note as a single WAV file and then play the whole thing with AVAudioPlayer.
To create a note sound of arbitrary duration, one that begins playing in response to a user action (like tapping a button) and then continues playing until a second user action (like tapping a "stop" button or lifting the finger off the first button) begins the process of ramping the looped region's volume down to zero (the "release" part), you will need to use AudioQueue (AVAudioPlayer can be used to play audio constructed entirely in memory, but the entire playback has to be constructed before play begins, meaning that you cannot change what is being played in response to user actions [other than to stop playback]).
Here's another question/answer that shows simply how to use AudioQueue. AudioQueue calls a callback method whenever it needs to load up more data to play - you would have to implement all the code that loops and envelope-wraps the original WAV file data.
creating your own envelope generator is very simple. the tough part will be updating your program to use lower level audio services in order to alter the signal directly.
to do this, you will need:
the audio file's samples
set up an AudioQueue (that's one approach, but i am going with it because it was mentioned in the OP, and it is relatively high level API for a user provided sample buffer)
provide a signal to the queue
determine if your program is best in realtime or pre-rendered
Realtime
Allows live variations
manage your loop points
manage your render position
be able to determine the amplitude to apply based on the sample position range you are reading
or
Prerendered
May require more memory
Requires less CPU
apply the envelope to your copy of the sample buffer
manage your render position
I also assume that you need only slow/simple transitions. If you want some crazy/fast LFO, without aliasing, you will have a lot more work to do. This approach should not produce audible aliasing unless your changes are too abrupt:
Writing a simple envelope generator (EG) is easy; check out Apple's SinSynth for a very basic EG if you need a push in that direction.

Best way to play different sounds one at a time on iphone?

I have a bunch of sounds I want to play. Right now I'm using AVAudioPlayer. A new sound should start when the user clicks a button. The url property of AVAudioPlayer is read-only. I'd rather not have to release and re-alloc the object every time I want a new sound played. What would be the best way to do this? Is there a different class I need to use?
The absolute easiest way to play sound is Playing UI Sound Effects or Invoking Vibration Using System Sound Services. Essentially you call AudioServicesCreateSystemSoundID() and AudioServicesPlaySystemSound(). Creating a system sound is relatively expensive and playback stops when it's disposed with AudioServicesDisposeSystemSoundID() anyway, so you really want to cache them:
Create them at app launch (or possibly the first time the sound is played). You can keep them in an NSDictionary keyed by sound file name and store the sound ID in an NSValue, or so.
If you can be bothered, dispose them when your app exits, is backgrounded, or on a memory warning.
The biggest caveat is that only a few sound formats are supported for "system sounds"; apple seems to recommend IMA4 (afconvert -f caff -d ima4 input.file output.caf and optionally -c 1 if you're happy with mono).
That said, it's pretty much perfect for playing short sounds provided you don't mind one-at-a-time sounds (it restarts playback if you play a sound while it's already playing). I suspect it's also played directly by the kernel (you can set a flag to make it continue playback when your app exits), which probably means it's more efficient CPU-wise. It might mix with sounds played by AVAudioPlayer.