Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
# synth.py
# manages AMY voice allocated Synths
# This version uses the AMY synth= mechanism.
import amy
import tulip
from patches import drumkit
from tulip_queue import Queue
import midi
class PatchSynth:
"""Manage a polyphonic synthesizer by rotating among a fixed pool of voices.
Provides methods:
synth.note_on(midi_note, velocity, time=None, sequence=None)
synth.note_off(midi_note, time=None, sequence=None)
synth.all_notes_off()
synth.program_change(patch) changes preset for all voices.
synth.control_change(control, value) modifies a parameter for all voices.
synth.amy_send(**kwargs) prepends the synth number, sends args to AMY.
Provides read-back attributes (for voices.py UI):
synth.patch
synth.patch_state - patch-specific data only used by clients e.g. UI state
"""
# Class-wide record of which amy synths and patches are in use.
amy_synth_allocated = set()
amy_synth_next = 16
amy_patch_allocated = set()
amy_patch_next = 1024
@classmethod
def reset(cls):
"""Resets AMY and PatchSynth's tracking of its state."""
cls.amy_synth_allocated = set()
cls.amy_synth_next = 16
amy.reset()
def __init__(self, num_voices=4, channel=None,
patch=None, patch_string=None, oscs_per_voice=None,
synth_flags=0, synth_already_initialized=False):
if channel is not None:
self.synth = channel
else:
self.synth = PatchSynth.amy_synth_next
PatchSynth.amy_synth_next += 1
self.num_voices = num_voices
self.oscs_per_voice = oscs_per_voice
self.patch_string = patch_string
self.patch = patch
self.patch_state = None
self.synth_flags = synth_flags
self.synth_init_args = None
# The actual setup of the synth is deferred until the first time this
# synth is used. The MIDI channel is not part of this API, but for AMY
# the MIDI channel determines the synth number, so before allocating a
# different (non-MIDI) synth number, let's wait and see if we're allocated
# to a channel.
self._initialized = synth_already_initialized
def deferred_init(self):
"""Finish synth initialization once we can assume all voices are available."""
if not self._initialized:
how_many_ways_are_patches_specified = (
(self.patch is not None)
+ (self.patch_string is not None)
+ (self.oscs_per_voice is not None)
)
if how_many_ways_are_patches_specified != 1:
raise ValueError(
'Just 1 of patch (%s) patch_string (%s) or oscs_per_voice (%s) needed.' % (
self.patch, self.patch_string, self.oscs_per_voice
)
)
amy_send_args = {'num_voices': self.num_voices, 'synth_flags': self.synth_flags}
# Maybe append additional init args (e.g. set by subclasses).
if self.synth_init_args:
amy_send_args |= self.synth_init_args
if self.patch_string is not None:
amy_send_args['patch_string'] = self.patch_string
elif self.patch is not None:
amy_send_args['patch'] = self.patch
else:
amy_send_args['oscs_per_voice'] = self.oscs_per_voice
self.amy_send(**amy_send_args)
self._initialized = True
# Fields used by UI
#self.num_voices = num_voices
# send an AMY message to the voices in this synth
def amy_send(self, **kwargs):
amy.send(synth=self.synth, **kwargs)
def note_off(self, note, time=None, sequence=None):
self.amy_send(note=note, vel=0, time=time, sequence=sequence)
def all_notes_off(self):
self.note_off(note=0) # Note=0 means all notes off.
def note_on(self, note, velocity=1, time=None, sequence=None, **kwargs):
self.deferred_init()
if self.synth is None:
# Note on after synth.release()?
raise ValueError('PatchSynth note on with no AMY synth - synth has been released?')
if velocity == 0:
self.note_off(note, time=time, sequence=sequence)
else: # Velocity > 0, note on.
self.amy_send(note=note, vel=velocity, time=time, sequence=sequence, **kwargs)
def sustain(self, state):
"""Turn sustain on/off."""
self.deferred_init()
self.amy_send(pedal=state)
def get_patch_state(self):
self.deferred_init()
return self.patch_state
def set_patch_state(self, state):
self.deferred_init()
self.patch_state = state
def program_change(self, patch):
import time
if patch != self.patch:
self.patch = patch
# Reset any modified state due to previous patch modifications.
self.patch_state = None
if not self._initialized:
self.deferred_init()
else:
self.amy_send(patch=patch)
def control_change(self, control, value):
print('control_change not implemented for amy-managed voices.')
def release(self):
"""Called to terminate this synth and release its amy_voice resources."""
# Release the AMY synth by setting its num_voices to 0.
self.amy_send(num_voices=0)
# Mark this object as not usable.
self._initialized = True
self.synth = None
def set_channel(self, channel):
if self._initialized:
# We need to renumber the old synth, if we made it already.
self.amy_send(to_synth=channel)
# Future commands go to the new synth
self.synth = channel
# This used to be a method on OscSynth, but it's just an alias for amy_send.
def update_oscs(self, **kwargs):
"""Update all our oscs with the params in the dict."""
# Pass the commands to all the voices.
self.deferred_init()
self.amy_send(**kwargs)
class OscSynth(PatchSynth):
"""PatchSynth that uses one osc per note. Osc parameters are specified at initialization."""
def __init__(self, num_voices=4, channel=None, synth_flags=0, **kwargs):
"""Create a synth that plays a single osc configured with keyword args."""
super().__init__(
oscs_per_voice=1, num_voices=num_voices, channel=channel, synth_flags=synth_flags
)
# Configure the synth's single osc per voice with the following args.
#self.amy_send(**kwargs)
# Make sure the args are used during the deferred_init.
self.synth_init_args = kwargs
class DrumSynth(OscSynth):
"""Synth for Drum channel (10).
Each MIDI note plays a separately-configured sound.
"""
def __init__(self, num_voices=4, channel=None):
# synth_flags 3 means do the drum translation and ignore note offs.
# We scale the PCM drums by 5 to make them comparable in level to Juno voices.
super().__init__(num_voices=num_voices, channel=channel, synth_flags=3, amp=5.0)
9c97482dfThis file is pinned to the selected release. Download the original to open it in your design tool.