From: WANG Date: Wed, 21 Aug 2013 13:21:55 +0000 (+0200) Subject: Csv IO class implemented. X-Git-Url: https://scm.cri.ensmp.fr/git/Faustine.git/commitdiff_plain/a6be79ccb7456a5181561f094c1f2a4f90aadb02 Csv IO class implemented. --- diff --git a/dsp_files/fft.dsp b/dsp_files/fft.dsp index 8769e5a..34a795e 100644 --- a/dsp_files/fft.dsp +++ b/dsp_files/fft.dsp @@ -8,7 +8,7 @@ import ( "mrfaustlib/complex.lib" ) ; fft_test(n,m) = vectorize(n) : fft(n) : pcplx_moduls(n) : nconcat(n); //process = +, _ : + : fft_test(128); -process = fft_test(128,128) : serialize : vectorize(1); +process = fft_test(128,128); //process = (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7) <: shuffle(8); diff --git a/interpretor/Makefile b/interpretor/Makefile index 39ec26e..e287f8b 100644 --- a/interpretor/Makefile +++ b/interpretor/Makefile @@ -45,7 +45,7 @@ CC = g++ # LIBS=$(WITHGRAPHICS) $(WITHUNIX) $(WITHSTR) $(WITHNUMS) $(WITHTHREADS)\ # $(WITHDBM) -LIBS = $(WITHSNDFILE) $(WITHUNIX) +LIBS = $(WITHSNDFILE) $(WITHUNIX) $(WITHSTR) # Should be set to -INCLUDE if you use any of the libraries above # or if any C code have to be linked with your program @@ -73,6 +73,8 @@ WITHUNIX =unix.cma WITHSNDFILE = sndfile.cma +WITHSTR = str.cma + # c++ wrap options GPP_CALL = -cc "g++" diff --git a/interpretor/basic.ml b/interpretor/basic.ml index 3131231..1e56142 100644 --- a/interpretor/basic.ml +++ b/interpretor/basic.ml @@ -85,14 +85,31 @@ let basic_to_float_array : basic -> float array = |_ -> [| (basic_to_float v)|];; -let basic_to_string : basic -> string = +let rec basic_to_string : basic -> string = fun (v : basic) -> match v with - |N i1 -> "N " ^ (string_of_int i1) - |R f1 -> "R " ^ (string_of_float f1) - |Vec vec -> "Vec" - |Zero -> "Zero" - |Error -> "Error";; + |N i1 -> string_of_int i1 + |R f1 -> string_of_float f1 + |Vec vec -> + let basics : basic array = + Array.init vec#size vec#nth in + let strings = Array.to_list + (Array.map basic_to_string basics) in + String.concat "," strings + |Zero -> "0" + |Error -> "0";; + +let basic_of_float : float -> basic = fun f -> R f;; + +let rec basic_of_float_array : float array -> basic = + fun (data : float array) -> + let n = Array.length data in + if n = 0 then + raise (Convert_Error "basic_of_float_array : empty.") + else if n = 1 then basic_of_float data.(0) + else + let vec = Array.get (Array.map basic_of_float data) in + Vec (new vector n vec);; (* VALUE OPERATIONS *) diff --git a/interpretor/beam.ml b/interpretor/beam.ml index ffe8128..273ae1c 100644 --- a/interpretor/beam.ml +++ b/interpretor/beam.ml @@ -98,6 +98,7 @@ class beam : signal_type array -> beam_type = | Signal_operation s -> "Signal_operation: " ^ s | Beam_matching s -> "Beam_Matching_Error: " ^ s | Invalid_argument s -> "Compute finished." + | _ -> "Unknown error" in let () = print_string error_message in transpose (Array.sub container 0 !index) diff --git a/interpretor/faustine b/interpretor/faustine index c21cebf..b87845f 100755 Binary files a/interpretor/faustine and b/interpretor/faustine differ diff --git a/interpretor/faustio.ml b/interpretor/faustio.ml index b9c7598..dc1ee1d 100644 --- a/interpretor/faustio.ml +++ b/interpretor/faustio.ml @@ -13,6 +13,7 @@ open Beam;; open Aux;; let default_output_path = "../output_sounds/";; +let csv_read_buffer_length = 0xFFFF;; class virtual io = object @@ -21,6 +22,12 @@ class virtual io = method private concat : float array array array -> float array array = fun (origin : float array array array) -> Array.map Array.concat (Array.map Array.to_list origin) + method private channels : float array array array -> int array = + fun data -> + let get_channel = fun s -> + let l = Array.length s in + Array.length s.(l - 1) in + Array.map get_channel data end;; class waveio : io_type = @@ -57,11 +64,7 @@ class waveio : io_type = default_output_path ^ "output" ^ (string_of_int (i + 1)) ^ ".wav") in let files = - let channels = - let get_channel = fun s -> - let l = Array.length s in - Array.length s.(l - 1) in - Array.map get_channel output in + let channels = self#channels output in let file_format = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in let openwr = fun path -> fun channel -> fun rate -> @@ -80,91 +83,64 @@ class waveio : io_type = end;; -(* + class csvio : io_type = object (self) inherit io - method private csvread = - method read : string array -> beam = - + method private csvread : in_channel -> signal = + fun (ic : in_channel) -> + let buffer = Buffer.create csv_read_buffer_length in + let () = + try + while true do + Buffer.add_string buffer (input_line ic); + Buffer.add_char buffer '\t'; + done; + with End_of_file -> () in + let content = Buffer.contents buffer in + let lines = Str.split (Str.regexp "\t") content in + let elements = List.map (Str.split (Str.regexp ",")) lines in + let data = + let data_in_list = List.map (List.map float_of_string) elements in + Array.of_list (List.map Array.of_list data_in_list) in + let values = + let convertor = new value Zero in + Array.map (convertor#of_float_array) data in + new signal 0 (Array.get values) + method read : string array -> beam = + fun (paths : string array) -> + let files = Array.map open_in paths in + let signals = Array.map self#csvread files in + new beam signals - method write : value_type array array -> string array + method write : int array -> float array array array -> string array = + fun (rates : int array) -> + fun (data : float array array array) -> + let () = print_string(" Faustine -> Writing csv files...") in + let tic = Sys.time () in + + let paths = + let n = Array.length data in + let path_pattern = fun i -> + default_output_path ^ "output" ^ (string_of_int (i + 1)) ^ ".csv" in + Array.init n path_pattern in + + let files = Array.map open_out paths in + let strings = + let value2string : float array -> string = + fun (v : float array) -> + let strings = Array.map string_of_float v in + String.concat "," (Array.to_list strings) in + let signal2string : float array array -> string = + fun (s : float array array) -> + let lines = Array.map value2string s in + String.concat "\n" (Array.to_list lines) in + Array.map signal2string data in + let _ = array_map2 output_string files strings in + let _ = Array.map close_out files in + let toc = Sys.time () in + let () = print_endline + (" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)") in + paths end;; -*) - - -(* -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 (Invalid_argument "xxx") - 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)");; - -*) diff --git a/interpretor/gmon.out b/interpretor/gmon.out index cf1a411..aca0945 100644 Binary files a/interpretor/gmon.out and b/interpretor/gmon.out differ diff --git a/interpretor/main.ml b/interpretor/main.ml index 082fba3..5e074a2 100644 --- a/interpretor/main.ml +++ b/interpretor/main.ml @@ -62,6 +62,7 @@ let main () = let () = Arg.parse speclist option_unknown option_usage in let wave = new waveio in + let csv = new csvio in let input = wave#read (Array.of_list !inwavs) in let faust_core = Preprocess.preprocess !path_dsp in let faust_exp = exp_of_string faust_core in @@ -70,8 +71,9 @@ let main () = let data = output#output time_maximum in let rates = output#frequency in - let output_paths = wave#write rates data in - let _ = Array.map print_endline output_paths in + let output_wave_paths = wave#write rates data in + let _ = csv#write rates data in + let _ = Array.map print_endline output_wave_paths in ();; diff --git a/interpretor/types.ml b/interpretor/types.ml index e0eace4..17dea67 100644 --- a/interpretor/types.ml +++ b/interpretor/types.ml @@ -22,6 +22,7 @@ class type value_type = method to_int : int method to_float : float method to_float_array : float array + method of_float_array : float array -> value_type method to_string : string method normalize : unit method add : value_type -> value_type diff --git a/interpretor/value.ml b/interpretor/value.ml index a13739e..3c97aa0 100644 --- a/interpretor/value.ml +++ b/interpretor/value.ml @@ -22,6 +22,8 @@ class value : basic -> value_type = method to_int = convert basic_to_int self#get method to_float_array = convert basic_to_float_array self#get method to_string = convert basic_to_string self#get + method of_float_array : float array -> value_type = + fun data -> new value (basic_of_float_array data) method private prim1 : (basic -> basic) -> value = fun oper ->