Skip to content
Browse files

Theres a UI!

  • Loading branch information...
1 parent 26f73ef commit 064306a0dfc3ceeb11a496c6db72953aeb5cea2a @spencersalazar committed Oct 18, 2012
Showing with 162 additions and 6 deletions.
  1. +11 −4 AudioUnit/AudioUnit-test.ck
  2. +149 −0 AudioUnit/AudioUnit.mm
  3. +2 −2 AudioUnit/makefile.osx
View
15 AudioUnit/AudioUnit-test.ck
@@ -1,11 +1,15 @@
+MAUI_Slider slider;
+slider.display();
// synchronize to period
-.5::second => dur T;
+1.25::second => dur T;
T - (now % T) => now;
// list available AUs
AudioUnit.list() @=> string aus[];
+//while(true) { 1::second => now; }
+
for(int i; i < aus.cap(); i++)
{
chout <= aus[i] <= IO.newline();
@@ -14,9 +18,12 @@ for(int i; i < aus.cap(); i++)
// setup patch
AudioUnit au => NRev reverb => dac;
// open General MIDI synth
-au.open("DLSMusicDevice");
+//au.open("DLSMusaicDevice");
+au.open("Massive");
// set reverb
0.1 => reverb.mix;
+// display UI
+au.display();
// set program (voice)
if(me.args())
@@ -27,7 +34,7 @@ if(me.args())
while(true)
{
- 59 + Math.random2(0,1)*12 + scale[Math.random2(0,4)] => int note;
+ 34 + Math.random2(0,1)*12 + scale[Math.random2(0,4)] => int note;
// noteOn
au.send(0x90, note, 127);
@@ -36,4 +43,4 @@ while(true)
// noteOff
au.send(0x80, note, 0);
-}
+}
View
149 AudioUnit/AudioUnit.mm
@@ -11,8 +11,12 @@
#include "chuck_type.h"
#include <AudioUnit/AudioUnit.h>
+#include <AudioUnit/AudioUnitCarbonView.h>
#include <CoreAudio/CoreAudio.h>
#include <CoreAudioKit/CoreAudioKit.h>
+#include <Foundation/Foundation.h>
+#include <Cocoa/Cocoa.h>
+#include <Carbon/Carbon.h>
#include "PublicUtility/CAComponent.h"
#include "PublicUtility/CABufferList.h"
@@ -38,6 +42,7 @@
CK_DLL_MFUN(audiounit_open);
CK_DLL_MFUN(audiounit_send);
+CK_DLL_MFUN(audiounit_display);
// this is a special offset reserved for Chugin internal data
t_CKINT audiounit_data_offset = 0;
@@ -128,6 +133,131 @@ void load_components()
};
+@interface CKAudioUnitHelper : NSObject
+{
+ AudioUnit m_au;
+ NSWindow * m_window;
+}
+
+// call on any thread
+- (id)initWithAudioUnit:(AudioUnit)au;
+// only call on main thread
+- (void)display;
+
+@end
+
+
+@implementation CKAudioUnitHelper
+
+// call on any thread
+- (id)initWithAudioUnit:(AudioUnit)au
+{
+ if(self = [super init])
+ {
+ m_au = au;
+ m_window = nil;
+ }
+
+ return self;
+}
+
+// only call on main thread
+- (void)display
+{
+ if(m_window == nil)
+ {
+// NSRect r = NSMakeRect(0, 0, 500, 500);
+// m_window = [[NSWindow alloc] initWithContentRect:r
+// styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
+// backing:NSBackingStoreBuffered
+// defer:YES];
+// [m_window setReleasedWhenClosed:NO];
+//// [m_window setContentView:[[[NSView alloc] initWithFrame:NSMakeRect(0, 0, 500, 500)] autorelease]];
+// [m_window center];
+ }
+
+#if !__LP64__
+// WindowRef carbonWindow = (WindowRef) [m_window windowRef];
+ OSStatus result;
+
+ HIWindowRef carbonWindow;
+
+ Rect windowRect = { 0, 0, 500, 500 };
+ result = CreateNewWindow(kDocumentWindowClass,
+ kWindowStandardHandlerAttribute |
+ kWindowCloseBoxAttribute |
+ kWindowCollapseBoxAttribute |
+ kWindowCompositingAttribute,
+ &windowRect, &carbonWindow);
+
+ CreateRootControl(carbonWindow, NULL);
+ ControlRef rootControl = NULL;
+ GetRootControl(carbonWindow, &rootControl);
+
+ UInt32 dataSize;
+ Boolean isWritable;
+ AudioComponentDescription * carbonViewDescs = NULL;
+ UInt32 numComponents;
+
+ result = AudioUnitGetPropertyInfo(m_au,
+ kAudioUnitProperty_GetUIComponentList,
+ kAudioUnitScope_Global,
+ 0,
+ &dataSize,
+ &isWritable );
+
+ numComponents = dataSize/sizeof(AudioComponentDescription);
+ carbonViewDescs = new AudioComponentDescription[numComponents];
+
+ result = AudioUnitGetProperty(m_au,
+ kAudioUnitProperty_GetUIComponentList,
+ kAudioUnitScope_Global,
+ 0,
+ carbonViewDescs,
+ &dataSize);
+
+ AudioComponent uiComponent = AudioComponentFindNext(NULL, &carbonViewDescs[0]);
+ AudioComponentInstance uiComponentInstance;
+ AudioComponentInstanceNew(uiComponent, &uiComponentInstance);
+
+ ControlRef auControl;
+ Float32Point auLoc = { 0, 0 };
+ Float32Point auSize = { 500, 500 };
+ result = AudioUnitCarbonViewCreate(uiComponentInstance, m_au, carbonWindow, rootControl,
+ &auLoc, &auSize, &auControl);
+
+ Rect controlRect;
+ GetControlBounds(auControl, &controlRect);
+
+ SizeWindow(carbonWindow, controlRect.right - controlRect.left,
+ controlRect.bottom - controlRect.top, true);
+
+// ActivateControl(auControl);
+// EnableControl(auControl);
+
+// ActivateWindow(carbonWindow, true);
+// BringToFront(carbonWindow);
+
+ m_window = [[NSWindow alloc] initWithWindowRef:carbonWindow];
+
+// Rect controlRect;
+// GetControlBounds(auControl, &controlRect);
+// [m_window setFrame:[m_window frameRectForContentRect:NSMakeRect(0, 0,
+// controlRect.right - controlRect.left,
+// controlRect.bottom - controlRect.top)]
+// display:YES];
+
+#endif
+
+ [m_window center];
+ [m_window makeKeyAndOrderFront:nil];
+ [m_window display];
+}
+
+@end
+
+
+
// class definition for internal Chugin data
// (note: this isn't strictly necessary, but serves as example
// of one recommended approach)
@@ -301,6 +431,16 @@ t_CKBOOL send_midi(t_CKUINT data1, t_CKUINT data2, t_CKUINT data3)
return FALSE;
}
+ t_CKBOOL display()
+ {
+ CKAudioUnitHelper * helper = [[CKAudioUnitHelper alloc] initWithAudioUnit:m_au];
+ [helper performSelectorOnMainThread:@selector(display)
+ withObject:nil
+ waitUntilDone:NO];
+
+ return TRUE;
+ }
+
private:
float m_fs;
@@ -315,6 +455,7 @@ t_CKBOOL send_midi(t_CKUINT data1, t_CKUINT data2, t_CKUINT data3)
};
+
// query function: chuck calls this when loading the Chugin
// NOTE: developer will need to modify this function to
// add additional functions to this Chugin
@@ -349,6 +490,8 @@ t_CKBOOL send_midi(t_CKUINT data1, t_CKUINT data2, t_CKUINT data3)
QUERY->add_arg(QUERY, "int", "data2");
QUERY->add_arg(QUERY, "int", "data3");
+ QUERY->add_mfun(QUERY, audiounit_display, "void", "display");
+
// this reserves a variable in the ChucK internal class to store
// referene to the c++ class we defined above
audiounit_data_offset = QUERY->add_mvar(QUERY, "int", "@au_data", false);
@@ -434,3 +577,9 @@ t_CKBOOL send_midi(t_CKUINT data1, t_CKUINT data2, t_CKUINT data3)
RETURN->v_int = c->send_midi(data1, data2, data3);
}
+CK_DLL_MFUN(audiounit_display)
+{
+ CKAudioUnit * c = (CKAudioUnit *) OBJ_MEMBER_INT(SELF, audiounit_data_offset);
+ c->display();
+}
+
View
4 AudioUnit/makefile.osx
@@ -9,6 +9,6 @@ LDFLAGS+=-shared -lstdc++ $(addprefix -framework ,$(FRAMEWORKS)) \
-ObjC $(ARCHOPTS) \
-Wl,-undefined,dynamic_lookup
-LD=gcc
-CXX=g++
+LD=clang
+CXX=clang++
OBJCXX=clang++

0 comments on commit 064306a

Please sign in to comment.
Something went wrong with that request. Please try again.