#import <CoreAudioKit/CoreAudioKit.h>
#import "MidiViewController.h"
#import "PGMidi.h"
@interface MidiViewController () <PGMidiDelegate, PGMidiSourceDelegate>
{
PGMidi *_midi;
__weak IBOutlet UILabel *_labelCount;
__weak IBOutlet UILabel *_labelReceive;
}
@end
@implementation MidiViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.title = @"MidiView";
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
_midi = [[PGMidi alloc] init];
_midi.networkEnabled = YES;
_midi.delegate = self;
for (PGMidiSource *source in _midi.sources) {
[source addDelegate:self];
}
_midi.virtualDestinationEnabled = YES;
_midi.virtualSourceEnabled = YES;
}
- (IBAction)btnConnect:(UIButton *)sender {
CABTMIDICentralViewController *pCentralVC = [[CABTMIDICentralViewController alloc] init];
[self.navigationController pushViewController:pCentralVC animated:YES];
}
- (IBAction)btnDownSD:(UIButton *)sender {
const UInt8 noteOn[] = {0x99, 38, 127};
[_midi sendBytes:noteOn size:sizeof(noteOn)];
}
- (IBAction)btnUpSD:(UIButton *)sender {
const UInt8 noteOff[] = {0x89, 38, 0};
[_midi sendBytes:noteOff size:sizeof(noteOff)];
}
- (IBAction)btnDownBD:(UIButton *)sender {
const UInt8 noteOn[] = {0x99, 36, 127};
[_midi sendBytes:noteOn size:sizeof(noteOn)];
}
- (IBAction)btnUpBD:(UIButton *)sender {
const UInt8 noteOff[] = {0x89, 36, 0};
[_midi sendBytes:noteOff size:sizeof(noteOff)];
}
#pragma mark - PGMidiDelegate
- (void)midi:(PGMidi *)midi sourceAdded:(PGMidiSource *)source
{
[source addDelegate:self];
[self updateCountLabel];
[self updateReceiveLabel:[NSString stringWithFormat:@"Source added: %@", ToString(source)]];
}
- (void)midi:(PGMidi *)midi sourceRemoved:(PGMidiSource *)source
{
[self updateCountLabel];
[self updateReceiveLabel:[NSString stringWithFormat:@"Source removed: %@", ToString(source)]];
}
// Connect USB keyboard
- (void)midi:(PGMidi *)midi destinationAdded:(PGMidiDestination *)destination
{
[self updateCountLabel];
[self updateReceiveLabel:[NSString stringWithFormat:@"Desintation added: %@", ToString(destination)]];
}
- (void)midi:(PGMidi *)midi destinationRemoved:(PGMidiDestination *)destination
{
[self updateCountLabel];
[self updateReceiveLabel:[NSString stringWithFormat:@"Desintation removed: %@", ToString(destination)]];
}
- (void)updateReceiveLabel:(NSString *)string
{
_labelReceive.text = string;
NSLog(@"%@", string);
}
- (void)updateCountLabel
{
NSString *strCount = [NSString stringWithFormat:@"sources=%u destinations=%u", (unsigned)_midi.sources.count, (unsigned)_midi.destinations.count];
_labelCount.text = strCount;
NSLog(@"%@", strCount);
}
const char *isToString(BOOL b)
{
return b ? "yes":"no";
}
NSString *ToString(PGMidiConnection *connection)
{
return [NSString stringWithFormat:@"< PGMidiConnection: name=%@ isNetwork=%s >", connection.name, isToString(connection.isNetworkSession)];
}
#pragma mark - PGMidiSourceDelegate
- (void)callMIDIPacket:(NSInteger)iIndex packet:(const MIDIPacket *)packet
{
Byte bNoteOn = 0x9;
Byte bNoteOff = 0x8;
if (packet->data[0] >> 4 == bNoteOn) {
[self updateReceiveLabel:[NSString stringWithFormat:@"NoteOn: %d %d %d", packet->data[0], packet->data[1], packet->data[2]]];
} else if (packet->data[0] >> 4 == bNoteOff) {
[self updateReceiveLabel:[NSString stringWithFormat:@"NoteOff: %d %d %d", packet->data[0], packet->data[1], packet->data[2]]];
}
}
- (void)midiSource:(PGMidiSource *)midi midiReceived:(const MIDIPacketList *)packetList
{
const MIDIPacket *packet = &packetList->packet[0];
for (int i = 0; i < packetList->numPackets; ++i) {
dispatch_async(dispatch_get_main_queue(), ^{
[self callMIDIPacket:i packet:packet];
});
packet = MIDIPacketNext(packet);
}
}
@end