Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5216

Other projects • Re: Modifying a music keyboard for my grandaughter to learn how to play.

$
0
0
If your keyboard supports midi and has a usb interface you can plug it into your pi and use it directly with a synthesizer.
Install a synth like yoshimi and it should recognise it directly.

I tried sonic pi and supercollider, then fluidsynth before yoshimi. For me, yoshimi works better because it is "simpler" and has a virtual keyboard, too. Although if you can dedicate a whole pi to sonic-pi that can be extremely educational.

Once you have your keyboard attached to a synth, you can "demonstrate" all sorts of stuff.

I have been working on using network between pi's (python-osc and rtmidi). Also a wind-chime simulator using markov chains) - keeps the senility at bay....

Code:

## derived from : https://www.gaussianwaves.com/2022/03/implementing-markov-chain-in-python/# among other things!# https://www.inspiredacoustics.com/en/MIDI_note_numbers_and_center_frequencies# https://github.com/SpotlightKid/python-rtmidi## RichR Jan 1024#import randomimport loggingimport sysfrom time import sleepfrom rtmidi.midiutil import open_midioutputfrom rtmidi.midiconstants import NOTE_OFF, NOTE_ON, CONTROL_CHANGE, PROGRAM_CHANGEfrom rtmidi.midiconstants import BANK_SELECT_MSB, BANK_SELECT_LSB# port = sys.argv[1] if len(sys.argv) > 1 else 1try:    midiout, port_name = open_midioutput(1)    print(port_name)except (EOFError, KeyboardInterrupt):    sys.exit()midiout.send_message([CONTROL_CHANGE, BANK_SELECT_MSB, 0]) # why 1 not zero? does it care?#    midiout.send_message([CONTROL_CHANGE, BANK_SELECT_LSB, 100]) # selects yoshimi bankmidiout.send_message([CONTROL_CHANGE, BANK_SELECT_LSB, 50]) # selects yoshimi bank#    midiout.send_message([PROGRAM_CHANGE +2, 0x04]) # changes yoshimi channel 3 to instrument 5midiout.send_message([PROGRAM_CHANGE +2, 96]) # changes yoshimi channel 3 to instrument 97        # Define a transition matrix for the Markov chaintransition_matrix = {    'c4': {'c4': 0.2,  'd4': 0.1 , 'e4': 0.3 , 'g4': 0.3  , 'a4': 0.1 },  # each line adds to 1.0    'd4': {'c4': 0.1,  'd4': 0.2 , 'e4': 0.1 , 'g4': 0.3  , 'a4': 0.3 },    'e4': {'c4': 0.3,  'd4': 0.1 , 'e4': 0.2 , 'g4': 0.1  , 'a4': 0.3 },    'g4': {'c4': 0.3,  'd4': 0.3 , 'e4': 0.1 , 'g4': 0.2  , 'a4': 0.1 },    'a4': {'c4': 0.1,  'd4': 0.3 , 'e4': 0.3 , 'g4': 0.1  , 'a4': 0.2 },}# Define starting probabilities for each statestarting_probabilities = {'c4': 0.2, 'd4': 0.2, 'e4': 0.2, 'g4': 0.2, 'a4': 0.2 } # each line adds to 1.0# Choose the starting state randomly based on the starting probabilitiescurrent = random.choices(    population=list(starting_probabilities.keys()),    weights=list(starting_probabilities.values()))[0]# Generate a sequence of states using the transition matrixiterations = 20count = [0,0,0,0,0]for i in range(iterations):#    print(current)    next = random.choices(        population=list(transition_matrix[current].keys()),        weights=list(transition_matrix[current].values())    )[0]    current = next        match current:        case 'c4':            count[0] = count[0] +1            midiout.send_message([NOTE_ON+2, 60, 112])  # channel 3, middle C, velocity 112        case 'd4':            count[1] = count[1] +1            midiout.send_message([NOTE_ON+2, 62, 112])        case 'e4':            count[2] = count[2] +1            midiout.send_message([NOTE_ON+2, 64, 112])        case 'g4':            count[3] = count[3] +1            midiout.send_message([NOTE_ON+2, 67, 112])        case 'a4':            count[4] = count[4] +1            midiout.send_message([NOTE_ON+2, 69, 112])        case _:            print("error in current")    sleep(0.15)print(count)    

Statistics: Posted by richrarobi — Wed Jan 24, 2024 4:07 pm



Viewing all articles
Browse latest Browse all 5216

Trending Articles