dimanche 3 mai 2015

Communicate between JavaScript webpage (in browser) and local (client-side) Python-script

I'm trying to get a webpage to receive signals from a MIDI keyboard.

WebMIDI is some way off, so I'm looking at other solutions.

In the past I've used a Python script to convert MIDI signals into UDP packets (which I've then picked up in Unity3D).

This was my Python script:

#!/usr/bin/python

# http://ift.tt/1hASrLP
import rtmidi_python as rtmidi

import time

import socket

def callback( data, time_stamp ):
    event, note, vel = data

    if event == 144: # note on/off
        endpoint = ( "127.0.0.1", 6500 )

        MESSAGE = "%d, %f" % ( note, float(vel) / 127.0 )

        print MESSAGE

        udp_socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )

        udp_socket.sendto( 
            MESSAGE, 
            endpoint
            )

def main( ):
    midi_in = rtmidi.MidiIn( )

    midi_in.callback = callback

    midi_in.open_port( 0 )

    # do something else here (but don't quit)
    while True: 
        time.sleep( 0.001 )

if __name__ == '__main__': 
    main()

It appears to be impossible to write JavaScript code that listens for UDP signals.

The only relevant questions I can find are:

Socket communication between Android app and web browser

Communication between a desktop app and a browser

It looks as though I must crank up a localhost server, but (1) how to integrate my Python script, and (2) how to write JavaScript that will pick up the signals.

I only require unidirectional communication.

Aucun commentaire:

Enregistrer un commentaire