X-Git-Url: https://svn.cri.ensmp.fr/git/Faustine.git/blobdiff_plain/f59ac471aa5a080301d70e7d7e1e81f34151ba2f..d1e05f17aa7a1169e3e8ea4ccef8817d7dffb0da:/interpretor/faustio.ml diff --git a/interpretor/faustio.ml b/interpretor/faustio.ml index b9c7598..3094ce9 100644 --- a/interpretor/faustio.ml +++ b/interpretor/faustio.ml @@ -12,15 +12,30 @@ open Signal;; open Beam;; open Aux;; -let default_output_path = "../output_sounds/";; +let csv_read_buffer_length = 0xFFFF;; class virtual io = object + val mutable _basename = "" + val mutable _dir = "" + method set : string -> string -> unit = + fun (dir : string) -> + fun (basename : string) -> + _basename <- basename; _dir <- dir + method virtual read : string array -> beam method virtual write : int array -> float array array array -> string array + 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 = @@ -49,122 +64,129 @@ class waveio : io_type = method write : int array -> float array array array -> string array = fun (rates : int array) -> fun (output : float array array array) -> - let () = print_string(" Faustine -> Writing wave files...") in - let tic = Sys.time () in - let n = Array.length output in let paths = Array.init n (fun i -> - default_output_path ^ "output" ^ (string_of_int (i + 1)) ^ ".wav") in + _dir ^ _basename ^ (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 -> Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, rate) path in array_map3 openwr paths channels rates in - let () = let data = self#concat output in let _ = array_map2 Sndfile.write files data in let _ = Array.map Sndfile.close files in - let toc = Sys.time () in - let duration = toc -. tic in - print_endline - (" Done. (duration: " ^ (string_of_float duration) ^ "s)") in paths 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 paths = + let n = Array.length data in + let path_pattern = fun i -> + _dir ^ _basename ^ (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 + 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)");; +class iomanager = + object (self) + val wave = new waveio + val csv = new csvio + val mutable _dir = "" + val mutable _format = "" + val mutable _basename = "" + + method private grab_format : string -> string = + fun (path : string) -> + let fragments = Str.split (Str.regexp "\.") path in + let n = List.length fragments in + List.nth fragments (n - 1) + + method read : string list -> beam_type = + fun (paths : string list) -> + let formats = List.map self#grab_format paths in + let read_one : string -> string -> beam_type = + fun (format : string) -> + fun (path : string) -> + if format = "wav" then wave#read [|path|] + else if format = "csv" then csv#read [|path|] + else raise (Invalid_argument "Unknown format.") in + let beams = List.map2 read_one formats paths in + let concat : beam_type -> beam_type -> beam_type = + fun b1 -> fun b2 -> b1#append b2 in + List.fold_left concat (new beam [||]) beams + + method set : string -> string -> string -> unit = + fun (dir : string) -> + fun (format : string) -> + fun (basename : string) -> + _dir <- dir; + _format <- format; + _basename <- basename; + wave#set _dir _basename; + csv#set _dir _basename -*) + method write : int array -> float array array array -> string array = + fun (rates : int array) -> + fun (data : float array array array) -> + if _format = "" then + raise (Invalid_argument "output format unset.") + else if _format = "wav" then + wave#write rates data + else if _format = "csv" then + csv#write rates data + else raise (Invalid_argument "unknown format.") + + end;;