Can iphone mix two sound files or build custom equalizer? - iphone

Can iphone mix two sound files or build custom equalizer?
I have studied for weeks about this problem,
and it seems unable to use iphone-sdk to mix two or more sound files or to build custom equalizer.
Is anyone have the experience to do this?

Yes you can. AVAudioPlayer can play multiple sounds and you can control the volume for each. Or you can use Audio Units and have more control over the audio data.
aurioTouch is a good sample app for what you are thinking of.

For simple playback of sound files you can use the AVAudioPlayer class introduced in the 2.2 SDK. It provides playback and volume controls for playing any audio file. As far as I am aware, there are no restrictions on the number of sound files you can play on the iPhone. The only restriction on playing sound files is that you may only play one AAC or MP3 compressed file at a time, the rest of the files must be either uncompressed or in the IMA4 format.
If your needs are more low-level (If you need to do DSP) you might want to look at AudioQueue Services or AudioUnits - two Mac OS X audio processing APIs that are also available on the iPhone.

Related

Playing audio using the iPhone SDK

I'm introducing a functionality in my iPhone app that allows user to mix and play selected audio files.
What is the difference between just using two AVAudioPlayer to play the music files concurrently versus using Apple's example class, MixerHost, to play the files?
The class MixerHost can be found on here:
http://developer.apple.com/library/ios/#samplecode/MixerHost/Listings/Classes_MixerHostAudio_h.html
MixerHost gives you much more precise control over the relative timing of the two audio inputs.

What is the Mac equivalent of AVAudioPlayer?

I am porting an iPhone app to Mac. On iPhone I use AVAudioPlayer for playing sounds, but it doesn't exist on Mac. What would be the Mac equivalent for playing audio (hopefully as simple as AVAudioPlayer so I can port my app easily)?
Additionally, are there any open source libraries / samples / wrappers available for playing music and sound files on Mac?
As of OS X 10.7, AVAudioPlayer is available on the Mac and Apple recommends using this wherever possible.
Depending on what capabilities you need, Core Audio might be unnecessarily complex. The obvious simple way is NSSound. It's also possible to use a hidden QTMovieView to play sound.
I've written a framework for audio playback, called SFBAudioEngine: http://github.com/sbooth/SFBAudioEngine
It might be a bit more than you're looking for, but it is more capable than NSSound and supports formats (FLAC, Musepack, Ogg Vorbis) that Core Audio doesn't handle natively.
Take a look at Core Audio:
Core Audio is designed to handle all audio needs in Mac OS X. You can use Core Audio to generate, record, mix, edit, process, and play audio.

Playing two simultaneous m4a sounds with audioqueue

I need to mix two simultaneous looping m4a sounds for my application, and the only 100% reliable loop method i have come accross is using AudioQueue with this method: http://developer.apple.com/mac/library/qa/qa2009/qa1636.html
However, when I initialize two instances of AudioQueue, I can only seem to get one instance playing. I know that the sdk used to only support playing one compressed audio file at a time, but that changed with 3.0, so I wonder if there is something I am missing?
One current devices, there seems to be hardware support for only playing one compressed audio file at a time, and I'm not sure if m4a is de-compressible in software in real-time (there are only specific types supported by the software decoder). You might be able to decompress the second sound before playing your mixed audio loops.

Play midi file on the iPhone

Is it possible to play .mid files directly via some API, or one have to convert the midi file to e.g. AAC first?
(2 years later…) You can use MusicPlayer and MusicSequence APIs. Available in iOS 5.
There is no Apple API for this. You could write your own, which i think would depend on what you are hoping it is going sound like.
There is lots of available source code for reading midi files and there are a few open source synths for the iphone - or you could use openAl for triggering samples. It probably isn't going to sound like Garageband tho.
If you want it to sound as good as possible you will have to convert it first.

Sound format for iPhone app

What kind of audio files are you using in your iPhone games/apps?
I have a game with 30MB of sounds in .wav format and I'm thinking of maybe converting to .mp3 to reduce the app size... Is there a major difference in performance? Any other issues?
Keep in mind that certain codecs run in hardware and others in software. Therefore not all compressions will allow for simultaneous playback of more than one sound. For example, if you have a sound playing, a UI sound like a beep may not play if both were trying to use the same codec. For more info, see:
http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AudioandVideoTechnologies/AudioandVideoTechnologies.html#//apple_ref/doc/uid/TP40007072-CH19-SW6
iPhone Audio Hardware Codecs
iPhone OS applications can use a wide range of audio data formats. Starting in iPhone OS 3.0, most of these formats can use software-based encoding and decoding. You can simultaneously play multiple sounds in all formats, although for performance reasons you should consider which format is best in a given scenario. Hardware decoding generally entails less of a performance impact than software decoding.
The following iPhone OS audio formats can employ hardware decoding for playback:
AAC
ALAC (Apple Lossless)
MP3
The device can play only a single instance of one of these formats at a time through hardware. For example, if you are playing a stereo MP3 sound, a second simultaneous MP3 sound will use software decoding. Similarly, you cannot simultaneously play an AAC and an ALAC sound using hardware. If the iPod application is playing an AAC sound in the background, your application plays AAC, ALAC, and MP3 audio using software decoding.
To play multiple sounds with best performance, or to efficiently play sounds while the iPod is playing in the background, use linear PCM (uncompressed) or IMA4 (compressed) audio.
To learn how to check which hardware and software codecs are available on a device, read the discussion for the kAudioFormatProperty_HardwareCodecCapabilities constant in Audio Format Services Reference.
Both AAC and CAF formats work fine and offer decent file sizes. For certain background looping tracks I found MP3 files getting too big, but YMMV. Experimenting with a decent sound editing app is the only way to find the right balance between size and quality. I've had pretty good luck with Audacity and Amadeus Pro.
Suggest listening to the output with a pair of really good noise-isolating headphones on the device itself. Most people won't be listening to your stuff with these but as you decrease sound quality to shrink file sizes you'll start getting static and hum artifacts. It's just a matter of balancing size vs. quality and what you're willing to live with.
I use a combination of WAV files (for sound effects) and MP3 (for music), which seems to work fine. You can have trouble if you try to play multiple MP3 files at the same time - drop outs, or performance degradation, depending on your AudioSession settings.
If I had to compress my sound effects, I'm not sure which codec has the least decoding overhead. Something like Apple Lossless would likely work well, and would cut the size roughly in half.
I find mp3 fine, but keep in mind that decoding on the iPhone/Touch2G is only about 2.5x realtime speed.