-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathMVOMIDIManager.m
251 lines (222 loc) · 7.78 KB
/
MVOMIDIManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#import "MVOMIDIManager.h"
#import <VVOSC/VVOSC.h>
@implementation MVOMIDIManager
- (void) generalInit {
//NSLog(@"%s",__func__);
receivedOSCStringArray = [[MutLockArray alloc] init];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSDictionary *tmpDict = [def objectForKey:@"snap"];
//NSLog(@"\tsnap is %@",tmpDict);
if (tmpDict != nil) {
sourceEnableStateDict = [tmpDict objectForKey:@"src"];
destEnableStateDict = [tmpDict objectForKey:@"dst"];
}
if (sourceEnableStateDict != nil)
sourceEnableStateDict = [sourceEnableStateDict mutableCopy];
else
sourceEnableStateDict = [[NSMutableDictionary dictionaryWithCapacity:0] retain];
if (destEnableStateDict != nil)
destEnableStateDict = [destEnableStateDict mutableCopy];
else
destEnableStateDict = [[NSMutableDictionary dictionaryWithCapacity:0] retain];
//NSLog(@"\tsourceEnableStateDict = %@",sourceEnableStateDict);
//NSLog(@"\tdestEnableStateDict = %@",destEnableStateDict);
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(appWillTerminateNotification:)
name:NSApplicationWillTerminateNotification
object:nil];
receivedOSCStringArray = [[MutLockArray alloc] init];
[super generalInit];
}
- (void) awakeFromNib {
[sourcesTableView reloadData];
[receiversTableView reloadData];
}
- (void) appWillTerminateNotification:(NSNotification *)note {
//NSLog(@"%s",__func__);
//NSLog(@"\tsourceEnableStateDict = %@",sourceEnableStateDict);
//NSLog(@"\tdestEnableStateDict = %@",destEnableStateDict);
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//[def setObject:sourceEnableStateDict forKey:@"sourceEnableStateDict"];
//[def setObject:destEnableStateDict forKey:@"destEnableStateDict"];
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionaryWithCapacity:0];
[tmpDict setObject:sourceEnableStateDict forKey:@"src"];
[tmpDict setObject:destEnableStateDict forKey:@"dst"];
[def setObject:tmpDict forKey:@"snap"];
[def synchronize];
}
- (NSString *) receivingNodeName {
return @"To 'MIDI via OSC'";
}
- (NSString *) sendingNodeName {
return @"From 'MIDI via OSC'";
}
- (void) receivedOSCMessage:(OSCMessage *)m {
//NSLog(@"%s",__func__);
if (m == nil)
return;
OSCValue *val = [m value];
if (val == nil)
return;
if ([val type] != OSCValMIDI)
return;
VVMIDIMessage *msg = nil;
msg = [VVMIDIMessage createFromVals:[val midiStatus]:[val midiPort]:[val midiData1]:[val midiData2]];
if (msg != nil)
[self sendMsg:msg];
// if the received message wasn't a clock pulse...
if ([msg type] != VVMIDIClockVal) {
// put together the string for the UI...
NSMutableString *tmpString = [NSMutableString stringWithCapacity:0];
[receivedOSCStringArray wrlock];
[receivedOSCStringArray addObject:[msg lengthyDescription]];
while ([receivedOSCStringArray count]>20)
[receivedOSCStringArray removeObjectAtIndex:0];
NSEnumerator *it = [[receivedOSCStringArray array] reverseObjectEnumerator];
for (NSString *midiString in it)
[tmpString appendString:[NSString stringWithFormat:@"%@\n",midiString]];
[receivedOSCStringArray unlock];
if ([receivedOSCPreviewToggle intValue] == NSOnState) {
dispatch_async(dispatch_get_main_queue(), ^{
[receivedOSCField setStringValue:tmpString];
});
}
}
}
- (void) loadMIDIInputSources {
//NSLog(@"%s",__func__);
// before i load anything, run through and save the enable state of the midi nodes in my dict
if ((sourceArray!=nil)&&([sourceArray count]>0)) {
//pthread_mutex_lock(&arrayLock);
[sourceArray rdlock];
for (VVMIDINode *nodePtr in [sourceArray array])
[sourceEnableStateDict setValue:[NSNumber numberWithBool:[nodePtr enabled]] forKey:[nodePtr name]];
//pthread_mutex_unlock(&arrayLock);
[sourceArray unlock];
}
// tell the super to load the midi input sources (this actually does the loading)
[super loadMIDIInputSources];
// now that i'm done loading, run through and either disable or restore the enable state of the nodes
if ((sourceArray!=nil) && ([sourceArray count]>0)) {
//pthread_mutex_lock(&arrayLock);
[sourceArray rdlock];
NSNumber *tmpNum = nil;
for (VVMIDINode *nodePtr in [sourceArray array]) {
tmpNum = [sourceEnableStateDict valueForKey:[nodePtr name]];
if (tmpNum != nil)
[nodePtr setEnabled:[tmpNum boolValue]];
else
[nodePtr setEnabled:NO];
}
//pthread_mutex_unlock(&arrayLock);
[sourceArray unlock];
}
}
- (void) loadMIDIOutputDestinations {
// before i load anything, run through and save the enable state of the midi nodes in my dict
if ((destArray!=nil)&&([destArray count]>0)) {
//pthread_mutex_lock(&arrayLock);
[destArray rdlock];
for (VVMIDINode *nodePtr in [destArray array])
[destEnableStateDict setValue:[NSNumber numberWithBool:[nodePtr enabled]] forKey:[nodePtr name]];
//pthread_mutex_unlock(&arrayLock);
[destArray unlock];
}
// tell the super to load the midi input destinations (this actually does the loading)
[super loadMIDIOutputDestinations];
// now that i'm done loading, run through and either disable or restore the enable state of the nodes
if ((destArray!=nil) && ([destArray count]>0)) {
//pthread_mutex_lock(&arrayLock);
[destArray rdlock];
NSNumber *tmpNum = nil;
for (VVMIDINode *nodePtr in [destArray array]) {
tmpNum = [destEnableStateDict valueForKey:[nodePtr name]];
if (tmpNum != nil)
[nodePtr setEnabled:[tmpNum boolValue]];
else
[nodePtr setEnabled:NO];
}
//pthread_mutex_unlock(&arrayLock);
[destArray unlock];
}
}
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tv {
if (tv == sourcesTableView)
return [sourceArray count];
else if (tv == receiversTableView)
return [destArray count];
return 0;
}
- (id) tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tc row:(NSInteger)row {
if (tv == sourcesTableView) {
//NSLog(@"\t\tsources");
id midiSource = [sourceArray lockObjectAtIndex:row];
if (midiSource == nil)
return nil;
if (tc == sourcesNameColumn) {
return [midiSource name];
}
else if (tc == sourcesEnableColumn) {
if ([midiSource enabled])
return [NSNumber numberWithInt:NSOnState];
else
return [NSNumber numberWithInt:NSOffState];
}
}
else if (tv == receiversTableView) {
//NSLog(@"\t\treceivers");
id midiSource = [destArray lockObjectAtIndex:row];
if (midiSource == nil)
return nil;
if (tc == receiversNameColumn) {
return [midiSource name];
}
else if (tc == receiversEnableColumn) {
if ([midiSource enabled])
return [NSNumber numberWithInt:NSOnState];
else
return [NSNumber numberWithInt:NSOffState];
}
}
return nil;
}
- (void) tableView:(NSTableView *)tv setObjectValue:(id)v forTableColumn:(NSTableColumn *)tc row:(NSInteger)row {
if (tv == sourcesTableView) {
//NSLog(@"\t\tsources");
id midiSource = [sourceArray lockObjectAtIndex:row];
if (midiSource == nil)
return;
if (tc == sourcesEnableColumn) {
if ([v intValue] == NSOnState)
[midiSource setEnabled:YES];
else
[midiSource setEnabled:NO];
}
if ((sourceArray!=nil)&&([sourceArray count]>0)) {
[sourceArray rdlock];
for (VVMIDINode *nodePtr in [sourceArray array])
[sourceEnableStateDict setValue:[NSNumber numberWithBool:[nodePtr enabled]] forKey:[nodePtr name]];
[sourceArray unlock];
}
}
else if (tv == receiversTableView) {
//NSLog(@"\t\treceivers");
id midiSource = [destArray lockObjectAtIndex:row];
if (midiSource == nil)
return;
if (tc == receiversEnableColumn) {
if ([v intValue] == NSOnState)
[midiSource setEnabled:YES];
else
[midiSource setEnabled:NO];
}
if ((destArray!=nil)&&([destArray count]>0)) {
[destArray rdlock];
for (VVMIDINode *nodePtr in [destArray array])
[destEnableStateDict setValue:[NSNumber numberWithBool:[nodePtr enabled]] forKey:[nodePtr name]];
[destArray unlock];
}
}
}
@end