Test commit into the README file.
[Faustine.git] / interpretor / main.ml
1 (**
2 Module: Interpreter
3 Description: Input wave -> interpretation -> output wave
4 @author WANG Haisheng
5 Created: 15/05/2013 Modified: 04/06/2013
6 *)
7
8 open Faustexp;;
9 open Interpreter;;
10
11 (* EXCEPTIONS *)
12
13 (** Exception raised when no string expression of faust process is typed in console.*)
14 exception Missing_Expression;;
15
16
17
18 (* MACRO *)
19
20 (** Macro constants of input/output route.*)
21 type io_macro =
22 | Input_Route_string
23 | Output_Route_string
24 | Dsp_Route_string;;
25
26 (** val io_macro_to_string : io_macro -> string.*)
27 let io_macro_to_string m = match m with
28 | Input_Route_string -> ""
29 | Output_Route_string -> "../output_sounds/"
30 | Dsp_Route_string -> "";;
31
32
33 (** val set_GC : unit -> unit *)
34 let set_GC () =
35 let _ = Gc.set { (Gc.get()) with Gc.minor_heap_size = 0xFFFFFF } in
36 let _ = Gc.set { (Gc.get()) with Gc.major_heap_increment = 0xFFFFFF } in
37 let _ = Gc.set { (Gc.get()) with Gc.space_overhead = 100 } in
38 let _ = Gc.set { (Gc.get()) with Gc.max_overhead = 0xFFFFF } in
39 let _ = Gc.set { (Gc.get()) with Gc.stack_limit = 0xFFFFF } in
40 let _ = Gc.set { (Gc.get()) with Gc.allocation_policy = 0 } in
41 () ;;
42
43 (* INPUT && OUTPUT*)
44
45 (** val read_input_wave : string array -> int list * float array list
46 [read_input_wave argv] gets information from command line,
47 returns sample rate list and data (in form of float array) list.
48 *)
49 let read_input_wave = fun argv ->
50 let n_input = (Array.length argv) - 4 in
51 if n_input < 0 then
52 raise Missing_Expression
53 else if n_input = 0 then
54 ([], [])
55 else
56 (* open wave file *)
57 let file_string_array = Array.sub argv 4 n_input in
58 let make_chemin s = io_macro_to_string Input_Route_string ^ s in
59 let file_chemin_string_array = Array.map make_chemin file_string_array in
60 let file_array = Array.map Sndfile.openfile file_chemin_string_array in
61 let file_list = Array.to_list file_array in
62
63 (* prepare data container *)
64 let frames_array = Array.map Int64.to_int (Array.map Sndfile.frames file_array) in
65 let create_data_array num = Array.create num 1. in
66 let data_float_array_array = Array.map create_data_array frames_array in
67 let data_float_array_list = Array.to_list data_float_array_array in
68
69 (* read sample rates and data *)
70 let rate_list = List.map Sndfile.samplerate file_list in
71 let _ = List.map2 Sndfile.read file_list data_float_array_list in
72 let _ = List.map Sndfile.close file_list in
73 (rate_list, data_float_array_list);;
74
75
76 (** val write_output_wave : int list -> int list -> float_array_list -> unit.
77 [write_output_wave channel_numbers sample_rates data]
78 *)
79 let write_output_wave = fun channel_int_list -> fun rate_int_list -> fun data_float_array_list ->
80 let () = print_string(" Faustine -> Writing wave files...") in
81 let tic = Sys.time () in
82
83 (* make output wave file names : output0, output1, ... *)
84 let n_output = List.length data_float_array_list in
85 let n_array = Array.init n_output (fun n -> n) in
86 let make_file_name i = "output" ^ (string_of_int i) ^ ".wav" in
87
88 (* make output wave file routes *)
89 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
90 let file_name_string_array = Array.map make_file_name n_array in
91 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
92 let file_chemin_string_list = Array.to_list file_chemin_string_array in
93
94 (* open files for writing with respects to channel numbers and sample rates *)
95 let file_format = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
96 let openwr = fun file_chemin_string -> fun channel -> fun rate ->
97 Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, rate) file_chemin_string in
98 let openwr_fun_list = fun fl -> fun cl -> fun rl -> fun i ->
99 openwr (List.nth fl i) (List.nth cl i) (List.nth rl i) in
100 let output_file_list = List.map
101 (openwr_fun_list file_chemin_string_list channel_int_list rate_int_list)
102 (Array.to_list (Array.init n_output (fun n -> n))) in
103
104 (* write data into files *)
105 let _ = List.map2 Sndfile.write output_file_list data_float_array_list in
106 let _ = List.map Sndfile.close output_file_list in
107 let toc = Sys.time () in
108 print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");;
109
110
111 let csvread = fun (ic : in_channel) ->
112 let string_list = ref [] in
113 try
114 while true do
115 string_list := !string_list @ [(input_line ic)]
116 done;
117 [||]
118 with End_of_file ->
119 (*let () = print_endline(List.nth !string_list 0) in*)
120 Array.of_list (List.map float_of_string !string_list);;
121
122 let read_input_csv = fun argv ->
123 let n_input = (Array.length argv) - 4 in
124 if n_input < 0 then
125 raise Missing_Expression
126 else if n_input = 0 then
127 ([], [])
128 else
129 (* open csv file *)
130 let file_string_array = Array.sub argv 4 n_input in
131 let make_chemin s = io_macro_to_string Input_Route_string ^ s in
132 let file_chemin_string_array = Array.map make_chemin file_string_array in
133 let file_array = Array.map open_in file_chemin_string_array in
134 let file_list = Array.to_list file_array in
135
136 (* read sample rates and data *)
137 let rate_list = Array.to_list (Array.create n_input 0) in
138 let data_float_array_list = List.map csvread file_list in
139 let _ = List.map close_in file_list in
140 (rate_list, data_float_array_list);;
141
142
143 let write_output_csv = fun channel_int_list -> fun data_float_array_list ->
144 let () = print_string(" Faustine -> Writing csv files...") in
145 let tic = Sys.time () in
146
147 (* make output txt file names : output0, output1, ... *)
148 let n_output = List.length data_float_array_list in
149 let n_array = Array.init n_output (fun n -> n) in
150 let make_file_name i = "output" ^ (string_of_int i) ^ ".csv" in
151
152 (* make output wave file routes *)
153 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
154 let file_name_string_array = Array.map make_file_name n_array in
155 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
156 let file_chemin_string_list = Array.to_list file_chemin_string_array in
157
158 (* open output channels *)
159 let file_list = List.map open_out file_chemin_string_list in
160 let data_string_array_list = List.map (Array.map string_of_float) data_float_array_list in
161 let array_to_string = fun data_string_array -> fun channel_int ->
162 let data_length = Array.length data_string_array in
163 let rec to_string_rec =
164 fun data -> fun channel -> fun n -> fun i -> fun column ->
165 if i < n then
166 (
167 let element = data.(i) in
168 if column < (channel - 1) then
169 element ^ "," ^ (to_string_rec data channel n (i + 1) (column + 1))
170 else if column = (channel - 1) then
171 element ^ "\n" ^ (to_string_rec data channel n (i + 1) 0)
172 else raise (Invalid_argument "write_output_txt.")
173 )
174 else "" in
175 to_string_rec data_string_array channel_int data_length 0 0 in
176
177 let data_string_list = List.map2 array_to_string data_string_array_list channel_int_list in
178 let _ = List.map2 output_string file_list data_string_list in
179 let _ = List.map close_out file_list in
180 let toc = Sys.time () in
181 print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");;
182
183
184 let read_input = fun option_in -> fun argv ->
185 if option_in = "-wav" then
186 read_input_wave argv
187 else if option_in = "-csv" then
188 read_input_csv argv
189 else raise (Invalid_argument ("Unkown option: " ^ option_in));;
190
191
192 (* MAIN *)
193
194 (** val main : unit -> unit
195 main function reads console input strings (Sys.argv) with
196 input: string of faust process, input waves in default directory 'input_sounds/'
197 output: output waves in default directory 'output_sounds/'.*)
198
199 let main () =
200
201 (* ignore system alarm clock *)
202 let _ = Sys.signal Sys.sigalrm Sys.Signal_ignore in
203
204 (* set garbage collector *)
205 let _ = set_GC () in
206
207 (* select output type *)
208 let option_in = Sys.argv.(1) in
209 let option_out = Sys.argv.(2) in
210
211 (* read input wave files *)
212
213 let (input_rate_list, input_float_array_list) = read_input option_in Sys.argv in
214
215 try
216 (* preprocess *)
217 let dsp_file_route_string = (io_macro_to_string Dsp_Route_string) ^ Sys.argv.(3) in
218 let () = print_string(" Faustine -> Preprocessing...") in
219 let tic = Sys.time () in
220 let exp_string = Preprocess.preprocess(dsp_file_route_string) in
221 let toc = Sys.time () in
222 let () = print_endline(" Done. (duration: " ^
223 (string_of_float (toc -. tic)) ^ "s)") in
224
225 (* parsing *)
226 let exp_faust = exp_of_string exp_string in
227
228 (* interpretation *)
229 let (output_channel_list, output_rate_list, output_float_array_list) =
230 interpreter exp_faust (input_rate_list, input_float_array_list) in
231
232 (* make output wave files *)
233 if option_out = "-wav" then
234 write_output_wave output_channel_list output_rate_list output_float_array_list
235 else if option_out = "-csv" then
236 write_output_csv output_channel_list output_float_array_list
237 else raise (Invalid_argument ("Unkown option: " ^ option_out))
238
239 with NotYetDone ->
240 print_endline("Operation not yet programed..");;
241
242 main();;
243