2f102f35b283eb36744047d8edd20f05da9081ea
[Faustine.git] / interpreter / main.ml
1 (**
2 Module: Interpreter
3 Description: Input wave -> interpretation -> output wave
4 @author WANG Haisheng
5 Created: 15/05/2013 Modified: 14/08/2013
6 *)
7
8 open Aux;;
9 open Beam;;
10 open Process;;
11 open Faustio;;
12
13 exception Missing_Expression;;
14
15 let version = "Faustine: 0.0.1";;
16
17 let set_GC () =
18 let _ = Gc.set { (Gc.get())
19 with Gc.minor_heap_size = 0xFFFFFF } in
20 let _ = Gc.set { (Gc.get())
21 with Gc.major_heap_increment = 0xFFFFFF } in
22 let _ = Gc.set { (Gc.get())
23 with Gc.space_overhead = 100 } in
24 let _ = Gc.set { (Gc.get())
25 with Gc.max_overhead = 0xFFF } in
26 let _ = Gc.set { (Gc.get())
27 with Gc.stack_limit = 0xFFFFF } in
28 let _ = Gc.set { (Gc.get())
29 with Gc.allocation_policy = 1 } in
30 () ;;
31
32 let path_dsp = ref "";;
33 let size_input = ref 0;;
34 let inputs = ref [];;
35 let time_max = ref 0xFFFF;;
36 let dir_output = ref "";;
37 let format_output = ref "csv";;
38 let basename_output = ref "output";;
39 let output = ref "";;
40
41 let option_usage = "usage: " ^ Sys.argv.(0)
42 ^ " [-f dsp_src] [-i input] [-l length] [--odir dir] [--oformat wav/csv] [--obasename name]";;
43
44 let option_unknown =
45 fun x -> raise (Arg.Bad ("Bad argument : " ^ x))
46
47 let speclist = [
48 ("-f", Arg.String (fun s -> path_dsp := s), ": faust .dsp source file");
49 ("-i", Arg.String (fun s -> incr size_input; inputs := !inputs @ [s]), ": set input wave file");
50 ("-l", Arg.Int (fun i -> time_max := i), ": maximun number of output samples");
51 ("--odir", Arg.String (fun s -> dir_output := s), ": set output directory");
52 ("--oformat", Arg.String (fun s -> format_output := s), ": set output format");
53 ("--obasename", Arg.String (fun s -> basename_output := s), ": set output basename");
54 ];;
55
56 let file_of_path : string -> string =
57 fun (path : string) ->
58 let fragments = Str.split (Str.regexp "/") path in
59 let n = List.length fragments in
60 List.nth fragments (n - 1);;
61
62 let stdinput = fun (x : unit) ->
63 let path = Unix.readlink "/proc/self/fd/0" in
64 if path <> "/dev/pts/4" then
65 ( incr size_input;
66 inputs := !inputs @ [path] )
67 else ();;
68
69 let stdoutput = fun (x : unit) ->
70 let path = Unix.readlink "/proc/self/fd/1" in
71 if path <> "/dev/pts/4" then output := path
72 else ();;
73
74 let stdio = fun (x : unit) ->
75 stdinput ();
76 stdoutput ();;
77
78 let main () =
79
80 (*
81 let () = print_endline (Unix.readlink "/proc/self/fd/0") in
82 let () = print_endline (Unix.readlink "/proc/self/fd/1") in
83 let () = print_endline (Unix.readlink "/proc/self/fd/2") in
84 *)
85 let () = stdio () in
86 let () = Arg.parse speclist option_unknown option_usage in
87 let _ = Sys.signal Sys.sigalrm Sys.Signal_ignore in
88 let _ = set_GC () in
89 let io = new iomanager in
90 let () = io#set !output !dir_output !format_output !basename_output in
91
92
93 let () = output_string stderr (" Faustine -> Reading input ...") in
94 let tic0 = Unix.time () in
95 let input : beam = io#read !inputs in
96 let toc0 = Unix.time () in
97 let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc0 -. tic0)) ^ "s.)\n") in
98
99
100 let () = output_string stderr (" Faustine -> Preprocessing...") in
101 let tic1 = Unix.time () in
102 let faust_core = Preprocess.preprocess !path_dsp in
103 let toc1 = Unix.time () in
104 let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc1 -. tic1)) ^ "s.)\n") in
105
106
107 let () = output_string stderr (" Faustine -> Constructing process...") in
108 let tic2 = Unix.time () in
109 let faust_exp = exp_of_string faust_core in
110 let proc = (new proc_factory)#make faust_exp in
111 let toc2 = Unix.time () in
112 let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc2 -. tic2)) ^ "s.)\n") in
113
114
115 let () = output_string stderr (" Faustine -> Constructing signals...") in
116 let tic3 = Unix.time () in
117 let output : beam = proc#eval input in
118 let toc3 = Unix.time () in
119 let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc3 -. tic3)) ^ "s.)\n") in
120
121
122 let () = output_string stderr (" Faustine -> Evaluating...") in
123 let tic4 = Unix.time () in
124 let data = output#output !time_max in
125 let rates = output#frequency in
126 let toc4 = Unix.time () in
127 let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc4 -. tic4)) ^ "s.)\n") in
128
129
130 let () = output_string stderr (" Faustine -> Writing output...") in
131 let tic5 = Unix.time () in
132 let output_paths = io#write rates data in
133 let toc5 = Unix.time () in
134 let () = output_string stderr (" Done. (duration: " ^ (string_of_float (toc5 -. tic5)) ^ "s.)\n") in
135
136 let _ = Array.map (output_string stderr)
137 (Array.map decorate output_paths) in
138 ();;
139
140 main();;
141