project1/main.py

79 lines
2.6 KiB
Python
Raw Normal View History

2024-11-09 16:30:08 +01:00
import lilv, numpy, time
2024-11-09 14:45:04 +01:00
import soundfile
2024-11-09 16:30:08 +01:00
CHUNK_SIZE = 128
2024-11-09 14:45:04 +01:00
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")
2024-11-09 16:30:08 +01:00
if plugin is None:
print("No plugin comp_delay_x2_stereo")
exit()
2024-11-09 14:45:04 +01:00
2024-11-09 16:30:08 +01:00
print("Using:", plugin.get_name())
2024-11-09 14:45:04 +01:00
for i in range(plugin.get_num_ports()):
port = plugin.get_port(i)
2024-11-09 16:30:08 +01:00
#print(f"Port {i}: {port.get_name()} = {port.get_range()[0]}")
2024-11-09 14:45:04 +01:00
instance = lilv.Instance(plugin, sample_rate)
2024-11-09 16:30:08 +01:00
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)
2024-11-09 14:45:04 +01:00
output_left = numpy.zeros(num_frames, frames.dtype)
output_right = numpy.zeros(num_frames, frames.dtype)
2024-11-09 16:30:08 +01:00
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)
2024-11-09 14:45:04 +01:00
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()
2024-11-09 16:30:08 +01:00
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)
2024-11-09 14:45:04 +01:00
chunk_size = end - start
2024-11-09 16:30:08 +01:00
print(start, chunk_size, end, " \033[F")
2024-11-09 14:45:04 +01:00
input_left_chunk[:chunk_size] = frames[start:end, 0]
input_right_chunk[:chunk_size] = frames[start:end, 1]
2024-11-09 16:30:08 +01:00
output_left_chunk.fill(0)
output_right_chunk.fill(0)
2024-11-09 14:45:04 +01:00
instance.run(chunk_size)
output_left[start:end] = output_left_chunk[:chunk_size]
output_right[start:end] = output_right_chunk[:chunk_size]
2024-11-09 16:30:08 +01:00
print("done processing ")
2024-11-09 14:45:04 +01:00
instance.deactivate()
merged = numpy.column_stack((output_left, output_right))
2024-11-09 16:30:08 +01:00
if numpy.allclose(frames, merged, rtol=0.5, atol=0.5):
print("Warning: output is identical")
2024-11-09 14:45:04 +01:00
soundfile.write('aoutt.flac', merged, sample_rate)