0

I have a Python code that outputs a MIDI file and I'm trying to send this automatically to Ableton (preferably) or MAX MSP so I can do further processing. Until now I've tried many suggested solutions but none of them has worked, and here's a summary of what I've done:

  1. In MAX MSP, I created a udpreceive PORT object and connected it to a message box (so I can see the possible output, screenshot attached here).

enter image description here

And only as a test, I tried to send a text to this port by running this [source]:


import socket

msgFromClient       = "cccc"

bytesToSend         = str.encode(msgFromClient)
serverAddressPort   = ("127.0.0.1", 1235)
bufferSize          = 1024

# Create a UDP socket at client side
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

# Send to server using created UDP socket
UDPClientSocket.sendto(bytesToSend, serverAddressPort)

There were two problems here:

  1. Message length had to be a multiple of 4 otherwise I would get an error stating exactly this.
  2. When I tried "cccc" as the message, I got the following error:
udpreceive: OSC Bad message name string: DataAfterAlignedString: Unreasonably long string
Dropping entire message.

And seems like this is related to the fact that MAX MSP creates a UDP server and Python is trying to connect to a TCP server.

Searching some more, I found out maybe by converting my MIDI file to OSC (Open Sound Control) format and then sending it, but none of the current solutions worked. For example as suggested here, pyosc can't be installed, giving me the following error:

(base) C:\Users\arash>pip install pyosc
Collecting pyosc
  Using cached pyOSC-0.3.5b-5294.tar.gz (33 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [9 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "C:\Users\arash\AppData\Local\Temp\pip-install-8zlix12t\pyosc_252847d8985244e1915a8ba3b8a28de0\setup.py", line 5, in <module>
          import OSC
        File "C:\Users\arash\AppData\Local\Temp\pip-install-8zlix12t\pyosc_252847d8985244e1915a8ba3b8a28de0\OSC.py", line 735
          binary = struct.pack('>ll', 0L, 1L)
                                       ^
      SyntaxError: invalid syntax
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
  1. Some people here suggested python-osc, and using that I tried running the following code[source]:
# from osc import *
from pythonosc.udp_client import SimpleUDPClient

ip = "127.0.0.1"
port = 1235

client = SimpleUDPClient(ip, port)  # Create client

client.send_message("home", "hello")

The code terminates with no errors but nothing happens in my MAX MSP console. I suspect maybe the problem is with the address (first argument) that I have to pass to client.send_message() because I'm not sure what it is.

  1. Some people suggested using Jython, which is not the option anymore since it only supports Python 2.

Also in Ableton, when I try to accept MIDI input from Python (more specifically MIDI from PythonMIDI1), the connection isn't successful.

I would really appreciate it if you could help me regarding this issue.

[Edit: Minor mistake in my Max patcher: I should have used a print object rather than a message box for printing output, but the problem still remains.]

1 Answers1

0

I think the UDP/OSC protocols are overcomplicating the situation more than they're helping. If you don't want to or don't need to interact with Max, you could just automate the midi file importing process through python or with an additional scripting language.

Within Live's user menu, the Create dropdown menu has a command at the bottom labeled "Import MIDI File..." - this can be accessed using something like pywinauto (example here), or AppleScript (example here). You might have to add in some keypresses if you want to automate your way through the "Import Tempo/Time Signature" pop-up, but that can be done with the same tools that select the dropdown menu.

If you're more interested in importing things through Max for additional post-processing and adding that to Live afterwards, check out the seq max object and the LOM - the Live Object Model. In particular, the create_clip function that ClipSlot has and the add_new_notes (post-Live 11.0) or set_notes (pre-Live 11.0) functions within the Clip object will let you automatically place the processed MIDI note information within a clip slot in Live's Session View.

Mussar
  • 1
  • 1