Refactoring of rec process "~" in faustexp.ml.
[Faustine.git] / interpretor / faustexp.ml
1 (**
2 Module: Faustexp
3 Description: Faust expression evaluation
4 @author WANG Haisheng
5 Created: 03/06/2013 Modified: 04/08/2013
6 *)
7
8 open Types;;
9 open Basic;;
10 open Symbol;;
11 open Value;;
12 open Signal;;
13 open Beam;;
14
15 exception NotYetDone;;
16 exception Dimension_error of string;;
17 exception Process_error of string;;
18
19
20 (* PARSER *)
21
22 let exp_of_string s = (Parser.main Lexer.token (Lexing.from_string s));;
23
24
25 class dimension : int * int -> dimension_type =
26 fun (init : int * int) ->
27 object (self)
28 val _input = fst init
29 val _output = snd init
30
31 method input = _input
32 method output = _output
33
34 method par : dimension_type -> dimension_type =
35 fun dim ->
36 new dimension
37 ((self#input + dim#input), (self#output + dim#output))
38
39 method seq : dimension_type -> dimension_type =
40 fun dim ->
41 if self#output = dim#input then
42 new dimension (self#input, dim#output)
43 else raise (Dimension_error "seq dimension not matched.")
44
45 method split : dimension_type -> dimension_type =
46 fun dim ->
47 if dim#input mod self#output = 0 then
48 new dimension (self#input, dim#output)
49 else raise (Dimension_error "split dimension not matched.")
50
51 method merge : dimension_type -> dimension_type =
52 fun dim ->
53 if self#output mod dim#input = 0 then
54 new dimension (self#input, dim#output)
55 else raise (Dimension_error "merge dimension not matched.")
56
57 method _rec : dimension_type -> dimension_type =
58 fun dim ->
59 if self#output >= dim#input && self#input >= dim#output then
60 new dimension (self#input - dim#output, self#output)
61 else raise (Dimension_error "rec dimension not matched.")
62 end;;
63
64 class virtual process =
65 fun (exp_init : faust_exp) ->
66 object
67 val _exp = exp_init
68 val virtual _dim : dimension_type
69 val virtual _delay : int
70 method exp = _exp
71 method dim = _dim
72 method delay = _delay
73 method virtual eval : beam_type -> beam_type
74 end
75
76 class proc_const : faust_exp -> process_type =
77 fun (exp_init : faust_exp) ->
78 let _const =
79 match exp_init with
80 | Const b -> b
81 | _ -> raise (Process_error "const process constructor.") in
82
83 object (self)
84 inherit process exp_init
85 val _dim = new dimension (0,1)
86 val _delay = 0
87 method private const = _const
88 method eval : beam_type -> beam_type =
89 fun (input : beam_type) ->
90 if input#get = [||] then
91 new beam [| new signal 0 (fun t -> new value self#const)|]
92 else
93 raise (Process_error "proc_const accepts no input.")
94 end;;
95
96
97 class proc_ident : faust_exp -> process_type =
98 fun (exp_init : faust_exp) ->
99 let _symbol =
100 match exp_init with
101 | Ident s -> s
102 | _ -> raise (Process_error "ident process constructor.") in
103
104 object (self)
105 inherit process exp_init
106 val _dim = new dimension (dimension_of_symbol _symbol)
107 val _delay = delay_of_symbol _symbol
108 method private symb = _symbol
109
110 method private beam_of_ident : int -> signal_type -> beam_type =
111 fun (n : int) ->
112 fun (s : signal_type) ->
113 if n = (self#dim)#input then
114 new beam [|s|]
115 else raise (Process_error ("Ident " ^ string_of_symbol self#symb))
116
117 method eval : beam_type -> beam_type =
118 fun (input : beam_type) ->
119 let n = Array.length input#get in
120 match self#symb with
121 | Pass -> self#beam_of_ident n input#get.(0)
122 | Stop -> if n = 1 then new beam [||]
123 else raise (Process_error "Ident !")
124 | Add -> self#beam_of_ident n
125 ((input#get.(0))#add input#get.(1))
126 | Sub -> self#beam_of_ident n
127 ((input#get.(0))#sub input#get.(1))
128 | Mul -> self#beam_of_ident n
129 ((input#get.(0))#mul input#get.(1))
130 | Div -> self#beam_of_ident n
131 ((input#get.(0))#div input#get.(1))
132 | Mem -> self#beam_of_ident n
133 ((input#get.(0))#mem)
134 | Delay -> self#beam_of_ident n
135 ((input#get.(0))#delay input#get.(1))
136 | Floor -> self#beam_of_ident n
137 ((input#get.(0))#floor)
138 | Int -> self#beam_of_ident n
139 ((input#get.(0))#int)
140 | Sin -> self#beam_of_ident n
141 ((input#get.(0))#sin)
142 | Cos -> self#beam_of_ident n
143 ((input#get.(0))#cos)
144 | Atan -> self#beam_of_ident n
145 ((input#get.(0))#atan)
146 | Atan2 -> self#beam_of_ident n
147 ((input#get.(0))#atan2 input#get.(1))
148 | Sqrt -> self#beam_of_ident n
149 ((input#get.(0))#sqrt)
150 | Rdtable -> self#beam_of_ident n
151 ((input#get.(1))#rdtable input#get.(0) input#get.(2))
152 | Mod -> self#beam_of_ident n
153 ((input#get.(0))#_mod input#get.(1))
154 | Vectorize -> self#beam_of_ident n
155 ((input#get.(0))#vectorize input#get.(1))
156 | Vconcat -> self#beam_of_ident n
157 ((input#get.(0))#vconcat input#get.(1))
158 | Vpick -> self#beam_of_ident n
159 ((input#get.(0))#vpick input#get.(1))
160 | Serialize -> self#beam_of_ident n
161 (input#get.(0))#serialize
162 | Larger -> self#beam_of_ident n
163 ((input#get.(0))#larger input#get.(1))
164 | Smaller -> self#beam_of_ident n
165 ((input#get.(0))#smaller input#get.(1))
166 | Prefix -> self#beam_of_ident n
167 ((input#get.(1))#prefix input#get.(0))
168 | Select2 -> self#beam_of_ident n
169 ((input#get.(0))#select2 input#get.(1) input#get.(2))
170 | Select3 -> self#beam_of_ident n
171 ((input#get.(0))#select3 input#get.(1)
172 input#get.(2) input#get.(3))
173 end;;
174
175 class virtual process_binary =
176 fun (exp_init : faust_exp) ->
177 let (exp_left, exp_right) =
178 match exp_init with
179 | Par (e1, e2) -> (e1, e2)
180 | Seq (e1, e2) -> (e1, e2)
181 | Split (e1, e2) -> (e1, e2)
182 | Merge (e1, e2) -> (e1, e2)
183 | Rec (e1, e2) -> (e1, e2)
184 | _ -> raise (Process_error "binary process constructor.") in
185 let proc_left = (new proc_factory)#make exp_left in
186 let proc_right = (new proc_factory)#make exp_right in
187
188 object
189 inherit process exp_init
190 method private proc_left = proc_left
191 method private proc_right = proc_right
192
193 val _dim =
194 match exp_init with
195 | Par (e1, e2) -> (proc_left#dim)#par proc_right#dim
196 | Seq (e1, e2) -> (proc_left#dim)#seq proc_right#dim
197 | Split (e1, e2) -> (proc_left#dim)#split proc_right#dim
198 | Merge (e1, e2) -> (proc_left#dim)#merge proc_right#dim
199 | Rec (e1, e2) -> (proc_left#dim)#_rec proc_right#dim
200 | _ -> raise (Process_error "binary process constructor.")
201
202 val _delay =
203 match exp_init with
204 | Par (e1, e2) -> max proc_left#delay proc_right#delay
205 | Seq (e1, e2) -> proc_left#delay + proc_right#delay
206 | Split (e1, e2) -> proc_left#delay + proc_right#delay
207 | Merge (e1, e2) -> proc_left#delay + proc_right#delay
208 | Rec (e1, e2) -> 1 + proc_left#delay + proc_right#delay
209 | _ -> raise (Process_error "binary process constructor.")
210 end
211
212 and proc_par : faust_exp -> process_type =
213 fun (exp_init : faust_exp) ->
214 object (self)
215 inherit process_binary exp_init
216 method eval : beam_type -> beam_type =
217 fun (input : beam_type) ->
218 let (sub_input1, sub_input2) = input#cut self#proc_left#dim#input in
219 let sub_output1 = self#proc_left#eval sub_input1 in
220 let sub_output2 = self#proc_right#eval sub_input2 in
221 sub_output1#append sub_output2
222 end
223
224 and proc_split : faust_exp -> process_type =
225 fun (exp_init : faust_exp) ->
226 object (self)
227 inherit process_binary exp_init
228 method eval : beam_type -> beam_type =
229 fun (input : beam_type) ->
230 let mid_output = self#proc_left#eval input in
231 let mid_input = mid_output#matching self#proc_right#dim#input in
232 self#proc_right#eval mid_input
233 end
234
235 and proc_merge : faust_exp -> process_type =
236 fun (exp_init : faust_exp) ->
237 object (self)
238 inherit process_binary exp_init
239 method eval : beam_type -> beam_type =
240 fun (input : beam_type) ->
241 let mid_output = self#proc_left#eval input in
242 let mid_input = mid_output#matching self#proc_right#dim#input in
243 self#proc_right#eval mid_input
244 end
245
246 and proc_seq : faust_exp -> process_type =
247 fun (exp_init : faust_exp) ->
248 object (self)
249 inherit process_binary exp_init
250 method eval : beam_type -> beam_type =
251 fun (input : beam_type) ->
252 let mid_output = self#proc_left#eval input in
253 self#proc_right#eval mid_output
254 end
255
256 and proc_rec : faust_exp -> process_type =
257 fun (exp_init : faust_exp) ->
258 object (self)
259 inherit process_binary exp_init
260 method eval : beam_type -> beam_type =
261 fun (input : beam_type) ->
262 let memory = Hashtbl.create self#delay in
263 let rates = ref (Array.make self#dim#output 0) in
264
265 let split : (time -> value_type array) -> (time -> value_type) array =
266 fun beam_at ->
267 let get_signal =
268 fun beam_func -> fun i -> fun t ->
269 (beam_func t).(i) in
270 Array.init self#dim#output (get_signal beam_at) in
271
272 let array_map2 = fun f -> fun a -> fun b ->
273 let n1 = Array.length a in
274 let n2 = Array.length b in
275 if n1 = n2 then Array.init n1 (fun i -> f a.(i) b.(i))
276 else raise (Process_error "Array.map2 size not matched.") in
277
278 let feedback : (time -> value_type array) -> beam =
279 fun beam_at ->
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
287
288 let rec beam_at : time -> value_type array =
289 fun (t : time) ->
290 if t < 0 then
291 Array.make self#dim#output (new value Zero)
292 else if Hashtbl.mem memory t then
293 Hashtbl.find memory t
294 else
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
304 values in
305 new beam (array_map2 (new signal) !rates (split beam_at))
306 end
307
308 and proc_factory =
309 object
310 method make : faust_exp -> process_type =
311 fun (exp : faust_exp) ->
312 match exp with
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
320 end;;