ofxPD and Audiobus

Hi there,
I’ve been trying to add Audiobus to an existing audio app.
I use ofxPD and pure data as the audio engine.
I’m having difficulties accessing the sound streams to be able to make the nesecary changes.
Does anyone have a working example of audio bus + of or even better of + pd + audio bus
Many Thanks,
Miles

IMO your best bet is probably to avoid the ofSoundStream and use something like The Amazing Audio Engine, EZAudio or just the AudioBus API directly. You can also look at ofxAudioUnit if you want to do something more DIY, but it’s iOS coverage is inconsistent.

You’ll probably end up fighting ofSoundStream far more than it’ll help, since it’s set up to magically handle audio for you automatically, but you want to do something that requires tight control.

It totally works. I can dig up some code for Audiobus 1.x.

@cerupcat Do you mind if I attach the ABiOSSoundStream class from Nodebeat? It’s working in NinjaJamm.

thanks @danomatika & @admsyn

ok, so it’d be best to stay away from using OF for audio, just using it for communication and the running of the pd patch ?

audiobus 2.x has been a big shift from 1.x tho, does NinjaJam work with AB2?

in terms of reconfiguring the project, would it be best for me to set up a new project, with of 008 and then set up all of the other dependencies and then reintegrate the app’s source code?

Thanks

No you don’t need to stay away from OF, not at all, You can do this with OF, you just need a layer that handles communication from OF sound stream to Audiobus using Obj-C.

AB is a big shift but the underlying layer of “give AB an audio unit and process stuff in there” is the same as far I know. Let me attach Seth’s class and you’ll see what I mean.

ABiOSSoundStream.zip (3.3 KB)

Here’s the ABiOSSoundStream. It’s a little old now and you’ll want to update the ofxAudiobus class to the newer AB 2.0 api, but you should get the idea.

The main trick is that you need to implement your own AppDelegate in order to set up things in Obj-C. Luckily, ofxiOS provides a way to start the app using a custom delegate.

You need to modify main.mm so it will launch using your own AppDelegate:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
ofSetupOpenGL(iOSWindow, 960, 640, OF_FULLSCREEN);
iOSWindow->startAppWithDelegate("AppDelegate");
[pool release];

In your AppDelegate.m, you can then setup AB using the AudioUnit inside the sound stream. Note that you have to create the app manually here since we’re using a custom app delegate and not the one inside ofxiOS.

The code below was not tested. You’ll need to pick up some Obj-C and UIKit understanding to fix it if it does’t work.

AppDelegate.h:

#import "ofxiOSAppDelegate.h"
#import "ofxiOSViewController.h"

// custom AppDelegate to implement SoundCloud, AudioBus, analytics, etc
@interface AppDelegate : ofxiOSAppDelegate

@property (nonatomic, strong) ofxiOSViewController * rootViewController;

@end

AppDelegate.mm:

#import "AppDelegate.h"
#import "ofxAudiobus.h"
#import "ofApp.h"

@interface AppDelegate()
@property (nonatomic, strong) ofxAudiobus * audiobus;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     [super applicationDidFinishLaunching:application];
     ofApp *app = new ofApp();
     self.glViewController = [[ofxiOSViewController alloc] initWithFrame:[[UIScreen mainScreen] bounds] app:app];
     [self.window setRootViewController:self.glViewController];
 
    SoundOutputStream *stream = app->getSoundStream()->getSoundOutStream();
    self.audiobus.audiobusAudioUnitWrapper.audioUnit = stream.audioUnit;
    self.audiobus = [[ofxAudiobus alloc] init];
    [self.audiobus initWithAppLaunchURL:@"YOUR_APP_NAME.audiobus://" 
                           apiKey:@"YOU_AB_API_KEY" audioUnit:stream.audioUnit];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    
     // if we're not connected to audiobus AND we're not doing background audio - stop sound
     if(!self.audiobus.audiobusController.connected){
         // stop pd?
     }
     else{
         ofxiOSSoundStream::setMixWithOtherApps(true);
     }
}

-(void)applicationWillTerminate:(UIApplication *)application {
    [super applicationWillTerminate:application];
    [self.audiobus terminate];
}

@end

You simply create a new ABiOSSoundStream and call setup(), like ofSoundStreamSetup, in you OF app setup() function.

ofApp.cpp::setup():

stream = new ABiOSSoundStream();
stream->setup(this, channels, 0, sampleRate, bufferSize, 0);

Allow access to the sound stream from your ofApp.

ofApp.h:

public:
...
ABiOSSoundStream* getSoundStream() {return stream;}
...
private:
ABiOSSoundStream *stream;
...
1 Like

sorry for the late reply to this, but thank you very much for the info.

so the app delegate etc is all working beautifully.

the one problem I’m having at the moment is pointing to the audioUnit.

there wasn’t a reference within ABIOSsoundstream, so i created and AudioUnit * audioUnit instance in there, but i don’t know how to apply the audio unit settings being setup in abiossoundstream to this.

I’ve been able to setup the ABIOSsoundstream etc ok, but theres no audiounit there for this call:

[self.audiobus initWithApiKey:AUDIOBUS_KEY audioUnit:stream->audioUnit];

is it a case of applying the abiossoundstream to the audioUnit so its accessible?

thanks

ABIOSsoundstream has the SoundInputStream & SoundOutputStream objects from ofxiOS which contain an audio unit aka the stream.audioUnit in the above AppDelegate code.

Im building this as a working example to share that uses ofxpd and audiobus and that works on oF 084 and Xcode 6
to get AB2 to work I’m building off of Seth’s SoundManager class. I have ofxpd working fine and audiobus initialising but using Null to bypass the setting of the audioUnit.
For me the audio unit is accessible within ABIOSSoundstream using the SoundOutputStream pointer, but i can’t seem to point to that pointer from the app delegate.
Ive been trying with this setup for a few days now and can’t crack it.
Im attaching the src files of what i have at the moment, it feels so close, but i think I’m starting to go a little :banana: :banana: tonight, so ill stop now and try and crunch it again tomorrow.
If someone could cast their eye over it that would be really appreciated, thanks again.

Archive.zip (13.4 KB)

ok, I’m getting burnt out with this now, I’ve been trying to get audio bus to work with ofxpd for a few months now…

@admsyn @danomatika @cerupcat I’m prepared to throw a bit of cash someones way to make this work now, i think @alkex is keen too

let me know if this is up your street.

best
Miles

I’m going to try and look into this this weekend since I hope to update my own apps soon too.

This all depends on a few things:

  • What version of OF you’re running
  • What version of Audiobus you’re running.

Oh, great!
I’m using oF 084 and Audiobus 2.1.5

Hi Guys,
Still blocked trying to initialise audiobus2, and as Miles said, happy to fork out some dosh for a hand. Really need to update to AB2 as ios8 doesn’t appear to support AB1. Thanks in advance for your help!

@alkex1982 @Miles If you guys want to jump on the #openframeworks IRC I can try and help you out. It sounds like the sort of issue that’d benefit from some quick back-and-forth.

I can’t really justify the time to dig into your full project and sort it out, but I’d be happy to talk through it with you.

Hello Admsyn,

Thanks for offering a hand, really appreciated. Let us know a time that suits you and I’ll hop on IRC.
Cheers
Alex

Hi, I’m an ofxPD user and I’m trying to implement audiobus for my app.

It seems like other audiobus apps are forcing to change the blocksize of ofxPD even without implementing audiobus at all.

So I’m considering to implement audiobus to fix this issue. but I wonder if it’s even possible to fix it.

Has anyone ever successfully implemented audiobus to work with ofxPD?

I’m using the latest OF 0.9.0 to compile with arm64.

I would really appreciate any feedbacks. Thanks. @Miles @danomatika

People have been able to make it work, I got close, but had to move onto other things. I came into blockSize and ticksPerBuffer issues etc tho…
@alkex1982 made a load of headway and was able to share a working example

How have you been able to get OF 0.9 to work with ofxPD?
I get this error when archiving or running on device - https://gist.github.com/Miles-Tone/e114223389d07d470fbf

Hey Miles,

I don’t remember exactly how I did it but I think I fixed it by drag&dropping ofxpd into the addon, not by project generator.

I attached an empty xcode project that contains ofxpd and ofxmidi. it works for me. try this and let me know if you still get errors.

I’m using the latest ofxpd and ofxmidi with OF 0.9.0.

I use xcode6 with default architecture which is armv7 & arm64.

EMPTYMAN.zip (134.4 KB)

1 Like

One thing I found regarding blocksize issue with audiobus is that audiobus apps seem to force ofxpd’s ticksPerBuffer to be multiplied 4 times of the original.

So for example, if you set ticksPerBuffer as 16, and setup the ofSoundStream.

int ticksPerBuffer = 16;
ofSoundStreamSetup(2, 1, this, 44100, ofxPd::blockSize()*ticksPerBuffer, 3);

and then if you initialize ofxPd with 1/4 of ticksPerBuffer which is 4 as below,

if(!pd.init(2, 1, 44100, ticksPerBuffer/4, false)) {
OF_EXIT_APP(1);
}

It no longer seemed to cause the blocksize mismatching issue.
But it works only if you have other audiobus compatible apps opened. without any other audiobus apps opened, it will still cause blocksize mismatching problem.

I really don’t know what is causing this and I’m still trying to fix this.
I would rather like ofxPd to not be affected by the other audiobus apps if there’s a simple way to do it.