0

Have a code that reads what note pressed on the midi:

import pygame.midi

def number_to_note(number):
    notes = ['c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'b']
    return notes[number%12]

def readInput(input_device):
    while True:
        if input_device.poll():
            event = input_device.read(1)[0]
            data = event[0]
            timestamp = event[1]
            note_number = data[1]
            velocity = data[2]
            print(number_to_note(note_number))
            note = number_to_note(note_number)

if __name__ == '__main__':
    pygame.midi.init()
    my_input = pygame.midi.Input(1)
    readInput(my_input)

It says the note, but how do I get in what octave the note is? For example: Not just A#, but A4#

  • 1
    Welcome to Stack Overflow. Hint: where the code says `return notes[number%12]`, how did you decide to use that? In particular, what is the significance of the `12` value? Do you know what the note number is for A4#? How about for A5#? How far apart are they? Do you see a pattern? If you were able to think of a mathematical rule to give you an index for the note name, I don't see the problem with coming up with one for the octave number - it is the same kind of math, and the same kind of logical reasoning. – Karl Knechtel Sep 24 '22 at 21:52

1 Answers1

0

You just need to do some arithmetic with your note:

note = number%12
octave = (number - note) / 12
return str(octave) + notes[note]
Christian
  • 1,341
  • 1
  • 16
  • 35