X-Git-Url: https://scm.cri.mines-paristech.fr/git/Faustine.git/blobdiff_plain/a891a827a9bad83d44164ffdd7b28f070c439e46..a3490abb2f472827f39b061c55029949ebb3eba5:/interpretor/main.ml?ds=inline diff --git a/interpretor/main.ml b/interpretor/main.ml index 54b7265..8637875 100644 --- a/interpretor/main.ml +++ b/interpretor/main.ml @@ -2,242 +2,111 @@ Module: Interpreter Description: Input wave -> interpretation -> output wave @author WANG Haisheng - Created: 15/05/2013 Modified: 04/06/2013 + Created: 15/05/2013 Modified: 14/08/2013 *) -open Faustexp;; -open Interpreter;; +open Aux;; +open Process;; +open Faustio;; -(* EXCEPTIONS *) - -(** Exception raised when no string expression of faust process is typed in console.*) exception Missing_Expression;; +let version = "Faustine: 0.0.1";; - -(* MACRO *) - -(** Macro constants of input/output route.*) -type io_macro = - | Input_Route_string - | Output_Route_string - | Dsp_Route_string;; - -(** val io_macro_to_string : io_macro -> string.*) -let io_macro_to_string m = match m with - | Input_Route_string -> "" - | Output_Route_string -> "../output_sounds/" - | Dsp_Route_string -> "";; - - -(** val set_GC : unit -> unit *) let set_GC () = - let _ = Gc.set { (Gc.get()) with Gc.minor_heap_size = 0xFFFFFF } in - let _ = Gc.set { (Gc.get()) with Gc.major_heap_increment = 0xFFFFFF } in - let _ = Gc.set { (Gc.get()) with Gc.space_overhead = 100 } in - let _ = Gc.set { (Gc.get()) with Gc.max_overhead = 0xFFFFF } in - let _ = Gc.set { (Gc.get()) with Gc.stack_limit = 0xFFFFF } in - let _ = Gc.set { (Gc.get()) with Gc.allocation_policy = 0 } in + let _ = Gc.set { (Gc.get()) + with Gc.minor_heap_size = 0xFFFFFF } in + let _ = Gc.set { (Gc.get()) + with Gc.major_heap_increment = 0xFFFFFF } in + let _ = Gc.set { (Gc.get()) + with Gc.space_overhead = 100 } in + let _ = Gc.set { (Gc.get()) + with Gc.max_overhead = 0xFFFFF } in + let _ = Gc.set { (Gc.get()) + with Gc.stack_limit = 0xFFFFF } in + let _ = Gc.set { (Gc.get()) + with Gc.allocation_policy = 0 } in () ;; -(* INPUT && OUTPUT*) - -(** val read_input_wave : string array -> int list * float array list - [read_input_wave argv] gets information from command line, - returns sample rate list and data (in form of float array) list. -*) -let read_input_wave = fun argv -> - let n_input = (Array.length argv) - 4 in - if n_input < 0 then - raise Missing_Expression - else if n_input = 0 then - ([], []) - else - (* open wave file *) - let file_string_array = Array.sub argv 4 n_input in - let make_chemin s = io_macro_to_string Input_Route_string ^ s in - let file_chemin_string_array = Array.map make_chemin file_string_array in - let file_array = Array.map Sndfile.openfile file_chemin_string_array in - let file_list = Array.to_list file_array in - - (* prepare data container *) - let frames_array = Array.map Int64.to_int (Array.map Sndfile.frames file_array) in - let create_data_array num = Array.create num 1. in - let data_float_array_array = Array.map create_data_array frames_array in - let data_float_array_list = Array.to_list data_float_array_array in - - (* read sample rates and data *) - let rate_list = List.map Sndfile.samplerate file_list in - let _ = List.map2 Sndfile.read file_list data_float_array_list in - let _ = List.map Sndfile.close file_list in - (rate_list, data_float_array_list);; - - -(** val write_output_wave : int list -> int list -> float_array_list -> unit. - [write_output_wave channel_numbers sample_rates data] -*) -let write_output_wave = fun channel_int_list -> fun rate_int_list -> fun data_float_array_list -> - let () = print_string(" Faustine -> Writing wave files...") in - let tic = Sys.time () in - - (* make output wave file names : output0, output1, ... *) - let n_output = List.length data_float_array_list in - let n_array = Array.init n_output (fun n -> n) in - let make_file_name i = "output" ^ (string_of_int i) ^ ".wav" in - - (* make output wave file routes *) - let make_chemin s = io_macro_to_string Output_Route_string ^ s in - let file_name_string_array = Array.map make_file_name n_array in - let file_chemin_string_array = Array.map make_chemin file_name_string_array in - let file_chemin_string_list = Array.to_list file_chemin_string_array in - - (* open files for writing with respects to channel numbers and sample rates *) - let file_format = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in - let openwr = fun file_chemin_string -> fun channel -> fun rate -> - Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, rate) file_chemin_string in - let openwr_fun_list = fun fl -> fun cl -> fun rl -> fun i -> - openwr (List.nth fl i) (List.nth cl i) (List.nth rl i) in - let output_file_list = List.map - (openwr_fun_list file_chemin_string_list channel_int_list rate_int_list) - (Array.to_list (Array.init n_output (fun n -> n))) in - - (* write data into files *) - let _ = List.map2 Sndfile.write output_file_list data_float_array_list in - let _ = List.map Sndfile.close output_file_list in - let toc = Sys.time () in - print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");; - - -let csvread = fun (ic : in_channel) -> - let string_list = ref [] in - try - while true do - string_list := !string_list @ [(input_line ic)] - done; - [||] - with End_of_file -> - (*let () = print_endline(List.nth !string_list 0) in*) - Array.of_list (List.map float_of_string !string_list);; - -let read_input_csv = fun argv -> - let n_input = (Array.length argv) - 4 in - if n_input < 0 then - raise Missing_Expression - else if n_input = 0 then - ([], []) - else - (* open csv file *) - let file_string_array = Array.sub argv 4 n_input in - let make_chemin s = io_macro_to_string Input_Route_string ^ s in - let file_chemin_string_array = Array.map make_chemin file_string_array in - let file_array = Array.map open_in file_chemin_string_array in - let file_list = Array.to_list file_array in - - (* read sample rates and data *) - let rate_list = Array.to_list (Array.create n_input 0) in - let data_float_array_list = List.map csvread file_list in - let _ = List.map close_in file_list in - (rate_list, data_float_array_list);; - - -let write_output_csv = fun channel_int_list -> fun data_float_array_list -> - let () = print_string(" Faustine -> Writing csv files...") in - let tic = Sys.time () in - - (* make output txt file names : output0, output1, ... *) - let n_output = List.length data_float_array_list in - let n_array = Array.init n_output (fun n -> n) in - let make_file_name i = "output" ^ (string_of_int i) ^ ".csv" in - - (* make output wave file routes *) - let make_chemin s = io_macro_to_string Output_Route_string ^ s in - let file_name_string_array = Array.map make_file_name n_array in - let file_chemin_string_array = Array.map make_chemin file_name_string_array in - let file_chemin_string_list = Array.to_list file_chemin_string_array in - - (* open output channels *) - let file_list = List.map open_out file_chemin_string_list in - let data_string_array_list = List.map (Array.map string_of_float) data_float_array_list in - let array_to_string = fun data_string_array -> fun channel_int -> - let data_length = Array.length data_string_array in - let rec to_string_rec = - fun data -> fun channel -> fun n -> fun i -> fun column -> - if i < n then - ( - let element = data.(i) in - if column < (channel - 1) then - element ^ "," ^ (to_string_rec data channel n (i + 1) (column + 1)) - else if column = (channel - 1) then - element ^ "\n" ^ (to_string_rec data channel n (i + 1) 0) - else raise (Invalid_argument "write_output_txt.") - ) - else "" in - to_string_rec data_string_array channel_int data_length 0 0 in - - let data_string_list = List.map2 array_to_string data_string_array_list channel_int_list in - let _ = List.map2 output_string file_list data_string_list in - let _ = List.map close_out file_list in - let toc = Sys.time () in - print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");; - - -let read_input = fun option_in -> fun argv -> - if option_in = "-wav" then - read_input_wave argv - else if option_in = "-csv" then - read_input_csv argv - else raise (Invalid_argument ("Unkown option: " ^ option_in));; - - -(* MAIN *) - -(** val main : unit -> unit -main function reads console input strings (Sys.argv) with -input: string of faust process, input waves in default directory 'input_sounds/' -output: output waves in default directory 'output_sounds/'.*) +let path_dsp = ref "";; +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 option_usage = "usage: " ^ Sys.argv.(0) + ^ " [-d dsp_src] [-i input] [-t time] [--odir dir] [--oformat wav/csv] [--obasename name]";; + +let option_unknown = + fun x -> raise (Arg.Bad ("Bad argument : " ^ x)) + +let speclist = [ + ("-d", Arg.String (fun s -> path_dsp := s), ": set dsp source file"); + ("-i", Arg.String (fun s -> incr size_input; inputs := !inputs @ [s]), ": set input wave file"); + ("-t", Arg.Int (fun i -> time_max := i), ": set max output length"); + ("--odir", Arg.String (fun s -> dir_output := s), ": set output directory"); + ("--oformat", Arg.String (fun s -> format_output := s), ": set output format"); + ("--obasename", Arg.String (fun s -> basename_output := s), ": set output basename"); + ];; + let main () = - (* ignore system alarm clock *) + let () = Arg.parse speclist option_unknown option_usage in let _ = Sys.signal Sys.sigalrm Sys.Signal_ignore in - - (* set garbage collector *) let _ = set_GC () in + let io = new iomanager in + let () = io#set !dir_output !format_output !basename_output in + + + let () = print_string(" Faustine -> Reading input ...") in + let tic0 = Sys.time () in + let input = io#read !inputs in + let toc0 = Sys.time () in + let () = print_endline(" Done. (duration: " ^ (string_of_float (toc0 -. tic0)) ^ "s.)") in + + + let () = print_string(" Faustine -> Preprocessing...") in + let tic1 = Sys.time () in + let faust_core = Preprocess.preprocess !path_dsp in + let toc1 = Sys.time () in + let () = print_endline(" Done. (duration: " ^ (string_of_float (toc1 -. tic1)) ^ "s.)") in + - (* select output type *) - let option_in = Sys.argv.(1) in - let option_out = Sys.argv.(2) in + let () = print_string(" Faustine -> Constructing process...") in + let tic2 = Sys.time () in + let faust_exp = exp_of_string faust_core in + let proc = (new proc_factory)#make faust_exp in + let toc2 = Sys.time () in + let () = print_endline(" Done. (duration: " ^ (string_of_float (toc2 -. tic2)) ^ "s.)") in - (* read input wave files *) - let (input_rate_list, input_float_array_list) = read_input option_in Sys.argv in + let () = print_string(" Faustine -> Evaluating...") in + let tic3 = Sys.time () in + let output = proc#eval input in + let toc3 = Sys.time () in + let () = print_endline(" Done. (duration: " ^ (string_of_float (toc3 -. tic3)) ^ "s.)") in - try - (* preprocess *) - let dsp_file_route_string = (io_macro_to_string Dsp_Route_string) ^ Sys.argv.(3) in - let () = print_string(" Faustine -> Preprocessing...") in - let tic = Sys.time () in - let exp_string = Preprocess.preprocess(dsp_file_route_string) in - let toc = Sys.time () in - let () = print_endline(" Done. (duration: " ^ - (string_of_float (toc -. tic)) ^ "s)") in - (* parsing *) - let exp_faust = exp_of_string exp_string in + let () = print_string(" Faustine -> Calculating...") in + let tic4 = Sys.time () in + let data = output#output !time_max in + let rates = output#frequency in + let toc4 = Sys.time () in + let () = print_endline(" Done. (duration: " ^ (string_of_float (toc4 -. tic4)) ^ "s.)") in - (* interpretation *) - let (output_channel_list, output_rate_list, output_float_array_list) = - interpreter exp_faust (input_rate_list, input_float_array_list) in - (* make output wave files *) - if option_out = "-wav" then - write_output_wave output_channel_list output_rate_list output_float_array_list - else if option_out = "-csv" then - write_output_csv output_channel_list output_float_array_list - else raise (Invalid_argument ("Unkown option: " ^ option_out)) + let () = print_string(" Faustine -> Writing output...") in + let tic5 = Sys.time () in + let output_paths = io#write rates data in + let toc5 = Sys.time () in + let () = print_endline(" Done. (duration: " ^ (string_of_float (toc5 -. tic5)) ^ "s.)") in - with NotYetDone -> - print_endline("Operation not yet programed..");; + let _ = Array.map print_endline + (Array.map decorate output_paths) in + ();; main();;