X-Git-Url: https://scm.cri.ensmp.fr/git/Faustine.git/blobdiff_plain/a6be79ccb7456a5181561f094c1f2a4f90aadb02..e77e9feca3956347ffeb6d58e97ec3b423ec99aa:/interpretor/faustio.ml?ds=inline diff --git a/interpretor/faustio.ml b/interpretor/faustio.ml index dc1ee1d..3094ce9 100644 --- a/interpretor/faustio.ml +++ b/interpretor/faustio.ml @@ -12,16 +12,24 @@ 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 -> @@ -56,12 +64,9 @@ 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 = self#channels output in @@ -71,14 +76,9 @@ class waveio : io_type = 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;; @@ -117,13 +117,10 @@ class csvio : io_type = 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 + _dir ^ _basename ^ (string_of_int (i + 1)) ^ ".csv" in Array.init n path_pattern in let files = Array.map open_out paths in @@ -139,8 +136,57 @@ class csvio : io_type = 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;; + + +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;;