Debian Codegraph @

iOS/Csound iOS Swift Examples/Csound iOS SwiftExamples/csound-iOS/classes/midi/MidiWidgetsManager.m

raw
1 /*
2
3 MidiWidgetsManager.m:
4
5 Copyright (C) 2014 Steven Yi, Aurelius Prochazka
6
7 This file is part of Csound for iOS.
8
9 The Csound for iOS Library is free software; you can redistribute it
10 and/or modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 Csound is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with Csound; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA
23
24 */
25
26 #import "MidiWidgetsManager.h"
27 #import "SliderMidiWidgetWrapper.h"
28
29 void MidiWidgetsManagerReadProc(const MIDIPacketList *pktlist, void *refcon, void *srcConnRefCon);
30
31 @implementation MidiWidgetsManager
32
33 -(id)init {
34 if(self = [super init]) {
35 _widgetWrappers = [[NSMutableArray alloc] init];
36
37 for (int i = 0; i < 128; i++) {
38 [_widgetWrappers addObject:[NSNull null]];
39 }
40
41 }
42 return self;
43 }
44
45
46 -(void)addSlider:(UISlider *)slider forControllerNumber:(int)controllerNumber {
47 SliderMidiWidgetWrapper *wrapper = [[SliderMidiWidgetWrapper alloc] init:slider];
48 [self addMidiWidgetWrapper:wrapper forControllerNumber:controllerNumber];
49 }
50
51 -(void)addMidiWidgetWrapper:(id<MidiWidgetWrapper>)wrapper
52 forControllerNumber:(int)controllerNumber {
53
54 if (controllerNumber < 0 || controllerNumber > 127) {
55 NSLog(@"Error: Attempted to add a widget with controller number outside of range 0-127: %d", controllerNumber);
56 return;
57 }
58
59 [_widgetWrappers replaceObjectAtIndex:controllerNumber withObject:wrapper];
60 }
61
62
63 /* coremidi callback, called when MIDI data is available */
64 void MidiWidgetsManagerReadProc(const MIDIPacketList *pktlist, void *refcon, void *srcConnRefCon){
65 MidiWidgetsManager* manager = (__bridge MidiWidgetsManager *)refcon;
66 MIDIPacket *packet = &((MIDIPacketList *)pktlist)->packet[0];
67 Byte *curpack;
68 int i, j;
69
70 for (i = 0; i < pktlist->numPackets; i++) {
71 for(j=0; j < packet->length; j+=3){
72 curpack = packet->data+j;
73
74 if ((*curpack++ | 0xB0) > 0) {
75 unsigned int controllerNumber = (unsigned int)(*curpack++);
76 unsigned int controllerValue = (unsigned int)(*curpack++);
77
78 id wrapper = [manager.widgetWrappers objectAtIndex:controllerNumber];
79
80 //NSLog(@"Controller Number: %d Value: %d", controllerNumber, controllerValue);
81
82 if (wrapper != [NSNull null]) {
83 [(id<MidiWidgetWrapper>)wrapper setMIDIValue:controllerValue];
84 }
85 }
86
87 }
88 packet = MIDIPacketNext(packet);
89 }
90
91 }
92
93 #pragma mark CoreMidi Code
94
95 -(void)openMidiIn {
96 int k;
97 ItemCount endpoints;
98
99 CFStringRef name = NULL, cname = NULL, pname = NULL;
100 CFStringEncoding defaultEncoding = CFStringGetSystemEncoding();
101 MIDIPortRef mport = 0;
102 MIDIEndpointRef endpoint;
103 OSStatus ret;
104
105 /* MIDI client */
106 cname = CFStringCreateWithCString(NULL, "my client", defaultEncoding);
107 ret = MIDIClientCreate(cname, NULL, NULL, &mclient);
108 if(!ret){
109 /* MIDI output port */
110 pname = CFStringCreateWithCString(NULL, "outport", defaultEncoding);
111 ret = MIDIInputPortCreate(mclient, pname, MidiWidgetsManagerReadProc, (__bridge void *)(self), &mport);
112 if(!ret){
113 /* sources, we connect to all available input sources */
114 endpoints = MIDIGetNumberOfSources();
115 //NSLog(@"midi srcs %d\n", endpoints);
116 for(k=0; k < endpoints; k++){
117 endpoint = MIDIGetSource(k);
118 MIDIPortConnectSource(mport, endpoint, NULL);
119
120 }
121 }
122 }
123 if(name) CFRelease(name);
124 if(pname) CFRelease(pname);
125 if(cname) CFRelease(cname);
126
127 }
128
129 -(void)closeMidiIn {
130 MIDIClientDispose(mclient);
131 }
132
133
134 @end