Skip to content

Commit

Permalink
Merge pull request #122 from shwestrick/reverb-example
Browse files Browse the repository at this point in the history
New example: audio reverb effect on .wav files
  • Loading branch information
shwestrick committed Jul 21, 2020
2 parents ee37053 + feea807 commit 4a2c284
Show file tree
Hide file tree
Showing 10 changed files with 813 additions and 35 deletions.
3 changes: 2 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ PROGRAMS= \
tokens \
nn \
dedup \
nqueens
nqueens \
reverb

all: $(PROGRAMS)

Expand Down
9 changes: 9 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ $ make nn
$ bin/nn @mpl procs 4 -- -N 10000 -output result.ppm -resolution 1000
```

## Reverb

Applies an artificial reverberation effect to a `.wav` sound file. At the
moment, it only supports either 8-bit or 16-bit PCM, and if there
are multiple channels, these will be mixed into a mono-channel output.
```
$ make reverb
$ bin/reverb @mpl procs 4 -- INPUT.wav -output OUTPUT.wav
```
36 changes: 30 additions & 6 deletions examples/lib/CommandLineArgs.sml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ structure CommandLineArgs :
sig
(* each takes a key K and a default value D, looks for -K V in the
* command-line arguments, and returns V if it finds it, or D otherwise. *)
val parseString : string -> string -> string
val parseInt : string -> int -> int
val parseBool : string -> bool -> bool
val parseString: string -> string -> string
val parseInt: string -> int -> int
val parseReal: string -> real -> real
val parseBool: string -> bool -> bool

(* parseFlag K returns true if --K given on command-line *)
val parseFlag : string -> bool
val parseFlag: string -> bool

val positional : unit -> string list
val positional: unit -> string list
end =
struct

Expand All @@ -20,7 +21,21 @@ struct
)

fun positional () =
List.filter (not o String.isPrefix "-") (CommandLine.arguments ())
let
fun loop found rest =
case rest of
[] => List.rev found
| [x] => List.rev (if not (String.isPrefix "-" x) then x::found else found)
| x::y::rest' =>
if not (String.isPrefix "-" x) then
loop (x::found) (y::rest')
else if String.isPrefix "--" x then
loop found (y::rest')
else
loop found rest'
in
loop [] (CommandLine.arguments ())
end

fun search key args =
case args of
Expand All @@ -45,6 +60,15 @@ struct
NONE => die ("Cannot parse integer from \"-" ^ key ^ " " ^ s ^ "\"")
| SOME x => x

fun parseReal key default =
case search ("-" ^ key) (CommandLine.arguments ()) of
NONE => default
| SOME [] => die ("Missing argument of \"-" ^ key ^ "\" ")
| SOME (s :: _) =>
case Real.fromString s of
NONE => die ("Cannot parse real from \"-" ^ key ^ " " ^ s ^ "\"")
| SOME x => x

fun parseBool key default =
case search ("-" ^ key) (CommandLine.arguments ()) of
NONE => default
Expand Down

0 comments on commit 4a2c284

Please sign in to comment.