3 Description: audio input/output, csv input/output
5 Created: 12/08/2013 Modified: 13/08/2013
12 let default_output_path = "../output_sounds/";;
16 method virtual read : string array -> beam
17 method virtual write : int array -> value_type array array -> string array
18 method private to_float : value_type array array -> float array array =
19 fun (origin : value_type array array) ->
21 let value2float = fun (v : value_type) -> v#to_float_array in
22 Array.map (Array.map value2float) origin in
23 Array.map Array.concat (Array.map Array.to_list data)
26 class waveio : io_type =
29 method read : string array -> beam =
30 fun (paths : string array) ->
31 let n = Array.length paths in
36 let files = Array.map Sndfile.openfile paths in
37 let frames = Array.map Int64.to_int (Array.map Sndfile.frames files) in
38 let rates = Array.map Sndfile.samplerate files in
39 let create_container = fun l -> Array.create l 1. in
40 let containers = Array.map create_container frames in
41 let _ = array_map2 Sndfile.read files containers in
42 let _ = Array.map Sndfile.close files in
43 array_map2 (new signal) rates containers in
46 method write : int array -> value_type array array -> string array =
47 fun (rates : int array) ->
48 fun (output : value_type array array) ->
49 let () = print_string(" Faustine -> Writing wave files...") in
50 let tic = Sys.time () in
52 let n = Array.length output in
53 let paths = Array.init n (fun i ->
54 default_output_path ^ "output" ^ (string_of_int i) ^ ".wav") in
58 let get_channel = fun l -> fun s ->
59 match s.(l - 1)#get with
62 Array.map (get_channel n) output in
63 let format = Sndfile.format Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
64 let openwr = fun path -> fun channel -> fun rate ->
65 Sndfile.openfile ~info:(Sndfile.RDWR, format, channel, rate) path in
66 array_map3 openwr paths channels rates in
69 let data = self#to_float output in
70 let _ = array_map2 Sndfile.write files data in
71 let _ = Array.map Sndfile.close files in
72 let toc = Sys.time () in
73 let duration = toc -. tic in
75 (" Done. (duration: " ^ (string_of_float duration) ^ "s)") in
80 class csvio : io_type =
83 method private csvread =
84 method read : string array -> beam =
88 method write : value_type array array -> string array
92 let csvread = fun (ic : in_channel) ->
93 let string_list = ref [] in
96 string_list := !string_list @ [(input_line ic)]
100 (*let () = print_endline(List.nth !string_list 0) in*)
101 Array.of_list (List.map float_of_string !string_list);;
103 let read_input_csv = fun argv ->
104 let n_input = (Array.length argv) - 4 in
106 raise Missing_Expression
107 else if n_input = 0 then
111 let file_string_array = Array.sub argv 4 n_input in
112 let make_chemin s = io_macro_to_string Input_Route_string ^ s in
113 let file_chemin_string_array = Array.map make_chemin file_string_array in
114 let file_array = Array.map open_in file_chemin_string_array in
115 let file_list = Array.to_list file_array in
117 (* read sample rates and data *)
118 let rate_list = Array.to_list (Array.create n_input 0) in
119 let data_float_array_list = List.map csvread file_list in
120 let _ = List.map close_in file_list in
121 (rate_list, data_float_array_list);;
124 let write_output_csv = fun channel_int_list -> fun data_float_array_list ->
125 let () = print_string(" Faustine -> Writing csv files...") in
126 let tic = Sys.time () in
128 (* make output txt file names : output0, output1, ... *)
129 let n_output = List.length data_float_array_list in
130 let n_array = Array.init n_output (fun n -> n) in
131 let make_file_name i = "output" ^ (string_of_int i) ^ ".csv" in
133 (* make output wave file routes *)
134 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
135 let file_name_string_array = Array.map make_file_name n_array in
136 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
137 let file_chemin_string_list = Array.to_list file_chemin_string_array in
139 (* open output channels *)
140 let file_list = List.map open_out file_chemin_string_list in
141 let data_string_array_list = List.map (Array.map string_of_float) data_float_array_list in
142 let array_to_string = fun data_string_array -> fun channel_int ->
143 let data_length = Array.length data_string_array in
144 let rec to_string_rec =
145 fun data -> fun channel -> fun n -> fun i -> fun column ->
148 let element = data.(i) in
149 if column < (channel - 1) then
150 element ^ "," ^ (to_string_rec data channel n (i + 1) (column + 1))
151 else if column = (channel - 1) then
152 element ^ "\n" ^ (to_string_rec data channel n (i + 1) 0)
153 else raise (Invalid_argument "write_output_txt.")
156 to_string_rec data_string_array channel_int data_length 0 0 in
158 let data_string_list = List.map2 array_to_string data_string_array_list channel_int_list in
159 let _ = List.map2 output_string file_list data_string_list in
160 let _ = List.map close_out file_list in
161 let toc = Sys.time () in
162 print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");;