3 Description: audio input/output, csv input/output
5 Created: 12/08/2013 Modified: 13/08/2013
15 let default_output_path = "../output_sounds/";;
19 method virtual read : string array -> beam
20 method virtual write : int array -> value_type array array -> string array
21 method private to_float : value_type array array -> float array array =
22 fun (origin : value_type array array) ->
24 let value2float = fun (v : value_type) -> v#to_float_array in
25 Array.map (Array.map value2float) origin in
26 Array.map Array.concat (Array.map Array.to_list data)
29 class waveio : io_type =
32 method read : string array -> beam =
33 fun (paths : string array) ->
34 let n = Array.length paths in
39 let files = Array.map Sndfile.openfile paths in
40 let frames = Array.map Int64.to_int
41 (Array.map Sndfile.frames files) in
42 let rates = Array.map Sndfile.samplerate files in
43 let create_container = fun l -> Array.create l 1. in
44 let containers = Array.map create_container frames in
45 let _ = array_map2 Sndfile.read files containers in
46 let _ = Array.map Sndfile.close files in
48 fun stream -> fun t -> new value (R stream.(t)) in
49 array_map2 (new signal) rates (Array.map stream2func containers) in
52 method write : int array -> value_type array array -> string array =
53 fun (rates : int array) ->
54 fun (output : value_type array array) ->
55 let () = print_string(" Faustine -> Writing wave files...") in
56 let tic = Sys.time () in
58 let n = Array.length output in
59 let paths = Array.init n (fun i ->
60 default_output_path ^ "output" ^ (string_of_int (i + 1)) ^ ".wav") in
64 let get_channel = fun s ->
65 let l = Array.length s in
66 match s.(l - 1)#get with
69 Array.map get_channel output in
70 let file_format = Sndfile.format
71 Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
72 let openwr = fun path -> fun channel -> fun rate ->
73 Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, rate) path in
74 array_map3 openwr paths channels rates in
76 let data = self#to_float output in
77 let _ = array_map2 Sndfile.write files data in
78 let _ = Array.map Sndfile.close files in
79 let toc = Sys.time () in
80 let duration = toc -. tic in
82 (" Done. (duration: " ^ (string_of_float duration) ^ "s)") in
88 class csvio : io_type =
91 method private csvread =
92 method read : string array -> beam =
96 method write : value_type array array -> string array
102 let csvread = fun (ic : in_channel) ->
103 let string_list = ref [] in
106 string_list := !string_list @ [(input_line ic)]
110 (*let () = print_endline(List.nth !string_list 0) in*)
111 Array.of_list (List.map float_of_string !string_list);;
113 let read_input_csv = fun argv ->
114 let n_input = (Array.length argv) - 4 in
116 raise (Invalid_argument "xxx")
117 else if n_input = 0 then
121 let file_string_array = Array.sub argv 4 n_input in
122 let make_chemin s = io_macro_to_string Input_Route_string ^ s in
123 let file_chemin_string_array = Array.map make_chemin file_string_array in
124 let file_array = Array.map open_in file_chemin_string_array in
125 let file_list = Array.to_list file_array in
127 (* read sample rates and data *)
128 let rate_list = Array.to_list (Array.create n_input 0) in
129 let data_float_array_list = List.map csvread file_list in
130 let _ = List.map close_in file_list in
131 (rate_list, data_float_array_list);;
134 let write_output_csv = fun channel_int_list -> fun data_float_array_list ->
135 let () = print_string(" Faustine -> Writing csv files...") in
136 let tic = Sys.time () in
138 (* make output txt file names : output0, output1, ... *)
139 let n_output = List.length data_float_array_list in
140 let n_array = Array.init n_output (fun n -> n) in
141 let make_file_name i = "output" ^ (string_of_int i) ^ ".csv" in
143 (* make output wave file routes *)
144 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
145 let file_name_string_array = Array.map make_file_name n_array in
146 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
147 let file_chemin_string_list = Array.to_list file_chemin_string_array in
149 (* open output channels *)
150 let file_list = List.map open_out file_chemin_string_list in
151 let data_string_array_list = List.map (Array.map string_of_float) data_float_array_list in
152 let array_to_string = fun data_string_array -> fun channel_int ->
153 let data_length = Array.length data_string_array in
154 let rec to_string_rec =
155 fun data -> fun channel -> fun n -> fun i -> fun column ->
158 let element = data.(i) in
159 if column < (channel - 1) then
160 element ^ "," ^ (to_string_rec data channel n (i + 1) (column + 1))
161 else if column = (channel - 1) then
162 element ^ "\n" ^ (to_string_rec data channel n (i + 1) 0)
163 else raise (Invalid_argument "write_output_txt.")
166 to_string_rec data_string_array channel_int data_length 0 0 in
168 let data_string_list = List.map2 array_to_string data_string_array_list channel_int_list in
169 let _ = List.map2 output_string file_list data_string_list in
170 let _ = List.map close_out file_list in
171 let toc = Sys.time () in
172 print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");;