3 Description: Faust process classes
5 Created: 03/06/2013 Modified: 14/08/2013
16 exception NotYetDone;;
17 exception Dimension_error of string;;
18 exception Process_error of string;;
23 let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;
26 class dimension : int * int -> dimension_type =
27 fun (init : int * int) ->
30 val _output = snd init
33 method output = _output
35 method par : dimension_type -> dimension_type =
38 ((self#input + dim#input), (self#output + dim#output))
40 method seq : dimension_type -> dimension_type =
42 if self#output = dim#input then
43 new dimension (self#input, dim#output)
44 else raise (Dimension_error "seq dimension not matched.")
46 method split : dimension_type -> dimension_type =
48 if dim#input mod self#output = 0 then
49 new dimension (self#input, dim#output)
50 else raise (Dimension_error "split dimension not matched.")
52 method merge : dimension_type -> dimension_type =
54 if self#output mod dim#input = 0 then
55 new dimension (self#input, dim#output)
56 else raise (Dimension_error "merge dimension not matched.")
58 method _rec : dimension_type -> dimension_type =
60 if self#output >= dim#input && self#input >= dim#output then
61 new dimension (self#input - dim#output, self#output)
62 else raise (Dimension_error "rec dimension not matched.")
65 class virtual process =
66 fun (exp_init : faust_exp) ->
69 val virtual _dim : dimension_type
70 val virtual _delay : int
74 method virtual eval : beam_type -> beam_type
77 class proc_const : faust_exp -> process_type =
78 fun (exp_init : faust_exp) ->
82 | _ -> raise (Process_error "const process constructor.") in
85 inherit process exp_init
86 val _dim = new dimension (0,1)
88 method private const = _const
89 method eval : beam_type -> beam_type =
90 fun (input : beam_type) ->
91 if input#get = [||] then
92 new beam [| new signal (new rate 0 1)
93 (fun t -> new value self#const)|]
95 raise (Process_error "proc_const accepts no input.")
99 class proc_ident : faust_exp -> process_type =
100 fun (exp_init : faust_exp) ->
104 | _ -> raise (Process_error "ident process constructor.") in
107 inherit process exp_init
108 val _dim = new dimension (dimension_of_symbol _symbol)
109 val _delay = delay_of_symbol _symbol
110 method private symb = _symbol
112 method private beam_of_ident : int -> signal_type -> beam_type =
114 fun (s : signal_type) ->
115 if n = (self#dim)#input then
117 else raise (Process_error ("Ident " ^ string_of_symbol self#symb))
119 method eval : beam_type -> beam_type =
120 fun (input : beam_type) ->
121 let n = Array.length input#get in
123 | Pass -> self#beam_of_ident n input#get.(0)
124 | Stop -> if n = 1 then new beam [||]
125 else raise (Process_error "Ident !")
126 | Add -> self#beam_of_ident n
127 ((input#get.(0))#add input#get.(1))
128 | Sub -> self#beam_of_ident n
129 ((input#get.(0))#sub input#get.(1))
130 | Mul -> self#beam_of_ident n
131 ((input#get.(0))#mul input#get.(1))
132 | Div -> self#beam_of_ident n
133 ((input#get.(0))#div input#get.(1))
134 | Mem -> self#beam_of_ident n
135 ((input#get.(0))#mem)
136 | Delay -> self#beam_of_ident n
137 ((input#get.(0))#delay input#get.(1))
138 | Floor -> self#beam_of_ident n
139 ((input#get.(0))#floor)
140 | Int -> self#beam_of_ident n
141 ((input#get.(0))#int)
142 | Sin -> self#beam_of_ident n
143 ((input#get.(0))#sin)
144 | Cos -> self#beam_of_ident n
145 ((input#get.(0))#cos)
146 | Atan -> self#beam_of_ident n
147 ((input#get.(0))#atan)
148 | Atan2 -> self#beam_of_ident n
149 ((input#get.(0))#atan2 input#get.(1))
150 | Sqrt -> self#beam_of_ident n
151 ((input#get.(0))#sqrt)
152 | Rdtable -> self#beam_of_ident n
153 ((input#get.(1))#rdtable input#get.(0) input#get.(2))
154 | Mod -> self#beam_of_ident n
155 ((input#get.(0))#_mod input#get.(1))
156 | Vectorize -> self#beam_of_ident n
157 ((input#get.(0))#vectorize input#get.(1))
158 | Vconcat -> self#beam_of_ident n
159 ((input#get.(0))#vconcat input#get.(1))
160 | Vpick -> self#beam_of_ident n
161 ((input#get.(0))#vpick input#get.(1))
162 | Serialize -> self#beam_of_ident n
163 (input#get.(0))#serialize
164 | Larger -> self#beam_of_ident n
165 ((input#get.(0))#larger input#get.(1))
166 | Smaller -> self#beam_of_ident n
167 ((input#get.(0))#smaller input#get.(1))
168 | Max -> self#beam_of_ident n
169 ((input#get.(0))#max input#get.(1))
170 | Min -> self#beam_of_ident n
171 ((input#get.(0))#min input#get.(1))
172 | Prefix -> self#beam_of_ident n
173 ((input#get.(1))#prefix input#get.(0))
174 | Select2 -> self#beam_of_ident n
175 ((input#get.(0))#select2 input#get.(1) input#get.(2))
176 | Select3 -> self#beam_of_ident n
177 ((input#get.(0))#select3 input#get.(1)
178 input#get.(2) input#get.(3))
181 class virtual process_binary =
182 fun (exp_init : faust_exp) ->
183 let (exp_left, exp_right) =
185 | Par (e1, e2) -> (e1, e2)
186 | Seq (e1, e2) -> (e1, e2)
187 | Split (e1, e2) -> (e1, e2)
188 | Merge (e1, e2) -> (e1, e2)
189 | Rec (e1, e2) -> (e1, e2)
190 | _ -> raise (Process_error "binary process constructor.") in
191 let proc_left = (new proc_factory)#make exp_left in
192 let proc_right = (new proc_factory)#make exp_right in
195 inherit process exp_init
196 method private proc_left = proc_left
197 method private proc_right = proc_right
201 | Par (e1, e2) -> (proc_left#dim)#par proc_right#dim
202 | Seq (e1, e2) -> (proc_left#dim)#seq proc_right#dim
203 | Split (e1, e2) -> (proc_left#dim)#split proc_right#dim
204 | Merge (e1, e2) -> (proc_left#dim)#merge proc_right#dim
205 | Rec (e1, e2) -> (proc_left#dim)#_rec proc_right#dim
206 | _ -> raise (Process_error "binary process constructor.")
210 | Par (e1, e2) -> max proc_left#delay proc_right#delay
211 | Seq (e1, e2) -> proc_left#delay + proc_right#delay
212 | Split (e1, e2) -> proc_left#delay + proc_right#delay
213 | Merge (e1, e2) -> proc_left#delay + proc_right#delay
214 | Rec (e1, e2) -> 1 + proc_left#delay + proc_right#delay
215 | _ -> raise (Process_error "binary process constructor.")
218 and proc_par : faust_exp -> process_type =
219 fun (exp_init : faust_exp) ->
221 inherit process_binary exp_init
222 method eval : beam_type -> beam_type =
223 fun (input : beam_type) ->
224 let (sub_input1, sub_input2) = input#cut self#proc_left#dim#input in
225 let sub_output1 = self#proc_left#eval sub_input1 in
226 let sub_output2 = self#proc_right#eval sub_input2 in
227 sub_output1#append sub_output2
230 and proc_split : faust_exp -> process_type =
231 fun (exp_init : faust_exp) ->
233 inherit process_binary exp_init
234 method eval : beam_type -> beam_type =
235 fun (input : beam_type) ->
236 let mid_output = self#proc_left#eval input in
237 let mid_input = mid_output#matching self#proc_right#dim#input in
238 self#proc_right#eval mid_input
241 and proc_merge : faust_exp -> process_type =
242 fun (exp_init : faust_exp) ->
244 inherit process_binary exp_init
245 method eval : beam_type -> beam_type =
246 fun (input : beam_type) ->
247 let mid_output = self#proc_left#eval input in
248 let mid_input = mid_output#matching self#proc_right#dim#input in
249 self#proc_right#eval mid_input
252 and proc_seq : faust_exp -> process_type =
253 fun (exp_init : faust_exp) ->
255 inherit process_binary exp_init
256 method eval : beam_type -> beam_type =
257 fun (input : beam_type) ->
258 let mid_output = self#proc_left#eval input in
259 self#proc_right#eval mid_output
262 and proc_rec : faust_exp -> process_type =
263 fun (exp_init : faust_exp) ->
265 inherit process_binary exp_init
266 method eval : beam_type -> beam_type =
267 fun (input : beam_type) ->
268 let memory = Hashtbl.create self#delay in
269 let rates = ref (Array.make self#dim#output (new rate 0 1)) in
271 let split : (time -> value_type array) -> (time -> value_type) array =
274 fun beam_func -> fun i -> fun t ->
276 Array.init self#dim#output (get_signal beam_at) in
278 let feedback : (time -> value_type array) -> beam =
280 let signals_at = split beam_at in
281 let delay_by_one = fun s -> fun t -> s (t - 1) in
282 let delay_signal_funcs = Array.map delay_by_one
283 (Array.sub signals_at 0 self#proc_right#dim#input) in
284 new beam (array_map2 (new signal)
285 (Array.sub !rates 0 self#proc_right#dim#input)
286 delay_signal_funcs) in
288 let rec beam_at : time -> value_type array =
291 Array.make self#dim#output (new value Zero)
292 else if Hashtbl.mem memory t then
293 Hashtbl.find memory t
295 let beam_fb_in = feedback beam_at in
296 let beam_fb_out = self#proc_right#eval beam_fb_in in
297 let beam_in = beam_fb_out#append input in
298 let beam_out = self#proc_left#eval beam_in in
299 let values = beam_out#at t in
300 let () = (rates := beam_out#frequency) in
301 let () = Hashtbl.add memory t values in
302 let () = if t - self#delay >= 0 then
303 Hashtbl.remove memory (t - self#delay) else () in
305 new beam (array_map2 (new signal) !rates (split beam_at))
310 method make : faust_exp -> process_type =
311 fun (exp : faust_exp) ->
313 | Const b -> new proc_const exp
314 | Ident s -> new proc_ident exp
315 | Par (e1, e2) -> new proc_par exp
316 | Seq (e1, e2) -> new proc_seq exp
317 | Split (e1, e2) -> new proc_split exp
318 | Merge (e1, e2) -> new proc_merge exp
319 | Rec (e1, e2) -> new proc_rec exp