project1/main.py

70 lines
2.1 KiB
Python
Raw Normal View History

2024-11-09 14:45:04 +01:00
import lilv, numpy, wave
import soundfile
from ctypes import c_float, c_int
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)}")
print(frames.dtype)
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")
print(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)
output_left = numpy.zeros(num_frames, frames.dtype)
output_right = numpy.zeros(num_frames, frames.dtype)
input_left_chunk = numpy.zeros(512, frames.dtype)
input_right_chunk = numpy.zeros(512, frames.dtype)
output_left_chunk = numpy.zeros(512, frames.dtype)
output_right_chunk = numpy.zeros(512, 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)
mode_left = numpy.array([2], dtype=numpy.int32)
instance.connect_port(5, mode_left)
time_left = numpy.array([0.0], dtype=numpy.float32)
instance.connect_port(11, time_left)
mode_right = numpy.array([2], dtype=numpy.int32)
instance.connect_port(16, mode_right)
time_right = numpy.array([500.0], dtype=numpy.float32)
instance.connect_port(22, time_right)
instance.activate()
print(f"frames: {num_frames}\n")
for start in range(0, num_frames, 512):
end = min(start + 512, num_frames)
chunk_size = end - start
print("\033[F", start, chunk_size, end)
input_left_chunk[:chunk_size] = frames[start:end, 0]
input_right_chunk[:chunk_size] = frames[start:end, 1]
instance.run(chunk_size)
output_left[start:end] = output_left_chunk[:chunk_size]
output_right[start:end] = output_right_chunk[:chunk_size]
instance.deactivate()
merged = numpy.column_stack((output_left, output_right))
soundfile.write('aoutt.flac', merged, sample_rate)