0

I have a code that represent numbers as notes, C1 is equal to 1 C_sharp1 is equal to 2 and so forth. Is there a way to take those numbers convert them into a midi output and send them live to a daw software like Ableton to play them?

this is an example from the output of the code:

[1, 3, 5, 6, 8, 10, 11]
rhythm: 1.3333333333333333
note: 8

[1, 3, 5, 6, 8, 10, 11]
coucalating_new_note
rhythm: 1.3333333333333333
note: 9

currently the program take the note_n variable, play it with this portion of the code and print the number of it:

if note_n == 1:
        winsound.PlaySound("1", winsound.SND_ASYNC)

elif note_n == 2:
        winsound.PlaySound("2", winsound.SND_ASYNC)

elif note_n == 3:
        winsound.PlaySound("3", winsound.SND_ASYNC)

I want to take those numbers and turn them in to midi signal that will feed the DAW and play them through it.

  • Which programming language? – fdcpp Jan 27 '22 at 21:23
  • In Python language – Nadav Cohen Jan 29 '22 at 11:25
  • There are two sides to this, there is the routing of midi data internally in your system and there is formatting your data to midi notes. The routing wouldn’t be done by python, I’d look at existing software solutions as it isn’t a trivial problem and it is OS dependant on what you’ll need to do. For formatting the data, it would save any heavy lifting if you could share a relevant, self-contained portion of your code. – fdcpp Jan 29 '22 at 16:12
  • If you haven’t already, https://stackoverflow.com/help/how-to-ask has some great tips on getting the most out of Stack Overflow – fdcpp Jan 29 '22 at 16:14
  • MIDI note numbers go from 0 - 127 in semitones, so you’ve basically got the same system. Try experimenting with something like mido https://github.com/mido/mido – fdcpp Jan 30 '22 at 08:26
  • Use something like loopMIDI for routing https://www.tobias-erichsen.de/software/loopmidi.html – fdcpp Jan 30 '22 at 08:28

1 Answers1

0

First, you'll need to install the mido Python library if you haven't already:

pip install mido

Now, here's how to generate MIDI signals and send them to Ableton Live:

import mido
import time

# Create a MIDI output port
output_port = mido.open_output('Your_MIDI_Output_Port_Name')

# Define a dictionary to map note numbers to MIDI note numbers
note_to_midi = {
    1: 24,  # C1
    2: 25,  # C#1
    # ... add more mappings for other notes
}

# Your example output
note_sequence = [1, 3, 5, 6, 8, 10, 11]
rhythm = 1.3333333333333333

# Function to play a note using MIDI
def play_midi_note(note_number):
    midi_note = note_to_midi.get(note_number)
    if midi_note is not None:
        msg = mido.Message('note_on', note=midi_note, velocity=64)  # You can adjust velocity
        output_port.send(msg)

# Loop through the note sequence
for note_n in note_sequence:
    print("Playing note:", note_n)
    play_midi_note(note_n)
    time.sleep(rhythm)  # Delay based on your rhythm

# Close the MIDI output port when done
output_port.close()

If you're on Windows you can use LoopMIDI to create a virtual MIDI port, which can be incredibly useful for routing MIDI data between applications, such as your code and Ableton Live.

Kosro
  • 1
  • 3