25a2868dc8e952a54da0fcfbddbd2eba6950c175
[Faustine.git] / interpretor / faustio.ml
1 (**
2 Module: Faustio
3 Description: audio input/output, csv input/output
4 @author WANG Haisheng
5 Created: 12/08/2013 Modified: 13/08/2013
6 *)
7
8 open Types;;
9 open Basic;;
10 open Value;;
11 open Signal;;
12 open Beam;;
13 open Aux;;
14
15 let default_output_path = "../output_sounds/";;
16
17 class virtual io =
18 object
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) ->
23 let data =
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)
27 end;;
28
29 class waveio : io_type =
30 object (self)
31 inherit io
32 method read : string array -> beam =
33 fun (paths : string array) ->
34 let n = Array.length paths in
35 if n = 0 then
36 new beam [||]
37 else
38 let signals =
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
47 let stream2func =
48 fun stream -> fun t -> new value (R stream.(t)) in
49 array_map2 (new signal) rates (Array.map stream2func containers) in
50 new beam signals
51
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
57
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
61
62 let files =
63 let channels =
64 let get_channel = fun s ->
65 let l = Array.length s in
66 match s.(l - 1)#get with
67 | Vec vec -> vec#size
68 | _ -> 1 in
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
75 let () =
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
81 print_endline
82 (" Done. (duration: " ^ (string_of_float duration) ^ "s)") in
83 paths
84 end;;
85
86
87 (*
88 class csvio : io_type =
89 object (self)
90 inherit io
91 method private csvread =
92 method read : string array -> beam =
93
94
95
96 method write : value_type array array -> string array
97 end;;
98 *)
99
100
101 (*
102 let csvread = fun (ic : in_channel) ->
103 let string_list = ref [] in
104 try
105 while true do
106 string_list := !string_list @ [(input_line ic)]
107 done;
108 [||]
109 with End_of_file ->
110 (*let () = print_endline(List.nth !string_list 0) in*)
111 Array.of_list (List.map float_of_string !string_list);;
112
113 let read_input_csv = fun argv ->
114 let n_input = (Array.length argv) - 4 in
115 if n_input < 0 then
116 raise (Invalid_argument "xxx")
117 else if n_input = 0 then
118 ([], [])
119 else
120 (* open csv file *)
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
126
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);;
132
133
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
137
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
142
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
148
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 ->
156 if i < n then
157 (
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.")
164 )
165 else "" in
166 to_string_rec data_string_array channel_int data_length 0 0 in
167
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)");;
173
174 *)