Painless high-performance audio on iOS and Mac OS X

Related tags

Audio novocaine
Overview

An analgesic for high-performance audio on iOS and OSX.

Really fast audio in iOS and Mac OS X using Audio Units is hard, and will leave you scarred and bloody. What used to take days can now be done with just a few lines of code.

Getting Audio

Novocaine *audioManager = [Novocaine audioManager];
[audioManager setInputBlock:^(float *newAudio, UInt32 numSamples, UInt32 numChannels) {
	// Now you're getting audio from the microphone every 20 milliseconds or so. How's that for easy?
	// Audio comes in interleaved, so,
	// if numChannels = 2, newAudio[0] is channel 1, newAudio[1] is channel 2, newAudio[2] is channel 1, etc.
}];
[audioManager play];

Playing Audio

Novocaine *audioManager = [Novocaine audioManager];
[audioManager setOutputBlock:^(float *audioToPlay, UInt32 numSamples, UInt32 numChannels) {
	// All you have to do is put your audio into "audioToPlay".
}];
[audioManager play];

Does anybody actually use it?

Yep. Novocaine is result of three years of work on the audio engine of Octave, Fourier and oScope, a powerful suite of audio analysis apps. Please do check them out!

A thing to note:

The RingBuffer class is written in C++ to make things extra zippy, so the classes that use it will have to be Objective-C++. Change all the files that use RingBuffer from MyClass.m to MyClass.mm.

Want some examples?

Inside of ViewController.mm are a bunch of tiny little examples I wrote. Uncomment one and see how it sounds.
Do note, however, for examples involving play-through, that you should be using headphones. Having the
mic and speaker close to each other will produce some gnarly feedback.

Want to learn the nitty-gritty of Core Audio?

If you want to get down and dirty, if you want to get brave and get close to the hardware, I can only point you to the places where I learned how to do this stuff. Chris Adamson and Michael Tyson are two giants in the field of iOS audio, and they each wrote indispensable blog posts (this is Chris's, this is Michael's). Also, Chris Adamson now has a whole gosh-darned BOOK on Core Audio. I would have done unspeakable things to get my hands on this when I was first starting.

Analytics

Comments
  • output on iOS is mono

    output on iOS is mono

    Am I crazy? Has this always been the case and nobody noticed until now? Novocaine is outputting audio in mono through headphones. ioData->mBuffers[0].mNumberChannels is 1.

    If I figure out why, I'll make a note, and, with luck and a good attitude, commit some code.

    opened by morganpackard 31
  • Crash when launching from background while audio playing

    Crash when launching from background while audio playing

    Hi there, First of all let me thank you for open sourcing this. It looks like a very valuable tool coming from somebody who has suffered from CoreAudio coding.

    I've been checking out your library to see how robust it is in regards to backgrounding and interuptions etc.

    I've found a crashing bug that initially makes me think it isn't that robust but maybe you can comment on it.

    Testing on iPad 3 with iOS 5.1. Simple app that launches a recording queue with: audioManager = [Novocaine audioManager]; //This starts an input Block for recording. Set to audioManager.inputBlock=nil to stop audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) { NSLog(@"stillgoing:%d",(int)numFrames); };

    I tried going in and out of background and that appears to work fine (for short term at least).

    But if I send the app to background, then start playing audio from the stock Music app, then go back to the app running Novocaine I get this error in the console: Error: Checking number of input channels ('!cat')

    opened by hoddez 19
  • Repeated audio route change causes left and right headphones to get out of sync

    Repeated audio route change causes left and right headphones to get out of sync

    If you repeatedly take your headphones in and out really fast, the sound that is playing either becomes out of sync in the left and right headphones (usually), or the sound becomes extremely choppy (sometimes).

    I noticed that you do a lot of stuff when the audio route changes so I commented that part out and it ended up fixing both symptoms of the issue for me. I'm going to investigate further.

    What fixes it:

    • Commenting out [sm checkSessionProperties]

    What doesn't fix it:

    • Dispatching [sm checkSessionProperties] to another queue (WEIRD!)
    • Commenting out all of the NSLogs
    • Commenting out the CheckErrors
    • Commenting out [self checkAudioSource] in checkSessionProperties

    SO I think the problem is all of the calls to AudioSessionGetProperty when the audioRoute changes. What I think you should ACTUALLY do is have custom getters for numInputChannels, numOutputChannels, etc. And when the audio route changes, set all of them to -1. When the getter is called, and if they are set to -1, THEN it calls AudioSessionGetProperty.

    edit: Nevermind, I tried implementing the above and it didn't fix the problem. I'm stumped.

    opened by rweichler 13
  • Including the NOVOCAINE into projects

    Including the NOVOCAINE into projects

    I have difficulty when I include the novocaine into my project. I get the Unknown type name 'class' error. If i have Novocaine as a project as downloaded from the Github - I am able to get it running. Any help?

    opened by Baskaran 10
  • Warnings and Link Errors

    Warnings and Link Errors

    When I tried to incorporate novocaine into my own project I get a lot of tricky warnings and link errors. It's kind of neat how you've integrated C++ and Obj-C++ into a minimal number of source files, but I wonder if you're confusing the compiler/linker

    1. I get warnings which I frankly do not understand surrounding the blocks. ("Capturing 'self' ...")

    2. Probably related: I get warnings about these declarations:

    @property (nonatomic, retain) OutputBlock outputBlock; @property (nonatomic, retain) InputBlock inputBlock;

    It wants me to use copy instead of retain.

    I assume that my project settings are wrong in some subtle way (I've tried to match them to yours) — but I want to understand what's going on since I need to integrate this stuff into a much larger project.

    opened by tonioloewald 10
  • What would be the right way?

    What would be the right way?

    The guide says "All you have to do is put your audio into "audioToPlay". What would be the appropriate way to do it? I have a NSData with the audio.

    Thank you very much!

    opened by Rjonhson 8
  • ARC-compatibility and modernization of Obj-c interfaces

    ARC-compatibility and modernization of Obj-c interfaces

    @alexbw I want to get your eyes on this, I've been using it for my own projects but I want to make sure I didn't miss anything and that this is a direction you're comfortable with.

    I tested all the demos on both iOS and OSX and they all seem to work, but you know the project much better than I do, so it's entirely possible that I missed something.

    If you'd prefer to keep this in its own branch for awhile that is also fine.

    The primary changes are as follows

    Code Style and Fixes
    • Uses ARC right out of the box
    • Removal of all ivars that were backing properties, in favor of auto-synthesized properties (much cleaner, works on XCode 4.4+)
    • Moved several properties/methods to class continuation in source file to make them "private"
      • Several things that don't need public access
    • Several other properties made readonly in public interface to prevent fiddling (for things that users should never set)
    • Block setters/getters explicitly declared so that auto-complete will fill in the arguments for you
    • Replaced deprecated AudioProperty getters for OSX (enumerating devices, etc)
    Functional Changes
    • Ability to route output to speaker on iPhone (boolean flag)
    • Must explicitly call "play" to hear audio
      • I wasn't sure if you'd go for this one or not. It will definitely have an impact but it makes pre-initialization config options possible, since before it would automatically play the first time you access the singleton.
    TODO
    • Ability to disable input processing entirely (commented out right now, not implemented yet)
      • Requiring manual call to "play" allows configuring this prior to starting AU
    • More advanced audio session/routing capabilities
      • Choosing output routes, etc
      • LOTS of things are available in newer OS versions, for example, audio routing to external devices, etc

    Thanks!

    opened by ndonald2 8
  • How to change sample rate

    How to change sample rate

    Hi, could you explain how to change current sample rate?

    Lines inputFormat.mSampleRate = 44100.0; outputFormat.mSampleRate = 44100.0;

    dont affect to this

    thanks

    opened by soulfly 7
  • Getting Novocaine to work with OSX and xcode 5

    Getting Novocaine to work with OSX and xcode 5

    Ok so when I just add the Novocaine folder with all the library files and I build it gives me automatic reference counting errors. What build settings should I be using?

    opened by neox38 6
  • Multiple runs of file write example gives error -48 duplicate filename on second execution and all following

    Multiple runs of file write example gives error -48 duplicate filename on second execution and all following

    Hi

    When I uncomment the filewrite example code in the demo view controller, on the first run everything is fine, but on the second run,

    CheckError(ExtAudioFileCreateWithURL(audioFileRef, kAudioFileM4AType, &outputFileDesc, NULL, 0, &_outputFile), "Creating file");

    exits the app with error code -48, which is declared in MacErrors.h as duplicate filename.

    I will try to find a work around this and post it here.

    best

    opened by ghost 6
  • adding Novocaine to my project

    adding Novocaine to my project

    Hi, First off I would like to say great work and thank you for contributing this open source project, it is much appreciated by many of us.

    I'm having a little trouble adding this to my iOS project, and actually i'm not quite sure if this is the best solution for me so I thought I would ask you since you seem to know a good bit about audio.

    I am designing an application for kids that allows them to play along with certain songs/stories. I dont need to do any complicated audio processing, all the sounds are going to be pre recorded samples in specific keys. The only manipulation to the audio that I'll need is that I'll need to play up to 6 different audio clips at the same time and I will need to be able to adjust the levels of each audio track.

    I tried adding your code to my project but I am having trouble with dependencies... and the whole .mm thing. If you could include a short guide on the easiest way to add this to an existing project it would be greatly appreciated.

    opened by rossbertsch 6
  • Mac Catalyst (UIKit for Mac) support

    Mac Catalyst (UIKit for Mac) support

    Hi there;

    I'm working on porting an iOS libpd project to Mac using Mac Catalyst, and I'm currently looking for a CoreAudio wrapper to process input/output samples in libpd from an Audio Unit. I was referred to this project by the main libpd dev; and while I was able to get Novocaine's sample projects to work, the project exits on launch in my Mac Catalyst project after calling [Novocaine audioManager].

    Here's the error I receive:

     [as] AudioSessionClientImpl.mm:232:-[AudioSessionClientImpl AudioSessionGetPropertyImpl:size:data:]: Error: AudioSession not initialized
    Error: Couldn't check the audio route ('!ini')
    

    CoreAudio seems to be pretty tricky on the Catalyst platform because its not really the Mac platform, and not really iOS. If you had any suggestions for possible workarounds please let me know — otherwise I'll keep researching ✌️

    opened by virusys 0
  • Don't hardcode the sampling rate, get it from the hardware

    Don't hardcode the sampling rate, get it from the hardware

    For iOS a sampling rate of 44,100 Hz is hardcoded. For newer iOS devices the sampling rate is 48,000 Hz. This causes a crash on setup. Sampling rate is for iOS already determined from the Hardware.

    opened by baspellis 1
  • How to 8000hz/16000hz get or put as short* array for audio ?

    How to 8000hz/16000hz get or put as short* array for audio ?

    Hi , is there any way to set audio sample rate to 8000hz or 16000hz and i want to get all array as short array.

    [audioManager setInputBlock:^(float *newAudio, UInt32 numSamples, UInt32 numChannels)

    And whats the float range ? is it returning between [-1,1] ? I need in short like this: [audioManager setInputBlock:^(short *newAudio, UInt32 numSamples, UInt32 numChannels)

    opened by eix128 0
  • RingBuffer.h - class RingBuffer syntax error

    RingBuffer.h - class RingBuffer syntax error

    Sorry if this a stupid question. Just wondering if anyone could shred a light on this... I've imported all Novocaine files to my project and renamed the files that will use Novocaine to .mm Just this error on RingBuffer.h refuses to go away during compiling:

    RingBuffer.h:31:1: Unknown type name 'class'; did you mean 'Class'? Parse Issue: RingBuffer.h:31:17: Expected ';' after top level declarator

    Is it anything I could be missing, like a compiler setting or a flag option somewhere else? I'm using Xcode 8.33 and iOS deployment target 10. Thanks in advance.

    opened by beecon 3
AudiosPlugin is a Godot iOS Audio Plugin that resolves the audio recording issue in iOS for Godot Engine.

This plugin solves the Godot game engine audio recording and playback issue in iOS devices. Please open the Audios Plugin XCode Project and compile the project. You can also use the libaudios_plugin.a binary in your project.

null 3 Dec 22, 2022
The Amazing Audio Engine is a sophisticated framework for iOS audio applications, built so you don't have to.

Important Notice: The Amazing Audio Engine has been retired. See the announcement here The Amazing Audio Engine The Amazing Audio Engine is a sophisti

null 523 Nov 12, 2022
Creating Custom Audio Effects - Simple Universal Version (Mac Catalyst)

Creating Custom Audio Effects - Simple Universal Version (Mac Catalyst) Adaptation of the sample provided by Apple Creating Custom Audio Effects. Ever

Fred Anton Corvest (FAC) 19 Nov 2, 2022
AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS, and tvOS.

AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 9.5k Dec 31, 2022
Voice Memos is an audio recorder App for iPhone and iPad that covers some of the new technologies and APIs introduced in iOS 8 written in Swift.

VoiceMemos Voice Memos is a voice recorder App for iPhone and iPad that covers some of the new technologies and APIs introduced in iOS 8 written in Sw

Zhouqi Mo 322 Aug 4, 2022
AudioPlayer is a simple class for playing audio in iOS, macOS and tvOS apps.

AudioPlayer AudioPlayer is a simple class for playing audio in iOS, macOS and tvOS apps.

Tom Baranes 260 Nov 27, 2022
Swift audio synthesis, processing, & analysis platform for iOS, macOS and tvOS

AudioKit AudioKit is an audio synthesis, processing, and analysis platform for iOS, macOS (including Catalyst), and tvOS. Installation To add AudioKit

AudioKit 8.7k Sep 30, 2021
Audio Filters on iOS and OSX

Audio Filters on iOS and OSX Implement high quality audio filters with just a few lines of code and Novocaine, or your own audio library of choice. NV

Bart Olsthoorn 411 Dec 16, 2022
AudioPlayer is syntax and feature sugar over AVPlayer. It plays your audio files (local & remote).

AudioPlayer AudioPlayer is a wrapper around AVPlayer. It also offers cool features such as: Quality control based on number of interruption (buffering

Kevin Delannoy 676 Dec 25, 2022
YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos.

YiVideoEditor YiVideoEditor is a library for rotating, cropping, adding layers (watermark) and as well as adding audio (music) to the videos. YiVideoE

coderyi 97 Dec 14, 2022
App for adding and listening audio files

SomeSa SomeSa (самса) – приложение, позволяющее загружать и воспроизводить произвольные аудиофайлы. Протестировано на форматах файлов .wav и .mp3, раз

Yegor Dobrodeyev 0 Nov 7, 2021
Simple command line utility for switching audio inputs and outputs on macOS

Switch Audio Simple command line utility for switching audio inputs and outputs

Daniel Hladík 3 Nov 22, 2022
This app demonstrates how to use the Google Cloud Speech API and Apple on-device Speech library to recognize speech in live recorded audio.

SpeechRecognitionIOS This app demonstrates how to use Google Cloud Speech API and Apple on-device Speech library to recognize speech in live audio rec

Josh Uvi 0 Mar 11, 2022
Beethoven is an audio processing Swift library

Beethoven is an audio processing Swift library that provides an easy-to-use interface to solve an age-old problem of pitch detection of musical signals.

Vadym Markov 735 Dec 24, 2022
FDWaveformView is an easy way to display an audio waveform in your app

FDWaveformView is an easy way to display an audio waveform in your app. It is a nice visualization to show a playing audio file or to select a position in a file.

William Entriken 1.1k Dec 21, 2022
SwiftAudioPlayer - Swift-based audio player with AVAudioEngine as its base

SwiftAudioPlayer Swift-based audio player with AVAudioEngine as its base. Allows for: streaming online audio, playing local file, changing audio speed

null 417 Jan 7, 2023
A drop-in universal library allows to record audio within the app with a nice User Interface.

IQAudioRecorderController IQAudioRecorderController is a drop-in universal library allows to record and crop audio within the app with a nice User Int

Mohd Iftekhar Qurashi 637 Nov 17, 2022
Audio visualisation of song

SonogramView Audio visualisation of song Requirements iOS 8.0+ macOS 10.10+ Xcode 8.0+ Installation: Manually First Check SonogramView.swift or MacSon

Gleb Karpushkin 66 Nov 3, 2022
AIB indicates for your app users which audio is playing. Just like the Podcasts app.

Audio Indicator Bars for iOS and tvOS Indicates for your app users which audio is playing. Just like the Podcasts app. Index Requirements and Details

Leonardo Cardoso 285 Nov 23, 2022