bixb922 / umidiparser

MIDI file parser for Micropython, CircuitPython and Python
MIT License
27 stars 4 forks source link

AttributeError: Midi event 0x90 does not support attribute #4

Closed SunboX closed 1 year ago

SunboX commented 1 year ago

I wanted to get the real Note from the NOTE_ON event. Not the Midi note (62) but the real Note (D4).

code:

import os
import busio
import digitalio
import board
import storage
import adafruit_sdcard
import umidiparser
import time

spi = busio.SPI(clock=board.GP10, MOSI=board.GP11, MISO=board.GP12)
cs = digitalio.DigitalInOut(board.GP1)

sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")

with open("/sd/test.txt", "w") as f:

    print("Printing lines in file:")

    for event in umidiparser.MidiFile("/sd/agree_to_stay.mid").play():
        # .play will sleep, avoiding time drift, before returning the event on time
        # Process the event according to type
        if event.status == umidiparser.NOTE_ON:
            #... start the note with midi number event.note 
            #on channel event.channel with event.velocity
            f.write("NOTE_ON %s - key: %s\r\n" % (event, event.key))
        elif event.status == umidiparser.NOTE_OFF:
            #... stop the note event.note .
            f.write("NOTE_OFF %s - key: %s\r\n" % (event, event.key))
        elif event.status == umidiparser.PROGRAM_CHANGE:
            #... change midi program to event.program on event.channel ...
            f.write("PROGRAM_CHANGE %s\r\n" % event)
        else:
            # Show all events not processed
            f.write("OTHER_EVENT %s\r\n" % event)

print("done.")

event:

NOTE_ON note_on delta[miditicks]=1800 data=b'>P' delta[usec]=1875000 velocity=80 status=144 channel=0 note=62
NOTE_ON note_on delta[miditicks]=0 data=b'AP' delta[usec]=0 velocity=80 status=144 channel=0 note=65
NOTE_ON note_on delta[miditicks]=455 data=b'>\x00' delta[usec]=473958 velocity=0 status=144 channel=0 note=62
NOTE_ON note_on delta[miditicks]=0 data=b'A\x00' delta[usec]=0 velocity=0 status=144 channel=0 note=65
bixb922 commented 1 year ago

Thanks for the post. I added a new file gmfunctions.py with functions I use to interpret numbers that appear in midi events. Please take a look at the note_name_english, note_name_german and note_name_doremi functions, (edited). Since the file is rather large, I am a bit reluctant to include all that in the basic package, that would increase RAM usage in another 7kbytes. Please extract and change what you need. The assignment of accidentals (b or #) is quite arbitrary, you might need to change that. Does this help? Sorry, I could not comment without closing the issue, don't know how to do that in github.

bixb922 commented 1 year ago

I'll reopen issue if needed.