Exemple #1
0
 def setUp(self):
     self.world = lilv.World()
     self.world.set_option(lilv.OPTION_FILTER_LANG,
                           self.world.new_bool(True))
     self.bundle_uri = self.world.new_uri(location)
     self.assertIsNotNone(self.bundle_uri,
                          "Invalid URI: '" + location + "'")
     self.world.load_bundle(self.bundle_uri)
     self.plugins = self.world.get_all_plugins()
     self.plugin = self.plugins.get(self.plugins.begin())
     self.assertTrue(self.plugin.verify())
     self.assertTrue(self.plugin in self.plugins)
     self.assertTrue(self.plugin.get_uri() in self.plugins)
     self.assertEqual(self.plugins[self.plugin.get_uri()], self.plugin)
     self.assertIsNotNone(self.plugin,
                          msg="Test plugin not found at location: '" +
                          location + "'")
     self.assertEqual(location, str(self.plugin.get_bundle_uri()))
     self.plugin_uri = self.plugin.get_uri()
     self.assertEqual(self.plugin.get_uri(), self.plugin_uri,
                      "URI equality broken")
     self.instance = lilv.Instance(self.plugin, 48000, None)
     self.assertIsNotNone(self.instance)
     self.lv2_InputPort = self.world.new_uri(lilv.LILV_URI_INPUT_PORT)
     self.lv2_OutputPort = self.world.new_uri(lilv.LILV_URI_OUTPUT_PORT)
     self.lv2_AudioPort = self.world.new_uri(lilv.LILV_URI_AUDIO_PORT)
     self.lv2_ControlPort = self.world.new_uri(lilv.LILV_URI_CONTROL_PORT)
Exemple #2
0
 def init(self, samplingRate):
     """argument: sample rate
     must be called before calling compute"""
     self._instance = lilv.Instance(self._plugin, samplingRate)
     n = 0
     for port in self._params.values():
         self._instance.connect_port(port.idx, port.val)
     self.activate()
     self._samplerate = samplingRate
Exemple #3
0
 def setUp(self):
     self.world = lilv.World()
     self.bundle_uri = self.world.new_uri(location)
     self.world.load_bundle(self.bundle_uri)
     self.plugins = self.world.get_all_plugins()
     self.plugin = self.plugins[0]
     self.instance = lilv.Instance(self.plugin, 48000)
     self.assertEqual(self.plugin.get_uri(), self.instance.get_uri())
     self.assertIsNone(
         self.instance.get_extension_data(
             self.world.new_uri("http://example.org/ext")))
     self.assertIsNone(
         self.instance.get_extension_data("http://example.org/ext"))
Exemple #4
0
 def setUp(self):
     self.world = lilv.World()
     location = "file://" + os.getcwd() + "/bindings/bindings_test_plugin.lv2/"
     self.plugin_uri = self.world.new_uri(location)
     self.assertIsNotNone(self.plugin_uri, "Invalid URI: '" + location + "'")
     self.world.load_bundle(self.plugin_uri)
     self.plugins = self.world.get_all_plugins()
     self.plugin  = self.plugins.get(self.plugins.begin())
     self.assertIsNotNone(self.plugin, msg="Test plugin not found at location: '" + location + "'")
     self.assertEqual(location, self.plugin.get_bundle_uri().as_string())
     self.instance = lilv.Instance(self.plugin, 48000, None)
     self.assertIsNotNone(self.instance)
     self.lv2_InputPort    = self.world.new_uri(lilv.LILV_URI_INPUT_PORT)
     self.lv2_OutputPort   = self.world.new_uri(lilv.LILV_URI_OUTPUT_PORT)
     self.lv2_AudioPort    = self.world.new_uri(lilv.LILV_URI_AUDIO_PORT)
     self.lv2_ControlPort  = self.world.new_uri(lilv.LILV_URI_CONTROL_PORT)
Exemple #5
0
def main():
    # Read command line arguments
    if len(sys.argv) != 4:
        print('USAGE: lv2_apply.py PLUGIN_URI INPUT_WAV OUTPUT_WAV')
        sys.exit(1)

    # Initialise Lilv
    world = lilv.World()
    world.load_all()

    plugin_uri   = sys.argv[1]
    wav_in_path  = sys.argv[2]
    wav_out_path = sys.argv[3]

    # Find plugin
    plugin_uri_node = world.new_uri(plugin_uri)
    plugin          = world.get_all_plugins().get_by_uri(plugin_uri_node)
    if not plugin:
        print("Unknown plugin `%s'\n" % plugin_uri)
        sys.exit(1)

    lv2_InputPort   = world.new_uri(lilv.LILV_URI_INPUT_PORT)
    lv2_OutputPort  = world.new_uri(lilv.LILV_URI_OUTPUT_PORT)
    lv2_AudioPort   = world.new_uri(lilv.LILV_URI_AUDIO_PORT)
    lv2_ControlPort = world.new_uri(lilv.LILV_URI_CONTROL_PORT)
    lv2_default     = world.new_uri("http://lv2plug.in/ns/lv2core#default")

    n_audio_in  = plugin.get_num_ports_of_class(lv2_InputPort,  lv2_AudioPort)
    n_audio_out = plugin.get_num_ports_of_class(lv2_OutputPort, lv2_AudioPort)
    if n_audio_out == 0:
        print("Plugin has no audio outputs\n")
        sys.exit(1)

    # Open input file
    try:
        wav_in = WavFile(wav_in_path)
    except:
        print("Failed to open input `%s'\n" % wav_in_path)
        sys.exit(1)

    if wav_in.nchannels != n_audio_in:
        print("Input has %d channels, but plugin has %d audio inputs\n" % (
            wav_in.nchannels, n_audio_in))
        sys.exit(1)

    # Open output file
    wav_out = wave.open(wav_out_path, 'w')
    if not wav_out:
        print("Failed to open output `%s'\n" % wav_out_path)
        sys.exit(1)

    # Set output file to same format as input (except possibly nchannels)
    wav_out.setparams(wav_in.wav_in.getparams())
    wav_out.setnchannels(n_audio_out)

    print('%s => %s => %s @ %d Hz'
          % (wav_in_path, plugin.get_name(), wav_out_path, wav_in.framerate))

    instance = lilv.Instance(plugin, wav_in.framerate)

    channels = wav_in.read()
    wav_in.close()

    # Connect all ports to buffers. NB if we fail to connect any buffer, lilv
    # will segfault.
    audio_input_buffers    = []
    audio_output_buffers   = []
    control_input_buffers  = []
    control_output_buffers = []
    for index in range(plugin.get_num_ports()):
        port = plugin.get_port_by_index(index)
        if port.is_a(lv2_InputPort):
            if port.is_a(lv2_AudioPort):
                audio_input_buffers.append(numpy.array(channels[len(audio_input_buffers)], numpy.float32))
                instance.connect_port(index, audio_input_buffers[-1])
            elif port.is_a(lv2_ControlPort):
                #if port.has_property(lv2_default):  # Doesn't seem to work
                default = lilv.lilv_node_as_float(lilv.lilv_nodes_get_first(port.get_value(lv2_default)))
                control_input_buffers.append(numpy.array([default], numpy.float32))
                instance.connect_port(index, control_input_buffers[-1])
            else:
                raise ValueError("Unhandled port type")
        elif port.is_a(lv2_OutputPort):
            if port.is_a(lv2_AudioPort):
                audio_output_buffers.append(numpy.array([0] * wav_in.nframes, numpy.float32))
                instance.connect_port(index, audio_output_buffers[-1])
            elif port.is_a(lv2_ControlPort):
                control_output_buffers.append(numpy.array([0], numpy.float32))
                instance.connect_port(index, control_output_buffers[-1])
            else:
                raise ValueError("Unhandled port type")

    # Run the plugin:
    instance.run(wav_in.nframes)

    # Interleave output buffers:
    data = numpy.dstack(audio_output_buffers).flatten()

    # Return to original int range:
    if wav_in.signed:
        data = data * float(wav_in.range / 2)
    else:
        data = (data + 1) * float(wav_in.range/2)

    # Write output file in chunks to stop memory usage getting out of hand:
    CHUNK_SIZE = 8192
    for chunk in numpy.array_split(data, CHUNK_SIZE):
        wav_out.writeframes(wave.struct.pack("%u%s" % (len(chunk), wav_in.struct_fmt_code), *chunk))
    wav_out.close()
Exemple #6
0
# Open output file
wav_out = wave.open(wav_out_path, 'w')
if not wav_out:
    print "Failed to open output `%s'\n" % wav_out_path
    sys.exit(1)

# Set output file to same format as input (except possibly nchannels)
wav_out.setparams(wav_in.getparams())
wav_out.setnchannels(n_audio_out)

rate = wav_in.getframerate()
nframes = wav_in.getnframes()

# Instantiate plugin
instance = lilv.Instance(plugin, rate)


def read_float(wf, nframes):
    wav = wf.readframes(nframes)
    if wf.getsampwidth() == 4:
        wav = wave.struct.unpack("<%ul" % (len(wav) / 4), wav)
        wav = [i / float(math.pow(2, 32)) for i in wav]
    elif wf.getsampwidth() == 2:
        wav = wave.struct.unpack("<%uh" % (len(wav) / 2), wav)
        wav = [i / float(math.pow(2, 16)) for i in wav]
    else:
        wav = wave.struct.unpack("%uB" % (len(wav)), wav)
        wav = [s - 128 for s in wav]
        wav = [i / float(math.pow(2, 8)) for i in wav]
Exemple #7
0
world.load_all()

plugin_uri = world.new_uri("http://lv2plug.in/plugins/eg-amp")

plugin = world.get_all_plugins().get_by_uri(plugin_uri)
assert plugin

lv2_InputPort = world.new_uri(lilv.LILV_URI_INPUT_PORT)
lv2_OutputPort = world.new_uri(lilv.LILV_URI_OUTPUT_PORT)
lv2_AudioPort = world.new_uri(lilv.LILV_URI_AUDIO_PORT)

# Find first audio in/out ports:
input_port_index = None
output_port_index = None
for i in range(plugin.get_num_ports()):
    port = plugin.get_port_by_index(i)
    if input_port_index is None and port.is_a(lv2_InputPort) and port.is_a(
            lv2_AudioPort):
        input_port_index = i
    if output_port_index is None and port.is_a(lv2_OutputPort) and port.is_a(
            lv2_AudioPort):
        output_port_index = i
assert input_port_index and output_port_index

instance = lilv.Instance(plugin, 48000)
in_buf = numpy.array([23] * 256, numpy.float32)
out_buf = numpy.array([0] * 256, numpy.float32)
instance.connect_port(input_port_index, in_buf)
instance.connect_port(output_port_index, out_buf)
instance.run(len(in_buf))  # <-- Segfaults