Giter Site home page Giter Site logo

Comments (5)

mgeier avatar mgeier commented on July 20, 2024

Thanks for this code example!

It doesn't quite work, though. It seems to work, but it doesn't actually forward all messages.

The problem is that you are accessing and modifying the Python lists midiInBuffer and midiOutBuffer from two different threads.

If you want to transport events from one thread to another, you should use something like jack.RingBuffer from this module or queue.Queue from the Python standard library.

Also, it is unclear to me why you transfer the messages out of the callback into the main thread, just to transfer them back into the callback without doing anything else?

Why don't you just forward the messages in the callback?

Then you'll have an example very similar to midi_chords.py. If you remove about half of the process function, you'll get just the forwarding feature:

@client.set_process_callback
def process(frames):
outport.clear_buffer()
for offset, indata in inport.incoming_midi_events():
# Note: This may raise an exception:
outport.write_midi_event(offset, indata) # pass through

from jackclient-python.

mgeier avatar mgeier commented on July 20, 2024

Related issue #47.

from jackclient-python.

icubex avatar icubex commented on July 20, 2024

You are so right ! I changed the example as follows:

import jack
import struct

client = jack.Client('jack')

midi_in = client.midi_inports.register('midi_in')
midi_out = client.midi_outports.register('midi_out')

@client.set_process_callback

def process(frames):
  midi_out.clear_buffer()
  #forward received MIDI data
  for offset, data in midi_in.incoming_midi_events():
    midi_out.write_midi_event(offset, data)
    #unpack data for printing
    msg = struct.unpack('3B', data)
    print('MIDI rcvd = ', msg)

client.activate()

#print all ports
print(client.get_ports())

#print client ports
#print(client.midi_inports)
#print(client.midi_outports)

#connect to MIDI data source
#client.connect('MyMIDISoftware:midi_out', midi_in)

#connect to MIDI data destination
#client.connect(midi_out, MyOtherMIDISoftware:midi_in')

#do something else like running a GUI to configure MIDI source/destination connections
with client:
    print('#' * 80)
    print('press Return to quit')
    print('#' * 80)
    input()

from jackclient-python.

mgeier avatar mgeier commented on July 20, 2024

Yes, now it nearly works. You are only missing an import statement:

import struct

And please note that you are waiting in a so-called "tight loop", that means the while loop is executed as fast as the CPU is able to. You should look at a CPU meter while your program is running!

Have a look at my examples for other ways of waiting that don't use all available CPU power.

Now your example is very close to my midi_chords.py example, isn't it?

BTW, I've just uploaded a new MIDI example: #61

What do you think about it?

from jackclient-python.

icubex avatar icubex commented on July 20, 2024

Ah yes, I forgot to copy the struct stmt over from my RPi where I was working on this. The while loop is bad indeed so I copied your code from midi_chords.py. And yes the example is very close to your midi_chords.py now. Not sure why I wasn't able to understand how to use write_midi_event from midi_chords.py back in November when I was working on this, see https://github.com/I-CubeX/PythonExamples/blob/master/AnalogReadMIDI.py. Thanks again for your help, and for the new example (one can't have enough examples .. ) !

from jackclient-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.