forked from mixedinkey-opensource/MIKMIDI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MIKMIDISequencer.m
1406 lines (1160 loc) · 63.1 KB
/
MIKMIDISequencer.m
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// MIKMIDISequencer.m
// MIKMIDI
//
// Created by Chris Flesner on 11/26/14.
// Copyright (c) 2014 Mixed In Key. All rights reserved.
// Changes by Rodrigo "RRC2Soft" Roman
//
#import "MIKMIDISequencer.h"
#import <mach/mach_time.h>
#import "MIKMIDISequence.h"
#import "MIKMIDITrack.h"
#import "MIKMIDIClock.h"
#import "MIKMIDITempoEvent.h"
#import "MIKMIDINoteEvent.h"
#import "MIKMIDIChannelEvent.h"
#import "MIKMIDINoteOnCommand.h"
#import "MIKMIDINoteOffCommand.h"
#import "MIKMIDIDeviceManager.h"
#import "MIKMIDIMetronome.h"
#import "MIKMIDIMetaTimeSignatureEvent.h"
#import "MIKMIDIUtilities.h"
#import "MIKMIDISynthesizer.h"
#import "MIKMIDISequencer+MIKMIDIPrivate.h"
#import "MIKMIDISequence+MIKMIDIPrivate.h"
#import "MIKMIDICommandScheduler.h"
#import "MIKMIDIClientDestinationEndpoint.h"
#import "MIKMIDIDestinationEndpoint.h"
#import "MIKMIDIOutputPort.h"
#if !__has_feature(objc_arc)
#error MIKMIDISequencer.m must be compiled with ARC. Either turn on ARC for the project or set the -fobjc-arc flag for MIKMIDISequencer.m in the Build Phases for this target
#endif
#define kDefaultTempo 120
NSString * const MIKMIDISequencerWillLoopNotification = @"MIKMIDISequencerWillLoopNotification";
const MusicTimeStamp MIKMIDISequencerEndOfSequenceLoopEndTimeStamp = -1;
#pragma mark -
@interface MIKMIDIEventWithDestination : NSObject
@property (nonatomic, strong) MIKMIDIEvent *event;
@property (nonatomic, strong) id<MIKMIDICommandScheduler> destination;
@property (nonatomic, readonly) BOOL representsNoteOff;
+ (instancetype)eventWithDestination:(id<MIKMIDICommandScheduler>)destination event:(MIKMIDIEvent *)event;
+ (instancetype)eventWithDestination:(id<MIKMIDICommandScheduler>)destination event:(MIKMIDIEvent *)event representsNoteOff:(BOOL)representsNoteOff;
@end
@interface MIKMIDICommandWithDestination : NSObject
@property (nonatomic, strong) MIKMIDICommand *command;
@property (nonatomic, strong) id<MIKMIDICommandScheduler> destination;
+ (instancetype)commandWithDestination:(id<MIKMIDICommandScheduler>)destination command:(MIKMIDICommand *)command;
@end
@interface MIKMIDIPendingNoteOffsForTimeStamp : NSObject
@property (nonatomic, strong) NSMutableArray *noteEventsWithEndTimeStamp;
@property (nonatomic) MusicTimeStamp endTimeStamp;
+ (instancetype)pendingNoteOffWithEndTimeStamp:(MusicTimeStamp)endTimeStamp;
@end
#pragma mark -
@interface MIKMIDISequencer ()
{
void *_processingQueueKey;
void *_processingQueueContext;
}
@property (readonly, nonatomic) MIKMIDIClock *clock;
@property (nonatomic, getter=isPlaying) BOOL playing;
@property (nonatomic, getter=isRecording) BOOL recording;
@property (nonatomic, getter=isLooping) BOOL looping;
@property (nonatomic) MIDITimeStamp latestScheduledMIDITimeStamp;
@property (nonatomic, strong) NSMutableDictionary *pendingNoteOffs;
@property (atomic, strong) NSMutableArray *pendingNoteOffsWithinCycle;
@property (nonatomic, strong) NSMutableDictionary *pendingRecordedNoteEvents;
@property (nonatomic) MusicTimeStamp startingTimeStamp;
@property (nonatomic) MusicTimeStamp initialStartingTimeStamp;
@property (nonatomic, strong) NSMapTable *tracksToDestinationsMap;
@property (nonatomic, strong) NSMapTable *tracksToDefaultSynthsMap;
@property (nonatomic) BOOL needsCurrentTempoUpdate;
@property (nonatomic) BOOL needsOverrideTempoUpdate;
@property (readonly, nonatomic) MusicTimeStamp sequenceLength;
@property (nonatomic) dispatch_queue_t processingQueue;
@property (nonatomic) dispatch_source_t processingTimer;
@property (atomic, assign) BOOL processingFakeLock; // HACK & BAD, but this is not a nuclear power plant :-P
@end
@implementation MIKMIDISequencer
#pragma mark - Lifecycle
- (instancetype)initWithSequence:(MIKMIDISequence *)sequence
{
if (self = [super init]) {
self.sequence = sequence;
_clock = [MIKMIDIClock clock];
_syncedClock = [_clock syncedClock];
_loopEndTimeStamp = MIKMIDISequencerEndOfSequenceLoopEndTimeStamp;
_preRoll = 0;
_clickTrackStatus = MIKMIDISequencerClickTrackStatusEnabledInRecord;
_tracksToDestinationsMap = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsStrongMemory];
_tracksToDefaultSynthsMap = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsStrongMemory];
_createSynthsIfNeeded = YES;
_processingQueueKey = &_processingQueueKey;
_processingQueueContext = &_processingQueueContext;
_maximumLookAheadInterval = 0.1;
_phaseToWait = 0.;
_playNotesOutsideLoop = YES;
}
return self;
}
+ (instancetype)sequencerWithSequence:(MIKMIDISequence *)sequence
{
return [[self alloc] initWithSequence:sequence];
}
- (instancetype)init
{
return [self initWithSequence:[MIKMIDISequence sequence]];
}
+ (instancetype)sequencer
{
return [[self alloc] init];
}
- (void)dealloc
{
[_sequence removeObserver:self forKeyPath:@"tracks"];
self.processingTimer = NULL;
}
#pragma mark - Playback
- (void)startPlayback
{
[self startPlaybackAtTimeStamp:0];
}
- (void)startPlaybackAtTimeStamp:(MusicTimeStamp)timeStamp
{
[self startPlaybackAtTimeStamp:timeStamp adjustForPreRollWhenRecording:YES];
}
- (void)startPlaybackAtTimeStamp:(MusicTimeStamp)timeStamp adjustForPreRollWhenRecording:(BOOL)adjustForPreRoll
{
MIDITimeStamp midiTimeStamp = MIKMIDIGetCurrentTimeStamp() + MIKMIDIClockMIDITimeStampsPerTimeInterval(0.001);
[self startPlaybackAtTimeStamp:timeStamp MIDITimeStamp:midiTimeStamp];
}
- (void)startPlaybackAtTimeStamp:(MusicTimeStamp)timeStamp MIDITimeStamp:(MIDITimeStamp)midiTimeStamp
{
[self startPlaybackAtTimeStamp:timeStamp MIDITimeStamp:midiTimeStamp adjustForPreRollWhenRecording:YES];
}
- (void)startPlaybackAtTimeStamp:(MusicTimeStamp)timeStamp MIDITimeStamp:(MIDITimeStamp)midiTimeStamp adjustForPreRollWhenRecording:(BOOL)adjustForPreRoll
{
//if (self.isPlaying) [self stop];
if (adjustForPreRoll /*&& self.isRecording*/) timeStamp -= self.preRoll;
NSString *queueLabel = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingFormat:@".%@.%p", [self class], self];
dispatch_queue_attr_t attr = DISPATCH_QUEUE_SERIAL;
#if defined (__MAC_10_10) || defined (__IPHONE_8_0)
if (&dispatch_queue_attr_make_with_qos_class != NULL) {
// See https://github.com/mixedinkey-opensource/MIKMIDI/issues/204
//attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, DISPATCH_QUEUE_PRIORITY_HIGH);
attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INITIATED, 0);
}
#endif
dispatch_queue_t queue = dispatch_queue_create(queueLabel.UTF8String, attr);
dispatch_queue_set_specific(queue, &_processingQueueKey, &_processingQueueContext, NULL);
self.processingQueue = queue;
dispatch_sync(queue, ^{
self.startingTimeStamp = timeStamp;
self.initialStartingTimeStamp = timeStamp;
_lastPlaybackTimeStamp = timeStamp; // RRC2Soft
Float64 startingTempo = [self.sequence tempoAtTimeStamp:timeStamp];
if (!startingTempo) startingTempo = kDefaultTempo;
[self updateClockWithMusicTimeStamp:timeStamp tempo:startingTempo atMIDITimeStamp:midiTimeStamp];
});
self.playing = YES;
dispatch_sync(queue, ^{
self.pendingNoteOffs = [NSMutableDictionary dictionary];
self.pendingNoteOffsWithinCycle = [[NSMutableArray alloc] init];
self.latestScheduledMIDITimeStamp = midiTimeStamp;
dispatch_source_t timer;
#if defined (__MAC_10_10) || defined (__IPHONE_8_0)
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, DISPATCH_TIMER_STRICT, self.processingQueue); // RRC2SOFT: Tell iOS to respect this timer
#else
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.processingQueue);
#endif
if (!timer) return NSLog(@"Unable to create processing timer for %@.", [self class]);
self.processingTimer = timer;
self.processingFakeLock = NO;
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC, 0.05 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
[self processSequenceStartingFromMIDITimeStampRRC2SOFT:self.latestScheduledMIDITimeStamp keepLock:NO];
});
dispatch_resume(timer);
});
}
- (void)resumePlayback
{
[self startPlaybackAtTimeStamp:self.currentTimeStamp];
}
- (void)stop
{
[self stopWithDispatchToProcessingQueue:YES];
}
- (void)stopAllPlayingNotesForCommandScheduler:(id<MIKMIDICommandScheduler>)scheduler
{
[self dispatchSyncToProcessingQueueAsNeeded:^{
NSMutableArray *commandsToSendNow = [NSMutableArray array];
MIDITimeStamp offTimeStamp = MIKMIDIGetCurrentTimeStamp() + MIKMIDIClockMIDITimeStampsPerTimeInterval(self.maximumLookAheadInterval);
for (MIKMIDIPendingNoteOffsForTimeStamp *pendingNoteOffsForTimeStamp in self.pendingNoteOffs.allValues) {
NSMutableArray *noteEvents = pendingNoteOffsForTimeStamp.noteEventsWithEndTimeStamp;
NSUInteger count = noteEvents.count;
NSMutableIndexSet *indexesToRemove = [NSMutableIndexSet indexSet];
for (NSUInteger i = 0; i < count; i++) {
MIKMIDIEventWithDestination *event = noteEvents[i];
if (event.destination == scheduler) {
[indexesToRemove addIndex:i];
MIKMIDINoteEvent *noteEvent = (MIKMIDINoteEvent *)event.event;
MIKMIDINoteOffCommand *command = [MIKMIDINoteOffCommand noteOffCommandWithNote:noteEvent.note velocity:0 channel:noteEvent.channel midiTimeStamp:offTimeStamp];
[commandsToSendNow addObject:command];
}
}
[noteEvents removeObjectsAtIndexes:indexesToRemove];
}
if (commandsToSendNow.count) [self scheduleCommands:commandsToSendNow withCommandScheduler:scheduler];
}];
}
- (void)stopWithDispatchToProcessingQueue:(BOOL)dispatchToProcessingQueue
{
MIDITimeStamp stopTimeStamp = MIKMIDIGetCurrentTimeStamp();
if (!self.isPlaying) return;
void (^stopPlayback)() = ^{
self.processingTimer = NULL;
MIKMIDIClock *clock = self.clock;
[self recordAllPendingNoteEventsWithOffTimeStamp:[clock musicTimeStampForMIDITimeStamp:stopTimeStamp]];
MusicTimeStamp allPendingNotesOffTimeStamp = MAX(self.latestScheduledMIDITimeStamp + 1, MIKMIDIGetCurrentTimeStamp() + MIKMIDIClockMIDITimeStampsPerTimeInterval(0.001));
[self sendAllPendingNoteOffsWithMIDITimeStamp:allPendingNotesOffTimeStamp];
self.pendingRecordedNoteEvents = nil;
self.looping = NO;
MusicTimeStamp stopMusicTimeStamp = [clock musicTimeStampForMIDITimeStamp:stopTimeStamp];
_currentTimeStamp = (stopMusicTimeStamp <= self.sequenceLength) ? stopMusicTimeStamp : self.sequenceLength;
_lastPlaybackTimeStamp = _currentTimeStamp; // RRC2Soft
[clock unsyncMusicTimeStampsAndTemposFromMIDITimeStamps];
[self.pendingNoteOffsWithinCycle removeAllObjects];
};
dispatchToProcessingQueue ? dispatch_sync(self.processingQueue, stopPlayback) : stopPlayback();
self.processingQueue = NULL;
self.playing = NO;
self.recording = NO;
}
- (void)processSequenceStartingFromMIDITimeStamp:(MIDITimeStamp)fromMIDITimeStamp
{
MIDITimeStamp toMIDITimeStamp = MIKMIDIGetCurrentTimeStamp() + MIKMIDIClockMIDITimeStampsPerTimeInterval(self.maximumLookAheadInterval);
if (toMIDITimeStamp < fromMIDITimeStamp) return;
MIKMIDIClock *clock = self.clock;
MIKMIDISequence *sequence = self.sequence;
MusicTimeStamp loopStartTimeStamp = self.loopStartTimeStamp;
MusicTimeStamp loopEndTimeStamp = self.effectiveLoopEndTimeStamp;
MusicTimeStamp fromMusicTimeStamp = [clock musicTimeStampForMIDITimeStamp:fromMIDITimeStamp];
MusicTimeStamp calculatedToMusicTimeStamp = [clock musicTimeStampForMIDITimeStamp:toMIDITimeStamp];
BOOL isLooping = (self.shouldLoop && calculatedToMusicTimeStamp > loopStartTimeStamp && loopEndTimeStamp > loopStartTimeStamp);
if (isLooping != self.isLooping) self.looping = isLooping;
MusicTimeStamp maxToMusicTimeStamp = self.isRecording ? DBL_MAX : self.sequenceLength; // If recording, don't limit max timestamp (Issue #45)
maxToMusicTimeStamp = isLooping ? loopEndTimeStamp : maxToMusicTimeStamp;
MusicTimeStamp toMusicTimeStamp = MIN(calculatedToMusicTimeStamp, maxToMusicTimeStamp);
MIDITimeStamp actualToMIDITimeStamp = [clock midiTimeStampForMusicTimeStamp:toMusicTimeStamp];
// Get relevant tempo events
NSMutableDictionary *allEventsByTimeStamp = [NSMutableDictionary dictionary];
NSMutableDictionary *tempoEventsByTimeStamp = [NSMutableDictionary dictionary];
Float64 overrideTempo = self.tempo;
if (!overrideTempo) {
NSArray *sequenceTempoEvents = [sequence.tempoTrack eventsOfClass:[MIKMIDITempoEvent class] fromTimeStamp:MAX(fromMusicTimeStamp, 0) toTimeStamp:toMusicTimeStamp];
for (MIKMIDITempoEvent *tempoEvent in sequenceTempoEvents) {
NSNumber *timeStampKey = @(tempoEvent.timeStamp);
allEventsByTimeStamp[timeStampKey] = [NSMutableArray arrayWithObject:tempoEvent];
tempoEventsByTimeStamp[timeStampKey] = tempoEvent;
}
}
if (self.needsCurrentTempoUpdate) {
if (!tempoEventsByTimeStamp.count) {
if (!overrideTempo) overrideTempo = [sequence tempoAtTimeStamp:fromMusicTimeStamp];
if (!overrideTempo) overrideTempo = kDefaultTempo;
MIKMIDITempoEvent *tempoEvent = [MIKMIDITempoEvent tempoEventWithTimeStamp:fromMusicTimeStamp tempo:overrideTempo];
NSNumber *timeStampKey = @(fromMusicTimeStamp);
allEventsByTimeStamp[timeStampKey] = [NSMutableArray arrayWithObject:tempoEvent];
tempoEventsByTimeStamp[timeStampKey] = tempoEvent;
}
self.needsCurrentTempoUpdate = NO;
}
// Get pending note off events
NSMutableDictionary *pendingNoteOffs = self.pendingNoteOffs;
for (NSNumber *timeStampKey in [pendingNoteOffs copy]) {
MusicTimeStamp pendingNoteOffsMusicTimeStamp = timeStampKey.doubleValue;
if (pendingNoteOffsMusicTimeStamp < fromMusicTimeStamp) continue;
if (pendingNoteOffsMusicTimeStamp > toMusicTimeStamp) continue;
if (isLooping && (pendingNoteOffsMusicTimeStamp == loopEndTimeStamp)) continue; // These pending note offs will be handled right before we loop
NSMutableArray *eventsAtTimeStamp = allEventsByTimeStamp[timeStampKey] ? allEventsByTimeStamp[timeStampKey] : [NSMutableArray array];
[eventsAtTimeStamp addObject:pendingNoteOffs[timeStampKey]];
allEventsByTimeStamp[timeStampKey] = eventsAtTimeStamp;
[pendingNoteOffs removeObjectForKey:timeStampKey];
}
// Get other events
NSMutableArray *nonMutedTracks = [[NSMutableArray alloc] init];
NSMutableArray *soloTracks = [[NSMutableArray alloc] init];
for (MIKMIDITrack *track in sequence.tracks) {
if (track.isMuted) continue;
[nonMutedTracks addObject:track];
if (track.solo) { [soloTracks addObject:track]; }
}
// Never play muted tracks. If any non-muted tracks are soloed, only play those. Matches MusicPlayer behavior
NSArray *tracksToPlay = soloTracks.count != 0 ? soloTracks : nonMutedTracks;
for (MIKMIDITrack *track in tracksToPlay) {
MusicTimeStamp startTimeStamp = MAX(fromMusicTimeStamp - track.offset, 0);
MusicTimeStamp endTimeStamp = toMusicTimeStamp - track.offset;
NSArray *events = [track eventsFromTimeStamp:startTimeStamp toTimeStamp:endTimeStamp];
if (track.offset != 0) {
// Shift events by offset
NSMutableArray *shiftedEvents = [NSMutableArray array];
for (MIKMIDIEvent *event in events) {
MIKMutableMIDIEvent *shiftedEvent = [event mutableCopy];
shiftedEvent.timeStamp += track.offset;
[shiftedEvents addObject:shiftedEvent];
}
events = shiftedEvents;
}
id<MIKMIDICommandScheduler> destination = events.count ? [self commandSchedulerForTrack:track] : nil; // only get the destination if there's events so we don't create a destination endpoint if not needed
for (MIKMIDIEvent *event in events) {
if ([event isKindOfClass:[MIKMIDINoteEvent class]] && [(MIKMIDINoteEvent *)event duration] <= 0) continue;
NSNumber *timeStampKey = @(event.timeStamp);
NSMutableArray *eventsAtTimeStamp = allEventsByTimeStamp[timeStampKey] ? allEventsByTimeStamp[timeStampKey] : [NSMutableArray array];
[eventsAtTimeStamp addObject:[MIKMIDIEventWithDestination eventWithDestination:destination event:event]];
allEventsByTimeStamp[timeStampKey] = eventsAtTimeStamp;
}
}
// Get click track events
for (MIKMIDIEventWithDestination *destinationEvent in [self clickTrackEventsFromTimeStamp:fromMusicTimeStamp toTimeStamp:toMusicTimeStamp]) {
NSNumber *timeStampKey = @(destinationEvent.event.timeStamp);
NSMutableArray *eventsAtTimesStamp = allEventsByTimeStamp[timeStampKey] ? allEventsByTimeStamp[timeStampKey] : [NSMutableArray array];
[eventsAtTimesStamp addObject:destinationEvent];
allEventsByTimeStamp[timeStampKey] = eventsAtTimesStamp;
}
// Schedule events
for (NSNumber *timeStampKey in [allEventsByTimeStamp.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
MusicTimeStamp musicTimeStamp = timeStampKey.doubleValue;
if (isLooping && (musicTimeStamp < loopStartTimeStamp || musicTimeStamp >= loopEndTimeStamp)) continue;
MIDITimeStamp midiTimeStamp = [clock midiTimeStampForMusicTimeStamp:musicTimeStamp];
if (midiTimeStamp < MIKMIDIGetCurrentTimeStamp() && midiTimeStamp > fromMIDITimeStamp) continue; // prevents events that were just recorded from being scheduled
MIKMIDITempoEvent *tempoEventAtTimeStamp = tempoEventsByTimeStamp[timeStampKey];
if (tempoEventAtTimeStamp) [self updateClockWithMusicTimeStamp:musicTimeStamp tempo:tempoEventAtTimeStamp.bpm atMIDITimeStamp:midiTimeStamp];
NSArray *events = allEventsByTimeStamp[timeStampKey];
for (id eventObject in events) {
if ([eventObject isKindOfClass:[MIKMIDIEventWithDestination class]]) {
[self scheduleEventWithDestination:eventObject];
} else if ([eventObject isKindOfClass:[MIKMIDIPendingNoteOffsForTimeStamp class]]) {
for (MIKMIDIEventWithDestination *noteOffEvent in [eventObject noteEventsWithEndTimeStamp]) {
[self scheduleEventWithDestination:noteOffEvent];
}
}
}
}
self.latestScheduledMIDITimeStamp = actualToMIDITimeStamp;
// Handle looping or stopping at the end of the sequence
if (isLooping) {
if (calculatedToMusicTimeStamp > toMusicTimeStamp) {
[self recordAllPendingNoteEventsWithOffTimeStamp:loopEndTimeStamp];
Float64 tempo = [sequence tempoAtTimeStamp:loopStartTimeStamp];
if (!tempo) tempo = kDefaultTempo;
MusicTimeStamp loopLength = loopEndTimeStamp - loopStartTimeStamp;
MIDITimeStamp loopStartMIDITimeStamp = [clock midiTimeStampForMusicTimeStamp:loopStartTimeStamp + loopLength];
[self sendAllPendingNoteOffsWithMIDITimeStamp:loopStartMIDITimeStamp];
[self updateClockWithMusicTimeStamp:loopStartTimeStamp tempo:tempo atMIDITimeStamp:loopStartMIDITimeStamp];
self.startingTimeStamp = loopStartTimeStamp;
[[NSNotificationCenter defaultCenter] postNotificationName:MIKMIDISequencerWillLoopNotification object:self userInfo:nil];
[self processSequenceStartingFromMIDITimeStamp:loopStartMIDITimeStamp];
}
} else if (!self.isRecording) { // Don't stop automatically during recording
MIDITimeStamp systemTimeStamp = MIKMIDIGetCurrentTimeStamp();
if ((systemTimeStamp > actualToMIDITimeStamp) && ([clock musicTimeStampForMIDITimeStamp:systemTimeStamp] >= self.sequenceLength)) {
[self stopWithDispatchToProcessingQueue:NO];
}
}
}
- (void)scheduleEventWithDestination:(MIKMIDIEventWithDestination *)destinationEvent
{
MIKMIDIEvent *event = destinationEvent.event;
id<MIKMIDICommandScheduler> destination = destinationEvent.destination;
MIKMIDIClock *clock = self.clock;
MIKMIDICommand *command;
if (event.eventType == MIKMIDIEventTypeMIDINoteMessage) {
if (destinationEvent.representsNoteOff) {
command = [MIKMIDICommand noteOffCommandFromNoteEvent:(MIKMIDINoteEvent *)event clock:clock];
} else {
MIKMIDINoteEvent *noteEvent = (MIKMIDINoteEvent *)event;
command = [MIKMIDICommand noteOnCommandFromNoteEvent:noteEvent clock:clock];
// Add note off to pending note offs
MusicTimeStamp endTimeStamp = noteEvent.endTimeStamp;
NSMutableDictionary *pendingNoteOffs = self.pendingNoteOffs;
MIKMIDIPendingNoteOffsForTimeStamp *pendingNoteOffsForEndTimeStamp = pendingNoteOffs[@(endTimeStamp)];
if (!pendingNoteOffsForEndTimeStamp) {
pendingNoteOffsForEndTimeStamp = [MIKMIDIPendingNoteOffsForTimeStamp pendingNoteOffWithEndTimeStamp:endTimeStamp];
pendingNoteOffs[@(endTimeStamp)] = pendingNoteOffsForEndTimeStamp;
}
[pendingNoteOffsForEndTimeStamp.noteEventsWithEndTimeStamp addObject:[MIKMIDIEventWithDestination eventWithDestination:destination event:event representsNoteOff:YES]];
}
} else if ([event isKindOfClass:[MIKMIDIChannelEvent class]]) {
command = [MIKMIDICommand commandFromChannelEvent:(MIKMIDIChannelEvent *)event clock:clock];
}
if (command) {
[self scheduleCommands:@[command] withCommandScheduler:destination];
}
}
- (void)sendAllPendingNoteOffsWithMIDITimeStamp:(MIDITimeStamp)offTimeStamp
{
NSMutableDictionary *noteOffs = self.pendingNoteOffs;
if (!noteOffs.count) return;
NSMapTable *noteOffDestinationsToCommands = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsStrongMemory];
MIKMIDIClock *clock = self.clock;
for (NSNumber *musicTimeStampNumber in noteOffs) {
MIKMIDIPendingNoteOffsForTimeStamp *pendingNoteOffs = noteOffs[musicTimeStampNumber];
for (MIKMIDIEventWithDestination *noteOffEventWithDestination in pendingNoteOffs.noteEventsWithEndTimeStamp) {
MIKMIDINoteEvent *event = (MIKMIDINoteEvent *)noteOffEventWithDestination.event;
id<MIKMIDICommandScheduler> destination = noteOffEventWithDestination.destination;
NSMutableArray *noteOffCommandsForDestination = [noteOffDestinationsToCommands objectForKey:destination] ? [noteOffDestinationsToCommands objectForKey:destination] : [NSMutableArray array];
MIKMutableMIDICommand *noteOffCommand = [[MIKMIDICommand noteOffCommandFromNoteEvent:event clock:clock] mutableCopy];
noteOffCommand.midiTimestamp = offTimeStamp;
[noteOffCommandsForDestination addObject:noteOffCommand];
[noteOffDestinationsToCommands setObject:noteOffCommandsForDestination forKey:destination];
}
}
for (id<MIKMIDICommandScheduler> scheduler in [[noteOffDestinationsToCommands keyEnumerator] allObjects]) {
[self scheduleCommands:[noteOffDestinationsToCommands objectForKey:scheduler] withCommandScheduler:scheduler];
}
[noteOffs removeAllObjects];
}
- (void)updateClockWithMusicTimeStamp:(MusicTimeStamp)musicTimeStamp tempo:(Float64)tempo atMIDITimeStamp:(MIDITimeStamp)midiTimeStamp
{
// Override tempo if neccessary
Float64 tempoOverride = self.tempo;
if (tempoOverride) tempo = tempoOverride;
[self.clock syncMusicTimeStamp:musicTimeStamp withMIDITimeStamp:midiTimeStamp tempo:tempo];
}
- (void)scheduleCommands:(NSArray *)commands withCommandScheduler:(id<MIKMIDICommandScheduler>)scheduler
{
[scheduler scheduleMIDICommands:[self modifiedMIDICommandsFromCommandsToBeScheduled:commands forCommandScheduler:scheduler]];
}
- (NSArray *)modifiedMIDICommandsFromCommandsToBeScheduled:(NSArray *)commandsToBeScheduled forCommandScheduler:(id<MIKMIDICommandScheduler>)scheduler { return commandsToBeScheduled; }
- (void)scheduleEventWithDestinationRRC2SOFT:(MIKMIDIEventWithDestination *)destinationEvent
{
MIKMIDIEvent *event = destinationEvent.event;
id<MIKMIDICommandScheduler> destination = destinationEvent.destination;
MIKMIDIClock *clock = self.clock;
MIKMIDICommand *command;
if (event.eventType == MIKMIDIEventTypeMIDINoteMessage) {
if (destinationEvent.representsNoteOff) {
command = [MIKMIDICommand noteOffCommandFromNoteEvent:(MIKMIDINoteEvent *)event clock:clock];
} else {
MIKMIDINoteEvent *noteEvent = (MIKMIDINoteEvent *)event;
command = [MIKMIDICommand noteOnCommandFromNoteEvent:noteEvent clock:clock];
}
} else if ([event isKindOfClass:[MIKMIDIChannelEvent class]]) {
command = [MIKMIDICommand commandFromChannelEvent:(MIKMIDIChannelEvent *)event clock:clock];
}
if (command) {
NSError *error;
// RRC2SOFT - Optimization - This function gets called a lot!
[[MIKMIDIDeviceManager sharedDeviceManager].outputPort sendCommand:command
toDestination:destination
error:&error];
// [self scheduleCommands:@[command] withCommandScheduler:destination];
}
}
- (MIKMIDIEventWithDestination *)addNoteOff: (MIKMIDIEvent *) event
WithDestinationRRC2SOFT:(id<MIKMIDICommandScheduler>) destination
{
MIKMIDIEventWithDestination *result = nil;
if (event.eventType == MIKMIDIEventTypeMIDINoteMessage) {
MIKMIDINoteEvent *noteEvent = (MIKMIDINoteEvent *)event;
// Add note off to pending note offs
MusicTimeStamp endTimeStamp = noteEvent.endTimeStamp;
NSMutableDictionary *pendingNoteOffs = self.pendingNoteOffs;
MIKMIDIPendingNoteOffsForTimeStamp *pendingNoteOffsForEndTimeStamp = pendingNoteOffs[@(endTimeStamp)];
if (!pendingNoteOffsForEndTimeStamp) {
pendingNoteOffsForEndTimeStamp = [MIKMIDIPendingNoteOffsForTimeStamp pendingNoteOffWithEndTimeStamp:endTimeStamp];
pendingNoteOffs[@(endTimeStamp)] = pendingNoteOffsForEndTimeStamp;
}
MIKMIDIEventWithDestination *eventOFF = [MIKMIDIEventWithDestination eventWithDestination:destination
event:event representsNoteOff:YES];
[pendingNoteOffsForEndTimeStamp.noteEventsWithEndTimeStamp addObject: eventOFF];
result = eventOFF;
}
return result;
}
- (void)processSequenceStartingFromMIDITimeStampRRC2SOFT:(MIDITimeStamp)fromMIDITimeStamp keepLock: (BOOL) keepLock
{
// LOCK
if (!keepLock) { _processingFakeLock = YES; }
// RRC2SOFT - Empty
[self.pendingNoteOffsWithinCycle removeAllObjects];
// Obtain relevant MusicTimeStamp AND MIDITimeStamp values
MIDITimeStamp toMIDITimeStamp = MIKMIDIGetCurrentTimeStamp() + MIKMIDIClockMIDITimeStampsPerTimeInterval(self.maximumLookAheadInterval);
if (toMIDITimeStamp < fromMIDITimeStamp) {
if (!keepLock) { _processingFakeLock = NO; }
return;
}
MIKMIDIClock *clock = self.clock;
// RRC2SOFT
const int64_t kOneMillion = 1000 * 1000;
static mach_timebase_info_data_t s_timebase_info;
if (s_timebase_info.denom == 0) {
(void) mach_timebase_info(&s_timebase_info);
}
// mach_absolute_time() returns billionth of seconds,
// so divide by one million to get milliseconds
uint64_t elapsed = toMIDITimeStamp - fromMIDITimeStamp;
uint64_t elapsedNano = ((elapsed * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
uint64_t fromNano = ((fromMIDITimeStamp * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
uint64_t toNano = ((toMIDITimeStamp * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
// RRC2SOFT End
MIKMIDISequence *sequence = self.sequence;
MusicTimeStamp loopStartTimeStamp = self.loopStartTimeStamp;
MusicTimeStamp loopEndTimeStamp = self.effectiveLoopEndTimeStamp;
MusicTimeStamp fromMusicTimeStamp = [clock musicTimeStampForMIDITimeStamp:fromMIDITimeStamp];
MusicTimeStamp calculatedToMusicTimeStamp = [clock musicTimeStampForMIDITimeStamp:toMIDITimeStamp];
BOOL isLooping = (self.shouldLoop && calculatedToMusicTimeStamp > loopStartTimeStamp && loopEndTimeStamp > loopStartTimeStamp);
if (isLooping != self.isLooping) self.looping = isLooping;
MusicTimeStamp maxToMusicTimeStamp = self.isRecording ? DBL_MAX : self.sequenceLength; // If recording, don't limit max timestamp (Issue #45)
// "RRC2Soft": If LoopEndTimeStamp is set, end it at that point
//maxToMusicTimeStamp = isLooping ? loopEndTimeStamp : maxToMusicTimeStamp;
maxToMusicTimeStamp = (loopEndTimeStamp > 0.) ? loopEndTimeStamp : maxToMusicTimeStamp;
MusicTimeStamp toMusicTimeStamp = MIN(calculatedToMusicTimeStamp, maxToMusicTimeStamp);
MIDITimeStamp actualToMIDITimeStamp = [clock midiTimeStampForMusicTimeStamp:toMusicTimeStamp];
_lastPlaybackTimeStamp = fromMusicTimeStamp; // RRC2SOFT - capture this value
// Initialize THE list that will be used to schedule all the events
NSMutableDictionary *allEventsByTimeStamp = [NSMutableDictionary dictionary];
// Get relevant tempo events
NSMutableDictionary *tempoEventsByTimeStamp = [NSMutableDictionary dictionary];
Float64 overrideTempo = self.tempo;
if ((!overrideTempo) || (self.needsCurrentTempoUpdate)) {
NSArray *sequenceTempoEvents = [sequence.tempoTrack eventsOfClass:[MIKMIDITempoEvent class] fromTimeStamp:MAX(fromMusicTimeStamp, 0) toTimeStamp:MAX(toMusicTimeStamp, 0)];
for (MIKMIDITempoEvent *tempoEvent in sequenceTempoEvents) {
NSNumber *timeStampKey = @(tempoEvent.timeStamp);
allEventsByTimeStamp[timeStampKey] = [NSMutableArray arrayWithObject:tempoEvent];
tempoEventsByTimeStamp[timeStampKey] = tempoEvent;
}
}
if ((self.needsCurrentTempoUpdate) || (self.needsOverrideTempoUpdate)) {
if (!tempoEventsByTimeStamp.count) {
if ((!overrideTempo) || (self.needsCurrentTempoUpdate))
overrideTempo = [sequence tempoAtTimeStamp:fromMusicTimeStamp]; // Get the tempo from the track
if (!overrideTempo) overrideTempo = kDefaultTempo;
MIKMIDITempoEvent *tempoEvent = [MIKMIDITempoEvent tempoEventWithTimeStamp:fromMusicTimeStamp tempo:overrideTempo];
NSNumber *timeStampKey = @(fromMusicTimeStamp);
allEventsByTimeStamp[timeStampKey] = [NSMutableArray arrayWithObject:tempoEvent];
tempoEventsByTimeStamp[timeStampKey] = tempoEvent;
}
self.needsOverrideTempoUpdate = NO;
}
// Get all other events
NSMutableArray *nonMutedTracks = [[NSMutableArray alloc] init];
NSMutableArray *soloTracks = [[NSMutableArray alloc] init];
for (MIKMIDITrack *track in sequence.tracks) {
// Never play muted tracks. If any non-muted tracks are soloed, only play those. Matches MusicPlayer behavior
if (track.isMuted) continue;
[nonMutedTracks addObject:track];
if (track.solo) { [soloTracks addObject:track]; }
}
NSArray *tracksToPlay = soloTracks.count != 0 ? soloTracks : nonMutedTracks;
for (MIKMIDITrack *track in tracksToPlay) {
MusicTimeStamp startTimeStamp = MAX(fromMusicTimeStamp - track.offset, 0);
MusicTimeStamp endTimeStamp = toMusicTimeStamp - track.offset;
NSArray *events = [track eventsFromTimeStamp:startTimeStamp toTimeStamp:endTimeStamp];
if (track.offset != 0) {
// Shift events by offset
NSMutableArray *shiftedEvents = [NSMutableArray array];
for (MIKMIDIEvent *event in events) {
MIKMutableMIDIEvent *shiftedEvent = [event mutableCopy];
shiftedEvent.timeStamp += track.offset;
[shiftedEvents addObject:shiftedEvent];
}
events = shiftedEvents;
}
id<MIKMIDICommandScheduler> destination = events.count ? [self commandSchedulerForTrack:track] : nil; // only get the destination if there's events so we don't create a destination endpoint if not needed
for (MIKMIDIEvent *event in events) {
if ([event isKindOfClass:[MIKMIDINoteEvent class]]) {
if ([(MIKMIDINoteEvent *)event duration] <= 0) continue;
}
// RRC2SOFT: If we are not looping BUT loopEndTimeStamp is active, do not schedule notes after it
if (loopEndTimeStamp > 0.) {
if ([event timeStamp] >= loopEndTimeStamp) continue;
}
NSNumber *timeStampKey = @(event.timeStamp);
NSMutableArray *eventsAtTimeStamp = allEventsByTimeStamp[timeStampKey] ? allEventsByTimeStamp[timeStampKey] : [NSMutableArray array];
MIKMIDIEventWithDestination *eventDestination = [MIKMIDIEventWithDestination
eventWithDestination:destination event:event];
[eventsAtTimeStamp addObject:eventDestination];
allEventsByTimeStamp[timeStampKey] = eventsAtTimeStamp;
[self addNoteOff:event WithDestinationRRC2SOFT:destination];
}
}
// Now, Get the pending note off events. We will retrieve only the note off events that are useful in this cycle
// RRC2SOFT: We will also copy them to the pending array
NSMutableDictionary *pendingNoteOffs = self.pendingNoteOffs;
for (NSNumber *timeStampKey in [pendingNoteOffs copy]) {
MusicTimeStamp pendingNoteOffsMusicTimeStamp = timeStampKey.doubleValue;
if (pendingNoteOffsMusicTimeStamp < fromMusicTimeStamp) continue;
if (pendingNoteOffsMusicTimeStamp > toMusicTimeStamp) continue;
[self.pendingNoteOffsWithinCycle addObject:pendingNoteOffs[timeStampKey]];
if (isLooping && (pendingNoteOffsMusicTimeStamp == loopEndTimeStamp)) continue; // These pending note offs will be handled right before we loop
NSMutableArray *eventsAtTimeStamp = allEventsByTimeStamp[timeStampKey] ? allEventsByTimeStamp[timeStampKey] : [NSMutableArray array];
[eventsAtTimeStamp addObject:pendingNoteOffs[timeStampKey]];
allEventsByTimeStamp[timeStampKey] = eventsAtTimeStamp;
[pendingNoteOffs removeObjectForKey:timeStampKey];
}
// Get click track events
for (MIKMIDIEventWithDestination *destinationEvent in [self clickTrackEventsFromTimeStamp:fromMusicTimeStamp toTimeStamp:toMusicTimeStamp]) {
NSNumber *timeStampKey = @(destinationEvent.event.timeStamp);
NSMutableArray *eventsAtTimesStamp = allEventsByTimeStamp[timeStampKey] ? allEventsByTimeStamp[timeStampKey] : [NSMutableArray array];
[eventsAtTimesStamp addObject:destinationEvent];
allEventsByTimeStamp[timeStampKey] = eventsAtTimesStamp;
}
// Schedule events
for (NSNumber *timeStampKey in [allEventsByTimeStamp.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
MusicTimeStamp musicTimeStamp = timeStampKey.doubleValue;
if (isLooping && (musicTimeStamp < loopStartTimeStamp || musicTimeStamp >= loopEndTimeStamp)) continue;
MIDITimeStamp midiTimeStamp = [clock midiTimeStampForMusicTimeStamp:musicTimeStamp];
if (midiTimeStamp < MIKMIDIGetCurrentTimeStamp() && midiTimeStamp > fromMIDITimeStamp) continue; // prevents events that were just recorded from being scheduled
if (self.preRoll && !isLooping && !self.playNotesOutsideLoop) {
if (musicTimeStamp < loopStartTimeStamp) continue; // Avoid playing notes on preroll
}
MIKMIDITempoEvent *tempoEventAtTimeStamp = tempoEventsByTimeStamp[timeStampKey];
if (tempoEventAtTimeStamp) [self updateClockWithMusicTimeStamp:musicTimeStamp tempo:tempoEventAtTimeStamp.bpm atMIDITimeStamp:midiTimeStamp];
// RRC2SOFT: Reorders array, so noteOFF are triggered first
NSArray *events = allEventsByTimeStamp[timeStampKey];
NSMutableArray *eventsOrdered = [[NSMutableArray alloc] initWithCapacity:events.count];
for (id eventObject in events) {
if ([eventObject isKindOfClass:[MIKMIDIPendingNoteOffsForTimeStamp class]]) {
[eventsOrdered insertObject:eventObject atIndex:0];
} else {
[eventsOrdered addObject:eventObject];
}
}
for (id eventObject in eventsOrdered) {
if ([eventObject isKindOfClass:[MIKMIDIEventWithDestination class]]) {
[self scheduleEventWithDestinationRRC2SOFT:eventObject];
} else if ([eventObject isKindOfClass:[MIKMIDIPendingNoteOffsForTimeStamp class]]) {
for (MIKMIDIEventWithDestination *noteOffEvent in [eventObject noteEventsWithEndTimeStamp]) {
// Fixes the timestamp with the REAL noteOff end
noteOffEvent.event = [[MIKMIDINoteEvent alloc]
initWithTimeStamp:((MIKMIDINoteEvent *)noteOffEvent.event).endTimeStamp
midiEventType:noteOffEvent.event.eventType
data:[noteOffEvent.event.data copy]];
[self scheduleEventWithDestinationRRC2SOFT:noteOffEvent];
}
}
}
}
self.latestScheduledMIDITimeStamp = actualToMIDITimeStamp;
// Handle looping or stopping at the end of the sequence
if (isLooping) {
//MIDITimeStamp systemTimeStamp = MIKMIDIGetCurrentTimeStamp();
//MIDITimeStamp actualLoopEndTimeStamp = [clock midiTimeStampForMusicTimeStamp:toMusicTimeStamp];
if (calculatedToMusicTimeStamp > toMusicTimeStamp) {
//if (systemTimeStamp >= actualLoopEndTimeStamp) {
[self recordAllPendingNoteEventsWithOffTimeStamp:loopEndTimeStamp];
Float64 tempo = overrideTempo ? _tempo : [sequence tempoAtTimeStamp:loopStartTimeStamp];
if (!tempo) tempo = kDefaultTempo;
MusicTimeStamp loopLength = loopEndTimeStamp - loopStartTimeStamp;
MIDITimeStamp loopStartMIDITimeStamp = [clock midiTimeStampForMusicTimeStamp:loopStartTimeStamp + loopLength];
[self sendAllPendingNoteOffsWithMIDITimeStamp:loopStartMIDITimeStamp];
[self updateClockWithMusicTimeStamp:loopStartTimeStamp tempo:tempo atMIDITimeStamp:loopStartMIDITimeStamp];
// Iterate
self.startingTimeStamp = loopStartTimeStamp;
[[NSNotificationCenter defaultCenter] postNotificationName:MIKMIDISequencerWillLoopNotification object:self userInfo:nil];
[self processSequenceStartingFromMIDITimeStampRRC2SOFT:loopStartMIDITimeStamp keepLock:YES];
}
} else if (!self.isRecording) { // Don't stop automatically during recording
MIDITimeStamp systemTimeStamp = MIKMIDIGetCurrentTimeStamp();
if ((systemTimeStamp > actualToMIDITimeStamp) && ([clock musicTimeStampForMIDITimeStamp:systemTimeStamp] >= loopEndTimeStamp)) { // loopEndTimeStamp equals sequence.length is there are no loops (RRC2SOFT)
[self stopWithDispatchToProcessingQueue:NO]; // already call record/send all pending note offs
}
}
// CLEANUP
if (self.needsCurrentTempoUpdate) {
// We already passed the first iteration. No need to use this fake forced tempo again.
_tempo = 0.;
self.needsCurrentTempoUpdate = NO;
}
// UNLOCK
if (!keepLock) { _processingFakeLock = NO; }
}
#pragma mark - Recording
- (void)startRecording
{
[self prepareForRecordingWithPreRoll:YES];
[self startPlayback];
}
- (void)startRecordingAtTimeStamp:(MusicTimeStamp)timeStamp
{
[self prepareForRecordingWithPreRoll:YES];
[self startPlaybackAtTimeStamp:timeStamp];
}
- (void)startRecordingAtTimeStamp:(MusicTimeStamp)timeStamp MIDITimeStamp:(MIDITimeStamp)midiTimeStamp
{
[self prepareForRecordingWithPreRoll:YES];
[self startPlaybackAtTimeStamp:timeStamp MIDITimeStamp:midiTimeStamp];
}
- (void)resumeRecording
{
[self prepareForRecordingWithPreRoll:YES];
[self resumePlayback];
}
- (void)prepareForRecordingWithPreRoll:(BOOL)includePreRoll
{
self.pendingRecordedNoteEvents = [NSMutableDictionary dictionary];
self.recording = YES;
}
- (void)recordMIDICommand:(MIKMIDICommand *)command
{
if (!self.isRecording) return;
MIDITimeStamp midiTimeStamp = command.midiTimestamp;
MusicTimeStamp musicTimeStamp = [self.clock musicTimeStampForMIDITimeStamp:midiTimeStamp];
if (musicTimeStamp < 0) { return; } // Command is in pre-roll
MIKMIDIEvent *event;
if ([command isKindOfClass:[MIKMIDINoteOnCommand class]]) { // note On
MIKMIDINoteOnCommand *noteOnCommand = (MIKMIDINoteOnCommand *)command;
if (noteOnCommand.velocity) {
MIDINoteMessage message = { .channel = noteOnCommand.channel, .note = noteOnCommand.note, .velocity = noteOnCommand.velocity, 0, 0 };
MIKMutableMIDINoteEvent *noteEvent = [MIKMutableMIDINoteEvent noteEventWithTimeStamp:musicTimeStamp message:message];
NSNumber *noteNumber = @(noteOnCommand.note);
NSMutableSet *noteEventsAtNote = self.pendingRecordedNoteEvents[noteNumber];
if (!noteEventsAtNote) {
noteEventsAtNote = [NSMutableSet setWithCapacity:1];
self.pendingRecordedNoteEvents[noteNumber] = noteEventsAtNote;
}
[noteEventsAtNote addObject:noteEvent];
} else { // Velocity is 0, treat as a note Off per MIDI spec
event = [self pendingNoteEventWithNoteNumber:@(noteOnCommand.note) channel:noteOnCommand.channel releaseVelocity:0 offTimeStamp:musicTimeStamp];
}
} else if ([command isKindOfClass:[MIKMIDINoteOffCommand class]]) { // note Off
MIKMIDINoteOffCommand *noteOffCommand = (MIKMIDINoteOffCommand *)command;
event = [self pendingNoteEventWithNoteNumber:@(noteOffCommand.note) channel:noteOffCommand.channel releaseVelocity:noteOffCommand.velocity offTimeStamp:musicTimeStamp];
}
if (event) [self.recordEnabledTracks makeObjectsPerformSelector:@selector(addEvent:) withObject:event];
}
- (void)recordAllPendingNoteEventsWithOffTimeStamp:(MusicTimeStamp)offTimeStamp
{
NSMutableSet *events = [NSMutableSet set];
NSMutableDictionary *pendingRecordedNoteEvents = self.pendingRecordedNoteEvents;
for (NSNumber *noteNumber in pendingRecordedNoteEvents) {
for (MIKMutableMIDINoteEvent *event in pendingRecordedNoteEvents[noteNumber]) {
event.releaseVelocity = 0;
event.duration = offTimeStamp - event.timeStamp;
[events addObject:event];
}
}
self.pendingRecordedNoteEvents = [NSMutableDictionary dictionary];
if ([events count]) {
for (MIKMIDITrack *track in self.recordEnabledTracks) {
[track addEvents:[events allObjects]];
}
}
}
- (MIKMIDINoteEvent *)pendingNoteEventWithNoteNumber:(NSNumber *)noteNumber channel:(UInt8)channel releaseVelocity:(UInt8)releaseVelocity offTimeStamp:(MusicTimeStamp)offTimeStamp
{
NSMutableSet *pendingRecordedNoteEventsAtNote = self.pendingRecordedNoteEvents[noteNumber];
for (MIKMutableMIDINoteEvent *noteEvent in [pendingRecordedNoteEventsAtNote copy]) {
if (channel == noteEvent.channel) {
noteEvent.releaseVelocity = releaseVelocity;
noteEvent.duration = offTimeStamp - noteEvent.timeStamp;
if (pendingRecordedNoteEventsAtNote.count > 1) {
[pendingRecordedNoteEventsAtNote removeObject:noteEvent];
} else {
[self.pendingRecordedNoteEvents removeObjectForKey:noteNumber];
}
return noteEvent;
}
}
return nil;
}
#pragma mark - Configuration
- (void)setCommandScheduler:(id<MIKMIDICommandScheduler>)commandScheduler forTrack:(MIKMIDITrack *)track
{
if (!commandScheduler) {
[self.tracksToDestinationsMap removeObjectForKey:track];
return;
}
[self.tracksToDestinationsMap setObject:commandScheduler forKey:track];
[self.tracksToDefaultSynthsMap removeObjectForKey:track];
}
- (id<MIKMIDICommandScheduler>)commandSchedulerForTrack:(MIKMIDITrack *)track
{
id<MIKMIDICommandScheduler> result = [self.tracksToDestinationsMap objectForKey:track];
if (!result && self.shouldCreateSynthsIfNeeded) {
// Create a default synthesizer
result = [[MIKMIDISynthesizer alloc] init];
[self setCommandScheduler:result forTrack:track];
[self.tracksToDefaultSynthsMap setObject:result forKey:track];
}
return result;
}
- (MIKMIDISynthesizer *)builtinSynthesizerForTrack:(MIKMIDITrack *)track
{
[[self commandSchedulerForTrack:track] self]; // Will force creation of a synth if one doesn't exist, but should
return [self.tracksToDefaultSynthsMap objectForKey:track];
}
- (void) internal_forceTempo: (Float64) tempo inBeat: (MusicTimeStamp) beat atTime: (uint64_t) mach {
//>>HELPERS
MusicTimeStamp oldEndBeatTimeStamp = [_clock musicTimeStampForMIDITimeStamp:self.latestScheduledMIDITimeStamp];
if (_playing) {
//>>CLEANUP
// Cancel the actual dispatch of processSequenceStartingFromMIDITimeStampRRC2SOFT. Will wait until the timer finishes, it is was running
self.processingTimer = NULL;
while (_processingFakeLock) {} // Wait for the lock to be relinquished
// Clean the "pendingNoteOffs" array of this cycle
// = The notes up to latestScheduledMIDITimeStamp will be turned off using _pendingNoteOffsWithinCycle.
// Therefore, there is no need to keep them within _pendingNoteOffs.
NSArray *timeStamps = [_pendingNoteOffs.allKeys sortedArrayUsingSelector:@selector(compare:)];
for (NSNumber *timeStampKey in timeStamps) {
MusicTimeStamp musicTimeStamp = timeStampKey.doubleValue;
if (musicTimeStamp < oldEndBeatTimeStamp) {
[_pendingNoteOffs removeObjectForKey:timeStampKey];
} else {
// We arrived where we wanted - finish
break;
}
}
// Cancel also other structures
[_pendingRecordedNoteEvents removeAllObjects];
// Cleans the notes in the MIDI queue (those notes follow the previous BPM, and are invalid)
//for (MIKMIDITrack *track in _sequence.tracks) {
// MIKMIDIDestinationEndpoint *dest = (MIKMIDIDestinationEndpoint *) [self commandSchedulerForTrack:track];
// MIDIFlushOutput(dest.objectRef);
//}
MIDIFlushOutput(0); // TEST
// Send a note OFF message to all pending note offs in this cycle (..latestScheduledMIDITimeStamp)
// These are all the notes that should have been turned off by processingTimer. We will turn them off NOW.
NSArray *iterationArray = [_pendingNoteOffsWithinCycle copy]; // Capture current atomic context
for (MIKMIDIPendingNoteOffsForTimeStamp *pendingNoteOffs in iterationArray) {
for (MIKMIDIEventWithDestination *noteOffEventWithDestination in pendingNoteOffs.noteEventsWithEndTimeStamp) {
// Create Command
MIKMIDINoteEvent *event = (MIKMIDINoteEvent *)noteOffEventWithDestination.event;
MIKMIDIClientDestinationEndpoint *destination = (MIKMIDIClientDestinationEndpoint *) noteOffEventWithDestination.destination;
MIKMIDICommand *noteOffCommand = [MIKMIDICommand noteOffCommandFromNoteEvent:event clock:_clock];
// Send the command directly to the destination
destination.receivedSingleMessagesHandler(destination, noteOffCommand);
}
// Remove the pending note
[_pendingNoteOffsWithinCycle removeObject:pendingNoteOffs];
}
}