/
OCNotePlayer.mm
185 lines (132 loc) · 5.39 KB
/
OCNotePlayer.mm
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
//
// OCNotePlayer.m
// Bridge
//
// Created by Philip Regan on 12/7/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "OCNotePlayer.h"
#import "OCPlayController.h"
#import "OCNoteObj.h"
#import "OCConstants.h"
@implementation OCNotePlayer
-(OCNotePlayer *) initWithPlayController:(OCPlayController *)playController {
self = [super init];
if (self) {
parentPlayController = playController;
[parentPlayController retain];
notes = [parentPlayController.windowController notes];
[notes retain];
}
return self;
}
- (void)dealloc {
[parentPlayController release];
[notes release];
[super dealloc];
}
-(void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
/*
This code is pulled almost verbatim from Apple's PlaySoftMIDI example project
and continues to around line 105-110
*/
AUGraph graph;
AudioUnit synthUnit;
OSStatus result;
UInt8 midiChannelInUse = 0; //we're using midi channel 1...
//create the nodes of the graph
AUNode synthNode;
AUNode limiterNode;
AUNode outNode;
AudioComponentDescription cd;
cd.componentManufacturer = kAudioUnitManufacturer_Apple;
cd.componentFlags = 0;
cd.componentFlagsMask = 0;
require_noerr (result = NewAUGraph (&graph), home);
cd.componentType = kAudioUnitType_MusicDevice;
cd.componentSubType = kAudioUnitSubType_DLSSynth;
require_noerr (result = AUGraphAddNode(graph, &cd, &synthNode), home);
cd.componentType = kAudioUnitType_Effect;
cd.componentSubType = kAudioUnitSubType_PeakLimiter;
require_noerr (result = AUGraphAddNode (graph, &cd, &limiterNode), home);
cd.componentType = kAudioUnitType_Output;
cd.componentSubType = kAudioUnitSubType_DefaultOutput;
require_noerr (result = AUGraphAddNode (graph, &cd, &outNode), home);
require_noerr (result = AUGraphOpen (graph), home);
require_noerr (result = AUGraphConnectNodeInput (graph, synthNode, 0, limiterNode, 0), home);
require_noerr (result = AUGraphConnectNodeInput (graph, limiterNode, 0, outNode, 0), home);
// ok we're good to go - get the Synth Unit...
require_noerr (result = AUGraphNodeInfo(graph, synthNode, 0, &synthUnit), home);
// ok we're set up to go - initialize and start the graph
require_noerr (result = AUGraphInitialize (graph), home);
//set our bank
require_noerr (result = MusicDeviceMIDIEvent(synthUnit,
kMidiMessage_ControlChange << 4 | midiChannelInUse,
kMidiMessage_BankMSBControl, 0,
0/*sample offset*/), home);
require_noerr (result = MusicDeviceMIDIEvent(synthUnit,
kMidiMessage_ProgramChange << 4 | midiChannelInUse,
0/*prog change num*/, 0,
0/*sample offset*/), home);
require_noerr (result = AUGraphStart (graph), home);
if ( ![self isCancelled] ) {
UInt32 noteOnCommand = kMidiMessage_NoteOn << 4 | midiChannelInUse;
/* This code is back to original project code */
do {
/*
The basic process is thus:
We iterate across the beats with the start, stop, and cursor positions
We iterate *up* through the notes (in case the user deletes any of
them). If the state of the note matches the cursor, then we act upon
the note
We cast to int safely because note properties are *always* whole numbers
To ensure logical playing, we stop notes first, and then play them.
Stopping the operation itself handles the complete stopping of all
notes for us.
*/
int noteCount = [notes count] - 1;
for ( int n = noteCount ; n >= 0 ; n-- ) {
OCNoteObj *note = [notes objectAtIndex:n];
if ( (int)note.startBeat + (int)note.length == parentPlayController.cursorPosition ) {
require_noerr (result = MusicDeviceMIDIEvent(synthUnit, noteOnCommand, (UInt32)note.pitch, 0, 0), home);
}
}
for ( int n = noteCount ; n >= 0 ; n-- ) {
OCNoteObj *note = [notes objectAtIndex:n];
if ( (int)note.startBeat == parentPlayController.cursorPosition ) {
require_noerr (result = MusicDeviceMIDIEvent(synthUnit, noteOnCommand, (UInt32)note.pitch, 120, 0), home);
}
}
// move the cursor forward and loop back if needed
parentPlayController.cursorPosition++;
if ( parentPlayController.cursorPosition > parentPlayController.stopPosition ) {
parentPlayController.cursorPosition = parentPlayController.startPosition;
}
// delay the required amount per the tempo and the basic beat amount
[self delay];
// lather, rinse, repeat until the user presses the stop button
} while ( ![self isCancelled] );
// ok we're done now
}
[pool release];
home:
if (graph) {
AUGraphStop (graph); // stop playback
DisposeAUGraph (graph);
}
}
/*
sleep for the calculatated interval for 120 bpm on the quarter note
*/
-(void)delay {
// bpm and time sig basic beat should be pulled from the document since those
// are user settings
int microseconds = 1000000;
int secondsPerMinute = 60;
int bpm = 120;
int timeSigBasicBeat = kNoteLength_04; // 1/4 note
usleep ( microseconds * secondsPerMinute / bpm / timeSigBasicBeat );
}
@end