From f3caf3d551b4ba1cd98d55678f0fd8085522669f Mon Sep 17 00:00:00 2001 From: WANG Date: Mon, 30 Sep 2013 17:41:30 +0200 Subject: [PATCH] Stdin, stdout and stderr updated, tested. --- examples/2d_fft/Makefile | 2 +- examples/close/Makefile | 2 +- examples/erosion/Makefile | 2 +- examples/fft/Makefile | 4 +- examples/licenceplate/Makefile | 2 +- examples/open/Makefile | 2 +- examples/primitives/Makefile | 5 +- interpreter/faustio.ml | 158 +++++++++++++++++++-------------- interpreter/main.ml | 11 +-- interpreter/types.ml | 2 +- 10 files changed, 103 insertions(+), 87 deletions(-) diff --git a/examples/2d_fft/Makefile b/examples/2d_fft/Makefile index 553124f..028583d 100644 --- a/examples/2d_fft/Makefile +++ b/examples/2d_fft/Makefile @@ -14,7 +14,7 @@ $(IMGOUT): $(CSVOUT) octave -qf img_write.m $(CSVOUT): $(SRC) $(CSVIN) - faustine -f $(SRC) -i $(CSVIN) -l $(LINES) --oformat $(FORMAT) --obasename $(BASENAME) + faustine -f $(SRC) -i $(CSVIN) -l $(LINES) > $@ $(CSVIN): $(IMGIN) octave -qf img_read.m diff --git a/examples/close/Makefile b/examples/close/Makefile index 22c4885..ca62757 100644 --- a/examples/close/Makefile +++ b/examples/close/Makefile @@ -14,7 +14,7 @@ $(IMGOUT): $(CSVOUT) octave -qf img_write.m $(CSVOUT): $(SRC) $(CSVIN) - faustine -f $(SRC) -i $(CSVIN) -l $(LINES) --oformat $(FORMAT) --obasename $(BASENAME) + faustine -f $(SRC) -i $(CSVIN) -l $(LINES) > $@ $(CSVIN): $(IMGIN) octave -qf img_read.m diff --git a/examples/erosion/Makefile b/examples/erosion/Makefile index d70affe..6e7d90c 100644 --- a/examples/erosion/Makefile +++ b/examples/erosion/Makefile @@ -14,7 +14,7 @@ $(IMGOUT): $(CSVOUT) octave -qf img_write.m $(CSVOUT): $(SRC) $(CSVIN) - faustine -f $(SRC) -i $(CSVIN) -l $(LINES) --oformat $(FORMAT) --obasename $(BASENAME) + faustine -f $(SRC) -i $(CSVIN) -l $(LINES) > $@ $(CSVIN): $(IMGIN) octave -qf img_read.m diff --git a/examples/fft/Makefile b/examples/fft/Makefile index 6d41558..67d69cc 100644 --- a/examples/fft/Makefile +++ b/examples/fft/Makefile @@ -16,10 +16,10 @@ $(IMGOUT): $(CSVOUT) octave -qf img_write.m $(CSVOUT): $(SRC) $(WAVIN) - faustine -f $(SRC) -i $(WAVIN) --oformat $(FORMAT) --obasename $(BASENAME) + faustine -f $(SRC) -i $(WAVIN) > $@ $(WAVIN): $(SINS) - faustine -f $(SINSUM) -l $(WAV_LENGTH) --oformat $(WAV_FORMAT) --obasename $(WAV_BASENAME) + faustine -f $(SINSUM) -l $(WAV_LENGTH) > $@ clean:: rm -f gmon.out $(CSVOUT) $(IMGOUT) $(WAVIN) *~ diff --git a/examples/licenceplate/Makefile b/examples/licenceplate/Makefile index a30591b..aa146f3 100644 --- a/examples/licenceplate/Makefile +++ b/examples/licenceplate/Makefile @@ -14,7 +14,7 @@ $(IMGOUT): $(CSVOUT) octave -qf img_write.m $(CSVOUT): $(SRC) $(CSVIN) - faustine -f $(SRC) -i $(CSVIN) -l $(LINES) --oformat $(FORMAT) --obasename $(BASENAME) + faustine -f $(SRC) -i $(CSVIN) -l $(LINES) > $@ $(CSVIN): $(IMGIN) octave -qf img_read.m diff --git a/examples/open/Makefile b/examples/open/Makefile index cdd7930..9089660 100644 --- a/examples/open/Makefile +++ b/examples/open/Makefile @@ -14,7 +14,7 @@ $(IMGOUT): $(CSVOUT) octave -qf img_write.m $(CSVOUT): $(SRC) $(CSVIN) - faustine -f $(SRC) -i $(CSVIN) -l $(LINES) --oformat $(FORMAT) --obasename $(BASENAME) + faustine -f $(SRC) -i $(CSVIN) -l $(LINES) > $@ $(CSVIN): $(IMGIN) octave -qf img_read.m diff --git a/examples/primitives/Makefile b/examples/primitives/Makefile index 7494e85..a119d26 100644 --- a/examples/primitives/Makefile +++ b/examples/primitives/Makefile @@ -3,11 +3,12 @@ SRC = $(EXAMPLE).dsp SAMPLES = 30 FILEOUT = primitives.csv +ERR_LOG = err_log.txt all: $(FILEOUT) $(FILEOUT): $(SRC) - faustine -f $(SRC) -l $(SAMPLES) 1> $@ 2> toto.txt + faustine -f $(SRC) -l $(SAMPLES) 1> $@ 2> $(ERR_LOG) clean:: - rm -f gmon.out $(FILEOUT) + rm -f gmon.out $(FILEOUT) $(ERR_LOG) diff --git a/interpreter/faustio.ml b/interpreter/faustio.ml index 9539bf3..8cca058 100644 --- a/interpreter/faustio.ml +++ b/interpreter/faustio.ml @@ -12,7 +12,7 @@ open Signal;; open Beam;; open Aux;; -exception Faustine_IO_Error of string;; +exception IO_Error of string;; let csv_read_buffer_length = 0xFFFF;; @@ -28,7 +28,7 @@ class virtual io = _filename <- filename; _basename <- basename; _dir <- dir method virtual read : string array -> beam - method virtual write : rate array -> data -> string array + method virtual write : rate array -> data -> string * string -> string array method private concat : data -> matrix = fun (origin : data) -> @@ -70,34 +70,37 @@ class waveio : io_type = array_map2 (new signal) rates (Array.map stream2func containers) in new beam signals - method write : rate array -> data -> string array = + method write : rate array -> data -> string * string -> string array = fun (rates : rate array) -> fun (output : data) -> - let n = Array.length output in - let paths = - if _filename = "" then - Array.init n (fun i -> - _dir ^ _basename ^ (string_of_int (i + 1)) ^ ".wav") - else if n = 1 then - let () = Unix.unlink _filename in [|_filename|] - else raise (Faustine_IO_Error ("The process has several output signals, - however stdout supports only one output signal. Please remove - the '> " ^ _filename ^ "'.")) in - let get_freq = fun (r : rate) -> r#to_int in - let freqs = Array.map get_freq rates in - - let files = - let channels = self#channels output in - let file_format = Sndfile.format - Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in - let openwr = fun path -> fun channel -> fun freq -> - Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, freq) path in - array_map3 openwr paths channels freqs in - + fun (info : string * string) -> + let stdoutput = fst info in + let basename = snd info in + let n = Array.length output in + let paths = + if n = 1 && stdoutput <> "" && basename = "" then + let () = Unix.unlink stdoutput in [|stdoutput|] + else if stdoutput = "" && basename <> "" then + Array.init n (fun i -> + _dir ^ _basename ^ (string_of_int (i + 1)) ^ ".wav") + else if stdoutput = "" && basename = "" then + raise (IO_Error "Please specify stdout or output basename.") + else raise (IO_Error "Stdout doesn't support multi-output process. Please remove '> stdout' and use --obasename --oformat.") in + let get_freq = fun (r : rate) -> r#to_int in + let freqs = Array.map get_freq rates in + + let files = + let channels = self#channels output in + let file_format = Sndfile.format + Sndfile.MAJOR_WAV Sndfile.MINOR_PCM_16 in + let openwr = fun path -> fun channel -> fun freq -> + Sndfile.openfile ~info:(Sndfile.RDWR, file_format, channel, freq) path in + array_map3 openwr paths channels freqs in + let data = self#concat output in let _ = array_map2 Sndfile.write files data in let _ = Array.map Sndfile.close files in - paths + paths end;; @@ -132,33 +135,39 @@ class csvio : io_type = let signals = Array.map self#csvread files in new beam signals - method write : rate array -> data -> string array = + method write : rate array -> data -> string * string -> string array = fun (rates : rate array) -> fun (data : data) -> - let n = Array.length data in - let paths = - if _filename = "" then - Array.init n (fun i -> - _dir ^ _basename ^ (string_of_int (i + 1)) ^ ".csv") - else if n = 1 then - let () = Unix.unlink _filename in [|_filename|] - else raise (Faustine_IO_Error ("The process has several output signals, - however stdout supports only one output signal. Please remove - the '> " ^ _filename ^ "'.")) in - let files = Array.map open_out paths in - let strings = - let value2string : float array -> string = - fun (v : float array) -> - let strings = Array.map string_of_float v in - String.concat "," (Array.to_list strings) in - let signal2string : float array array -> string = - fun (s : float array array) -> - let lines = Array.map value2string s in - String.concat "\n" (Array.to_list lines) in - Array.map signal2string data in - let _ = array_map2 output_string files strings in - let _ = Array.map close_out files in - paths + fun (info : string * string) -> + let stdoutput = fst info in + let basename = snd info in + let n = Array.length data in + let strings = + let value2string : float array -> string = + fun (v : float array) -> + let strings = Array.map string_of_float v in + String.concat "," (Array.to_list strings) in + let signal2string : float array array -> string = + fun (s : float array array) -> + let lines = Array.map value2string s in + String.concat "\n" (Array.to_list lines) in + Array.map signal2string data in + + if stdoutput = "" && basename = "" then + let _ = Array.map (output_string stdout) strings in + [|"Stdout"|] + else + let paths = + if n = 1 && stdoutput <> "" && basename = "" then + let () = Unix.unlink stdoutput in [|stdoutput|] + else if stdoutput = "" && basename <> "" then + Array.init n (fun i -> + _dir ^ _basename ^ (string_of_int (i + 1)) ^ ".csv") + else raise (IO_Error "Stdout doesn't support multi-output process. Please remove '> stdout' and use --obasename --oformat.") in + let files = Array.map open_out paths in + let _ = array_map2 output_string files strings in + let _ = Array.map close_out files in + paths end;; @@ -166,7 +175,7 @@ class iomanager = object (self) val wave = new waveio val csv = new csvio - val mutable _output_filename = "" + val mutable _filename = "" val mutable _dir = "" val mutable _format = "" val mutable _basename = "" @@ -190,29 +199,40 @@ class iomanager = fun (dir : string) -> fun (format : string) -> fun (basename : string) -> - _output_filename <- filename; + _filename <- filename; _dir <- dir; _format <- format; _basename <- basename; - wave#set _output_filename _dir _basename; - csv#set _output_filename _dir _basename + wave#set _filename _dir _basename; + csv#set _filename _dir _basename method write : rate array -> data -> string array = fun (rates : rate array) -> fun (data : data) -> - if _output_filename = "" then ( - if _format = "" then - raise (Invalid_argument "output format unset.") - else if _format = "wav" then - wave#write rates data - else if _format = "csv" then - csv#write rates data - else raise (Invalid_argument "unknown format.")) - else ( - let format = format_of_file _output_filename in - if format = "wav" then - wave#write rates data - else if format = "csv" then - csv#write rates data - else raise (Invalid_argument ("unknown format" ^ format))) + let n = Array.length rates in + if n = 1 then ( + if _filename <> "" then ( + let fmt = format_of_file _filename in + if fmt = "csv" then csv#write rates data (_filename, "") + else if fmt = "wav" then wave#write rates data (_filename, "") + else raise (IO_Error "Unknown stdout format.")) + else if _basename <> "" && _format <> "" then ( + if _format = "csv" then csv#write rates data ("", _basename) + else if _format = "wav" then wave#write rates data ("", _basename) + else raise (IO_Error "Unknown --oformat.")) + else if _filename = "" && _basename = "" && _format = "" then + csv#write rates data ("", "") + else raise (IO_Error "Please specify both --obasename and --oformat.")) + + else if n > 1 then ( + if _filename <> "" then + raise (IO_Error "Stdout doesn't support multi-output process. Please remove '> stdout' and use --obasename --oformat.") + else if _basename <> "" && _format <> "" then ( + if _format = "csv" then csv#write rates data ("", _basename) + else if _format = "wav" then wave#write rates data ("", _basename) + else raise (IO_Error "Unknown --oformat.")) + else raise (IO_Error "Please specify both --obasename and --oformat.")) + + else + [|"no output signal."|] end;; diff --git a/interpreter/main.ml b/interpreter/main.ml index 3c5ef42..7275d01 100644 --- a/interpreter/main.ml +++ b/interpreter/main.ml @@ -34,7 +34,7 @@ let size_input = ref 0;; let inputs = ref [];; let time_max = ref 0xFFFF;; let dir_output = ref "";; -let format_output = ref "csv";; +let format_output = ref "";; let basename_output = ref "";; let output = ref "";; @@ -77,20 +77,15 @@ let stdio = fun (x : unit) -> let main () = -(* - let () = print_endline (Unix.readlink "/proc/self/fd/0") in - let () = print_endline (Unix.readlink "/proc/self/fd/1") in - let () = print_endline (Unix.readlink "/proc/self/fd/2") in -*) - let () = stdio () in let () = Arg.parse speclist option_unknown option_usage in + let () = stdio () in let _ = Sys.signal Sys.sigalrm Sys.Signal_ignore in let _ = set_GC () in let io = new iomanager in let () = io#set !output !dir_output !format_output !basename_output in - let () = output_string stderr (" Faustine -> Reading input ...") in + let () = output_string stderr ("\n Faustine -> Reading input ...") in let tic0 = Unix.time () in let input : beam = io#read !inputs in let toc0 = Unix.time () in diff --git a/interpreter/types.ml b/interpreter/types.ml index da486f2..585b0ed 100644 --- a/interpreter/types.ml +++ b/interpreter/types.ml @@ -255,5 +255,5 @@ class type io_type = object method set : string -> string -> string -> unit method read : string array -> beam_type - method write : rate_type array -> data -> string array + method write : rate_type array -> data -> string * string -> string array end;; -- 2.20.1