Source file ladspa.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(*
 * Copyright 2007-2010 Samuel Mimram
 *
 * This file is part of ocaml-ladspa.
 *
 * ocaml-ladspa is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ocaml-ladspa is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with ocaml-ladspa; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * As a special exception to the GNU Library General Public License, you may
 * link, statically or dynamically, a "work that uses the Library" with a publicly
 * distributed version of the Library to produce an executable file containing
 * portions of the Library, and distribute that executable file under terms of
 * your choice, without any of the additional requirements listed in clause 6
 * of the GNU Library General Public License.
 * By "a publicly distributed version of the Library", we mean either the unmodified
 * Library as distributed by The Savonet Team, or a modified version of the Library that is
 * distributed under the conditions defined in clause 3 of the GNU Library General
 * Public License. This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU Library General Public License.
 *
 *)

let () = Callback.register_exception "ocaml_ladspa_exn_not_found" Not_found

external version : unit -> string = "ocaml_ladspa_version"
external version_major : unit -> int = "ocaml_ladspa_version_major"
external version_minor : unit -> int = "ocaml_ladspa_version_minor"

module Plugin = struct
  type t

  exception Not_a_plugin

  external load : string -> t = "ocaml_ladspa_open"
  external unload : t -> unit = "ocaml_ladspa_close"
end

let () =
  Callback.register_exception "ocaml_ladspa_exn_not_a_plugin"
    Plugin.Not_a_plugin

module Descriptor = struct
  type t

  external descriptor : Plugin.t -> int -> t = "ocaml_ladspa_descriptor"

  let rec descriptors p n =
    try
      let d = descriptor p n in
      let dd = descriptors p (n + 1) in
      d :: dd
    with Not_found -> []

  let descriptors p = Array.of_list (descriptors p 0)

  external unique_id : t -> int = "ocaml_ladspa_unique_id"
  external label : t -> string = "ocaml_ladspa_label"
  external name : t -> string = "ocaml_ladspa_name"
  external maker : t -> string = "ocaml_ladspa_maker"
  external copyright : t -> string = "ocaml_ladspa_copyright"

  let copyright d =
    let c = copyright d in
    (* "None" is the standard answer when there is no copyright. *)
    if c = "None" then None else Some c

  external port_count : t -> int = "ocaml_ladspa_port_count"
  external port_names : t -> string array = "ocaml_ladspa_port_names"

  let port_name d n = (port_names d).(n)

  external port_is_input : t -> int -> bool = "ocaml_ladspa_port_is_input"
  external port_is_output : t -> int -> bool = "ocaml_ladspa_port_is_output"
  external port_is_control : t -> int -> bool = "ocaml_ladspa_port_is_control"
  external port_is_audio : t -> int -> bool = "ocaml_ladspa_port_is_audio"
  external port_is_integer : t -> int -> bool = "ocaml_ladspa_port_is_integer"
  external port_is_boolean : t -> int -> bool = "ocaml_ladspa_port_is_boolean"

  external port_is_logarithmic : t -> int -> bool
    = "ocaml_ladspa_port_is_logarithmic"

  external port_get_default : t -> int -> int -> float option
    = "ocaml_ladspa_port_get_default"

  let port_get_default d ?(samplerate = 44100) p =
    port_get_default d samplerate p

  external port_get_min : t -> int -> int -> float option
    = "ocaml_ladspa_port_get_min"

  let port_get_min d ?(samplerate = 44100) p = port_get_min d samplerate p

  external port_get_max : t -> int -> int -> float option
    = "ocaml_ladspa_port_get_max"

  let port_get_max d ?(samplerate = 44100) p = port_get_max d samplerate p

  type instance

  external get_descriptor : instance -> t = "ocaml_ladspa_get_descriptor"
  external instantiate : t -> int -> instance = "ocaml_ladspa_instantiate"

  external connect_port :
    instance ->
    int ->
    (float, Bigarray.float32_elt, Bigarray.c_layout) Bigarray.Array1.t ->
    unit = "ocaml_ladspa_connect_port"

  let set_control_port i n v =
    assert (port_is_control (get_descriptor i) n);
    let buf = Bigarray.Array1.create Bigarray.float32 Bigarray.c_layout 1 in
    buf.{0} <- v;
    connect_port i n buf

  external activate : instance -> unit = "ocaml_ladspa_activate"
  external deactivate : instance -> unit = "ocaml_ladspa_deactivate"
  external run : instance -> int -> unit = "ocaml_ladspa_run"
end