| 1 |
type note = Sound.sound -> Sound.sound;; |
|---|
| 2 |
|
|---|
| 3 |
let from_sound s = function _ -> s;; |
|---|
| 4 |
|
|---|
| 5 |
let from_envelope = Envelope.apply;; |
|---|
| 6 |
|
|---|
| 7 |
exception InappropriatePlugin |
|---|
| 8 |
|
|---|
| 9 |
let get_ports ladspa = |
|---|
| 10 |
let ports = Ladspa_raw.port_count ladspa in |
|---|
| 11 |
let rec _portdesc n = |
|---|
| 12 |
if n = ports then [] |
|---|
| 13 |
else (n, Ladspa_raw.port_descriptor ladspa n)::_portdesc (n + 1) in |
|---|
| 14 |
_portdesc 0 |
|---|
| 15 |
|
|---|
| 16 |
let set_control_value plugin handle port value = |
|---|
| 17 |
let v = Ladspa_raw.control_value_to_buffer value in |
|---|
| 18 |
Ladspa_raw.connect_port plugin handle port v;; |
|---|
| 19 |
|
|---|
| 20 |
let ladspa_init func ladspa handle = |
|---|
| 21 |
let ports = get_ports ladspa in |
|---|
| 22 |
let inControlPorts = List.filter (function (a,(b,c)) -> |
|---|
| 23 |
b = Ladspa_raw.Input && c = Ladspa_raw.Control) ports in |
|---|
| 24 |
List.iter (function p -> set_control_value ladspa handle p (func p)) |
|---|
| 25 |
(List.map fst inControlPorts); |
|---|
| 26 |
let outControlPorts = List.filter (function (a,(b,c)) -> |
|---|
| 27 |
b = Ladspa_raw.Output && c = Ladspa_raw.Control) ports in |
|---|
| 28 |
List.iter (function p -> set_control_value ladspa handle p 0.0) |
|---|
| 29 |
(List.map fst outControlPorts);; |
|---|
| 30 |
|
|---|
| 31 |
let from_ladspa_impl ladspa setup href insound remix extent render_extent = |
|---|
| 32 |
let build_handles _ = |
|---|
| 33 |
let create_f _ = |
|---|
| 34 |
let h = Ladspa_raw.instantiate ladspa (Remix.samplerate remix) in |
|---|
| 35 |
Ladspa_raw.activate ladspa h; |
|---|
| 36 |
setup ladspa h; h in |
|---|
| 37 |
Array.init (Remix.channels remix) create_f in |
|---|
| 38 |
let raw_start, raw_length = Time.extent_to_raw_time remix render_extent in |
|---|
| 39 |
let handles = (match !href with |
|---|
| 40 |
| None -> |
|---|
| 41 |
( |
|---|
| 42 |
let h = build_handles () in |
|---|
| 43 |
href := Some (h, raw_start + raw_length); |
|---|
| 44 |
h |
|---|
| 45 |
) |
|---|
| 46 |
| Some (h, t) -> |
|---|
| 47 |
( |
|---|
| 48 |
if t = raw_start |
|---|
| 49 |
then ( |
|---|
| 50 |
href := Some (h, t + raw_length); |
|---|
| 51 |
h |
|---|
| 52 |
) else ( |
|---|
| 53 |
let h = build_handles () in |
|---|
| 54 |
href := Some (h, raw_start + raw_length); |
|---|
| 55 |
h |
|---|
| 56 |
) |
|---|
| 57 |
)) in |
|---|
| 58 |
let ports = get_ports ladspa in |
|---|
| 59 |
let inAudioPorts = List.filter (function (a,(b,c)) -> |
|---|
| 60 |
b = Ladspa_raw.Input && c = Ladspa_raw.Audio) ports in |
|---|
| 61 |
let outAudioPorts = List.filter (function (a,(b,c)) -> |
|---|
| 62 |
b = Ladspa_raw.Output && c = Ladspa_raw.Audio) ports in |
|---|
| 63 |
let num_in = List.length inAudioPorts in |
|---|
| 64 |
let num_out = List.length outAudioPorts in |
|---|
| 65 |
let create_out_buffers n length = |
|---|
| 66 |
Array.init n (function _ -> FB.create length 1) in |
|---|
| 67 |
let connect portlist portpos h b i = match b.(i) with |
|---|
| 68 |
(buf, _, _) -> |
|---|
| 69 |
Ladspa_raw.connect_port ladspa h.(i) (fst (List.nth portlist portpos)) |
|---|
| 70 |
buf 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 = FB.split raw_insound 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 |
FB.merge outbuffers |
|---|
| 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 |
FB.merge outbuffers |
|---|
| 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 |
|
|---|