project1/main.py
2024-11-09 16:30:08 +01:00

79 lines
No EOL
2.6 KiB
Python

import lilv, numpy, time
import soundfile
CHUNK_SIZE = 128
frames, sample_rate = soundfile.read('a.flac')
num_frames = len(frames)
print(f"{sample_rate}Hz | {int(num_frames/sample_rate/60)}:{int(num_frames/sample_rate%60)}")
frames = frames.astype(numpy.float32)
world = lilv.World()
world.load_all()
plugins = world.get_all_plugins()
plugin = plugins.get_by_uri("http://lsp-plug.in/plugins/lv2/comp_delay_x2_stereo")
if plugin is None:
print("No plugin comp_delay_x2_stereo")
exit()
print("Using:", plugin.get_name())
for i in range(plugin.get_num_ports()):
port = plugin.get_port(i)
#print(f"Port {i}: {port.get_name()} = {port.get_range()[0]}")
instance = lilv.Instance(plugin, sample_rate)
instance.connect_port(4, numpy.array([1], dtype=numpy.int32)) # while 1 is default it needs to be set as int32 for whatever reason
knob_mode = numpy.array([2], dtype=numpy.float32) # by the way, this can't be put into connect_port, even if just the number is a variable
instance.connect_port(16, knob_mode)
knob_time = numpy.array([10.0], dtype=numpy.float32) # but this can?????? it can be any of the three
instance.connect_port(22, knob_time)
output_left = numpy.zeros(num_frames, frames.dtype)
output_right = numpy.zeros(num_frames, frames.dtype)
input_left_chunk = numpy.zeros(CHUNK_SIZE, frames.dtype)
input_right_chunk = numpy.zeros(CHUNK_SIZE, frames.dtype)
output_left_chunk = numpy.zeros(CHUNK_SIZE, frames.dtype)
output_right_chunk = numpy.zeros(CHUNK_SIZE, frames.dtype)
instance.connect_port(0, input_left_chunk)
instance.connect_port(1, input_right_chunk)
instance.connect_port(2, output_left_chunk)
instance.connect_port(3, output_right_chunk)
instance.activate()
print(f"\nFrames: {num_frames}")
print(f"Chunk size: {CHUNK_SIZE}")
print(f"So {int(numpy.ceil(num_frames/CHUNK_SIZE))} iterations\n")
for start in range(0, num_frames, CHUNK_SIZE):
end = min(start + CHUNK_SIZE, num_frames)
chunk_size = end - start
print(start, chunk_size, end, " \033[F")
input_left_chunk[:chunk_size] = frames[start:end, 0]
input_right_chunk[:chunk_size] = frames[start:end, 1]
output_left_chunk.fill(0)
output_right_chunk.fill(0)
instance.run(chunk_size)
output_left[start:end] = output_left_chunk[:chunk_size]
output_right[start:end] = output_right_chunk[:chunk_size]
print("done processing ")
instance.deactivate()
merged = numpy.column_stack((output_left, output_right))
if numpy.allclose(frames, merged, rtol=0.5, atol=0.5):
print("Warning: output is identical")
soundfile.write('aoutt.flac', merged, sample_rate)