Why mido library gives two different values for 'time' attribute?

29 Views Asked by At

I have this two codes to extract information from a simple MIDI file, in the first the 'time' is so little: 0.3, 2.1, 0.9, etc. but in the second the 'time' is more big: 60, 420, 180, etc.

Can someone explain why there is this difference? What does it mean each 'time' in each code? I believe the second will be more sure to other processing, cause they are integers. Thanks in advance for your interesting comments.

import mido
from mido import MidiFile
import numpy as np

# Cargar el archivo MIDI
mid = MidiFile('clusters.mid')
mididict = []#crea una lista mididict para guardar los eventos midi del diccionario 
lista_notas = []#crea una lista output para guardar la salida final.

# Recorre los mensajes midi hasta encontrar 'set_tempo'
for msg in mid:
    if msg.type=='set_tempo':
        bpm=mido.tempo2bpm(msg.tempo)#guarda el tempo en bpm
        break #se sale si no lo encuentra

# Recorre el midi buscando los eventos 'note_on', 'note_off' y 'time_signature'
for i in mid:
    if i.type == 'note_on' or i.type == 'note_off' or i.type == 'time_signature':
        mididict.append(i.dict())#agrega los eventos encontrados a mididict 

# Recorre el midi para convertir los eventos 'note_on' en 'note_off' si 'velocity'=0
for i in mididict:
     if i['type'] == 'note_on' and i['velocity'] == 0:
        i['type'] = 'note_off'
        
print("Datos extraídos del MIDI:")
# Imprime el tempo y el número de pulsos por tiempo (ticks por beat)
print(f"tempo: {bpm}, ticks_per_beat: {mid.ticks_per_beat}")#clocks_per_click: {clocks_per_click},
h=0 #inicia un contador de elementos 
print("Lista Mididict:")
for i in mididict:
    print(f"Evento: {h}, {i}") #, número de evento y atributos del evento 
    h=h+1 # incrementa el contador



Datos extraídos del MIDI:
tempo: 100.0, ticks_per_beat: 120
Lista Mididict:
Evento: 0, {'type': 'time_signature', 'numerator': 4, 'denominator': 4, 'clocks_per_click': 24, 'notated_32nd_notes_per_beat': 8, 'time': 0}
Evento: 1, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 60, 'velocity': 100}
Evento: 2, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 60, 'velocity': 0}
Evento: 3, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 62, 'velocity': 100}
Evento: 4, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 62, 'velocity': 0}
Evento: 5, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 60, 'velocity': 100}
Evento: 6, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 60, 'velocity': 0}
Evento: 7, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 64, 'velocity': 100}
Evento: 8, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 64, 'velocity': 0}
Evento: 9, {'type': 'note_on', 'time': 0.3, 'channel': 0, 'note': 60, 'velocity': 100}
Evento: 10, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 60, 'velocity': 0}                                  Evento: 11, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 64, 'velocity': 100}                                   Evento: 12, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 64, 'velocity': 0}                                  Evento: 13, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 67, 'velocity': 100}                                   Evento: 14, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 67, 'velocity': 0}                                  Evento: 15, {'type': 'note_on', 'time': 2.1, 'channel': 0, 'note': 70, 'velocity': 100}                                 Evento: 16, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 70, 'velocity': 0}                                  Evento: 17, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 72, 'velocity': 100}                                   Evento: 18, {'type': 'note_off', 'time': 0.9, 'channel': 0, 'note': 72, 'velocity': 0}                                  Evento: 19, {'type': 'note_on', 'time': 0.3, 'channel': 0, 'note': 74, 'velocity': 100}                                 Evento: 20, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 74, 'velocity': 0}                                  Evento: 21, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 76, 'velocity': 100}
Evento: 22, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 76, 'velocity': 0}
Evento: 23, {'type': 'note_on', 'time': 0, 'channel': 0, 'note': 72, 'velocity': 100}
Evento: 24, {'type': 'note_off', 'time': 0.3, 'channel': 0, 'note': 72, 'velocity': 0}

from mido import MidiFile
mid = MidiFile('Clusters.mid')

print("Informacion de los tracks")
for i, track in enumerate(mid.tracks):
    print('Track {}: {}'.format(i, track.name))
    
i=0 
print("\nInformacion de los eventos")
for msg in track:
    print(i, msg)
    i=i+1



Informacion de los eventos
0 MetaMessage('midi_port', port=0, time=0)
1 MetaMessage('track_name', name='', time=0)
2 note_on channel=0 note=60 velocity=100 time=0
3 note_on channel=0 note=60 velocity=0 time=60
4 note_on channel=0 note=62 velocity=100 time=0
5 note_on channel=0 note=62 velocity=0 time=60
6 note_on channel=0 note=60 velocity=100 time=0
7 note_on channel=0 note=60 velocity=0 time=60
8 note_on channel=0 note=64 velocity=100 time=0
9 note_on channel=0 note=64 velocity=0 time=60
10 note_on channel=0 note=60 velocity=100 time=60
11 note_on channel=0 note=60 velocity=0 time=60
12 note_on channel=0 note=64 velocity=100 time=0
13 note_on channel=0 note=64 velocity=0 time=60
14 note_on channel=0 note=67 velocity=100 time=0
15 note_on channel=0 note=67 velocity=0 time=60
16 note_on channel=0 note=70 velocity=100 time=420
17 note_on channel=0 note=70 velocity=0 time=60
18 note_on channel=0 note=72 velocity=100 time=0
19 note_on channel=0 note=72 velocity=0 time=180
20 note_on channel=0 note=74 velocity=100 time=60
21 note_on channel=0 note=74 velocity=0 time=60
22 note_on channel=0 note=76 velocity=100 time=0
23 note_on channel=0 note=76 velocity=0 time=60
24 note_on channel=0 note=72 velocity=100 time=0
25 note_on channel=0 note=72 velocity=0 time=60
26 MetaMessage('end_of_track', time=0)

[Program finished]
0

There are 0 best solutions below