Initial import.
[Faustine.git] / backup / 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 -> "input_sounds/"
29 | Output_Route_string -> "output_sounds/"
30 | Dsp_Route_string -> "dsp_files/";;
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) - 3 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 3 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_endline("Making output wave files...") in
81
82 (* make output wave file names : output0, output1, ... *)
83 let n_output = List.length data_float_array_list in
84 let n_array = Array.init n_output (fun n -> n) in
85 let make_file_name i = "output" ^ (string_of_int i) ^ ".wav" in
86
87 (* make output wave file routes *)
88 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
89 let file_name_string_array = Array.map make_file_name n_array in
90 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
91 let file_chemin_string_list = Array.to_list file_chemin_string_array in
92
93 (* open files for writing with respects to channel numbers and sample rates *)
94 let file_format = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
95 let openwr = fun file_chemin_string -> fun channel -> fun rate ->
96 Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, rate) file_chemin_string in
97 let openwr_fun_list = fun fl -> fun cl -> fun rl -> fun i ->
98 openwr (List.nth fl i) (List.nth cl i) (List.nth rl i) in
99 let output_file_list = List.map
100 (openwr_fun_list file_chemin_string_list channel_int_list rate_int_list)
101 (Array.to_list (Array.init n_output (fun n -> n))) in
102
103 (* write data into files *)
104 let _ = List.map2 Sndfile.write output_file_list data_float_array_list in
105 let _ = List.map Sndfile.close output_file_list in
106 print_endline("Success.");;
107
108
109 let write_output_txt = fun channel_int_list -> fun data_float_array_list ->
110 let () = print_endline("Making output txt files...") in
111
112 (* make output txt file names : output0, output1, ... *)
113 let n_output = List.length data_float_array_list in
114 let n_array = Array.init n_output (fun n -> n) in
115 let make_file_name i = "output" ^ (string_of_int i) ^ ".csv" in
116
117 (* make output wave file routes *)
118 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
119 let file_name_string_array = Array.map make_file_name n_array in
120 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
121 let file_chemin_string_list = Array.to_list file_chemin_string_array in
122
123 (* open output channels *)
124 let file_list = List.map open_out file_chemin_string_list in
125 let array_to_string = fun data_float_array -> fun channel_int ->
126 let data_length = Array.length data_float_array in
127 let rec to_string_rec =
128 fun data -> fun channel -> fun n -> fun i -> fun column ->
129 if i < n then
130 (
131 let element = string_of_float (data.(i)) in
132 if column < (channel - 1) then
133 element ^ "," ^ (to_string_rec data channel n (i + 1) (column + 1))
134 else if column = (channel - 1) then
135 element ^ "\n" ^ (to_string_rec data channel n (i + 1) 0)
136 else raise (Invalid_argument "write_output_txt.")
137 )
138 else "" in
139 to_string_rec data_float_array channel_int data_length 0 0 in
140
141 let data_string_list = List.map2 array_to_string data_float_array_list channel_int_list in
142 let _ = List.map2 output_string file_list data_string_list in
143 let _ = List.map close_out file_list in
144 print_endline("Success");;
145
146
147 (* MAIN *)
148
149 (** val main : unit -> unit
150 main function reads console input strings (Sys.argv) with
151 input: string of faust process, input waves in default directory 'input_sounds/'
152 output: output waves in default directory 'output_sounds/'.*)
153
154 let main () =
155
156 (* ignore system alarm clock *)
157 let _ = Sys.signal Sys.sigalrm Sys.Signal_ignore in
158
159 (* set garbage collector *)
160 let _ = set_GC () in
161
162 (* read input wave files *)
163 let (input_rate_list, input_float_array_list) = read_input_wave Sys.argv in
164
165 (* select output type *)
166 let output_type = Sys.argv.(1) in
167
168 try
169 (* preprocess *)
170 let dsp_file_route_string = (io_macro_to_string Dsp_Route_string) ^ Sys.argv.(2) in
171 let exp_string = Preprocess.preprocess(dsp_file_route_string) in
172
173 (* parsing *)
174 let exp_faust = exp_of_string exp_string in
175
176 (* interpretation *)
177 let (output_channel_list, output_rate_list, output_float_array_list) =
178 interpreter exp_faust (input_rate_list, input_float_array_list) in
179
180 (* make output wave files *)
181 if output_type = "-wav" then
182 write_output_wave output_channel_list output_rate_list output_float_array_list
183 else if output_type = "-txt" then
184 write_output_txt output_channel_list output_float_array_list
185 else raise (Invalid_argument ("Unkown option: " ^ output_type))
186
187 with NotYetDone ->
188 print_endline("Operation not yet programed..");;
189
190 main();;
191