Hi,
I am just getting started with scamp and putting together a program that receives data from a keyboard, modifies it in some form and then sends it to a midi player.
My code and set up are shown below:
If I connect the virtual keyboard to QSynth then everything works fine. I create a midi channel in LoopMidi. I connect virtualkeys and QSynth to that channel and as I play notes I can hear them on QSynth.
If I try and route through Scamp then when I play a key on virtual keys I see that the note is received, but as I play it the note sounds indefinitely until it fades out over a few seconds and then no subsequent notes are received on the listener.
It seems like the played note has gotten stuck and is blocking any further events, both on the incoming and outgoing channels.
If I do not listen and play at the same time the code works fine. The problem only occurs when I try to play a note I have received.
Is this some form of threading issue or is there some way I can further debug it?
from scamp import *
from collections import namedtuple
import datetime
USE_PLAY_NOTE=False
class Note:
def __init__(self,pitch,volume,clock):
self.__duration = 0.0
self.__pitch = pitch
self.__volume = volume
self.__clock = clock
def show(self):
return "Pitch {}, Volume = {}, clock = {}, Duration = {}".format(self.__pitch,self.__volume,self.__clock, self.__duration)
@property
def duration(self):
"""The duration property."""
return self.__duration
@duration.setter
def duration(self, value):
self.__duration = value
@property
def pitch(self):
"""The pitch property."""
return self.__pitch
@pitch.setter
def pitch(self, value):
self.__pitch = value
class Manipulator:
def __init__(self):
self.session = Session()
self.session.print_available_midi_output_devices()
self.session.print_available_midi_input_devices()
try:
self.piano = self.session.new_midi_part("piano", midi_output_device=4, num_channels=8) # Scamp to QSynth
except:
print("Failed to open MidiLoop Port for output")
self.onTime = None
self.offTime = None
def midi_callback(self,midi_message):
code, pitch, volume = midi_message
print("Keyboard {}".format(midi_message))
if(volume > 0): # this is an on-event
self.onTime = datetime.datetime.now()
self.note = Note(pitch,volume,self.onTime.timestamp())
else:
self.offTime = datetime.datetime.now()
delta = self.offTime - self.onTime
self.note.duration = float(delta.total_seconds())
# Eventually do something with the note here
if(USE_PLAY_NOTE):
self.piano.play_note(self.note.pitch,.8,self.note.duration)
else:
noteReference = self.piano.start_note(self.note.pitch, .8)
wait(self.note.duration)
noteReference.end()
def Run(self):
try:
self.session.register_midi_listener(3, self.midi_callback) #[Port 4]: Input from Virtual Keyboard
except:
print("Failed to open MidiLoop Port for input")
self.session.wait_forever()
m = Manipulator()
m.Run()
I placed an image of the setup here: