Skip to content

Instantly share code, notes, and snippets.

View standinga's full-sized avatar

standinga

View GitHub Profile
@standinga
standinga / ViewController.swift
Created May 3, 2019 22:17
ViewController.swift for medium post about Audio Unit V3 Extensions updated adding plugin view controller
import Cocoa
import AUFramework
class ViewController: NSViewController {
private var audioPlayer: AudioPlayer!
private var pluginVC: AudioUnitViewController!
@IBOutlet weak var auContainer: NSView!
@standinga
standinga / AudioUnitViewController.swift
Last active May 4, 2019 14:02
AudioUnitViewController.swift for medium post about Audio Unit V3 Extension, updated Framework's extension view controller
import CoreAudioKit
public class AudioUnitViewController: AUViewController, AUAudioUnitFactory {
public var audioUnit: VolumePluginAudioUnit? {
didSet {
DispatchQueue.main.async {
if self.isViewLoaded {
self.connectWithAU()
}
}
@standinga
standinga / Buffer.hpp
Last active May 4, 2019 12:24
Buffer.hpp struct Buffer
#import <AVFoundation/AVFoundation.h>
struct Buffer {
AVAudioPCMBuffer *pcmBuffer = nullptr;
AudioBufferList* mutableAudioBufferList = nullptr;
AudioBufferList const* originalAudioBufferList = nullptr;
AUAudioFrameCount maxFrames = 0;
float volume = 1.0;
/*
prepareInputBufferList populates the mutableAudioBufferList with the data
@standinga
standinga / AudioPlayer.swift
Created May 4, 2019 00:31
updated AudioPlayer.swift for medium post about Audio Unit V3 Extensions
public func selectAudioUnitWithComponentDescription(_ componentDescription: AudioComponentDescription?, completionHandler: @escaping (() -> Void)) {
// Internal function to resume playing and call the completion handler.
func done() {
if isPlaying {
playerNode.play()
}
completionHandler()
}
let hardwareFormat = self.engine.outputNode.outputFormat(forBus: 0)
@standinga
standinga / ViewController.swift
Last active June 10, 2021 18:00
connectAudioUnitWithPlayer() connecting host view controller with audio unit plugin
private func connectAudioUnitWithPlayer() {
var componentDescription = AudioComponentDescription()
componentDescription.componentType = kAudioUnitType_Effect
// used https://codebeautify.org/string-hex-converter to convert strings to fourCC hex
componentDescription.componentSubType = 0x44656d6f // "Demo"
componentDescription.componentManufacturer = 0x44656d6f // "Demo"
componentDescription.componentFlags = 0
componentDescription.componentFlagsMask = 0
AUAudioUnit.registerSubclass(VolumePluginAudioUnit.self, as: componentDescription, name: "demo: VolumePlugin", version: UInt32.max)
@standinga
standinga / VolumePluginAudioUnit.m
Created May 4, 2019 13:20
VolumePluginAudioUnit properties for busses and instance variable for buffer
// Define parameter addresses.
const AudioUnitParameterID myParam1 = 0;
@interface VolumePluginAudioUnit ()
@property (nonatomic, readwrite) AUParameterTree *parameterTree;
@property AUAudioUnitBus *inputBus;
@property AUAudioUnitBus *outputBus;
@property AUAudioUnitBusArray *inputBusArray;
@property AUAudioUnitBusArray *outputBusArray;
@standinga
standinga / VolumePluginAudioUnit.m
Last active May 4, 2019 13:31
updated initWithComponentDescription
- (instancetype)initWithComponentDescription:(AudioComponentDescription)componentDescription options:(AudioComponentInstantiationOptions)options error:(NSError **)outError {
self = [super initWithComponentDescription:componentDescription options:options error:outError];
if (self == nil) { return nil; }
// initialize Buffer
_buffer = Buffer();
_buffer.maxFrames = 0;
_buffer.pcmBuffer = nullptr;
_buffer.mutableAudioBufferList = nullptr;
@standinga
standinga / VolumePluginAudioUnit.m
Created May 4, 2019 13:34
update allocateRenderResourcesAndReturnError
- (BOOL)allocateRenderResourcesAndReturnError:(NSError **)outError {
if (![super allocateRenderResourcesAndReturnError:outError]) {
return NO;
}
// Validate that the bus formats are compatible.
if (self.outputBus.format.channelCount != _inputBus.format.channelCount) {
if (outError) {
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:kAudioUnitErr_FailedInitialization userInfo:nil];
NSLog(@"kAudioUnitErr_FailedInitialization at %d", __LINE__);
@standinga
standinga / VolumePluginAudioUnit.m
Last active September 26, 2019 01:29
updated internalRenderBlock
- (AUInternalRenderBlock)internalRenderBlock {
// Capture in locals to avoid ObjC member lookups. If "self" is captured in render, we're doing it wrong. See sample code.
__block Buffer* buffer = &_buffer;
return ^AUAudioUnitStatus(AudioUnitRenderActionFlags *actionFlags, const AudioTimeStamp *timestamp, AVAudioFrameCount frameCount, NSInteger outputBusNumber, AudioBufferList *outputData, const AURenderEvent *realtimeEventListHead, AURenderPullInputBlock pullInputBlock) {
// Do event handling and signal processing here.
AudioUnitRenderActionFlags pullFlags = 0;
buffer->prepareInputBufferList();
AUAudioUnitStatus err = pullInputBlock(&pullFlags, timestamp, frameCount, 0, buffer->mutableAudioBufferList);
@standinga
standinga / AudioPlayer.swift
Last active May 4, 2019 21:19
final update to AudioPlayer.swift for medium post about audio units
import AVFoundation
class AudioPlayer: NSObject {
private let engine = AVAudioEngine()
private let playerNode = AVAudioPlayerNode()
private var file: AVAudioFile!
private var isPlaying = false
private let audioPlayerQueue = DispatchQueue(label: "AudioPlayerQueue")