aboutsummaryrefslogtreecommitdiff
path: root/ios/WALT/MIDIClient.m
blob: 1c51f5fafcd0df7d13c9b3482adce7f15d963c5b (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "MIDIClient.h"

#include <CoreMIDI/CoreMIDI.h>

#import "MIDIEndpoint.h"
#import "MIDIMessage.h"

NSString * const MIDIClientErrorDomain = @"MIDIClientErrorDomain";

@interface MIDIClient ()
@property (readwrite, nonatomic) MIDISource *source;
@property (readwrite, nonatomic) MIDIDestination *destination;
// Used by midiRead() for SysEx messages spanning multiple packets.
@property (readwrite, nonatomic) NSMutableData *sysExBuffer;

/** Returns whether the client's source or destination is attached to a particular device. */
- (BOOL)attachedToDevice:(MIDIDeviceRef)device;
@end

// Note: These functions (midiStateChanged and midiRead) are not called on the main thread!
static void midiStateChanged(const MIDINotification *message, void *context) {
  MIDIClient *client = (__bridge MIDIClient *)context;
  
  switch (message->messageID) {
    case kMIDIMsgObjectAdded: {
      const MIDIObjectAddRemoveNotification *notification =
          (const MIDIObjectAddRemoveNotification *)message;
      
      @autoreleasepool {
        if ((notification->childType & (kMIDIObjectType_Source|kMIDIObjectType_Destination)) != 0 &&
            [client.delegate respondsToSelector:@selector(MIDIClientEndpointAdded:)]) {
          [client.delegate MIDIClientEndpointAdded:client];
        }
      }
      break;
    }

    case kMIDIMsgObjectRemoved: {
      const MIDIObjectAddRemoveNotification *notification =
          (const MIDIObjectAddRemoveNotification *)message;

      @autoreleasepool {
        if ((notification->childType & (kMIDIObjectType_Source|kMIDIObjectType_Destination)) != 0 &&
            [client.delegate respondsToSelector:@selector(MIDIClientEndpointRemoved:)]) {
          [client.delegate MIDIClientEndpointRemoved:client];
        }
      }
      break;
    }

    case kMIDIMsgSetupChanged:
    case kMIDIMsgPropertyChanged:
    case kMIDIMsgSerialPortOwnerChanged:
    case kMIDIMsgThruConnectionsChanged: {
      @autoreleasepool {
        if ([client.delegate respondsToSelector:@selector(MIDIClientConfigurationChanged:)]) {
          [client.delegate MIDIClientConfigurationChanged:client];
        }
      }
      break;
    }

    case kMIDIMsgIOError: {
      const MIDIIOErrorNotification *notification = (const MIDIIOErrorNotification *)message;
      
      if ([client attachedToDevice:notification->driverDevice]) {
        @autoreleasepool {
          NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain
                                               code:notification->errorCode
                                           userInfo:nil];
          if ([client.delegate respondsToSelector:@selector(MIDIClient:receivedError:)]) {
            [client.delegate MIDIClient:client receivedError:error];
          }
        }
      }
      break;
    }
      
    default: {
      NSLog(@"Unhandled MIDI state change: %d", (int)message->messageID);
    }
  }
}

static void midiRead(const MIDIPacketList *packets, void *portContext, void *sourceContext) {
  MIDIClient *client = (__bridge MIDIClient *)portContext;
  
  // Read the data out of each packet and forward it to the client's delegate.
  // Each MIDIPacket will contain either some MIDI commands, or the start/continuation of a SysEx
  // command. The start of a command is detected with a byte greater than or equal to 0x80 (all data
  // must be 7-bit friendly). The end of a SysEx command is marked with 0x7F.
  
  // TODO(pquinn): Should something be done with the timestamp data?
  
  UInt32 packetCount = packets->numPackets;
  const MIDIPacket *packet = &packets->packet[0];
  @autoreleasepool {
    while (packetCount--) {
      if (packet->length == 0) {
        continue;
      }
      
      const Byte firstByte = packet->data[0];
      const Byte lastByte = packet->data[packet->length - 1];
      
      if (firstByte >= 0x80 && firstByte != MIDIMessageSysEx && firstByte != MIDIMessageSysExEnd) {
        // Packet describes non-SysEx MIDI messages.
        NSMutableData *data = nil;
        for (UInt16 i = 0; i < packet->length; ++i) {
          // Packets can contain multiple MIDI messages.
          if (packet->data[i] >= 0x80) {
            if (data.length > 0) {  // Tell the delegate about the last extracted command.
              [client.delegate MIDIClient:client receivedData:data];
            }
            data = [[NSMutableData alloc] init];
          }
          [data appendBytes:&packet->data[i] length:1];
        }
        
        if (data.length > 0) {
          [client.delegate MIDIClient:client receivedData:data];
        }
      }
      
      if (firstByte == MIDIMessageSysEx) {
        // The start of a SysEx message; collect data into sysExBuffer.
        client.sysExBuffer = [[NSMutableData alloc] initWithBytes:packet->data
                                                           length:packet->length];
      } else if (firstByte < 0x80 || firstByte == MIDIMessageSysExEnd) {
        // Continuation or end of a SysEx message.
        [client.sysExBuffer appendBytes:packet->data length:packet->length];
      }
      
      if (lastByte == MIDIMessageSysExEnd) {
        // End of a SysEx message.
        [client.delegate MIDIClient:client receivedData:client.sysExBuffer];
        client.sysExBuffer = nil;
      }
      
      packet = MIDIPacketNext(packet);
    }
  }
}

@implementation MIDIClient {
  NSString *_name;
  MIDIClientRef _client;
  MIDIPortRef _input;
  MIDIPortRef _output;
}

- (instancetype)initWithName:(NSString *)name error:(NSError **)error {
  if ((self = [super init])) {
    _name = name;  // Hold onto the name because MIDIClientCreate() doesn't retain it.
    OSStatus result = MIDIClientCreate((__bridge CFStringRef)name,
                                       midiStateChanged,
                                       (__bridge void *)self,
                                       &_client);
    if (result != noErr) {
      if (error) {
        *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
      }
      self = nil;
    }
  }
  return self;
}

- (void)dealloc {
  MIDIClientDispose(_client);  // Automatically disposes of the ports too.
}

- (BOOL)connectToSource:(MIDISource *)source error:(NSError **)error {
  OSStatus result = noErr;
  if (!_input) {  // Lazily create the input port.
    result = MIDIInputPortCreate(_client,
                                 (__bridge CFStringRef)_name,
                                 midiRead,
                                 (__bridge void *)self,
                                 &_input);
    if (result != noErr) {
      if (error) {
        *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
      }
      return NO;
    }
  }
  
  // Connect the source to the port.
  result = MIDIPortConnectSource(_input, source.endpoint, (__bridge void *)self);
  if (result != noErr) {
    if (error) {
      *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
    }
    return NO;
  }
  
  self.source = source;
  return YES;
}

- (BOOL)connectToDestination:(MIDIDestination *)destination error:(NSError **)error {
  if (!_output) {  // Lazily create the output port.
    OSStatus result = MIDIOutputPortCreate(_client,
                                           (__bridge CFStringRef)_name,
                                           &_output);
    if (result != noErr) {
      if (error) {
        *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
      }
      return NO;
    }
  }
  
  self.destination = destination;
  return YES;
}

- (BOOL)sendData:(NSData *)data error:(NSError **)error {
  if (data.length > sizeof(((MIDIPacket *)0)->data)) {
    // TODO(pquinn): Dynamically allocate a buffer. 
    if (error) {
      *error = [NSError errorWithDomain:MIDIClientErrorDomain
                                   code:0
                               userInfo:@{NSLocalizedDescriptionKey:
                                            @"Too much data for a basic MIDIPacket."}];
    }
    return NO;
  }
  
  MIDIPacketList packetList;
  MIDIPacket *packet = MIDIPacketListInit(&packetList);
  packet = MIDIPacketListAdd(&packetList, sizeof(packetList), packet, 0, data.length, data.bytes);
  if (!packet) {
    if (error) {
      *error = [NSError errorWithDomain:MIDIClientErrorDomain
                                   code:0
                               userInfo:@{NSLocalizedDescriptionKey:
                                            @"Packet too large for buffer."}];
    }
    return NO;
  }
  
  OSStatus result = MIDISend(_output, self.destination.endpoint, &packetList);
  if (result != noErr) {
    if (error) {
      *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:result userInfo:nil];
    }
    return NO;
  }
  return YES;
}

- (BOOL)attachedToDevice:(MIDIDeviceRef)device {
  MIDIDeviceRef sourceDevice = 0, destinationDevice = 0;
  MIDIEntityGetDevice(self.source.endpoint, &sourceDevice);
  MIDIEntityGetDevice(self.destination.endpoint, &destinationDevice);
  
  SInt32 sourceID = 0, destinationID = 0, deviceID = 0;
  MIDIObjectGetIntegerProperty(sourceDevice, kMIDIPropertyUniqueID, &sourceID);
  MIDIObjectGetIntegerProperty(destinationDevice, kMIDIPropertyUniqueID, &destinationID);
  MIDIObjectGetIntegerProperty(device, kMIDIPropertyUniqueID, &deviceID);

  return (deviceID == sourceID || deviceID == destinationID);
}
@end