Adding new source file faustio.ml, including class waveio and class csvio.
[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 Beam;;
10 open Aux;;
11
12 let default_output_path = "../output_sounds/";;
13
14 class virtual io =
15 object
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) ->
20 let data =
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)
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 (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
44 new beam signals
45
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
51
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
55
56 let files =
57 let channels =
58 let get_channel = fun l -> fun s ->
59 match s.(l - 1)#get with
60 | Vec vec -> vec#size
61 | _ -> 1 in
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
67
68 let () =
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
74 Print_endline
75 (" Done. (duration: " ^ (string_of_float duration) ^ "s)") in
76 paths
77 end;;
78
79 (*
80 class csvio : io_type =
81 object (self)
82 inherit io
83 method private csvread =
84 method read : string array -> beam =
85
86
87
88 method write : value_type array array -> string array
89 end;;
90 *)
91
92 let csvread = fun (ic : in_channel) ->
93 let string_list = ref [] in
94 try
95 while true do
96 string_list := !string_list @ [(input_line ic)]
97 done;
98 [||]
99 with End_of_file ->
100 (*let () = print_endline(List.nth !string_list 0) in*)
101 Array.of_list (List.map float_of_string !string_list);;
102
103 let read_input_csv = fun argv ->
104 let n_input = (Array.length argv) - 4 in
105 if n_input < 0 then
106 raise Missing_Expression
107 else if n_input = 0 then
108 ([], [])
109 else
110 (* open csv file *)
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
116
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);;
122
123
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
127
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
132
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
138
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 ->
146 if i < n then
147 (
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.")
154 )
155 else "" in
156 to_string_rec data_string_array channel_int data_length 0 0 in
157
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)");;
163