Re: Newbie documentation for MIDI
Re: Newbie documentation for MIDI
- Subject: Re: Newbie documentation for MIDI
- From: Michael Thornburgh <email@hidden>
- Date: Tue, 26 Nov 2002 12:36:54 -0800
for cross-thread communication, you're _supposed_ to use
"Distributed Objects". i've never really bothered trying
to figure DO out though. it seems like you have to jump
through a mess of nasty looking hoops so you can end up
with simple objc messages.
if i wanted to signal my UI thread from my real-time thread
that it should do some drawing for me at some point, i'd
use NSPorts and NSPortMessages. here's some simple code
to illustrate how to do this (compile at the command line
with "cc -framework Foundation filename.m", run with "./a.out"):
---
#import <Foundation/Foundation.h>
#import <stdio.h>
NSPort * myPort;
@interface MTMessageTest : NSObject {
}
- (void) threadMain:anObject;
- (void) handlePortMessage:(NSPortMessage *)portMessage;
@end
@implementation MTMessageTest
- (void) threadMain:anObject
{
NSAutoreleasePool * pool = [NSAutoreleasePool new];
[[NSRunLoop currentRunLoop] addPort:myPort
forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool release];
}
- (void) handlePortMessage:(NSPortMessage *)portMessage
{
printf ( "handlePortMessage: %p = %s\n", portMessage,
[[[[NSString alloc] initWith
Data:[[portMessage components]
objectAtIndex:0] encoding:NSUnicodeStringEncoding ] autorelease]
cString] );
}
@end
main()
{
NSAutoreleasePool * pool = [NSAutoreleasePool new];
MTMessageTest * messageTest = [[[MTMessageTest alloc] init]
autorelease];
NSPortMessage * portMessage;
myPort = [NSPort port];
portMessage = [[[NSPortMessage alloc] initWithSendPort:myPort
receivePort:myPort components:[NSArray arrayWithObject:[@"this is a
test" dataUsingEncoding:NSUnicodeStringEncoding]]] autorelease];
[myPort setDelegate:messageTest];
[NSThread detachNewThreadSelector:@selector(threadMain:)
toTarget:messageTest withObject:nil];
[portMessage sendBeforeDate:nil];
[portMessage sendBeforeDate:nil];
sleep(1);
[portMessage sendBeforeDate:nil];
[portMessage sendBeforeDate:nil];
sleep(1);
[portMessage sendBeforeDate:nil];
[portMessage sendBeforeDate:nil];
[portMessage sendBeforeDate:nil];
sleep(1);
sleep(5);
[pool release];
return 0;
}
---
the NSPort can queue up a few messages. using a sendBeforeDate
of nil will make the message send fail rather than block until
the port's queue drains. note in the example above, messages
are received and handled in threadMain:'s runloop, not the main
thread's runloop.
-mike
On Tuesday, November 26, 2002, at 11:48 AM, Philippe Wicker wrote:
On Tuesday, November 26, 2002, at 06:35 PM, Robert Grant wrote:
Use Notifications!
Sounds like you're using Cocoa and so what you want to do is a piece
of
cake. I do something similar for my flashing LEDs in Rax.
CoreMIDI invokes your call back in a separate thread so you're
already multi-threaded
for free. Just post a Notification in the callback thread and the
main UI thread will
pick it up and process it nicely.
I don't know how notification posting works, but I assume it is
similar to posting a carbon event. Posting an event is a potentially
blocking operation, the system has to acquire a lock on a queue. The
posting thread may be preempted because the queue is locked.The UI
thread may also be preempted while locking the queue, keeping the
posting thread in a sleeping state, until it regains the CPU and
unlocks the queue. The locking time is - theoretically - unbounded. So
this mechanism should not be used when the posting thread has real
time constraints which is the case for the MIDI or AUDIO callback's
threads. This may be less important for a MIDI thread than for an
AUDIO thread. In the first case (MIDI thread), the result will be some
occasional high latency values. In the second case (AUDIO thread), the
result will be some glitches (audio drops).
I don't know the solution for that problem. I'll have to think of it
because I need to solve this point for my own project. The idea is to
write "events" in a non blocking FIFO which is periodically polled by
the UI thread (CFRunLoop with timer ?).
Philippe Wicker
email@hidden
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.