b9c7598d392ff1917b88ea5a45dd7cc02adb9d07
[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 -> float array array array -> string array
21 method private concat : float array array array -> float array array =
22 fun (origin : float array array array) ->
23 Array.map Array.concat (Array.map Array.to_list origin)
24 end;;
25
26 class waveio : io_type =
27 object (self)
28 inherit io
29 method read : string array -> beam =
30 fun (paths : string array) ->
31 let n = Array.length paths in
32 if n = 0 then
33 new beam [||]
34 else
35 let signals =
36 let files = Array.map Sndfile.openfile paths in
37 let frames = Array.map Int64.to_int
38 (Array.map Sndfile.frames files) in
39 let rates = Array.map Sndfile.samplerate files in
40 let create_container = fun l -> Array.create l 1. in
41 let containers = Array.map create_container frames in
42 let _ = array_map2 Sndfile.read files containers in
43 let _ = Array.map Sndfile.close files in
44 let stream2func =
45 fun stream -> fun t -> new value (R stream.(t)) in
46 array_map2 (new signal) rates (Array.map stream2func containers) in
47 new beam signals
48
49 method write : int array -> float array array array -> string array =
50 fun (rates : int array) ->
51 fun (output : float array array array) ->
52 let () = print_string(" Faustine -> Writing wave files...") in
53 let tic = Sys.time () in
54
55 let n = Array.length output in
56 let paths = Array.init n (fun i ->
57 default_output_path ^ "output" ^ (string_of_int (i + 1)) ^ ".wav") in
58
59 let files =
60 let channels =
61 let get_channel = fun s ->
62 let l = Array.length s in
63 Array.length s.(l - 1) in
64 Array.map get_channel output in
65 let file_format = Sndfile.format
66 Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in
67 let openwr = fun path -> fun channel -> fun rate ->
68 Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, rate) path in
69 array_map3 openwr paths channels rates in
70
71 let () =
72 let data = self#concat output in
73 let _ = array_map2 Sndfile.write files data in
74 let _ = Array.map Sndfile.close files in
75 let toc = Sys.time () in
76 let duration = toc -. tic in
77 print_endline
78 (" Done. (duration: " ^ (string_of_float duration) ^ "s)") in
79 paths
80 end;;
81
82
83 (*
84 class csvio : io_type =
85 object (self)
86 inherit io
87 method private csvread =
88 method read : string array -> beam =
89
90
91
92 method write : value_type array array -> string array
93 end;;
94 *)
95
96
97 (*
98 let csvread = fun (ic : in_channel) ->
99 let string_list = ref [] in
100 try
101 while true do
102 string_list := !string_list @ [(input_line ic)]
103 done;
104 [||]
105 with End_of_file ->
106 (*let () = print_endline(List.nth !string_list 0) in*)
107 Array.of_list (List.map float_of_string !string_list);;
108
109 let read_input_csv = fun argv ->
110 let n_input = (Array.length argv) - 4 in
111 if n_input < 0 then
112 raise (Invalid_argument "xxx")
113 else if n_input = 0 then
114 ([], [])
115 else
116 (* open csv file *)
117 let file_string_array = Array.sub argv 4 n_input in
118 let make_chemin s = io_macro_to_string Input_Route_string ^ s in
119 let file_chemin_string_array = Array.map make_chemin file_string_array in
120 let file_array = Array.map open_in file_chemin_string_array in
121 let file_list = Array.to_list file_array in
122
123 (* read sample rates and data *)
124 let rate_list = Array.to_list (Array.create n_input 0) in
125 let data_float_array_list = List.map csvread file_list in
126 let _ = List.map close_in file_list in
127 (rate_list, data_float_array_list);;
128
129
130 let write_output_csv = fun channel_int_list -> fun data_float_array_list ->
131 let () = print_string(" Faustine -> Writing csv files...") in
132 let tic = Sys.time () in
133
134 (* make output txt file names : output0, output1, ... *)
135 let n_output = List.length data_float_array_list in
136 let n_array = Array.init n_output (fun n -> n) in
137 let make_file_name i = "output" ^ (string_of_int i) ^ ".csv" in
138
139 (* make output wave file routes *)
140 let make_chemin s = io_macro_to_string Output_Route_string ^ s in
141 let file_name_string_array = Array.map make_file_name n_array in
142 let file_chemin_string_array = Array.map make_chemin file_name_string_array in
143 let file_chemin_string_list = Array.to_list file_chemin_string_array in
144
145 (* open output channels *)
146 let file_list = List.map open_out file_chemin_string_list in
147 let data_string_array_list = List.map (Array.map string_of_float) data_float_array_list in
148 let array_to_string = fun data_string_array -> fun channel_int ->
149 let data_length = Array.length data_string_array in
150 let rec to_string_rec =
151 fun data -> fun channel -> fun n -> fun i -> fun column ->
152 if i < n then
153 (
154 let element = data.(i) in
155 if column < (channel - 1) then
156 element ^ "," ^ (to_string_rec data channel n (i + 1) (column + 1))
157 else if column = (channel - 1) then
158 element ^ "\n" ^ (to_string_rec data channel n (i + 1) 0)
159 else raise (Invalid_argument "write_output_txt.")
160 )
161 else "" in
162 to_string_rec data_string_array channel_int data_length 0 0 in
163
164 let data_string_list = List.map2 array_to_string data_string_array_list channel_int_list in
165 let _ = List.map2 output_string file_list data_string_list in
166 let _ = List.map close_out file_list in
167 let toc = Sys.time () in
168 print_endline(" Done. (duration: " ^ (string_of_float (toc -. tic)) ^ "s)");;
169
170 *)