Skip to content

Commit

Permalink
added support for MIDI sysex messages
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieroberts-rit committed Sep 26, 2011
1 parent 5931c60 commit 3cd91ca
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 27 deletions.
104 changes: 79 additions & 25 deletions iOS/Control/Classes/MIDI.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define as 0xfe
#define clock 0xf8
#define PgmChange 0xc0
#define Sysex 0xf0

MIDI *me;
static void notifyProc(const MIDINotification *message, void *refCon) {// if MIDI setup is changed
Expand Down Expand Up @@ -62,6 +63,7 @@ - (PhoneGapCommand*) initWithWebView:(UIWebView*)theWebView {
INT(CC), @"cc",
INT(PitchBend), @"pitchbend",
INT(PgmChange), @"programchange",
INT(Sysex), @"sysex",
nil];
[self setWebView:theWebView];
}
Expand Down Expand Up @@ -169,25 +171,27 @@ - (void) _pollJavascript:(id)obj {
NSArray *bytes = [msg componentsSeparatedByString:@","];
if([bytes count] < 3) continue;

MIDIPacket myMessage;
myMessage.timeStamp = 0;
myMessage.length = [bytes count] - 1; // -1 becuase first data byte is msgType + channel

NSString *msgType = [bytes objectAtIndex:0];
NSString *msgType = [bytes objectAtIndex:0];

MIDIPacket myMessage;
myMessage.timeStamp = 0;
myMessage.length = [bytes count] - 1; // -1 becuase first data byte is msgType + channel

// if([msgType isEqualToString:@"noteon"] && [[bytes objectAtIndex:3] intValue] == 0) {
// msgType = @"noteoff";
// }

int firstByte = [[midiDict objectForKey:msgType] intValue];
firstByte += [[bytes objectAtIndex:1] intValue];
myMessage.data[0] = firstByte;
myMessage.data[1] = [[bytes objectAtIndex:2] intValue];
int firstByte = [[midiDict objectForKey:msgType] intValue];
firstByte += [[bytes objectAtIndex:1] intValue];
myMessage.data[0] = firstByte;
myMessage.data[1] = [[bytes objectAtIndex:2] intValue];

if([bytes count] > 3)
myMessage.data[2] = [[bytes objectAtIndex:3] intValue];
if([bytes count] > 3)
myMessage.data[2] = [[bytes objectAtIndex:3] intValue];

myList.packet[i] = myMessage;
myList.packet[i] = myMessage;


//MIDISend(outPort, dst, &myList);
}
Expand Down Expand Up @@ -219,7 +223,7 @@ - (void) pollJavascript:(id)obj {

NSArray *bytes = [msg componentsSeparatedByString:@","];
if([bytes count] < 3) continue;
MIDIPacket myMessage;
myMessage.timeStamp = 0;
myMessage.length = [bytes count] - 1; // -1 becuase first data byte is msgType + channel
Expand All @@ -246,6 +250,12 @@ - (void) pollJavascript:(id)obj {
[pool drain];
}

void MyCompletionProc(void *ptr) {
NSLog(@"sysex complete");
MIDISysexSendRequest * sysex = (MIDISysexSendRequest *) ptr;
free(sysex);
// nothing for now
};
- (void)send:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
MIDIPacketList myList;
myList.numPackets = 1;
Expand All @@ -254,19 +264,63 @@ - (void)send:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
myMessage.timeStamp = 0;
myMessage.length = [arguments count] - 1;

int msgType = [[midiDict objectForKey:[arguments objectAtIndex:0]] intValue];

myMessage.data[0] = msgType + [[arguments objectAtIndex:1] intValue] - 1;
myMessage.data[1] = [[arguments objectAtIndex:2] intValue];

if (myMessage.length > 2)
myMessage.data[2] = [[arguments objectAtIndex:3] intValue];

myList.packet[0] = myMessage;

MIDISend(outPort, dst, &myList);
NSLog(@"midi type = %@", [arguments objectAtIndex:0]);
int msgType = [[midiDict objectForKey:[arguments objectAtIndex:0]] intValue];

if(msgType != Sysex) {
myMessage.data[0] = msgType + [[arguments objectAtIndex:1] intValue] - 1;
myMessage.data[1] = [[arguments objectAtIndex:2] intValue];

if (myMessage.length > 2)
myMessage.data[2] = [[arguments objectAtIndex:3] intValue];

myList.packet[0] = myMessage;

MIDISend(outPort, dst, &myList);
}else{
NSLog(@"%@", [arguments description]);
NSLog(@"sending sysex");
NSMutableString *data = [NSMutableString stringWithString:[arguments objectAtIndex:1]];
[data deleteCharactersInRange:NSMakeRange(0, 1)];
[data deleteCharactersInRange:NSMakeRange([data length] - 1, 1)];

Byte * charData = malloc(sizeof(Byte) * [data length]);

NSArray *charArray = [data componentsSeparatedByString:@","];

for(int i = 0; i < [charArray count]; i++) {
int _i = [[charArray objectAtIndex:i] intValue];
NSLog(@"int = %d", _i);
charData[i] = (Byte)_i;
NSLog(@"charData %d = %X", i, charData[i]);
}

NSLog(@"before making sysex");
MIDISysexSendRequest * sysex = malloc(sizeof(MIDISysexSendRequest));
sysex->destination = dst;
sysex->data = charData;
sysex->bytesToSend = 11; //[[arguments objectAtIndex:2] intValue];
sysex->complete = false;
sysex->completionProc = MyCompletionProc;
sysex->completionRefCon = sysex;
NSLog(@"after making sysex %X", charData[8]);
MIDISendSysex(sysex);
}
}

/*
struct
{
MIDIEndpointRef destination;
const Byte * data;
UInt32 bytesToSend;
Boolean complete;
Byte reserved[3];
MIDICompletionProc completionProc;
void * completionRefCon;
};
*/

- (void) dealloc {
if(inPort != nil) { MIDIPortDispose(inPort); }
if(client != nil) { MIDIClientDispose(client); }
Expand Down
10 changes: 8 additions & 2 deletions www/js/MIDIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ MIDIManager.prototype.processMIDI = function(midiType, midiChannel, midiNumber,
}

MIDIManager.prototype.sendMIDI = function(msgType, channel, number, value) {

console.log("MIDI SEND 1 " + value);
if(_protocol == "MIDI") {
if(value != -1) { // -1 means the value was undefined, ie for a program change message
console.log("MIDI SEND 2");
if(typeof value != "undefined") { // -1 means the value was undefined, ie for a program change message
console.log("MIDI SEND WRONG");
PhoneGap.exec('MIDI.send', msgType, channel, number, value);
}else{
PhoneGap.exec('MIDI.send', msgType, channel, number);
console.log(channel);
console.log("MIDI SEND 3");
PhoneGap.exec('MIDI.send', msgType, JSON.stringify(channel), number);
}
}
}

0 comments on commit 3cd91ca

Please sign in to comment.