Bug fixed for stdin and stdout in a different platform.
[Faustine.git] / interpreter / main.ml
index 54c7f1a..94b0ae1 100644 (file)
@@ -6,6 +6,7 @@
 *)
 
 open Aux;;
+open Beam;;
 open Process;;
 open Faustio;;
 
@@ -33,8 +34,9 @@ let size_input = ref 0;;
 let inputs = ref [];;
 let time_max = ref 0xFFFF;;
 let dir_output = ref "";;
-let format_output = ref "wav";;
-let basename_output = ref "output";;
+let format_output = ref "";;
+let basename_output = ref "";;
+let output = ref "";;
 
 let option_usage = "usage: " ^ Sys.argv.(0) 
   ^ " [-f dsp_src] [-i input] [-l length] [--odir dir] [--oformat wav/csv] [--obasename name]";;
@@ -51,65 +53,102 @@ let speclist = [
   ("--obasename", Arg.String (fun s -> basename_output := s), ": set output basename");
   ];;
  
+let file_of_path : string -> string = 
+  fun (path : string) ->
+    let fragments = Str.split (Str.regexp "/") path in
+    let n = List.length fragments in
+    List.nth fragments (n - 1);;  
+
+let valid_input_file : string -> bool = 
+  fun (file : string) ->
+    let fragments = Str.split (Str.regexp "\.") file in
+    let n = List.length fragments in
+    let extension = List.nth fragments (n - 1) in
+    if extension = "csv" || extension = "wav" then true
+    else false;;
+
+let chk_input_path : string -> bool =
+  fun (path : string) ->
+    let file_in = file_of_path path in
+    valid_input_file file_in;;
+
+let stdinput = fun (x : unit) ->
+  let path = Unix.readlink "/proc/self/fd/0" in
+  if chk_input_path path then
+    ( incr size_input;
+      inputs := !inputs @ [path] )
+  else ();;
+
+let chk_output_path : string -> bool =
+  fun (path : string) ->
+    let fragments = Str.split (Str.regexp "/") path in
+    let location = List.nth fragments 0 in
+    if location = "dev" then false
+    else true;;
+
+let stdoutput = fun (x : unit) ->
+  let path = Unix.readlink "/proc/self/fd/1" in
+  if chk_output_path path then output := path
+  else ();;
+
+let stdio = fun (x : unit) -> 
+  stdinput ();
+  stdoutput ();;
 
 let main () = 
-(*
-        let () = print_endline (Scanf.Scanning.name_of_input (Scanf.Scanning.stdin)) in
-       let () = print_endline (Unix.readlink "/proc/self/fd/0") in
-       let () = print_endline (Unix.readlink "/proc/self/fd/1") in
-       let () = print_endline (Unix.readlink "/proc/self/fd/2") in
-*)
+
        let () = Arg.parse speclist option_unknown option_usage in
+        let () = stdio () in 
         let _ = Sys.signal Sys.sigalrm Sys.Signal_ignore in
        let _ = set_GC () in
        let io = new iomanager in
-       let () = io#set !dir_output !format_output !basename_output in
+       let () = io#set !output !dir_output !format_output !basename_output in
 
 
-       let () = print_string("    Faustine -> Reading input ...") in
+       let () = output_string stderr ("\n    Faustine -> Reading input ...") in
        let tic0 = Unix.time () in
-       let input = io#read !inputs in
+       let input : beam = io#read !inputs in
        let toc0 = Unix.time () in
-       let () = print_endline(" Done. (duration: " ^ (string_of_float (toc0 -. tic0)) ^ "s.)") in
+       let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc0 -. tic0)) ^ "s.)\n") in
 
 
-       let () = print_string("    Faustine -> Preprocessing...") in
+       let () = output_string stderr ("    Faustine -> Preprocessing...") in
        let tic1 = Unix.time () in
        let faust_core = Preprocess.preprocess !path_dsp in
        let toc1 = Unix.time () in
-       let () = print_endline(" Done. (duration: " ^ (string_of_float (toc1 -. tic1)) ^ "s.)") in
+       let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc1 -. tic1)) ^ "s.)\n") in
 
 
-       let () = print_string("    Faustine -> Constructing process...") in
+       let () = output_string stderr ("    Faustine -> Constructing process...") in
        let tic2 = Unix.time () in
        let faust_exp = exp_of_string faust_core in
        let proc = (new proc_factory)#make faust_exp in
        let toc2 = Unix.time () in
-       let () = print_endline(" Done. (duration: " ^ (string_of_float (toc2 -. tic2)) ^ "s.)") in
+       let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc2 -. tic2)) ^ "s.)\n") in
 
 
-       let () = print_string("    Faustine -> Constructing signals...") in
+       let () = output_string stderr ("    Faustine -> Constructing signals...") in
        let tic3 = Unix.time () in
-       let output = proc#eval input in
+       let output : beam = proc#eval input in
        let toc3 = Unix.time () in
-       let () = print_endline(" Done. (duration: " ^ (string_of_float (toc3 -. tic3)) ^ "s.)") in
+       let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc3 -. tic3)) ^ "s.)\n") in
 
 
-       let () = print_string("    Faustine -> Evaluating...") in
+       let () = output_string stderr ("    Faustine -> Evaluating...") in
        let tic4 = Unix.time () in
        let data = output#output !time_max in
        let rates = output#frequency in
        let toc4 = Unix.time () in
-       let () = print_endline(" Done. (duration: " ^ (string_of_float (toc4 -. tic4)) ^ "s.)") in
+       let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc4 -. tic4)) ^ "s.)\n") in
 
 
-       let () = print_string("    Faustine -> Writing output...") in
+       let () = output_string stderr ("    Faustine -> Writing output...") in
        let tic5 = Unix.time () in
        let output_paths = io#write rates data in
        let toc5 = Unix.time () in
-       let () = print_endline(" Done. (duration: " ^ (string_of_float (toc5 -. tic5)) ^ "s.)") in
+       let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc5 -. tic5)) ^ "s.)\n") in
 
-       let _ = Array.map print_endline 
+       let _ = Array.map (output_string stderr)  
            (Array.map decorate output_paths) in
        ();;