Attention! For Japanese customers, my Japanese blog might help.
Showing posts with label AudioUnit. Show all posts
Showing posts with label AudioUnit. Show all posts

Oct 16, 2017

Steps to make your homemade V3 AudioUnit support in-process loading

NOTE: This is the English version of my Japanese article.

To make my homemade AudioUnit available in GarageBand in the last article,
It seems necessary to support in-process loading.

As a result of trial and error, I was able to release GarageBand compatible version.
After all, I imitated AudioUnitV3Example's implementation.
Here is a rough modification procedure.

Steps:
1: Make the new cocoa framework target for AU.
2: Move all implementation files of AU (.appex) target to the framework target. However, .xib and image resources need to be included in both targets.
3: Prepare a .m file and implement a dummy function. Include it in the .appex target.
4: Edit Info.plist of .appex. Add a key named "AudioComponentBundle" to NSExtension -> NSExtensionAttributes and set the bundle identifier of the framework target as a value.
5: Override the init of the AU view controller with reference to the sample code FilterDemoViewController.m. In my case, it is as follows:
- (id)init {
    self = [super initWithNibName:@"AudioUnitViewController"
                           bundle:[NSBundle bundleForClass:NSClassFromString(@"AudioUnitViewController")]];
    return self;
}
Without this we could not display the view with GarageBand.
I think that handling AU of GarageBand may be different from that of sample code.

Other tips:
If you use external frameworks, which were already used before supporting in-process loading,
it seems better to embed them in both the .app and the .framework.
If the external frameworks are not embeded in .appex, GarageBand may not be able to load your AU with an error like the following:
Error loading {...snipped...}/SYVibratoFramework:  dlopen({...
snipped...}/SYVibratoApp.app/Contents/Frameworks/SYVibratoFramework.framework/SYVibratoFramework, 265): Library not loaded: @rpath/SYFW.framework/Versions/A/SYFW
  Referenced from: {...
snipped...}/SYVibratoApp.app/Contents/Frameworks/SYVibratoFramework.framework/SYVibratoFramework
  Reason: image not found
Perhaps you can solve it without embedding if you can set @rpath.


--
I'm looking for job for macOS/iOS application development by outsourcing.
For details, please visit my web site.

[Solved] My homemade V3 AudioUnit could not be loaded on GarageBand on macOS 10.13

NOTE: This is the English version of my Japanese article.

- 2017/10/14 NOTE -
I have posted a solution.
- 2017/10/14 End of NOTE -

summary:
It seems that my AU needs to support "in-process loading" to use in GarageBand.
For that, the all of AU implementation should be moved to a homemade cocoa framework.

Details:
When I was testing my homemade AU on macOS 10.13, I found that GarageBand can not load my AU normally.
Although the AU was displayed in the list, a surprise mark was displayed when my AU was selected.



Even if you click it, AU's view was not displayed.
And, the effect functionality was also worked.

Although I can not identify the cause yet, it is probably because it is because my original AU can not handle in-process reading.

Reading logs in Console.app, I confirmed that GarageBand was outputting an error:
254: Extension <private> advertises loadable bundle <private>, but we couldn't find it
To solve this error, I rechecked build settings, Info.plist, Sandbox related settings, etc...
But I could not solve it.

In subsequent investigation with my AU containing app, if kAudioComponentInstantiation_LoadInProcess is specified as the second argument of + [AVAudioUnit instantiateWithComponentDescription: options: completionHandler:],
I found that similar errors are displayed.
Apparently, GarageBand seems to be trying to load my AU in-process.

Supporting in-process loading:
It seems necessary to provide all the implementation of self-made AU as .framework in order to make homemade AU support in-process loading.
The crew is commented out in FilterDemoExtension.m in the sample code "AudioUnitV3ExampleABasicAudioUnitExtensionandHostImplementation":
To facilitate loading in-process and into a separate extension process, the .appx main binary cannot contain any code. Therefore, all the plugin functionality is contained in the FilterDemo.framework and the .appx Info.plist contains an AudioComponentBundle entry specifying the frameworks bundle identifier.
In fact, FilterDemoExtension.m has only a function called dummy (), and all AU implementations seem to be thrust into FilterDemoFramework.framework.

From now on, when we created the new V3 AudioUnit project, it may be better to use this sample code as a template.
I confirmed that the sample AU (FilterDemo) could be used in GarageBand without problems.

Also, as you can see in this comment, bundle identifier seems to need a bit of ingenuity,
I do not know well so I would like to investigate in the future.
It seems that bundle identifier in the sample code was changed compared to older one.
So, it may be necessary change to support in-process loading.

To be continued...

--
I'm looking for job for macOS/iOS application development by outsourcing.
For details, please visit my web site.

Homemade V3 AudioUnit did not work on a third party host application.

NOTE: This is the English version of my Japanese article.

While verifying a homemade AudioUnit on macOS 10.13, an error was displayed with auval command.
The error looks like the following:

2017-10-10 11:45:27.172 auvaltool[508:5335] Error: Session 091F49F0-C167-4CFB-820E-4DBCC049FD3B was unable to communicate with the remote service: Error Domain=NSCocoaErrorDomain Code=4099 "The connection from pid 512 was interrupted, but the message was sent over an additional proxy and therefore this proxy has become invalid." UserInfo={NSDebugDescription=The connection from pid 512 was interrupted, but the message was sent over an additional proxy and therefore this proxy become invalid.}


As a result of the investigation, the error is because my AU did not link to my own framework.
 I solved the problem by linking AU to my framework(adding it to [Build Phases]-[Link Binary With Library] on AU's target setting on Xcode).



I have no idea why this problem did not occur in the past build.

--
I'm looking for job for macOS/iOS application development by outsourcing.
For details, please visit my web site.

Could not create the view of homemade V3 AudioUnit

NOTE: This is the English version of my Japanese article.

As mentioned in this article, although I could make an AVAudioUnit instance,  the view controller for the AU  was not created this time.

I have used -[AUAudioUnit requestViewControllerWithCompletionHandler:] to create the view controller, but "viewController" in completionHandler was always nil.

By referring to the implementation of -[ViewController embedPlugInView] in an Apple's sample code "AudioUnitV3Example", I fixed my code to make a view directly from a bundle.
Finally, I could create a view controller.

My implementation of embedPlugInView is as follows:

- (void)embedPlugInView {
    NSURL *builtInPlugInURL = [[NSBundle mainBundlebuiltInPlugInsURL];
    NSURL *pluginURL = [builtInPlugInURL URLByAppendingPathComponent@"SYVibrato.appex"];
    NSBundle *appExtensionBundle = [NSBundle bundleWithURL: pluginURL];
  
    self.vibratoViewController = [[AudioUnitViewController allocinitWithNibName:@"AudioUnitViewController"                                                                           bundle:appExtensionBundle];
  
    [self.vibratoProgressIndicator stopAnimation:self];    NSRect r = self.vibratoView.frame;
    [self.vibratoViewController view].frame = NSMakeRect(00NSWidth(r), NSHeight(r));    [self.vibratoView addSubview:self.vibratoViewController.view];  
    [self.vibratoViewController connectAU:self.vibratoUnit.AUAudioUnit];}

Note that, if your AU can be recognized with a third party host apps, you can create a view with the conventional method requestViewControllerWithCompletionHandler: as well.

Because AU cannot be recognized(installed) immediately on macOS 10.13, it may be that such a correction has become necessary.

To avoid wasting time, we had better use projects in "AudioUnitV3Example" as a template.

--
I'm looking for job for macOS/iOS application development by outsourcing.
For details, please visit my web site.

Recognizing homemade V3 AudioUnit on third-party AU host apps on macOS 10.13

NOTE: This is the English version of my Japanese article.

To recognize v3 AudioUnits on macOS 10.13, it seemed that it is not enough to just launch an AU containing application.
To be recognized,

  1. Confirm that your AU is not listed by a command 'auval -a'
  2. Build and launch your AU containing application
  3. Log out or restart the machine
  4. log in
  5. Confirm that your AU is listed by 'auval -a'
Re-login is probably important and there may be no need to launch your application.(more survey is needed)
Conversely, if you do not want to recognize your AU any more,

  1. Confirm that your AU is listed by 'auval -a'
  2. Delete your AU containing application (delete also from trash box)
  3. Log out or restart the machine
  4. log in
  5. Confirm that your AU is not listed by 'auval -a'
If there are multiple same applications, it seems necessary to delete them all.
For example, Debug Build, Release Build, and Archive Build are generated in a separate directory (~/Library/Xcode/DerivedData/...)
So, you have to delete all of them.
(It should be noted that even if Clean is executed on Xcode, the build of other configurations will not be deleted.)


--
I'm looking for job for macOS/iOS application development by outsourcing.
For details, please visit my web site.

Oct 15, 2017

Homemade V3 AudioUnit did NOT work on macOS 10.13

NOTE: This is the English version of my Japanese article.

My development environment is Xcode 9.0 on macOS 10.13.

Can't instantiate AVAudioUnit

My AU seemed to fail instantiating an AVAudioUnit at +[AVAudioUnit instantiateWithComponentDescription:options:completionHandler:].

An 'error' variable in completionHandler was
Error Domain=NSOSStatusErrorDomain Code=-3000 "invalidComponentID"
After a struggle, I found the error could be removed by turning off Sandboxing of my containing app.
FYI,  Apple's sample code "AudioUnitV3Example" also disables Sandbox for host apps.

Note that, even if Sandbox is enabled, I could make AUAudioUnit instance using +[AUAudioUnit instantiateWithComponentDescription:options:completionHandler].
So, if Sandbox environment is required and AVAudioUnit is not, the method will be better.
And more, I tried AudioComponentInstanceNew(), but failed on Sandbox environment.

Can't make AUViewController

Next to the problem above, there was another problem that an AU view was not displayed.

After a call of -[AUAudioUnit requestViewControllerWithCompletionHandler:],
'viewController' variable in completionHandler was nil.

It seemed that the controller instance was never created.

In the sample code "AudioUnitV3Example",  the controller seems to be created by using a bundle directly in -[ViewController embedPlugInView].
So, I will imitate that implementation. (not started yet)

Remarks

On 10.13, even if an AU containing app is activated, the AU is not shown in the list by a command 'auval -a'.
This means other host apps can't use my AU. So I need to resolve the problem too.

It's a bother!

To be continued...

--
I'm looking for job for macOS/iOS application development by outsourcing.
For details, please visit http://shakeyama.com/about/index_e.php.

Aug 5, 2016

I wanted an AUGenericView for handemade v3 AudioUnit

NOTE: This is the english version of my Japanese article.

As shown in the title, I wanted to get an AUGenericView of my own v3 AudioUnit.
I could not get the view from an AUAudioUnit object because AUGenericView's initializer requires the instance of type 'AudioUnit'.
As far as I know, there was no way to get an AudioUnit instance from an AUAudioUnit object.

While, I could get an AudioUnit instance from AVAudioUnit object, which was supplied by AVFoundation framework.

So I tried to make a host app and made the AUGenericView of my own AU.
The code is following:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    AudioComponentDescription desc;
    desc.componentType = 'aufx';
    desc.componentSubType = 'vbrt';
    desc.componentManufacturer = 'Symi';
    [AVAudioUnit instantiateWithComponentDescription:desc
                                             options:0
                                   completionHandler:^(__kindof AVAudioUnit * _Nullable audioUnit, NSError * _Nullable error) {
                                       self.unit = audioUnit;
                                       AudioUnit unit = [audioUnit audioUnit];
                                       AUGenericView *view = [[AUGenericView allocinitWithAudioUnit:unit];
                                       view.frame = self.window.contentView.frame;
                                       [self.window.contentView addSubview:view];
                                   }];
}
As a result:


Altough I could get the view, no parameters were displayed.
By comparing my AU with FilterDemo, I found that it was needed to give kAudioUnitParameterFlag_IsReadable and kAudioUnitParameterFlag_IsWritableAUParameter flags when creating AUParameter for my AU as follows:
AUParameter *freqParameter = [AUParameterTree createParameterWithIdentifier:VibratoParameterIdentifierFrequency
                                                                           name:VibratoParameterNameFrequency
                                                                        address:VibratoParameterAddressFrequency
                                                                            min:0
                                                                            max:.01
                                                                           unit:kAudioUnitParameterUnit_Hertz
                                                                       unitName:nil
                                                                          flags:kAudioUnitParameterFlag_IsReadable | kAudioUnitParameterFlag_IsWritable
                                                                   valueStrings:nil
                                                            dependentParameters:nil];
I again ran the host app, then
YES! I could finally get a view like Apple's AUs!

By the way, when I set only a writable flag to Depth and only a readable flag to Frequency,
the generic view were:
Depth was adjustable, but Frequency was not.
Hmm...

Summary:
  • When you want to get an AUGenericView for v3 AU in your host app, you can get an 'AudioUnit' instance from an AVAudioUnit object.
  • As for your own AU, you need to set kAudioUnitParameterFlag_IsReadable | kAudioUnitParameterFlag_IsWritable flags when creating AUParameter.
That's all for today.



Aug 4, 2016

Display a custom UI of my own v3 AudioUnit in host applications

This is the sequel article of previous one.

I was looking for the reason why my v3 AudioUnit can not display a custom UI in host apps such as AUv3Host or Hosting AU...

As as result, It is seemed that my AU needs to set preferredContentSize property.
In FilterDemo, the property seems to be set in viewDidLoad in FilterDemoViewController.
So, I also set the property as follows:
- (void) viewDidLoad {
    [super viewDidLoad];
    
    self.preferredContentSize = NSMakeSize(480272);
}
By the way, in the code which was generated when we added AudioUnit App Extension,
viewDidLoad implementation was
- (void) viewDidLoad {
    [super viewDidLoad];
    
    if (!audioUnit) {
        return;
    }
    
    // Get the parameter tree and add observers for any parameters that the UI needs to keep in sync with the AudioUnit
}
However,  because the audioUnit instance was always nil, the program immediately returns from viewDidLoad.
The audioUnit object is set in createAudioUnitWithComponentDescription:error:, which conforms to AUAudioUnitFactory protocol as follows:
- (AUAudioUnit *)createAudioUnitWithComponentDescription:(AudioComponentDescription)desc error:(NSError **)error {
    audioUnit = [[MyAudioUnit allocinitWithComponentDescription:desc error:error];
    
    return audioUnit;
}
But, because viewDidLoad is called earlier than when createAudioUnitWithComponentDescription:error: is called, audioUnit property remains nil in viewDidLoad.

I doubt that codes automatically generated by AudioUnit App Extension target are strange (not qualified as standard AudioUnit).

I have wasted a time because I suspected auto layout or other factors.

Next, I want to add UIs to the custom view.






Aug 3, 2016

How to add parameters to your own v3 AudioUnit

NOTE: This is the English version of my Japanese article.

I'm on the way to developing a vibrato unit by referring to FilterDemo at AudioUnitV3Example: A Basic AudioUnit Extension and Host Implementation.

In this article, I tried to add parameters.
In the template code of AudioUnit App Extension, it is seemed to add "Parameter 1" parameter as follows:
// Create parameter objects.
AUParameter *param1 = [AUParameterTree createParameterWithIdentifier:@"param1" name:@"Parameter 1" address:myParam1 min:0 max:100 unit:kAudioUnitParameterUnit_Percent unitName:nil flags:0 valueStrings:nil dependentParameters:nil];
A myParam1 is defined as
// Define parameter addresses. (These needn't be static).
#define myParam1 0
In FilterDemo, the addresses are defined as enum as:
enum {
FilterParamCutoff = 0,
FilterParamResonance = 1
};
I think any unique values will be OK.

In my case, I defined the paas follows
typedef NS_ENUM(AUParameterAddress, VibratoParameterAddress) {
    VibratoParameterAddressFrequency = 0,
    VibratoParameterAddressDepth = 1
};
Then, I actually added parameters as:
AUParameter *freqParameter = [AUParameterTree createParameterWithIdentifier:VibratoParameterIdentifierFrequency
                                                                           name:VibratoParameterNameFrequency
                                                                        address:VibratoParameterAddressFrequency
                                                                            min:0
                                                                            max:1
                                                                           unit:kAudioUnitParameterUnit_Hertz
                                                                       unitName:nil
                                                                          flags:0
                                                                   valueStrings:nil
                                                            dependentParameters:nil];
freqParameter.value = .0025;
self.freq = freqParameter.value;
    
AUParameter *depthParameter = [AUParameterTree createParameterWithIdentifier:VibratoParameterIdentifierDepth
                                                                            name:VibratoParameterNameDepth
                                                                         address:VibratoParameterAddressDepth
                                                                             min:0
                                                                             max:250
                                                                            unit:kAudioUnitParameterUnit_Seconds
                                                                        unitName:nil
                                                                           flags:0
                                                                    valueStrings:nil
                                                             dependentParameters:nil];
depthParameter.value = 250;
self.depth = depthParameter.value;
    
self.parameterTree = [AUParameterTree createTreeWithChildren:@[freqParameter, depthParameter]];
where, name and identifier strings were defined as
NSString *VibratoParameterIdentifierFrequency = @"VibratoParameterIdentifierFrequency";
NSString *VibratoParameterIdentifierDepth = @"VibratoParameterIdentifierDepth";

NSString *VibratoParameterNameFrequency = @"Frequency";
NSString *VibratoParameterNameDepth = @"Depth";
By the way, as for detecting the changes in these parameters from outside, FilterDemo's implementation is
// implementorValueObserver is called when a parameter changes value.
_parameterTree.implementorValueObserver = ^(AUParameter *param, AUValue value) {
        filterKernel->setParameter(param.address, value);
};
// implementorValueProvider is called when the value needs to be refreshed.
_parameterTree.implementorValueProvider = ^(AUParameter *param) {
return filterKernel->getParameter(param.address);
};
I think this code means that blocks are executed when parameters were set/got.
In my case, I implemented as
MyAudioUnit *unit = self;
self.parameterTree.implementorValueObserver = ^(AUParameter *param, AUValue value) {
    switch (param.address) {
        case VibratoParameterAddressFrequency:
            unit.freq = value;
            break;
        case VibratoParameterAddressDepth:
            unit.depth = value;
        default:
            break;
    }
};
    
self.parameterTree.implementorValueProvider = ^(AUParameter *param) {
    switch (param.address) {
        case VibratoParameterAddressFrequency:
            return unit.freq;
        case VibratoParameterAddressDepth:
            return unit.depth;
        default:
            break;
    }
    return 0.0f;
};
By the way, it is noted that a block is needed to express these parameters as strings.
In FilterDemo, the implementations are
// A function to provide string representations of parameter values.
_parameterTree.implementorStringFromValueCallback = ^(AUParameter *param, const AUValue *__nullable valuePtr) {
AUValue value = valuePtr == nil ? param.value : *valuePtr;
switch (param.address) {
case FilterParamCutoff:
return [NSString stringWithFormat:@"%.f", value];
case FilterParamResonance:
return [NSString stringWithFormat:@"%.2f", value];
default:
return @"?";
}
};
So, I implemented as follows:
self.parameterTree.implementorStringFromValueCallback = ^(AUParameter *param, const AUValue *__nullable valuePtr) {
        AUValue value = valuePtr == nil ? param.value : *valuePtr;
        
        switch (param.address) {
            case VibratoParameterAddressFrequency:
                return [NSString stringWithFormat:@"%.f", value];
            case VibratoParameterAddressDepth:
                return [NSString stringWithFormat:@"%.0f", value];
            default:
                return @"?";
        }
    };
Finally, parameter setting will be completed.

Nextly, I tried to change the parameters from outside.
However,  my AudioUnit's UI was not displayed on host applications such as AUV3Host or Hosting AU...

For v3 AudioUnits, custom UIs may be required.
So, before checking parameter changes, I think I'm going to make a custom UI...

That's all for today!!