Refactoring of process classes in faustexp.ml.
[Faustine.git] / interpretor / faustexp.ml
index 684eb75..d0b5ec0 100644 (file)
@@ -6,21 +6,24 @@
 *)
 
 open Types;;
+open Basic;;
+open Symbol;;
 open Value;;
 open Signal;;
 open Beam;;
 
 exception NotYetDone;;
 exception Dimension_error of string;;
+exception Process_error of string;;
 
 class dimension : int * int -> dimension_type = 
   fun (init : int * int) -> 
     object (self)
-      val dim_input = fst init
-      val dim_output = snd init
+      val _input = fst init
+      val _output = snd init
 
-      method input = dim_input
-      method output = dim_output
+      method input = _input
+      method output = _output
 
       method par : dimension_type -> dimension_type = 
        fun dim -> 
@@ -52,6 +55,7 @@ class dimension : int * int -> dimension_type =
          else raise (Dimension_error "rec dimension not matched.")
     end;;
 
+(*
 class process : faust_exp -> process_type = 
   fun (exp_init : faust_exp) ->
   object (self)
@@ -86,58 +90,270 @@ class process : faust_exp -> process_type =
       method to_string = "NotYetDone"
       method virtual evaluate : beam_type -> beam_type
   end;;
-
+*)
 
 class proc_const : faust_exp -> process_type = 
   fun (exp_init : faust_exp) ->
     object (self)
-      val exp = exp_init
-      val dim = 
-      method evaluate = fun b1 ->
-       
-      end;;
-
-
-class exp_ident = 
-  object
-      inherit expression
-
+      val _exp = exp_init
+      val _dim = new dimension (0,1)
+      val _delay = 0
+      val _const = 
+       match exp_init with
+       | Const b -> b
+       | _ -> raise (Process_error "const process constructor.")
+
+      method exp = _exp
+      method dim = _dim
+      method delay = _delay
+      method private const = _const
+
+      method eval : beam_type -> beam_type = 
+       fun (input : beam_type) ->
+         if input#get = [||] then
+           new beam [| new signal 0 (fun t -> new value self#const)|]
+         else
+           raise (Process_error "proc_const accepts no input.")
       end;;
 
 
-class exp_par = 
-  object
-     inherit expression
-
-      end;;
-
-
-class exp_split = 
-  object
-     inherit expression
-
+class proc_ident : faust_exp -> process_type = 
+  fun (exp_init : faust_exp) ->
+    object (self)
+      val _exp = exp_init
+      val _symbol = 
+       match exp_init with
+       | Ident s -> s
+       | _ -> raise (Process_error "ident process constructor.")
+
+      val _dim = new dimension (dimension_of_symbol self#symb)
+      val _delay = delay_of_symbol _symbol      
+
+      method exp = _exp
+      method dim = _dim
+      method delay = _delay
+      method symb = _symbol
+
+      method private beam_of_ident : int -> signal_type -> beam_type = 
+       fun (n : int) ->
+         fun (s, signal_type) ->
+           if n = (self#dim)#input then 
+             new beam [|s|]
+           else raise (Process_error ("Ident " ^ string_of_symbol self#symb))
+
+      method eval : beam_type -> beam_type = 
+       fun (input : beam_type) ->
+         let n = Array.length input#get in
+         match self#symb with
+         | Pass -> self#beam_of_ident n input#get.(0)
+         | Stop -> if n = 1 then new beam [||] 
+                   else raise (Process_error "Ident !")
+         | Add -> self#beam_of_ident n ((input#get.(0))#add input#get.(1))
+         | Sub -> self#beam_of_ident n ((input#get.(0))#sub input#get.(1))
+         | Mul -> self#beam_of_ident n ((input#get.(0))#mul input#get.(1))
+         | Div -> self#beam_of_ident n ((input#get.(0))#div input#get.(1))
+         | Mem -> self#beam_of_ident n ((input#get.(0))#mem)
+         | Delay -> self#beam_of_ident n ((input#get.(0))#delay input#get.(1))
+         | Floor -> self#beam_of_ident n ((input#get.(0))#floor)
+         | Int -> self#beam_of_ident n ((input#get.(0))#int)
+         | Sin -> self#beam_of_ident n ((input#get.(0))#sin)
+         | Cos -> self#beam_of_ident n ((input#get.(0))#cos)
+         | Atan -> self#beam_of_ident n ((input#get.(0))#atan)
+         | Atan2 -> self#beam_of_ident n ((input#get.(0))#atan2 input#get.(1))
+         | Sqrt -> self#beam_of_ident n ((input#get.(0))#sqrt)
+         | Rdtable -> self#beam_of_ident n 
+               ((input#get.(1))#rdtable input#get.(0) input#get.(2))
+         | Mod -> self#beam_of_ident n 
+               ((input#get.(0))#_mod input#get.(1))
+         | Vectorize -> self#beam_of_ident n 
+               ((input#get.(0))#vectorzie input#get.(1))
+         | Vconcat -> self#beam_of_ident n 
+               ((input#get.(0))#vconcat input#get.(1))
+         | Vpick -> self#beam_of_ident n 
+               ((input#get.(0))#vpick input#get.(1))
+         | Serialize -> self#beam_of_ident n 
+               (input#get.(0))#serialize
+         | Larger -> self#beam_of_ident n 
+               ((input#get.(0))#larger input#get.(1))
+         | Smaller -> self#beam_of_ident n 
+               ((input#get.(0))#smaller input#get.(1))
+         | Prefix -> self#beam_of_ident n 
+               ((input#get.(1))#prefix input#get.(0))
+         | Selec2 -> self#beam_of_ident n 
+               ((input#get.(0))#select2 input#get.(1) input#get.(2))
+         | Select3 -> self#beam_of_ident n 
+               ((input#get.(0))#select3 input#get.(1) 
+                  input#get.(2) input#get.(3))
       end;;
 
 
-class exp_merge = 
-  object
-     inherit expression
-
-      end;;
+class proc_par : faust_exp -> process_type = 
+  fun (exp_init : faust_exp) ->
+    object (self)
+      val _exp = exp_init
+      val _exp_left = 
+       match exp_init with
+       | Par (e1, e2) -> e1
+       | _ -> raise (Process_error "par process constructor.")
+      val _exp_right = 
+       match exp_init with
+       | Par (e1, e2) -> e2
+       | _ -> raise (Process_error "par process constructor.")
+
+      val proc_left = (new proc_factory)#make _exp_left
+      val proc_right = (new proc_factory)#make _exp_right
+
+      val _dim = (proc_left#dim)#par proc_right#dim
+      val _delay = max proc_left#delay proc_right#delay
+
+      method exp = _exp
+      method dim = _dim
+      method delay = _delay
+
+      method eval : beam_type -> beam_type = 
+       fun (input : beam_type) ->
+         let (sub_input1, sub_input2) = input#cut proc_left#dim#input in
+         let sub_output1 = proc_left#eval sub_input1 in
+         let sub_output2 = proc_right#eval sub_input2 in
+         sub_output1#append sub_output2
+      end
+
+and proc_split : faust_exp -> process_type =
+  fun (exp_init : faust_exp) ->
+    object (self)
+      val _exp = exp_init
+      val _exp_left = 
+       match exp_init with
+       | Split (e1, e2) -> e1
+       | _ -> raise (Process_error "par process constructor.")
+      val _exp_right = 
+       match exp_init with
+       | Split (e1, e2) -> e2
+       | _ -> raise (Process_error "par process constructor.")
+
+      val proc_left = (new proc_factory)#make _exp_left
+      val proc_right = (new proc_factory)#make _exp_right
+
+      val _dim = (proc_left#dim)#split proc_right#dim
+      val _delay = proc_left#delay + proc_right#delay
+
+      method exp = _exp
+      method dim = _dim
+      method delay = _delay
+
+      method eval : beam_type -> beam_type = 
+       fun (input : beam_type) ->
+         let mid_output = proc_left#eval input in
+         let mid_input = mid_output#matching proc_right#dim#input in
+         proc_right#eval mid_input
+      end
+
+
+and proc_merge : faust_exp -> process_type =
+  fun (exp_init : faust_exp) -> 
+    object (self)
+      val _exp = exp_init
+      val _exp_left = 
+       match exp_init with
+       | Merge (e1, e2) -> e1
+       | _ -> raise (Process_error "merge process constructor.")
+      val _exp_right = 
+       match exp_init with
+       | Merge (e1, e2) -> e2
+       | _ -> raise (Process_error "merge process constructor.")
+
+      val proc_left = (new proc_factory)#make _exp_left
+      val proc_right = (new proc_factory)#make _exp_right
+
+      val _dim = (proc_left#dim)#merge proc_right#dim
+      val _delay = proc_left#delay + proc_right#delay
+
+      method exp = _exp
+      method dim = _dim
+      method delay = _delay
+
+      method eval : beam_type -> beam_type = 
+       fun (input : beam_type) ->
+         let mid_output = proc_left#eval input in
+         let mid_input = mid_output#matching proc_right#dim#input in
+         proc_right#eval mid_input
+
+      end
+
+and proc_seq : faust_exp -> process_type =
+  fun (exp_init : faust_exp) -> 
+    object (self)
+      val _exp = exp_init
+      val _exp_left = 
+       match exp_init with
+       | Seq (e1, e2) -> e1
+       | _ -> raise (Process_error "seq process constructor.")
+      val _exp_right = 
+       match exp_init with
+       | Seq (e1, e2) -> e2
+       | _ -> raise (Process_error "seq process constructor.")
+
+      val proc_left = (new proc_factory)#make _exp_left
+      val proc_right = (new proc_factory)#make _exp_right
+
+      val _dim = (proc_left#dim)#seq proc_right#dim
+      val _delay = proc_left#delay + proc_right#delay
+
+      method exp = _exp
+      method dim = _dim
+      method delay = _delay
+
+      method eval : beam_type -> beam_type = 
+       fun (input : beam_type) ->
+         let mid_output = proc_left#eval input in
+         proc_right#eval mid_output
+      end
+
+and proc_rec : faust_exp -> process_type =
+  fun (exp_init : faust_exp) -> 
+  object (self)
+    val _exp = exp_init
+    val _exp_left = 
+      match exp_init with
+      | Rec (e1, e2) -> e1
+      | _ -> raise (Process_error "rec process constructor.")
+    val _exp_right = 
+      match exp_init with
+      | Rec (e1, e2) -> e2
+      | _ -> raise (Process_error "rec process constructor.")
+           
+    val proc_left = (new proc_factory)#make _exp_left
+    val proc_right = (new proc_factory)#make _exp_right
 
-class exp_seq = 
-  object
-     inherit expression
+    val _dim = (proc_left#dim)#_rec proc_right#dim
+    val _delay = proc_left#delay
+       
+    method exp = _exp
+    method dim = _dim
+    method delay = _delay
+       
+    method eval : beam_type -> beam_type = 
+      fun (input : beam_type) ->
+       let mid_output = proc_left#eval input in
+       proc_right#eval mid_output
+         
 
-      end;;
+  end
 
-class exp_rec = 
+and proc_factory = 
   object
-     inherit expression
-
-      end;;
+      method make : faust_exp -> process_type = 
+       fun (exp : faust_exp) ->
+         match exp with
+         | Const b -> new proc_const exp
+         | Ident s -> new proc_ident exp
+         | Par (e1, e2) -> new proc_par exp
+         | Seq (e1, e2) -> new proc_seq exp
+         | Split (e1, e2) -> new proc_split exp
+         | Merge (e1, e2) -> new proc_merge exp
+         | Rec (e1, e2) -> new proc_rec exp
+  end;;
 
-*)