| 1 |
type plugin = Sound.sound -> Sound.sound;; |
|---|
| 2 |
|
|---|
| 3 |
let from_sound s = function _ -> s;; |
|---|
| 4 |
|
|---|
| 5 |
exception InappropriatePlugin |
|---|
| 6 |
|
|---|
| 7 |
let get_ports ladspa = |
|---|
| 8 |
let ports = Ladspa_raw.port_count ladspa in |
|---|
| 9 |
let rec _portdesc n = |
|---|
| 10 |
if n = ports then [] |
|---|
| 11 |
else (n, Ladspa_raw.port_descriptor ladspa n)::_portdesc (n + 1) in |
|---|
| 12 |
_portdesc 0 |
|---|
| 13 |
|
|---|
| 14 |
let set_control_value plugin handle port value = |
|---|
| 15 |
let v = Ladspa_raw.control_value_to_buffer value in |
|---|
| 16 |
Ladspa_raw.connect_port plugin handle port v;; |
|---|
| 17 |
|
|---|
| 18 |
let ladspa_init func ladspa handle = |
|---|
| 19 |
let ports = get_ports ladspa in |
|---|
| 20 |
let inControlPorts = List.filter (function (a,(b,c)) -> |
|---|
| 21 |
b = Ladspa_raw.Input && c = Ladspa_raw.Control) ports in |
|---|
| 22 |
List.iter (function p -> set_control_value ladspa handle p (func p)) |
|---|
| 23 |
(List.map fst inControlPorts); |
|---|
| 24 |
let outControlPorts = List.filter (function (a,(b,c)) -> |
|---|
| 25 |
b = Ladspa_raw.Output && c = Ladspa_raw.Control) ports in |
|---|
| 26 |
List.iter (function p -> set_control_value ladspa handle p 0.0) |
|---|
| 27 |
(List.map fst outControlPorts);; |
|---|
| 28 |
|
|---|
| 29 |
let from_ladspa_impl ladspa setup href insound remix extent render_extent = |
|---|
| 30 |
let build_handles _ = |
|---|
| 31 |
let create_f _ = |
|---|
| 32 |
let h = Ladspa_raw.instantiate ladspa (Remix.samplerate remix) in |
|---|
| 33 |
Ladspa_raw.activate ladspa h; |
|---|
| 34 |
setup ladspa h; h in |
|---|
| 35 |
Array.init (Remix.channels remix) create_f in |
|---|
| 36 |
let raw_start, raw_length = Time.extent_to_raw_time remix render_extent in |
|---|
| 37 |
let handles = (match !href with |
|---|
| 38 |
| None -> |
|---|
| 39 |
( |
|---|
| 40 |
let h = build_handles () in |
|---|
| 41 |
href := Some (h, raw_start + raw_length); |
|---|
| 42 |
h |
|---|
| 43 |
) |
|---|
| 44 |
| Some (h, t) -> |
|---|
| 45 |
( |
|---|
| 46 |
if t = raw_start |
|---|
| 47 |
then ( |
|---|
| 48 |
href := Some (h, t + raw_length); |
|---|
| 49 |
h |
|---|
| 50 |
) else ( |
|---|
| 51 |
let h = build_handles () in |
|---|
| 52 |
href := Some (h, raw_start + raw_length); |
|---|
| 53 |
h |
|---|
| 54 |
) |
|---|
| 55 |
)) in |
|---|
| 56 |
let ports = get_ports ladspa in |
|---|
| 57 |
let inAudioPorts = List.filter (function (a,(b,c)) -> |
|---|
| 58 |
b = Ladspa_raw.Input && c = Ladspa_raw.Audio) ports in |
|---|
| 59 |
let outAudioPorts = List.filter (function (a,(b,c)) -> |
|---|
| 60 |
b = Ladspa_raw.Output && c = Ladspa_raw.Audio) ports in |
|---|
| 61 |
let num_in = List.length inAudioPorts in |
|---|
| 62 |
let num_out = List.length outAudioPorts in |
|---|
| 63 |
let create_in_buffers n sound length = |
|---|
| 64 |
let b = Array.init n (function _ -> Ladspa_raw.empty_buffer length) in |
|---|
| 65 |
Ladspa_raw.raw_sound_to_buffers sound b length; b in |
|---|
| 66 |
let create_out_buffers n length = |
|---|
| 67 |
Array.init n (function _ -> Ladspa_raw.empty_buffer length) in |
|---|
| 68 |
let connect portlist portpos h b i = |
|---|
| 69 |
Ladspa_raw.connect_port ladspa h.(i) (fst (List.nth portlist portpos)) |
|---|
| 70 |
b.(i) in |
|---|
| 71 |
if num_in = 1 && num_out = 1 then ( |
|---|
| 72 |
let raw_insound = insound remix extent render_extent in |
|---|
| 73 |
let channels = Remix.channels remix in |
|---|
| 74 |
let buffers = create_in_buffers channels raw_insound raw_length in |
|---|
| 75 |
let outbuffers = create_out_buffers channels raw_length in |
|---|
| 76 |
for i = 0 to (channels - 1) do ( |
|---|
| 77 |
connect inAudioPorts 0 handles buffers i; |
|---|
| 78 |
connect outAudioPorts 0 handles outbuffers i; |
|---|
| 79 |
Ladspa_raw.run ladspa handles.(i) raw_length |
|---|
| 80 |
) done; |
|---|
| 81 |
Ladspa_raw.buffers_to_raw_sound outbuffers raw_length |
|---|
| 82 |
) else if num_in = 0 && num_out = 1 then ( |
|---|
| 83 |
let channels = Remix.channels remix in |
|---|
| 84 |
let outbuffers = create_out_buffers channels raw_length in |
|---|
| 85 |
for i = 0 to (channels - 1) do ( |
|---|
| 86 |
connect outAudioPorts 0 handles outbuffers i; |
|---|
| 87 |
Ladspa_raw.run ladspa handles.(i) raw_length |
|---|
| 88 |
) done; |
|---|
| 89 |
Ladspa_raw.buffers_to_raw_sound outbuffers raw_length |
|---|
| 90 |
) else raise InappropriatePlugin;; |
|---|
| 91 |
|
|---|
| 92 |
let from_ladspa ladspa setup = |
|---|
| 93 |
let href = ref None in |
|---|
| 94 |
from_ladspa_impl ladspa setup href;; |
|---|
| 95 |
|
|---|
| 96 |
|
|---|