Search code examples
pythonmusic21

Music21, window analysis get time?


I tried the WindowAnalysis from music21. However I was wondering how i can convert the index of the window, into the time (in seconds or so) of the song. So for example how could I know at which time the first window is played? e.g. 2.5 seconds into the song

s = music21.converter.parse('../data/medley/15046.midi')

p = music21.analysis.discrete.KrumhanslSchmuckler()
wa = music21.analysis.windowed.WindowedAnalysis(s, p)
windo_count=4
a, b = wa.analyze(windo_count)
song = pm.PrettyMIDI('../data/medley/15046.midi')
cello_program = pm.instrument_name_to_program('Cello')
cello = pm.Instrument(program=cello_program)

prev=a[0]
change_loc=[]
#some code that detects change and should append now the time in seconds
for i,beat in enumerate(a[1:]):
    #i is the tuple (key,major/minor/etc,time)
    if beat[0]!=prev[0] and beat[1]!=prev[1]:
        #key changed, at index
        change_loc.append(my_question)

Solution

  • using pretty_midi one can obtain a mapping from beat to time e.g.

    import pretty_midi as pm
    song = pm.PrettyMIDI('data/medley/15046.midi')
    print(song.get_beats())
    

    EDIT: More information can be found here: How to convert bar count to time, in midi? (music)