| 1 | /* Copyright © 2007 Apple Inc. All Rights Reserved. |
| 2 | |
| 3 | Disclaimer: IMPORTANT: This Apple software is supplied to you by |
| 4 | Apple Inc. ("Apple") in consideration of your agreement to the |
| 5 | following terms, and your use, installation, modification or |
| 6 | redistribution of this Apple software constitutes acceptance of these |
| 7 | terms. If you do not agree with these terms, please do not use, |
| 8 | install, modify or redistribute this Apple software. |
| 9 | |
| 10 | In consideration of your agreement to abide by the following terms, and |
| 11 | subject to these terms, Apple grants you a personal, non-exclusive |
| 12 | license, under Apple's copyrights in this original Apple software (the |
| 13 | "Apple Software"), to use, reproduce, modify and redistribute the Apple |
| 14 | Software, with or without modifications, in source and/or binary forms; |
| 15 | provided that if you redistribute the Apple Software in its entirety and |
| 16 | without modifications, you must retain this notice and the following |
| 17 | text and disclaimers in all such redistributions of the Apple Software. |
| 18 | Neither the name, trademarks, service marks or logos of Apple Inc. |
| 19 | may be used to endorse or promote products derived from the Apple |
| 20 | Software without specific prior written permission from Apple. Except |
| 21 | as expressly stated in this notice, no other rights or licenses, express |
| 22 | or implied, are granted by Apple herein, including but not limited to |
| 23 | any patent rights that may be infringed by your derivative works or by |
| 24 | other works in which the Apple Software may be incorporated. |
| 25 | |
| 26 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
| 27 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 28 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
| 29 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
| 30 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
| 31 | |
| 32 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
| 33 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 35 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
| 36 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
| 37 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
| 38 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
| 39 | POSSIBILITY OF SUCH DAMAGE. |
| 40 | */ |
| 41 | #include <CoreMIDI/MIDIServices.h> |
| 42 | #include <CoreFoundation/CFRunLoop.h> |
| 43 | #include <stdio.h> |
| 44 | |
| 45 | // ___________________________________________________________________________________________ |
| 46 | // test program to echo MIDI In to Out |
| 47 | // ___________________________________________________________________________________________ |
| 48 | |
| 49 | MIDIPortRef gOutPort = NULL; |
| 50 | MIDIEndpointRef gDest = NULL; |
| 51 | int gChannel = 0; |
| 52 | |
| 53 | static void MyReadProc(const MIDIPacketList *pktlist, void *refCon, void *connRefCon) |
| 54 | { |
| 55 | if (gOutPort != NULL && gDest != NULL) { |
| 56 | MIDIPacket *packet = (MIDIPacket *)pktlist->packet; // remove const (!) |
| 57 | for (unsigned int j = 0; j < pktlist->numPackets; ++j) { |
| 58 | for (int i = 0; i < packet->length; ++i) { |
| 59 | // printf("%02X ", packet->data<i>); |
| 60 | |
| 61 | // rechannelize status bytes |
| 62 | if (packet->data<i> >= 0x80 && packet->data<i> < 0xF0) |
| 63 | packet->data<i> = (packet->data<i> & 0xF0) | gChannel; |
| 64 | } |
| 65 | |
| 66 | // printf("\n"); |
| 67 | packet = MIDIPacketNext(packet); |
| 68 | } |
| 69 | |
| 70 | MIDISend(gOutPort, gDest, pktlist); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | int main(int argc, char *argv[]) |
| 75 | { |
| 76 | if (argc >= 2) { |
| 77 | // first argument, if present, is the MIDI channel number to echo to (1-16) |
| 78 | sscanf(argv[1], "%d", &gChannel); |
| 79 | if (gChannel < 1) gChannel = 1; |
| 80 | else if (gChannel > 16) gChannel = 16; |
| 81 | --gChannel; // convert to 0-15 |
| 82 | } |
| 83 | |
| 84 | // create client and ports |
| 85 | MIDIClientRef client = NULL; |
| 86 | MIDIClientCreate(CFSTR("MIDI Echo"), NULL, NULL, &client); |
| 87 | |
| 88 | MIDIPortRef inPort = NULL; |
| 89 | MIDIInputPortCreate(client, CFSTR("Input port"), MyReadProc, NULL, &inPort); |
| 90 | MIDIOutputPortCreate(client, CFSTR("Output port"), &gOutPort); |
| 91 | |
| 92 | // enumerate devices (not really related to purpose of the echo program |
| 93 | // but shows how to get information about devices) |
| 94 | int i, n; |
| 95 | CFStringRef pname, pmanuf, pmodel; |
| 96 | char name[64], manuf[64], model[64]; |
| 97 | |
| 98 | n = MIDIGetNumberOfDevices(); |
| 99 | for (i = 0; i < n; ++i) { |
| 100 | MIDIDeviceRef dev = MIDIGetDevice(i); |
| 101 | |
| 102 | MIDIObjectGetStringProperty(dev, kMIDIPropertyName, &pname); |
| 103 | MIDIObjectGetStringProperty(dev, kMIDIPropertyManufacturer, &pmanuf); |
| 104 | MIDIObjectGetStringProperty(dev, kMIDIPropertyModel, &pmodel); |
| 105 | |
| 106 | CFStringGetCString(pname, name, sizeof(name), 0); |
| 107 | CFStringGetCString(pmanuf, manuf, sizeof(manuf), 0); |
| 108 | CFStringGetCString(pmodel, model, sizeof(model), 0); |
| 109 | CFRelease(pname); |
| 110 | CFRelease(pmanuf); |
| 111 | CFRelease(pmodel); |
| 112 | |
| 113 | printf("name=%s, manuf=%s, model=%s\n", name, manuf, model); |
| 114 | } |
| 115 | |
| 116 | // open connections from all sources |
| 117 | n = MIDIGetNumberOfSources(); |
| 118 | printf("%d sources\n", n); |
| 119 | for (i = 0; i < n; ++i) { |
| 120 | MIDIEndpointRef src = MIDIGetSource(i); |
| 121 | MIDIPortConnectSource(inPort, src, NULL); |
| 122 | } |
| 123 | |
| 124 | // find the first destination |
| 125 | n = MIDIGetNumberOfDestinations(); |
| 126 | if (n > 0) |
| 127 | gDest = MIDIGetDestination(0); |
| 128 | |
| 129 | if (gDest != NULL) { |
| 130 | MIDIObjectGetStringProperty(gDest, kMIDIPropertyName, &pname); |
| 131 | CFStringGetCString(pname, name, sizeof(name), 0); |
| 132 | CFRelease(pname); |
| 133 | printf("Echoing to channel %d of %s\n", gChannel + 1, name); |
| 134 | } else { |
| 135 | printf("No MIDI destinations present\n"); |
| 136 | } |
| 137 | |
| 138 | CFRunLoopRun(); |
| 139 | // run until aborted with control-C |
| 140 | |
| 141 | return 0; |
| 142 | } |