Programming with MIDI in Python | Getting started and sending MIDI Messages

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ส.ค. 2024
  • In this video, we're going to explore how MIDI works and write some simple programmes in Python to control a MIDI device. Please feel free to ask me questions in the comments or join our Discord server ( / discord . If you'd like to follow along, you can find links to some resources below.
    David van Brinks MIDI guide: www.cs.cmu.edu...
    Anaconda Python distribution: www.anaconda.c...
    If you'd like to support the channel:
    - Become a Patron: / mochreach
    - Tip me: ko-fi.com/moch...
    X/Twitter: / mochreachmusict

ความคิดเห็น • 94

  • @carlnicholson5008
    @carlnicholson5008 หลายเดือนก่อน +2

    Thanks for getting me started. I can now control my Novation Peak from a Raspberry Pi!!

    • @mochreach
      @mochreach  หลายเดือนก่อน

      That's really cool! Glad the video was useful. 😊

  • @graham287
    @graham287 ปีที่แล้ว +1

    Don't know who these people are that find this simple and clear. Up to 12 minutes in, the info was nice and easy and answered a load of questions I had about midi messages, After that you lost me, I kept askin Why? What's one of those and what does it do? But having said that, you have taken me to the next level. Now I need to know how to trigger messages using input devices

    • @mochreach
      @mochreach  ปีที่แล้ว

      😅 Sorry about that, feel free to ask me if you have specific questions . I made a follow up video about listening for MIDI messages from an external device, that might answer some of your questions: th-cam.com/video/zpZDwqsgSpc/w-d-xo.html

  • @MrOpaDave
    @MrOpaDave ปีที่แล้ว +4

    That was very simple and clear and helped me a lot. Thank you!

    • @mochreach
      @mochreach  ปีที่แล้ว

      You're welcome!

  • @jamesarthurkimbell
    @jamesarthurkimbell ปีที่แล้ว +2

    This is really cool, man. I'm glad it came up in my recommendations

    • @mochreach
      @mochreach  ปีที่แล้ว +1

      Brilliant, I'm glad you found it useful! Let me know if you have any questions about it.

  • @lovelivinglife904
    @lovelivinglife904 ปีที่แล้ว +1

    Really appreciated the video. Good job

    • @mochreach
      @mochreach  ปีที่แล้ว +1

      Thanks very much, glad you found it useful!

  • @afrigal2420
    @afrigal2420 2 หลายเดือนก่อน

    love it!!

  • @exceptioncodeinfinity4013
    @exceptioncodeinfinity4013 ปีที่แล้ว

    awesome info thanks, and you have a great delivery style that is easy to follow :)

  • @jithendra.k.sfirst_yr_b.sc9574
    @jithendra.k.sfirst_yr_b.sc9574 6 หลายเดือนก่อน

    This content is seriously useful....❤❤👌👌

    • @mochreach
      @mochreach  6 หลายเดือนก่อน

      Glad to hear that, thanks!

  • @goldcoastj
    @goldcoastj ปีที่แล้ว

    A great explanation. Thanks!

  • @summerjee17
    @summerjee17 6 หลายเดือนก่อน

    Great tutorial!

    • @mochreach
      @mochreach  6 หลายเดือนก่อน

      Thanks!

  • @artyschopy
    @artyschopy 10 หลายเดือนก่อน

    Thanks, man. Great vid.

    • @mochreach
      @mochreach  10 หลายเดือนก่อน

      Glad you liked it!

  • @Brick_Briceno
    @Brick_Briceno ปีที่แล้ว

    Cool video! ✨👌🏻

  • @bobjuice123
    @bobjuice123 8 หลายเดือนก่อน

    Great stuff, very helpful. Can you please let me know which USB-midi interfaces work best with python? Roland um one ? Python doesn't see my motu m4 , I'm told that's because it thinks it's an audio interface. Thanks

    • @mochreach
      @mochreach  8 หลายเดือนก่อน

      I've only tried a Behringer one that I have, it's a pretty basic one, works fine.

  • @supernonkey
    @supernonkey 8 หลายเดือนก่อน

    Bit of a newbie here, who managed to stumble upon this video. Is it possible to use python running on a raspberry pi 4, in order to route midi messages from a midi-over-usb device to a groove box? I have an akai mpd218 and would like to try to write a script which would allow me to use the velocity sensitive pads, to control a Roland MC101. I think in this instance the Raspberry Pi would be a kind of a host/hub, or an interface -- I am not clued into the lingo unfortunately :/

    • @mochreach
      @mochreach  8 หลายเดือนก่อน

      Yes, that's definitely possible, you can get rtmidi running on Arm. Lots of Raspberry Pi Linux distributions already have python installed, so you should be able to get up and running quite quickly.

  • @JVmaster101
    @JVmaster101 9 หลายเดือนก่อน

    I'm having a bit of trouble getting this to work on virtual devices, rtmidi shows no available ports. Not sure if I'm misunderstanding this, but with something like FluidSynth installed, shouldn't I be able to send messages to FluidSynth and have it play through my computer audio? Is there a different process to set this up? Thanks!

    • @mochreach
      @mochreach  9 หลายเดือนก่อน

      You might need a bit of software to make "virtual MIDI" ports on your system. There are lots of options for software that does this, but the exact one will depend on your operating system. Let me know if that helps.

    • @JVmaster101
      @JVmaster101 9 หลายเดือนก่อน

      @@mochreach thanks for your response. I eventually got FluidSynth to pop up as an available port, unfortunately I don't remember what I did exactly, but the problem was with FS and its connection to my project rather than with rtmidi. Works great now, your video was very helpful to get my project up and running, thanks!

  • @sedgi404wizard
    @sedgi404wizard 8 หลายเดือนก่อน

    boss mate thanks

  • @RobbyKilgore
    @RobbyKilgore ปีที่แล้ว

    Great vid. Very helpful and clear.
    Im working on a new version of my midi rotator as a pi zero hardware version. I’m having trouble changing the midi in or out ports from within my software at runtime. What’s the proper way to reassign an in or out? Do I have to del the port and reassign from scratch? Or is there a simpler way?

    • @mochreach
      @mochreach  ปีที่แล้ว +1

      Sounds like a cool project! Yes, as far as I'm aware, the best way is to `del` the port or use the `with` context manager and it'll happen automatically. Edit: also, out of curiosity, what does your midi rotator do?

  • @xavierchenot2836
    @xavierchenot2836 ปีที่แล้ว

    HI, thank you for your great video !
    I have a Lk S450 piano, do you know how can I turn on the light with a midi command ?

    • @mochreach
      @mochreach  ปีที่แล้ว

      You're welcome! I took a look at the manual and looks like it's possible. If you go into the settings, there's an option called "MIDI In Navigate" that controls this. If you turn it on, you should be able to send MIDI notes to channel 3 and 4 to control the lighting. I don't have that keyboard to test this, but if definitely looks like you can.

  • @SHLDMusic
    @SHLDMusic ปีที่แล้ว

    Thanks for your video! Which Python Midi lib is the best? (easy, well-maintained, etc)

    • @mochreach
      @mochreach  ปีที่แล้ว +1

      You're welcome! If you don't need low level control, then I'd go for Mido. It's much easier to use for most routine tasks.

    • @SHLDMusic
      @SHLDMusic ปีที่แล้ว

      @@mochreach Thanks! Now I have to find a way to edit and run Python scripts from a web browser, and I will have a nice custom MIDI router 😊

  • @KandMe1
    @KandMe1 ปีที่แล้ว

    My question is about a script with some sort of global variable that establishes the channels used by the midi devices used for any one project or song. I don't know much about midi but how to set it up is on my mind.

    • @mochreach
      @mochreach  ปีที่แล้ว

      That sounds totally feasible. Have you tried it yet? You could have a python class that manages the connections.

    • @KandMe1
      @KandMe1 ปีที่แล้ว

      @@mochreach I am very keen to try it. Just pulled an old pi zero out and found it gets power from a powered usb hub which I have daisy chained 2 of them together because I get 7 connections that way and this zero has 2 mini usb inputs on it, so things are looking good so far just got to digging this stuff out of my shelves. Had problems finding an app to do it properly on my ipad so very keen to get something working right now. Ok I will start by looking into a python class to start with. Had only a very little experience with code but I did write a factory class at one time in PHP that worked very well just took a few weeks to work out how to do it from the onlne manual.

  • @stephen285
    @stephen285 ปีที่แล้ว

    can you clarify the with? how often should we open and close the port in our app, if we have a 1 -4 chord progression do we create a function for each chord that includes open and close for the port? or how best to proceed?

    • @mochreach
      @mochreach  ปีที่แล้ว

      Hi @Stephen, you an keep the port open the whole time you're using it, no need to close and open it for each chord. For the actual application, it's open it once at the beginning and use it until you're done.

  • @geetapuri4973
    @geetapuri4973 3 หลายเดือนก่อน

    what if i dont have a midi device connected to my Mac? Can i still program the same way?

    • @mochreach
      @mochreach  3 หลายเดือนก่อน

      Yes, you can use MIDI loopback to control a DAW, for example.

  • @JahidulislamRuman
    @JahidulislamRuman 2 หลายเดือนก่อน

    I want to make a new musical sound piano roll. How do I start?

    • @mochreach
      @mochreach  2 หลายเดือนก่อน

      Well, if you know Python, I'd recommend looking into a library called Mido for handling midi events and a GUI library (I'd recommend PyQT).

  • @RafaelCosta-ly5jb
    @RafaelCosta-ly5jb 10 หลายเดือนก่อน

    Hi, awesome video! Do you know any way to play the notes without the MIDI controller?
    I need to convert frequencies and durations into playable notes on piano or a violin, for example. But all i can do now is work with that 16 bit weird sound rsrsr
    Thank you

    • @mochreach
      @mochreach  10 หลายเดือนก่อน

      Hi @RafaelCosta-ly5jb you'll need some device that accepts MIDI, which could be a hardwae synth, or software like a DAW or VST plugin. There are lots of free VSTs out there with sample based piano and violins.

  • @impalagasper
    @impalagasper ปีที่แล้ว

    Hi, thanks for the video! Is there a way to put a function containing "with" statement inside a while loop? I can't seem to get it working (it stays in the loop and waits, but executes the function only once). If you find the time to answer, thanks!

    • @mochreach
      @mochreach  ปีที่แล้ว +1

      Thanks for watching! It should work, could you post your code please? If you have a GitHub account, you could make a Gist (gist.github.com) and share a link.

    • @impalagasper
      @impalagasper ปีที่แล้ว

      ​@@mochreach YT is blocking my link, so I sent you the link directly to your email. Hope you can help me and thanks a lot!

  • @Rocketos
    @Rocketos ปีที่แล้ว

    I dont have an opz can I use a Akai mpk249?

    • @mochreach
      @mochreach  ปีที่แล้ว

      This works with any midi capable device, although the messages if can receive will be different, so you'll need to check the manual. Feel free to message if you need help.

    • @Rocketos
      @Rocketos ปีที่แล้ว

      @@mochreach But i dont get it ): I am supposed to send this messages to Midi In of my controller, how this midi In messages will be sent through the midi out in order (for example) FL studio will recieve them and make a vst sound?

    • @mochreach
      @mochreach  ปีที่แล้ว

      You should be able to send to MIDI in of any device connected to your computer, then mpc or maybe even FL Studio directly. If you run this, what's the output? :
      import rtmidi
      midiout = rtmidi.MidiOut()
      print(midiout.get_ports())

  • @rolfjohansen5376
    @rolfjohansen5376 ปีที่แล้ว

    thanks for the vid: I am sitting and playing with sending CC messages, NOTE_OFF and SOUND_OFF , I have the impression you can "reset" midi-controllers that have gone into bad mode, do you know anything about this ?

    • @mochreach
      @mochreach  ปีที่แล้ว +1

      There is a system reset message, you can add it using a Command controller. What it does depends on the midi device you're connecting to, so check your manual before using it!

    • @rolfjohansen5376
      @rolfjohansen5376 ปีที่แล้ว

      @@mochreach of course, there is a sort of reset message in the beginning of a midi-file (GM, GS reset in Sysex) , but the player has to be in their respective mode to intercept these ...

    • @mochreach
      @mochreach  ปีที่แล้ว

      Ah okay. What device are you connecting it to? Edit: sorry I just noticed this was on a different video than I thought, that's why I was talking about commands!

  • @qwe-de7xd
    @qwe-de7xd 10 หลายเดือนก่อน

    Hi, I keep getting "No module named 'rtmidi'" when trying to import rtmidi in ipython. I already have rtmidi installed but still get this message, thank you 😁

    • @qwe-de7xd
      @qwe-de7xd 10 หลายเดือนก่อน

      I have python 3.10.6 because other things I'm doing don't work with later versions, Will I have to update python?

    • @mochreach
      @mochreach  10 หลายเดือนก่อน

      No that should be fine. I think the most likely explanation is that pip is installing it to another Python install. If you're on Linux or Mac, you could run `which python` and `which pip` just to make sure they're related to the same Python install. Either that or there was an error on install, in which case, there should be an error message in the log.

  • @MoacyrPradoNeto
    @MoacyrPradoNeto 11 หลายเดือนก่อน

    Thank you for lesson! Do you know how to code for ble-midi?

    • @mochreach
      @mochreach  11 หลายเดือนก่อน

      The midi part would be the same, and after the bluetooth connection is made to the computer, it might just appear like any other midi device, although I've never tested it. I do have a couple of ble devices, so I might give it a go.

  • @octaviop.4870
    @octaviop.4870 11 หลายเดือนก่อน

    Hello. this seems very interesting.
    May i ask you a few questiosn?
    Would python be a good choice to develope a precisely timed multi track midi sequencer?
    I currently have a working prototype that runs on ruby (sonic pi) but the timing is inconsistent.
    Would python be more precise?

    • @mochreach
      @mochreach  11 หลายเดือนก่อน +1

      Hi @octaviop.4870, Python might be a bit better than Ruby, and you could probably make it work, but neither are ideal I'm afraid. You'd be much better off using a fast compiled language like C, C++, Zig or Rust. You'll have much more performance headroom to make sure the timing is tight.

    • @octaviop.4870
      @octaviop.4870 11 หลายเดือนก่อน

      @@mochreachyes...im aware of compiled languages but im not really a programmer. I dont want to spend years to learn c or rust.
      But thank you :)

    • @mochreach
      @mochreach  11 หลายเดือนก่อน

      @octaviop.4870 Well, an alternative might be a game engine like unity or my personal favourite, Godot. Godot's scripting language has a python like syntax and even has built in midi support. Game engines are made for this type of real time application.

    • @octaviop.4870
      @octaviop.4870 11 หลายเดือนก่อน

      @@mochreach but wont they requiere a lot of resources to run?

    • @mochreach
      @mochreach  11 หลายเดือนก่อน +1

      No, they're optimised to be as efficient as possible for real time applications, so they'll be ideal.

  • @stephen285
    @stephen285 ปีที่แล้ว

    Thanks for your video! any chance you could do a basic rtmidi in howto video? absolutely nothing fancy just a basic python script that sits and listens for input from a keyboard and prints the data to the console?

    • @mochreach
      @mochreach  ปีที่แล้ว

      Hi @Stephen, yes I definitely could. I'll see if I have time later this week.

    • @stephen285
      @stephen285 ปีที่แล้ว

      @@mochreach very much appreciated! Thanks Mo

    • @mochreach
      @mochreach  ปีที่แล้ว

      Hi Stephen, I've just release a video on this topic, thanks for the suggestion. Let me know if you have any questions.

  • @leolmx
    @leolmx ปีที่แล้ว

    Hey man, why am I getting this error? "AttributeError: module 'rtmidi' has no attribute 'MidiOut'"

    • @mochreach
      @mochreach  ปีที่แล้ว

      @Leo Rickli that's very weird, not sure why that's happening. When you installed with pip, were there any errors?

    • @leolmx
      @leolmx ปีที่แล้ว

      ​@@mochreach Yeah, very weird. What package do you use, "rtmidi" or "python-rtmidi"? Because I can't even install "python-rtmidi", it says that it encountered an error while installing the package. "rtmidi" was fine though.

    • @mochreach
      @mochreach  ปีที่แล้ว

      I install with `pip install python-rtmidi`, that must be the problem. What was the error when you tried to install?

  • @TheCautionwetpaint
    @TheCautionwetpaint 5 หลายเดือนก่อน

    Can you write a program that compares the current midi input with a stored midi song from a database?

    • @mochreach
      @mochreach  5 หลายเดือนก่อน +1

      Yes, that's definitely possible. Using the "mido" library would be the best way to do that, it has lots of functionality for working with midi files.

    • @TheCautionwetpaint
      @TheCautionwetpaint 5 หลายเดือนก่อน

      @@mochreach thanks... Follow up question though, is it possible to write a program that distinguishes song sections from the drum beat and playing sequence... Like if I want to automatically display lyrics on the screen for each song section when the section is actually being played in the input... I understand that the current pattern being played should compare from a stored song pattern and sequence... Or maybe is there a program or device that does that already?

    • @mochreach
      @mochreach  5 หลายเดือนก่อน +1

      I think the easiest way would be to have a record of the best number the lyrics should be displayed on, maybe a file with the best number and the lyric line to display, then read that in and count the beats while playing. Would that work?

    • @TheCautionwetpaint
      @TheCautionwetpaint 5 หลายเดือนก่อน

      @@mochreach I thought that if it was a live performance chances are the beats may not align... That is why I thought there should be some pattern recognition as well...

    • @mochreach
      @mochreach  5 หลายเดือนก่อน

      🤔 If you just keep track of the current beat of the stored song, that's the only thing that should matter, right? There's a Discord server associated with the channel, you're welcome to chat about this there, might be easier to keep track of things. Link at the top of the channel page.

  • @user-mc2el9nm6b
    @user-mc2el9nm6b 4 หลายเดือนก่อน

    can we create midi file to music script generator

    • @mochreach
      @mochreach  4 หลายเดือนก่อน

      Yes you can, check out the Mido library: mido.readthedocs.io/en/latest/

    • @user-mc2el9nm6b
      @user-mc2el9nm6b 4 หลายเดือนก่อน

      No i have midi file and i wanted to generate music script i have tried this mido but results are not satisfactory by using mido i somehow able to generate xml or html on this I have only Alphabetical representation but i wanted music script how can i do that

    • @mochreach
      @mochreach  4 หลายเดือนก่อน

      No idea I'm afraid!

  • @carlopernoti3141
    @carlopernoti3141 6 หลายเดือนก่อน +1

    😠It is not working with Windows output port... your video should be named "Programming with MIDI in Python with OP-Z ONLY!!" I lost 15 mn of my time... NEXT video...😤

    • @mochreach
      @mochreach  6 หลายเดือนก่อน +6

      It's not OP-Z only, you can send MIDI to any device, including virtual MIDI. Maybe there's something unusual with your setup. Best of luck finding a solution.

  • @JahidulislamRuman
    @JahidulislamRuman 2 หลายเดือนก่อน

    I want to make a new musical sound piano roll. How do I start?

    • @mochreach
      @mochreach  2 หลายเดือนก่อน

      Hi @JahidulislamRuman, sorry I didn't see this message when it came in. If you want to write it in Python, I would suggest the PyQT library for making the GUI and Mido for handling midi connections. If you're not determined to use Python, you might want to consider using the Godot game engine, it is open source and a pleasure to use, and comes with some MIDI integration (although not MIDI out, but this is easy to implement using an external library).