Created
August 27, 2012 17:33
-
-
Save bzgeb/3490647 to your computer and use it in GitHub Desktop.
A wrapper for PdBase which can be called in Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PdAudio.h" | |
#import "PdBase.h" | |
#import "PdDispatcher.h" | |
#import "Listener.h" | |
#import "PointerObject.h" | |
extern "C" { | |
extern void lrshift_tilde_setup(void); | |
extern void argument_setup(void); | |
extern void phasorshot_tilde_setup(void); | |
extern void path_setup(void); | |
extern void soundfile_info_setup(void); | |
extern void expr_tilde_setup(void); | |
extern void demultiplex_setup(void); | |
extern void iem_send_setup(void); | |
extern void prepend_setup(void); | |
extern void avg_tilde_setup(void); | |
extern void tosymbol_setup(void); | |
extern void pong_tilde_setup(void); | |
extern void fiddle_tilde_setup(void); | |
extern void record_tilde_setup(void); | |
extern void switch_setup(void); | |
static PdAudio *pdAudio; | |
static PdDispatcher* dispatcher; | |
static NSMutableDictionary* openFiles; | |
void _copyDemoPatchesToUserDomain() | |
{ | |
NSFileManager *fm = [NSFileManager defaultManager]; | |
NSError *fileError; | |
NSBundle *mainBundle = [NSBundle mainBundle]; | |
NSString *bundlePath = [mainBundle bundlePath]; | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSArray *bundleFiles = [fm contentsOfDirectoryAtPath:bundlePath error:&fileError]; | |
NSLog(@"%@", bundleFiles); | |
for( NSString *patchFile in bundleFiles ) | |
{ | |
if ([[patchFile pathExtension] isEqualToString:@"pd"] || [[patchFile pathExtension] isEqualToString:@"wav"] | |
|| [patchFile isEqualToString:@"iemlib"] || [patchFile isEqualToString:@"zexy"] || [patchFile isEqualToString:@"tof"] || [patchFile isEqualToString:@"cyclone"]) | |
{ | |
NSString *bundlePatchFilePath = [bundlePath stringByAppendingPathComponent:patchFile]; | |
NSString *documentsPatchFilePath = [documentsDirectory stringByAppendingPathComponent:patchFile]; | |
if ([fm fileExistsAtPath:bundlePatchFilePath]) | |
{ | |
if( ![fm fileExistsAtPath:documentsPatchFilePath] ) | |
{ | |
if( ![fm copyItemAtPath:bundlePatchFilePath toPath: documentsPatchFilePath error:&fileError] ) | |
NSLog([NSString stringWithCString:"Error copying demo patch:%@" encoding:NSASCIIStringEncoding], [fileError localizedDescription]); | |
else | |
NSLog(@"Copied: %@", bundlePatchFilePath); | |
} | |
} | |
} | |
} | |
} | |
void _openFile(char * filename, int length) | |
{ | |
NSString* file = [[NSString alloc] initWithData:[NSData dataWithBytes:filename length:length] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSLog(@"Opening File: %@", file); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
PointerObject* filePointer = [[PointerObject alloc] init]; | |
[filePointer setPointer:[PdBase openFile:file path:documentsDirectory]]; | |
[openFiles setObject:filePointer forKey:file]; | |
[file release]; | |
} | |
void _closeFile(char * filename, int length) | |
{ | |
NSString* file = [[NSString alloc] initWithData:[NSData dataWithBytes:filename length:length] encoding:NSUTF16LittleEndianStringEncoding]; | |
void * filePointer = [openFiles objectForKey:file]; | |
[PdBase closeFile:filePointer]; | |
[openFiles removeObjectForKey:file]; | |
} | |
void _initPd(float newSampleRate, int ticks, int inputChannels, int outputChannels) | |
{ | |
pdAudio = [[PdAudio alloc] initWithSampleRate:newSampleRate andTicksPerBuffer:ticks | |
andNumberOfInputChannels:inputChannels andNumberOfOutputChannels:outputChannels]; | |
dispatcher = [[PdDispatcher alloc] init]; | |
openFiles = [[NSMutableDictionary alloc] init]; | |
[PdBase setDelegate:dispatcher]; | |
lrshift_tilde_setup(); | |
argument_setup(); | |
phasorshot_tilde_setup(); | |
path_setup(); | |
soundfile_info_setup(); | |
expr_tilde_setup(); | |
demultiplex_setup(); | |
iem_send_setup(); | |
prepend_setup(); | |
tosymbol_setup(); | |
avg_tilde_setup(); | |
pong_tilde_setup(); | |
fiddle_tilde_setup(); | |
record_tilde_setup(); | |
switch_setup(); | |
_copyDemoPatchesToUserDomain(); | |
} | |
void _startAudio() | |
{ | |
[PdBase computeAudio:YES]; | |
[pdAudio play]; | |
} | |
void _sendBangToReceiver(char * receiver, int length) | |
{ | |
NSString* rec = [[NSString alloc] initWithData:[NSData dataWithBytes:receiver length:length] encoding:NSUTF16LittleEndianStringEncoding]; | |
[PdBase sendBangToReceiver:rec]; | |
[rec release]; | |
} | |
void _sendFloat(const float value, char * receiver, int length) | |
{ | |
NSString* rec = [[NSString alloc] initWithData:[NSData dataWithBytes:receiver length:length] encoding:NSUTF16LittleEndianStringEncoding]; | |
[PdBase sendFloat:value toReceiver:rec]; | |
[rec release]; | |
} | |
void * _subscribe(char * symbol, int symLength, char * gameObject, int objLength, char * methodName, int methLength) | |
{ | |
NSString* sym = [[NSString alloc] initWithData:[NSData dataWithBytes:symbol length:symLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSString* obj = [[NSString alloc] initWithData:[NSData dataWithBytes:gameObject length:objLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSString* meth = [[NSString alloc] initWithData:[NSData dataWithBytes:methodName length:methLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
Listener* newListener = [[Listener alloc] init]; | |
[newListener setUnityObject:obj]; | |
[newListener setUnityMethod:meth]; | |
[dispatcher addListener:newListener forSource:sym]; | |
void * subscription = [PdBase subscribe:sym]; | |
[sym release]; | |
return subscription; | |
} | |
void _unsubscribe(void * subscription) | |
{ | |
[PdBase unsubscribe:subscription]; | |
} | |
void _sendSymbolToReceiver(char * symbol, int symLength, char * receiver, int recLength) | |
{ | |
NSString * _receiver = [[NSString alloc] initWithData:[NSData dataWithBytes:receiver length:recLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSString * _symbol = [[NSString alloc] initWithData:[NSData dataWithBytes:symbol length:symLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
[PdBase sendSymbol:_symbol toReceiver:_receiver]; | |
[_symbol release]; | |
[_receiver release]; | |
} | |
void _sendMessageToReceiver(char * message, int messageLength, char * arguments, int argumentsLength, char * receiver, int recLength) | |
{ | |
NSString * _receiver = [[NSString alloc] initWithData:[NSData dataWithBytes:receiver length:recLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSString * _message = [[NSString alloc] initWithData:[NSData dataWithBytes:message length:messageLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSString * _arguments = [[NSString alloc] initWithData:[NSData dataWithBytes:arguments length:argumentsLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
// NSArray * argumentList = [_arguments componentsSeparatedByString:@":"]; | |
NSMutableArray * argumentList = [[[_arguments componentsSeparatedByString:@":"] mutableCopy] autorelease]; | |
for (int c = 0; c < [argumentList count]; ++c) { | |
NSString * arg = [argumentList objectAtIndex:c]; | |
if ([arg rangeOfString:@"%f"].location != NSNotFound) { | |
[argumentList replaceObjectAtIndex:c withObject:[NSNumber numberWithFloat:[arg floatValue]]]; | |
} | |
} | |
NSLog(@"String: %@", _arguments); | |
NSLog(@"Array: %@", argumentList); | |
[PdBase sendMessage:_message withArguments:argumentList toReceiver:_receiver]; | |
[_message release]; | |
[_receiver release]; | |
[_arguments release]; | |
} | |
void _sendListToReceiver(char * arguments, int argumentsLength, char * receiver, int recLength) | |
{ | |
NSString * _receiver = [[NSString alloc] initWithData:[NSData dataWithBytes:receiver length:recLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSString * _arguments = [[NSString alloc] initWithData:[NSData dataWithBytes:arguments length:argumentsLength] encoding:NSUTF16LittleEndianStringEncoding]; | |
NSMutableArray * argumentList = [[[_arguments componentsSeparatedByString:@":"] mutableCopy] autorelease]; | |
for (int c = 0; c < [argumentList count]; ++c) { | |
NSString * arg = [argumentList objectAtIndex:c]; | |
if ([arg rangeOfString:@"%f"].location != NSNotFound) { | |
[argumentList replaceObjectAtIndex:c withObject:[NSNumber numberWithFloat:[arg floatValue]]]; | |
} | |
} | |
NSLog(@"String: %@", _arguments); | |
NSLog(@"Array: %@", argumentList); | |
[PdBase sendList:argumentList toReceiver:_receiver]; | |
[_arguments release]; | |
[_receiver release]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment