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:
- 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).
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:
- Message length had to be a multiple of 4 otherwise I would get an error stating exactly this.
- 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.
# 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.
- 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.]