-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequences_lib.py
1917 lines (1573 loc) · 77.5 KB
/
sequences_lib.py
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
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Defines sequence of notes objects for creating datasets."""
import collections
import copy
import itertools
import math
from operator import itemgetter
import random
import numpy as np
from six.moves import range # pylint: disable=redefined-builtin
import tensorflow as tf
from magenta.music import chord_symbols_lib
from magenta.music import constants
from magenta.protobuf import music_pb2
# Set the quantization cutoff.
# Note events before this cutoff are rounded down to nearest step. Notes
# above this cutoff are rounded up to nearest step. The cutoff is given as a
# fraction of a step.
# For example, with quantize_cutoff = 0.75 using 0-based indexing,
# if .75 < event <= 1.75, it will be quantized to step 1.
# If 1.75 < event <= 2.75 it will be quantized to step 2.
# A number close to 1.0 gives less wiggle room for notes that start early,
# and they will be snapped to the previous step.
QUANTIZE_CUTOFF = 0.5
# Shortcut to text annotation types.
BEAT = music_pb2.NoteSequence.TextAnnotation.BEAT
CHORD_SYMBOL = music_pb2.NoteSequence.TextAnnotation.CHORD_SYMBOL
UNKNOWN_PITCH_NAME = music_pb2.NoteSequence.UNKNOWN_PITCH_NAME
# The amount to upweight note-on events vs note-off events.
ONSET_UPWEIGHT = 5.0
# The size of the frame extension for onset event.
# Frames in [onset_frame-ONSET_WINDOW, onset_frame+ONSET_WINDOW]
# are considered to contain onset events.
ONSET_WINDOW = 1
class BadTimeSignatureException(Exception):
pass
class MultipleTimeSignatureException(Exception):
pass
class MultipleTempoException(Exception):
pass
class NegativeTimeException(Exception):
pass
class QuantizationStatusException(Exception):
"""Exception for when a sequence was unexpectedly quantized or unquantized.
Should not happen during normal operation and likely indicates a programming
error.
"""
pass
class InvalidTimeAdjustmentException(Exception):
pass
class RectifyBeatsException(Exception):
pass
def trim_note_sequence(sequence, start_time, end_time):
"""Trim notes from a NoteSequence to lie within a specified time range.
Notes starting before `start_time` are not included. Notes ending after
`end_time` are truncated.
Args:
sequence: The NoteSequence for which to trim notes.
start_time: The float time in seconds after which all notes should begin.
end_time: The float time in seconds before which all notes should end.
Returns:
A copy of `sequence` with all notes trimmed to lie between `start_time` and
`end_time`.
Raises:
QuantizationStatusException: If the sequence has already been quantized.
"""
if is_quantized_sequence(sequence):
raise QuantizationStatusException(
'Can only trim notes and chords for unquantized NoteSequence.')
subsequence = music_pb2.NoteSequence()
subsequence.CopyFrom(sequence)
del subsequence.notes[:]
for note in sequence.notes:
if note.start_time < start_time or note.start_time >= end_time:
continue
new_note = subsequence.notes.add()
new_note.CopyFrom(note)
new_note.end_time = min(note.end_time, end_time)
subsequence.total_time = min(sequence.total_time, end_time)
return subsequence
def _extract_subsequences(sequence, split_times, sustain_control_number=64):
"""Extracts multiple subsequences from a NoteSequence.
Args:
sequence: The NoteSequence to extract subsequences from.
split_times: A Python list of subsequence boundary times. The first
subsequence will start at `split_times[0]` and end at `split_times[1]`,
the next subsequence will start at `split_times[1]` and end at
`split_times[2]`, and so on with the last subsequence ending at
`split_times[-1]`.
sustain_control_number: The MIDI control number for sustain pedal.
Returns:
A Python list of new NoteSequence containing the subsequences of `sequence`.
Raises:
QuantizationStatusException: If the sequence has already been quantized.
ValueError: If there are fewer than 2 split times, or the split times are
unsorted, or if any of the subsequences would start past the end of the
sequence.
"""
if is_quantized_sequence(sequence):
raise QuantizationStatusException(
'Can only extract subsequences from unquantized NoteSequence.')
if len(split_times) < 2:
raise ValueError('Must provide at least a start and end time.')
if any(t1 > t2 for t1, t2 in zip(split_times[:-1], split_times[1:])):
raise ValueError('Split times must be sorted.')
if any(time >= sequence.total_time for time in split_times[:-1]):
raise ValueError('Cannot extract subsequence past end of sequence.')
subsequence = music_pb2.NoteSequence()
subsequence.CopyFrom(sequence)
subsequence.total_time = 0.0
del subsequence.notes[:]
del subsequence.time_signatures[:]
del subsequence.key_signatures[:]
del subsequence.tempos[:]
del subsequence.text_annotations[:]
del subsequence.control_changes[:]
del subsequence.pitch_bends[:]
subsequences = [
copy.deepcopy(subsequence) for _ in range(len(split_times) - 1)
]
# Extract notes into subsequences.
subsequence_index = -1
for note in sorted(sequence.notes, key=lambda note: note.start_time):
if note.start_time < split_times[0]:
continue
while (subsequence_index < len(split_times) - 1 and
note.start_time >= split_times[subsequence_index + 1]):
subsequence_index += 1
if subsequence_index == len(split_times) - 1:
break
subsequences[subsequence_index].notes.extend([note])
subsequences[subsequence_index].notes[-1].start_time -= (
split_times[subsequence_index])
subsequences[subsequence_index].notes[-1].end_time = min(
note.end_time,
split_times[subsequence_index + 1]) - split_times[subsequence_index]
if (subsequences[subsequence_index].notes[-1].end_time >
subsequences[subsequence_index].total_time):
subsequences[subsequence_index].total_time = (
subsequences[subsequence_index].notes[-1].end_time)
# Extract time signatures, key signatures, tempos, and chord changes (beats
# are handled below, other text annotations and pitch bends are deleted).
# Additional state events will be added to the beginning of each subsequence.
events_by_type = [
sequence.time_signatures, sequence.key_signatures, sequence.tempos,
[
annotation for annotation in sequence.text_annotations
if annotation.annotation_type == CHORD_SYMBOL
]
]
new_event_containers = [[s.time_signatures for s in subsequences],
[s.key_signatures for s in subsequences],
[s.tempos for s in subsequences],
[s.text_annotations for s in subsequences]]
for events, containers in zip(events_by_type, new_event_containers):
previous_event = None
subsequence_index = -1
for event in sorted(events, key=lambda event: event.time):
if event.time <= split_times[0]:
previous_event = event
continue
while (subsequence_index < len(split_times) - 1 and
event.time > split_times[subsequence_index + 1]):
subsequence_index += 1
if subsequence_index == len(split_times) - 1:
break
if previous_event is not None:
# Add state event to the beginning of the subsequence.
containers[subsequence_index].extend([previous_event])
containers[subsequence_index][-1].time = 0.0
if subsequence_index == len(split_times) - 1:
break
# Only add the event if it's actually inside the subsequence (and not on
# the boundary with the next one).
if event.time < split_times[subsequence_index + 1]:
containers[subsequence_index].extend([event])
containers[subsequence_index][-1].time -= split_times[subsequence_index]
previous_event = event
# Add final state event to the beginning of all remaining subsequences.
while subsequence_index < len(split_times) - 2:
subsequence_index += 1
if previous_event is not None:
containers[subsequence_index].extend([previous_event])
containers[subsequence_index][-1].time = 0.0
# Copy stateless events to subsequences. Unlike the stateful events above,
# stateless events do not have an effect outside of the subsequence in which
# they occur.
stateless_events_by_type = [[
annotation for annotation in sequence.text_annotations
if annotation.annotation_type in (BEAT,)
]]
new_stateless_event_containers = [[s.text_annotations for s in subsequences]]
for events, containers in zip(stateless_events_by_type,
new_stateless_event_containers):
subsequence_index = -1
for event in sorted(events, key=lambda event: event.time):
if event.time < split_times[0]:
continue
while (subsequence_index < len(split_times) - 1 and
event.time >= split_times[subsequence_index + 1]):
subsequence_index += 1
if subsequence_index == len(split_times) - 1:
break
containers[subsequence_index].extend([event])
containers[subsequence_index][-1].time -= split_times[subsequence_index]
# Extract sustain pedal events (other control changes are deleted). Sustain
# pedal state is maintained per-instrument and added to the beginning of each
# subsequence.
sustain_events = [
cc for cc in sequence.control_changes
if cc.control_number == sustain_control_number
]
previous_sustain_events = {}
subsequence_index = -1
for sustain_event in sorted(sustain_events, key=lambda event: event.time):
if sustain_event.time <= split_times[0]:
previous_sustain_events[sustain_event.instrument] = sustain_event
continue
while (subsequence_index < len(split_times) - 1 and
sustain_event.time > split_times[subsequence_index + 1]):
subsequence_index += 1
if subsequence_index == len(split_times) - 1:
break
# Add the current sustain pedal state to the beginning of the subsequence.
for previous_sustain_event in previous_sustain_events.values():
subsequences[subsequence_index].control_changes.extend(
[previous_sustain_event])
subsequences[subsequence_index].control_changes[-1].time = 0.0
if subsequence_index == len(split_times) - 1:
break
# Only add the sustain event if it's actually inside the subsequence (and
# not on the boundary with the next one).
if sustain_event.time < split_times[subsequence_index + 1]:
subsequences[subsequence_index].control_changes.extend([sustain_event])
subsequences[subsequence_index].control_changes[-1].time -= (
split_times[subsequence_index])
previous_sustain_events[sustain_event.instrument] = sustain_event
# Add final sustain pedal state to the beginning of all remaining
# subsequences.
while subsequence_index < len(split_times) - 2:
subsequence_index += 1
for _, previous_sustain_event in previous_sustain_events.items():
subsequences[subsequence_index].control_changes.extend(
[previous_sustain_event])
subsequences[subsequence_index].control_changes[-1].time = 0.0
# Set subsequence info for all subsequences.
for subsequence, start_time in zip(subsequences, split_times[:-1]):
subsequence.subsequence_info.start_time_offset = start_time
subsequence.subsequence_info.end_time_offset = (
sequence.total_time - start_time - subsequence.total_time)
return subsequences
def extract_subsequence(sequence,
start_time,
end_time,
sustain_control_number=64):
"""Extracts a subsequence from a NoteSequence.
Notes starting before `start_time` are not included. Notes ending after
`end_time` are truncated. Time signature, tempo, key signature, chord changes,
and sustain pedal events outside the specified time range are removed;
however, the most recent event of each of these types prior to `start_time` is
included at `start_time`. This means that e.g. if a time signature of 3/4 is
specified in the original sequence prior to `start_time` (and is not followed
by a different time signature), the extracted subsequence will include a 3/4
time signature event at `start_time`. Pitch bends and control changes other
than sustain are removed entirely.
The extracted subsequence is shifted to start at time zero.
Args:
sequence: The NoteSequence to extract a subsequence from.
start_time: The float time in seconds to start the subsequence.
end_time: The float time in seconds to end the subsequence.
sustain_control_number: The MIDI control number for sustain pedal.
Returns:
A new NoteSequence containing the subsequence of `sequence` from the
specified time range.
Raises:
QuantizationStatusException: If the sequence has already been quantized.
ValueError: If `start_time` is past the end of `sequence`.
"""
return _extract_subsequences(
sequence,
split_times=[start_time, end_time],
sustain_control_number=sustain_control_number)[0]
def shift_sequence_times(sequence, shift_seconds):
"""Shifts times in a notesequence.
Only forward shifts are supported.
Args:
sequence: The NoteSequence to shift.
shift_seconds: The amount to shift.
Returns:
A new NoteSequence with shifted times.
Raises:
ValueError: If the shift amount is invalid.
QuantizationStatusException: If the sequence has already been quantized.
"""
if shift_seconds <= 0:
raise ValueError('Invalid shift amount: {}'.format(shift_seconds))
if is_quantized_sequence(sequence):
raise QuantizationStatusException(
'Can shift only unquantized NoteSequences.')
shifted = music_pb2.NoteSequence()
shifted.CopyFrom(sequence)
# Delete subsequence_info because our frame of reference has shifted.
shifted.ClearField('subsequence_info')
# Shift notes.
for note in shifted.notes:
note.start_time += shift_seconds
note.end_time += shift_seconds
events_to_shift = [
shifted.time_signatures, shifted.key_signatures, shifted.tempos,
shifted.pitch_bends, shifted.control_changes, shifted.text_annotations,
shifted.section_annotations
]
for event in itertools.chain(*events_to_shift):
event.time += shift_seconds
shifted.total_time += shift_seconds
return shifted
def remove_redundant_data(sequence):
"""Returns a copy of the sequence with redundant data removed.
An event is considered redundant if it is a time signature, a key signature,
or a tempo that differs from the previous event of the same type only by time.
For example, a tempo mark of 120 qpm at 5 seconds would be considered
redundant if it followed a tempo mark of 120 qpm and 4 seconds.
Fields in sequence_metadata are considered redundant if the same string is
repeated.
Args:
sequence: The sequence to process.
Returns:
A new sequence with redundant events removed.
"""
fixed_sequence = copy.deepcopy(sequence)
for events in [
fixed_sequence.time_signatures, fixed_sequence.key_signatures,
fixed_sequence.tempos
]:
events.sort(key=lambda e: e.time)
for i in range(len(events) - 1, 0, -1):
tmp_ts = copy.deepcopy(events[i])
tmp_ts.time = events[i - 1].time
# If the only difference between the two events is time, then delete the
# second one.
if tmp_ts == events[i - 1]:
del events[i]
if fixed_sequence.HasField('sequence_metadata'):
# Add composers and genres, preserving order, but dropping duplicates.
del fixed_sequence.sequence_metadata.composers[:]
added_composer = set()
for composer in sequence.sequence_metadata.composers:
if composer not in added_composer:
fixed_sequence.sequence_metadata.composers.append(composer)
added_composer.add(composer)
del fixed_sequence.sequence_metadata.genre[:]
added_genre = set()
for genre in sequence.sequence_metadata.genre:
if genre not in added_genre:
fixed_sequence.sequence_metadata.genre.append(genre)
added_genre.add(genre)
return fixed_sequence
def concatenate_sequences(sequences, sequence_durations=None):
"""Concatenate a series of NoteSequences together.
Individual sequences will be shifted using shift_sequence_times and then
merged together using the protobuf MergeFrom method. This means that any
global values (e.g., ticks_per_quarter) will be overwritten by each sequence
and only the final value will be used. After this, redundant data will be
removed with remove_redundant_data.
Args:
sequences: A list of sequences to concatenate.
sequence_durations: An optional list of sequence durations to use. If not
specified, the total_time value will be used. Specifying durations is
useful if the sequences to be concatenated are effectively longer than
their total_time (e.g., a sequence that ends with a rest).
Returns:
A new sequence that is the result of concatenating *sequences.
Raises:
ValueError: If the length of sequences and sequence_durations do not match
or if a specified duration is less than the total_time of the sequence.
"""
if sequence_durations and len(sequences) != len(sequence_durations):
raise ValueError(
'sequences and sequence_durations must be the same length.')
current_total_time = 0
cat_seq = music_pb2.NoteSequence()
for i in range(len(sequences)):
sequence = sequences[i]
if sequence_durations and sequence_durations[i] < sequence.total_time:
raise ValueError(
'Specified sequence duration ({}) must not be less than the '
'total_time of the sequence ({})'.format(sequence_durations[i],
sequence.total_time))
if current_total_time > 0:
cat_seq.MergeFrom(shift_sequence_times(sequence, current_total_time))
else:
cat_seq.MergeFrom(sequence)
if sequence_durations:
current_total_time += sequence_durations[i]
else:
current_total_time = cat_seq.total_time
# Delete subsequence_info because we've joined several subsequences.
cat_seq.ClearField('subsequence_info')
return remove_redundant_data(cat_seq)
def expand_section_groups(sequence):
"""Expands a NoteSequence based on its section_groups.
Args:
sequence: The sequence to expand.
Returns:
A copy of the original sequence, expanded based on its section_groups. If
the sequence has no section_groups, a copy of the original sequence will be
returned.
"""
if not sequence.section_groups:
return copy.deepcopy(sequence)
sections = {}
section_durations = {}
for i in range(len(sequence.section_annotations)):
section_id = sequence.section_annotations[i].section_id
start_time = sequence.section_annotations[i].time
if i < len(sequence.section_annotations) - 1:
end_time = sequence.section_annotations[i + 1].time
else:
end_time = sequence.total_time
subsequence = extract_subsequence(sequence, start_time, end_time)
# This is a subsequence, so the section_groups no longer make sense.
del subsequence.section_groups[:]
# This subsequence contains only 1 section and it has been shifted to time
# 0.
del subsequence.section_annotations[:]
subsequence.section_annotations.add(time=0, section_id=section_id)
sections[section_id] = subsequence
section_durations[section_id] = end_time - start_time
# Recursively expand section_groups.
def sections_in_group(section_group):
sections = []
for section in section_group.sections:
field = section.WhichOneof('section_type')
if field == 'section_id':
sections.append(section.section_id)
elif field == 'section_group':
sections.extend(sections_in_group(section.section_group))
return sections * section_group.num_times
sections_to_concat = []
for section_group in sequence.section_groups:
sections_to_concat.extend(sections_in_group(section_group))
return concatenate_sequences(
[sections[i] for i in sections_to_concat],
[section_durations[i] for i in sections_to_concat])
def _is_power_of_2(x):
return x and not x & (x - 1)
def is_quantized_sequence(note_sequence):
"""Returns whether or not a NoteSequence proto has been quantized.
Args:
note_sequence: A music_pb2.NoteSequence proto.
Returns:
True if `note_sequence` is quantized, otherwise False.
"""
# If the QuantizationInfo message has a non-zero steps_per_quarter or
# steps_per_second, assume that the proto has been quantized.
return (note_sequence.quantization_info.steps_per_quarter > 0 or
note_sequence.quantization_info.steps_per_second > 0)
def is_relative_quantized_sequence(note_sequence):
"""Returns whether a NoteSequence proto has been quantized relative to tempo.
Args:
note_sequence: A music_pb2.NoteSequence proto.
Returns:
True if `note_sequence` is quantized relative to tempo, otherwise False.
"""
# If the QuantizationInfo message has a non-zero steps_per_quarter, assume
# that the proto has been quantized relative to tempo.
return note_sequence.quantization_info.steps_per_quarter > 0
def is_absolute_quantized_sequence(note_sequence):
"""Returns whether a NoteSequence proto has been quantized by absolute time.
Args:
note_sequence: A music_pb2.NoteSequence proto.
Returns:
True if `note_sequence` is quantized by absolute time, otherwise False.
"""
# If the QuantizationInfo message has a non-zero steps_per_second, assume
# that the proto has been quantized by absolute time.
return note_sequence.quantization_info.steps_per_second > 0
def assert_is_quantized_sequence(note_sequence):
"""Confirms that the given NoteSequence proto has been quantized.
Args:
note_sequence: A music_pb2.NoteSequence proto.
Raises:
QuantizationStatusException: If the sequence is not quantized.
"""
if not is_quantized_sequence(note_sequence):
raise QuantizationStatusException(
'NoteSequence %s is not quantized.' % note_sequence.id)
def assert_is_relative_quantized_sequence(note_sequence):
"""Confirms that a NoteSequence proto has been quantized relative to tempo.
Args:
note_sequence: A music_pb2.NoteSequence proto.
Raises:
QuantizationStatusException: If the sequence is not quantized relative to
tempo.
"""
if not is_relative_quantized_sequence(note_sequence):
raise QuantizationStatusException(
'NoteSequence %s is not quantized or is '
'quantized based on absolute timing.' % note_sequence.id)
def assert_is_absolute_quantized_sequence(note_sequence):
"""Confirms that a NoteSequence proto has been quantized by absolute time.
Args:
note_sequence: A music_pb2.NoteSequence proto.
Raises:
QuantizationStatusException: If the sequence is not quantized by absolute
time.
"""
if not is_absolute_quantized_sequence(note_sequence):
raise QuantizationStatusException(
'NoteSequence %s is not quantized or is '
'quantized based on relative timing.' % note_sequence.id)
def steps_per_bar_in_quantized_sequence(note_sequence):
"""Calculates steps per bar in a NoteSequence that has been quantized.
Args:
note_sequence: The NoteSequence to examine.
Returns:
Steps per bar as a floating point number.
"""
assert_is_relative_quantized_sequence(note_sequence)
quarters_per_beat = 4.0 / note_sequence.time_signatures[0].denominator
quarters_per_bar = (
quarters_per_beat * note_sequence.time_signatures[0].numerator)
steps_per_bar_float = (
note_sequence.quantization_info.steps_per_quarter * quarters_per_bar)
return steps_per_bar_float
def split_note_sequence(note_sequence,
hop_size_seconds,
skip_splits_inside_notes=False):
"""Split one NoteSequence into many at specified time intervals.
If `hop_size_seconds` is a scalar, this function splits a NoteSequence into
multiple NoteSequences, all of fixed size (unless `split_notes` is False, in
which case splits that would have truncated notes will be skipped; i.e. each
split will either happen at a multiple of `hop_size_seconds` or not at all).
Each of the resulting NoteSequences is shifted to start at time zero.
If `hop_size_seconds` is a list, the NoteSequence will be split at each time
in the list (unless `split_notes` is False as above).
Args:
note_sequence: The NoteSequence to split.
hop_size_seconds: The hop size, in seconds, at which the NoteSequence will
be split. Alternatively, this can be a Python list of times in seconds at
which to split the NoteSequence.
skip_splits_inside_notes: If False, the NoteSequence will be split at all
hop positions, regardless of whether or not any notes are sustained across
the potential split time, thus sustained notes will be truncated. If True,
the NoteSequence will not be split at positions that occur within
sustained notes.
Returns:
A Python list of NoteSequences.
"""
notes_by_start_time = sorted(
list(note_sequence.notes), key=lambda note: note.start_time)
note_idx = 0
notes_crossing_split = []
if isinstance(hop_size_seconds, list):
split_times = sorted(hop_size_seconds)
else:
split_times = np.arange(hop_size_seconds, note_sequence.total_time,
hop_size_seconds)
valid_split_times = [0.0]
for split_time in split_times:
# Update notes crossing potential split.
while (note_idx < len(notes_by_start_time) and
notes_by_start_time[note_idx].start_time < split_time):
notes_crossing_split.append(notes_by_start_time[note_idx])
note_idx += 1
notes_crossing_split = [
note for note in notes_crossing_split if note.end_time > split_time
]
if not (skip_splits_inside_notes and notes_crossing_split):
valid_split_times.append(split_time)
# Handle the final subsequence.
if note_sequence.total_time > valid_split_times[-1]:
valid_split_times.append(note_sequence.total_time)
if len(valid_split_times) > 1:
return _extract_subsequences(note_sequence, valid_split_times)
else:
return []
def split_note_sequence_on_time_changes(note_sequence,
skip_splits_inside_notes=False):
"""Split one NoteSequence into many around time signature and tempo changes.
This function splits a NoteSequence into multiple NoteSequences, each of which
contains only a single time signature and tempo, unless `split_notes` is False
in which case all time signature and tempo changes occur within sustained
notes. Each of the resulting NoteSequences is shifted to start at time zero.
Args:
note_sequence: The NoteSequence to split.
skip_splits_inside_notes: If False, the NoteSequence will be split at all
time changes, regardless of whether or not any notes are sustained across
the time change. If True, the NoteSequence will not be split at time
changes that occur within sustained notes.
Returns:
A Python list of NoteSequences.
"""
current_numerator = 4
current_denominator = 4
current_qpm = constants.DEFAULT_QUARTERS_PER_MINUTE
time_signatures_and_tempos = sorted(
list(note_sequence.time_signatures) + list(note_sequence.tempos),
key=lambda t: t.time)
time_signatures_and_tempos = [
t for t in time_signatures_and_tempos if t.time < note_sequence.total_time
]
notes_by_start_time = sorted(
list(note_sequence.notes), key=lambda note: note.start_time)
note_idx = 0
notes_crossing_split = []
valid_split_times = [0.0]
for time_change in time_signatures_and_tempos:
if isinstance(time_change, music_pb2.NoteSequence.TimeSignature):
if (time_change.numerator == current_numerator and
time_change.denominator == current_denominator):
# Time signature didn't actually change.
continue
else:
if time_change.qpm == current_qpm:
# Tempo didn't actually change.
continue
# Update notes crossing potential split.
while (note_idx < len(notes_by_start_time) and
notes_by_start_time[note_idx].start_time < time_change.time):
notes_crossing_split.append(notes_by_start_time[note_idx])
note_idx += 1
notes_crossing_split = [
note for note in notes_crossing_split
if note.end_time > time_change.time
]
if time_change.time > valid_split_times[-1]:
if not (skip_splits_inside_notes and notes_crossing_split):
valid_split_times.append(time_change.time)
# Even if we didn't split here, update the current time signature or tempo.
if isinstance(time_change, music_pb2.NoteSequence.TimeSignature):
current_numerator = time_change.numerator
current_denominator = time_change.denominator
else:
current_qpm = time_change.qpm
# Handle the final subsequence.
if note_sequence.total_time > valid_split_times[-1]:
valid_split_times.append(note_sequence.total_time)
if len(valid_split_times) > 1:
return _extract_subsequences(note_sequence, valid_split_times)
else:
return []
def quantize_to_step(unquantized_seconds,
steps_per_second,
quantize_cutoff=QUANTIZE_CUTOFF):
"""Quantizes seconds to the nearest step, given steps_per_second.
See the comments above `QUANTIZE_CUTOFF` for details on how the quantizing
algorithm works.
Args:
unquantized_seconds: Seconds to quantize.
steps_per_second: Quantizing resolution.
quantize_cutoff: Value to use for quantizing cutoff.
Returns:
The input value quantized to the nearest step.
"""
unquantized_steps = unquantized_seconds * steps_per_second
return int(unquantized_steps + (1 - quantize_cutoff))
def steps_per_quarter_to_steps_per_second(steps_per_quarter, qpm):
"""Calculates steps per second given steps_per_quarter and a qpm."""
return steps_per_quarter * qpm / 60.0
def _quantize_notes(note_sequence, steps_per_second):
"""Quantize the notes and chords of a NoteSequence proto in place.
Note start and end times, and chord times are snapped to a nearby quantized
step, and the resulting times are stored in a separate field (e.g.,
quantized_start_step). See the comments above `QUANTIZE_CUTOFF` for details on
how the quantizing algorithm works.
Args:
note_sequence: A music_pb2.NoteSequence protocol buffer. Will be modified in
place.
steps_per_second: Each second will be divided into this many quantized time
steps.
Raises:
NegativeTimeException: If a note or chord occurs at a negative time.
"""
for note in note_sequence.notes:
# Quantize the start and end times of the note.
note.quantized_start_step = quantize_to_step(note.start_time,
steps_per_second)
note.quantized_end_step = quantize_to_step(note.end_time, steps_per_second)
if note.quantized_end_step == note.quantized_start_step:
note.quantized_end_step += 1
# Do not allow notes to start or end in negative time.
if note.quantized_start_step < 0 or note.quantized_end_step < 0:
raise NegativeTimeException(
'Got negative note time: start_step = %s, end_step = %s' %
(note.quantized_start_step, note.quantized_end_step))
# Extend quantized sequence if necessary.
if note.quantized_end_step > note_sequence.total_quantized_steps:
note_sequence.total_quantized_steps = note.quantized_end_step
# Also quantize control changes and text annotations.
for event in itertools.chain(note_sequence.control_changes,
note_sequence.text_annotations):
# Quantize the event time, disallowing negative time.
event.quantized_step = quantize_to_step(event.time, steps_per_second)
if event.quantized_step < 0:
raise NegativeTimeException(
'Got negative event time: step = %s' % event.quantized_step)
def quantize_note_sequence(note_sequence, steps_per_quarter):
"""Quantize a NoteSequence proto relative to tempo.
The input NoteSequence is copied and quantization-related fields are
populated. Sets the `steps_per_quarter` field in the `quantization_info`
message in the NoteSequence.
Note start and end times, and chord times are snapped to a nearby quantized
step, and the resulting times are stored in a separate field (e.g.,
quantized_start_step). See the comments above `QUANTIZE_CUTOFF` for details on
how the quantizing algorithm works.
Args:
note_sequence: A music_pb2.NoteSequence protocol buffer.
steps_per_quarter: Each quarter note of music will be divided into this many
quantized time steps.
Returns:
A copy of the original NoteSequence, with quantized times added.
Raises:
MultipleTimeSignatureException: If there is a change in time signature
in `note_sequence`.
MultipleTempoException: If there is a change in tempo in `note_sequence`.
BadTimeSignatureException: If the time signature found in `note_sequence`
has a 0 numerator or a denominator which is not a power of 2.
NegativeTimeException: If a note or chord occurs at a negative time.
"""
qns = copy.deepcopy(note_sequence)
qns.quantization_info.steps_per_quarter = steps_per_quarter
if qns.time_signatures:
time_signatures = sorted(qns.time_signatures, key=lambda ts: ts.time)
# There is an implicit 4/4 time signature at 0 time. So if the first time
# signature is something other than 4/4 and it's at a time other than 0,
# that's an implicit time signature change.
if time_signatures[0].time != 0 and not (
time_signatures[0].numerator == 4 and
time_signatures[0].denominator == 4):
raise MultipleTimeSignatureException(
'NoteSequence has an implicit change from initial 4/4 time '
'signature to %d/%d at %.2f seconds.' %
(time_signatures[0].numerator, time_signatures[0].denominator,
time_signatures[0].time))
for time_signature in time_signatures[1:]:
if (time_signature.numerator != qns.time_signatures[0].numerator or
time_signature.denominator != qns.time_signatures[0].denominator):
raise MultipleTimeSignatureException(
'NoteSequence has at least one time signature change from %d/%d to '
'%d/%d at %.2f seconds.' %
(time_signatures[0].numerator, time_signatures[0].denominator,
time_signature.numerator, time_signature.denominator,
time_signature.time))
# Make it clear that there is only 1 time signature and it starts at the
# beginning.
qns.time_signatures[0].time = 0
del qns.time_signatures[1:]
else:
time_signature = qns.time_signatures.add()
time_signature.numerator = 4
time_signature.denominator = 4
time_signature.time = 0
if not _is_power_of_2(qns.time_signatures[0].denominator):
raise BadTimeSignatureException(
'Denominator is not a power of 2. Time signature: %d/%d' %
(qns.time_signatures[0].numerator, qns.time_signatures[0].denominator))
if qns.time_signatures[0].numerator == 0:
raise BadTimeSignatureException(
'Numerator is 0. Time signature: %d/%d' %
(qns.time_signatures[0].numerator, qns.time_signatures[0].denominator))
if qns.tempos:
tempos = sorted(qns.tempos, key=lambda t: t.time)
# There is an implicit 120.0 qpm tempo at 0 time. So if the first tempo is
# something other that 120.0 and it's at a time other than 0, that's an
# implicit tempo change.
if tempos[0].time != 0 and (tempos[0].qpm !=
constants.DEFAULT_QUARTERS_PER_MINUTE):
raise MultipleTempoException(
'NoteSequence has an implicit tempo change from initial %.1f qpm to '
'%.1f qpm at %.2f seconds.' % (constants.DEFAULT_QUARTERS_PER_MINUTE,
tempos[0].qpm, tempos[0].time))
for tempo in tempos[1:]:
if tempo.qpm != qns.tempos[0].qpm:
raise MultipleTempoException(
'NoteSequence has at least one tempo change from %.1f qpm to %.1f '
'qpm at %.2f seconds.' % (tempos[0].qpm, tempo.qpm, tempo.time))
# Make it clear that there is only 1 tempo and it starts at the beginning.
qns.tempos[0].time = 0
del qns.tempos[1:]
else:
tempo = qns.tempos.add()
tempo.qpm = constants.DEFAULT_QUARTERS_PER_MINUTE
tempo.time = 0
# Compute quantization steps per second.
steps_per_second = steps_per_quarter_to_steps_per_second(
steps_per_quarter, qns.tempos[0].qpm)
qns.total_quantized_steps = quantize_to_step(qns.total_time, steps_per_second)
_quantize_notes(qns, steps_per_second)
return qns
def quantize_note_sequence_absolute(note_sequence, steps_per_second):
"""Quantize a NoteSequence proto using absolute event times.
The input NoteSequence is copied and quantization-related fields are
populated. Sets the `steps_per_second` field in the `quantization_info`
message in the NoteSequence.