Coder Social home page Coder Social logo

tpaacaudioconverter's Introduction

Objective-C wrapper for AAC audio conversion

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).

Introduction

From the iPhone 3Gs up, it's possible to encode compressed AAC audio from PCM audio data. That means great things for apps that deal with audio sharing and transmission, as the audio can be sent in compressed form, rather than sending huge PCM audio files over the network.

Apple's produced some sample code (iPhoneExtAudioFileConvertTest), which demonstrates how it's done, but their implementation isn't particularly easy to use in existing projects, as it requires some wrapping to make it play nice.

Hence, TPAACAudioConverter: A simple to use Objective-C wrapper.

Usage

  • Include the class in your project, and make sure you've got the AudioToolbox framework added, too.
  • Audio session setup:

If you already have an audio session set up in your app, make sure you disable mixing with other device audio for the duration of the copy operation, as this stops the hardware encoder from working (you'll see funny errors like kAudioQueueErr_InvalidCodecAccess (Error 66672)). I know that AVAudioSessionCategoryPlayAndRecord, AVAudioSessionCategorySoloAmbient and AVAudioSessionCategoryAudioProcessing work for sure. TPAACAudioConverter will automatically disable kAudioSessionProperty_OverrideCategoryMixWithOthers, if it's set.

If you're not already setting up an audio session, you could do so just before you start the conversion process.

You'll need to provide an interruption handler to be notified of audio session interruptions, which impact the encoding process. You'll also need to create a member variable to store the converter instance, so you can tell it when interruptions begin and end (via interrupt and resume).

// Callback to be notified of audio session interruptions (which have an impact on the conversion process)
static void interruptionListener(void *inClientData, UInt32 inInterruption)
{
	AACConverterViewController *THIS = (AACConverterViewController *)inClientData;

	if (inInterruption == kAudioSessionEndInterruption) {
		// make sure we are again the active session
		checkResult(AudioSessionSetActive(true), "resume audio session");
        if ( THIS->audioConverter ) [THIS->audioConverter resume];
	}

	if (inInterruption == kAudioSessionBeginInterruption) {
        if ( THIS->audioConverter ) [THIS->audioConverter interrupt];
    }
}

/*snip*/

-(void)startConverting {

    /*snip*/

    // Initialise audio session, and register an interruption listener, important for AAC conversion
    if ( !checkResult(AudioSessionInitialize(NULL, NULL, interruptionListener, self), "initialise audio session") ) {
        [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"")
                                     message:NSLocalizedString(@"Couldn't initialise audio session!", @"")
                                    delegate:nil
                           cancelButtonTitle:nil
                           otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show];
        return;
    }


    // Set up an audio session compatible with AAC conversion.  Note that AAC conversion is incompatible with any session that provides mixing with other device audio.
    UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;
    if ( !checkResult(AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(audioCategory), &audioCategory), "setup session category") ) {
        [[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Converting audio", @"")
                                     message:NSLocalizedString(@"Couldn't setup audio category!", @"")
                                    delegate:nil
                           cancelButtonTitle:nil
                           otherButtonTitles:NSLocalizedString(@"OK", @""), nil] autorelease] show];
        return;
    } 


    /*snip*/
}
  • Make the relevant view controller implement the TPAACAudioConverterDelegate protocol: That means implementing AACAudioConverterDidFinishConversion:, and AACAudioConverter:didFailWithError:, and optionally AACAudioConverter:didMakeProgress: to receive progress updates.

  • Create an instance of the converter, pass it the view controller as the delegate, and call start:

    audioConverter = [[[TPAACAudioConverter alloc] initWithDelegate:self source:mySourcePath destination:myDestinationPath] autorelease];

    [audioConverter start];

Alternatively, if you wish to encode live audio, or provide another source of audio data, you can implement the TPAACAudioConverterDataSource protocol, which defines AACAudioConverter:nextBytes:length:, which provides a buffer to copy at most "length" bytes of audio into, and then expects you to update "length" to the amount of bytes provided. For that you'll need to use the second initialiser, initWithDelegate:dataSource:audioFormat:destination:.

License

This code is licensed under the terms of the MIT license.

Michael Tyson
A Tasty Pixel

tpaacaudioconverter's People

Contributors

k4ety avatar michaeltyson avatar zaphodddd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tpaacaudioconverter's Issues

Two things

Hi, thanks for this awesome class! I have a couple questions-

  1. Is it possible to convert to mp3? From what I can tell from Apple's documentation it seems like you can't (See Table 1.2 in this reference) but I wonder if you've tried it.
  2. Could you convert this project to ARC? I converted the class, and there are two places where Xcode wants to fix a cast but does so incorrectly, leaving out a closing parenthesis. Most people will want ARC version of the class, so that would be helpful.

Thanks again.

Can't support iOS7.1

when i use iPhone 4s iOS7.1 to test Converter.
It crash on this code "!checkResult(ExtAudioFileSetProperty(destinationFile, kExtAudioFileProperty_ClientDataFormat, size, &clientFormat),
"ExtAudioFileSetProperty(destinationFile, kExtAudioFileProperty_ClientDataFormat")" at TPAACAudioConverter.m

Create m4a that's optimized for streaming.

Hi there,

I'm using this class to convert WAV files to m4a for hosting and streaming. It mostly works perfectly, however there's a few files that seem to fail with all the typical streaming solutions for iOS (FreeStreamer / StreamingKit) - with either UnexpectedErrror or a log message about how the file is not optimized for streaming.

Looking into it further it seems that longer files - (over 5 minutes) are the culprits.

I wondered if you had any idea about this ?

Thanks

Jonah

Support build App

I've just downloaded your sour code.However, there is an error that I got from xcode.

/TPAACAudioConverter/TPAACAudioConverter.m:91: error: '_condition' undeclared (first use in this function)

Can you help me? I expect your help.

Stereo to Mono conversion

Hi,

After spending days looking all over the internet, I have found nothing. But this class seems to be able to quickly do what I need, to convert a Stereo sound (music) to Mono.

Is it possible to combine both channels, left and right? So, add channel 1 to channel 2 and vice versa.

TPAACAudioConverter breaks after iOS 7 updates

i have issues with using TPAACAudioConverter once i updated my iPhone to iOS 7.

error given “setup session category result 560557673 21696E69 ini!” error given “initialise audio session result 1768843636 696E6974 tini”

how can i fix it ? and sometimes it works.

Please help admin !

Write data to buffer

Any suggestion on how to access encoded data? I need to write to socket instead of file.

Bad error message on line 248?

Hi.
Is it just me, or should the error message formulated on line 248 of TPAACAudioConverter.m read
"Couldn't open the destination file" rather than "Couldn't open the source file"?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.