iOS: Create an MP3 on device - iphone

I am trying to create an MP3 audio file on the device (iPhone).
I have found that there is no possibility to record directly in MP3 format.
Only .caf file that may contain AIFF, Linear PCM etc.
I have also found here that there is an AudioConverterFillComplexBuffer function that might, theoretically, convert CAF file to MP3, but I can't find any sample code or explanation on how to use this function.
Any help will be greatly appreciated.
--
Michael Kessler

Try using LAME - open source MP3 encoder library (liblame)
http://lame.sourceforge.net/

As far as I know LAME is GPL which may be an issue for you if you are building an iPhone app.
I believe AudioConverterFillComplexBuffer indeed is the way to go. You might want to take a look at the book "Core Audio Rough Cuts" which has explanations and examples on how to convert audio.
Good luck.

Related

Where to start for making a program to convert MP3 files to iphone audiobook (M4B) format?

I'm new to programming and want to find something that I can work on to help learn more about it. I want to do this is C++ if possible. What i want to do is start working on developing a program that has a user interface and will convert an mp3 into an m4b (the format iphone uses for audiobooks. I have been looking for some source code examples but have had no luck. If anyone can give me some places to start that would be great. Thanks
This is really trivial with the Audio Converter Services that are part of Core Audio.
I know it is not cool to just post a book reference but I really highly recommend Learning Core Audio: A Hands-on Guide to Audio Programming for Mac and iOS if you want to do Core Audio stuff.
Chapter 6 is all about audio conversion from one format to another.
M4B simply is a MP4 file (AAC encoded) with the extension M4B. So it basically is an MP4 file with a renamed file extension.
Perhaps consider using an encoding library such as ffmpeg.

Convert .wav file to .mp3

I want to convert an audio file saved with .wav file format to a file with .mp3 format.
Please suggest that how can I achieve this through Objective C coding.
Thanks in advance.
You'll have to use LAME to do this. Apple doesn't ship an MP3 encoder due to patent issues.
Take a look at the AVFoundation framework, especially the AVAssetWriter class. It’s very powerful, but it takes some time to digest. There is a nice blog entry by Chris Adamson on decoding songs from the iPod library to PCM. It’s exactly the other way you want it, but it’s a good start.
I don't think there is an API in AVFoundation for that. I think If you use AVAssetWritter you can write *.m4a (AVFileTypeAppleM4A) files which are *.acc, I believe. (ACC are lighter and better than mp3, so a good alternative to mp3)
If you use CoreAudio probably is going to be a lot much work but it should be possible to write mp3. I asked how to read mp3 using coreaudio in apple-lists like 2 years ago and I got code that makes speakHere sample app able to read *.mp3 ;)
Perhaps, you are interested: Newbie trying to play mp3 files instead wav files
CoreAudio framework has an API called Extended Audio File Services, which enables converting from one format to another. See here: http://developer.apple.com/library/ios/#samplecode/iPhoneExtAudioFileConvertTest/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009222-Intro-DontLinkElementID_2
Following on to sbooth's answer about LAME, there's a separate post on StackOverflow that gives you what you need to create a .a for the library:
How can I compile lame as static library(.a) for armv6 and armv7 of iPhone?
Once you have done that, follow LAME's docs and you're set.

Is it possible at all to record audio in AAC/MP4 format on the iPhone?

In this link it says it does:
http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html
However I have searched for an example that works on the web and can only see people complaining they can't get it to work. I have a working AudioQueue example for PCM but the very moment I switch this to AAC initialization fails. SpeakHere example also only uses PCM.
Has anyone ever managed to make this work or has a link to a code snippet that works?
Basically iPhone only record PCM. To get an AAC encoded file the original PCM stream should be stored into a AudioFile and then this AudioFile should be converted to AAC.
There's no way to record and convert on the fly on iPhone unless you include a third-party library that will do this for you.

Convert .m4r/.mp3 to .caf/.aiff in iphone app

As of right now I am using ffmpeg to convert from .mp3 --> to .aiff but I would like a native way to do this in xcode but i do not understand how to use AudioConverter.h in AudioToolbox soo if someone could please help me I would be really appreciative.
Thank You
Enea
ExtAudioFile can do a fantastic job of this. One reason to take some compressed formats like MP3 and convert to PCM is that there is much less of a latency when playing the sound. An MP3 sound effect, for example, can take close to a second to start playing back, when the same sound in PCM could start almost immediately. I know since I am converting my sounds to PCM at launch time and eliminating the delays I was plagued with.
Go download iPhoneExtAudioFileConvertTest and you will find everything you need to do your own conversions
I think the real question is why would you want to? The iphone and AudioQueue has full mp3 support for playback and mixing. I know of no way to do this on the fly in the iPhone. It is a fairly processor intensive task and I doubt you could do it efficiently on the iPhone anyway.
You can use ExtAudioFile to open and convert the file to CAF with PCM data.

How to record something other than Linear PCM on iPhone

I'm having a hard time trying to record something other than linear PCM on the iPhone :-(
The samples I've found (SDK's SpeakHere, Zdziarski's and Sadun's books and the one at trailsinthesand.com) all use linear PCM but I'd like a commonly used compressed format instead (no ima4 or whatever the name is...).
I just cannot figure out how to tweak the sample code to be used with, for example AAC, MP3 or AMR instead. Any suggestions and hints for how to do that are much appreciated!
(Btw, I do not think an MP3-encoder nor AMR-encoder are available due to licensing issues, but AAC does exist, or???)
Edit/Update: I stumbled upon the following text in Apple's "iPhone Application Programming Guide", 2009-01-06, page 137, section: Recording Audio:
"You can record audio in any of the formats listed in “Preferred Audio Formats in iPhone OS” (page 140)", and as preferred audio formats on page 140 are: "For compressed audio when playing one sound at a time, and when you don’t need to play audio simultaneously with the iPod application, use the AAC format packaged in a CAF or m4a file."
Thus, I interpret that as a clear indication that it is indeed, not only possible, but even preferable, to record audio in AAC format wrapped up in a m4a file, which is just what I want. But still, I am not able to achieve that?!
Thanks,
/John
Keep looking at those docs. In "Core Audio Essentials", the section "Core Audio Plug-ins: Audio Units and Codecs" notes that:
iPhone OS contains the recording
codecs listed in Table 2-5. As you can
see, neither MP3 nor AAC recording is
available. This is due to the high CPU
overhead, and consequent battery
drain, of these formats.
Table 2-5 lists several formats, but as the text notes does not include the ones you're looking for. If you want those formats you'll have to bring your own encoder.
It is possible in iPhone 3GS and iPod 2nd generation and above. They have a hardware enconder for AAC.
There is an Apple example project for this that does exactly what you want to do:
iPhoneExtAudioFileConvertTest
A tip for using faac to convert from iPhone-output linear pcm files to aac format:
I found by experimentation that you have to use the -X flag on the command-line version of faac (running on Mac OS X), which "swaps the input bytes." (I guess it changes the endian-ness.)
So, for example, if you audio recorded the file linear.pcm on your iPhone, you could then run:
faac -XP linear.pcm
on your mac to convert that into the aac file linear.aac.
I'm guessing this means you might be able to use faac within your app to do the conversion, if you wanted to, since it's a C library.
Also note that, technically, the files output by iPhone's audio services are usually CAF files, Core Audio Format, which is sort of a wrapper format around a bunch of possible encodings: Apple's docs on CAF. The above command line works fine for me even though linear.pcm starts with the caff header.
Have you tried Lame mp3 encoder? Or faac/faad AAC codecs? You can embed them in ffmpeg for even more audio codecs.
Why don't try TPAACAudioConverter written by the legendary Michael Tyson ?
TPAACAudioConverter is a simple Objective-C class that performs the
conversion of any audio file to an AAC-encoded m4a, asynchronously
with a delegate, or converts any audio provided by a data source class
(which provides for recording straight to AAC).