<jupyter_start><jupyter_text>Rhythm regularity and self similarity  <NAME>, University of Victoria In this notebook we will look into how regularity and self-similarity can be used to detect different levels of rhythm periodicities. We will use Music21 and tinynotation to create some simple example illustrating these concepts.<jupyter_code>import music21 as m21 
from music21 import *
stream1 = m21.converter.parse("""tinynotation: 4/4 f4 f8 f8 f4 r8 f8 f4 f8 f8 f4 r8 f8 
                              f4 f8 f8 f4 r8 f8 f4 f8 f8 f4 r8 f8""")
stream1.show() 
sp = midi.realtime.StreamPlayer(stream1)
sp.play()<jupyter_output><empty_output><jupyter_text>Even though there are notes of different durations and rests many of the note onsets coincide with the onsets of a regular sequence of notes. For example lets hear a sequence of quarter notes - listen to how many of the onsets coincide. We can leverage this regularity to design algorithm for tempo estimation and beat tracking.<jupyter_code>stream2 = m21.converter.parse("tinynotation: 4/4 c4 c c c c c c c c c c c c c c c")
s = stream.Score(id='mainScore')
s.insert(stream1)
s.insert(stream2)
s.show()
sp = midi.realtime.StreamPlayer(s)
sp.play()<jupyter_output><empty_output><jupyter_text>Using a regular sequence of eighth notes also show regularity in relation to our rhythm.<jupyter_code>stream2 = m21.converter.parse("tinynotation: 4/4 c8 c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c")
s = stream.Score(id='mainScore')
s.insert(stream1)
s.insert(stream2)
s.show()
sp = midi.realtime.StreamPlayer(s)
sp.play()<jupyter_output><empty_output><jupyter_text>Finally we can observer that there is a pattern (1 quarter note, 2 eighth notes, 1 quarter note, 1 eighth note rest, 1 eight note). By using a periodicity detection approach based on self similarity such as autocorrelation we would be able to detect this unit as a repeating pattern and the detected periodicity could inform tempo estimation and beat tracking algorithms.<jupyter_code>stream2 = m21.converter.parse("tinynotation: 4/4 c1 c c c")
s = stream.Score(id='mainScore')
s.insert(stream1)
s.insert(stream2)
s.show()
sp = midi.realtime.StreamPlayer(s)
sp.play()<jupyter_output><empty_output><jupyter_text>Notice that self similarity could give us the same periodicity but at a different "phase" for example starting at the second beat of the first measure.<jupyter_code>stream2 = m21.converter.parse("tinynotation: 4/4 r4 c1 c c c")
s = stream.Score(id='mainScore')
s.insert(stream1)
s.insert(stream2)
s.show()
sp = midi.realtime.StreamPlayer(s)
sp.play()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08428680396643784, 'fraction_numerical': 0.02936689549961861, 'mean_word_length': 4.947845804988662, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '602750', 'license': 'CC0-1.0', 'n_tokens_mistral': 887, 'n_tokens_neox': 842, 'n_words': 333, 'path': 'course1/session5/kadenze_mir_c1_s5_3_rhythm_regularity_selfsimilarity.ipynb', 'repo_name': 'Achilleasein/mir_program_kadenze', 'size': '2633'}