Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
abbysmal committed Jan 25, 2024
1 parent a899ec8 commit 75a425a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
13 changes: 11 additions & 2 deletions bin/midi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Device = struct
type t = { device_id : int; device : Portmidi.Output_stream.t }

(* TODO: don't hardcode device_id. get it from the portmidi [get_device] *)
let create device_id =
let create_output device_id =
match Portmidi.open_output ~device_id ~buffer_size:0l ~latency:1l with
| Error _ ->
Printf.eprintf "Can't find midi device with id: %i\nIs it connected?\n"
Expand All @@ -23,8 +23,16 @@ module Device = struct
(* let err = Printf.sprintf "Can't find midi device with id %i.Is it connected?" device_id in failwith err *)
| Ok device -> { device; device_id }

let create_input device_id =
match Portmidi.open_input ~device_id ~buffer_size:1024l with
| Error _ ->
Printf.eprintf "Can't find midi device with id: %i\nIs it connected?\n"
device_id;
exit 1
| Ok device -> device

let turn_off_everything device_id =
let device = create device_id in
let device = create_output device_id in
let* _ =
Portmidi.write_output device.device
[
Expand All @@ -33,6 +41,7 @@ module Device = struct
in
Portmidi.close_output device.device


let shutdown { device; device_id } =
let* _ = Portmidi.close_output device in
Unix.sleepf 0.5;
Expand Down
4 changes: 3 additions & 1 deletion bin/midi.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ val error_to_string : Portmidi.Portmidi_error.t -> string
module Device : sig
type t

val create : int -> t
val create_output : int -> t
val create_input : int -> Portmidi.Input_stream.t

val shutdown : t -> (unit, Portmidi.Portmidi_error.t) result
end

Expand Down
2 changes: 1 addition & 1 deletion bin/play.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let play ~tracing device_id scale argv =
| prog :: args -> (prog, Array.of_list args)
| _ -> failwith "No program given"
in
let device = Midi.Device.create device_id in
let device = Midi.Device.create_output device_id in
let _ = handle_control_c () in
(* Extract the user supplied program and arguments. *)
let proc =
Expand Down
26 changes: 11 additions & 15 deletions bin/stat_engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,12 @@ let get_increment num_beats event =
Mutex.unlock quantifier_table_lock;
incr

let rec sequencer_main_func num_beats tones device bpm _ =
let rec sequencer_main_func num_beats tones device bpm queue _ =
let interesting_stuff =
let compare e1 e2 =
Int.neg (Event.compare ~f:(get_increment num_beats) e1 e2)
in
let sorted_events = List.sort compare Event.all in
List.iter
(fun e ->
Format.printf "%s: %f\n%!"
(Runtime_events.runtime_phase_name e)
(get_increment num_beats e))
sorted_events;
print_endline "-------------------";

let rec loop acc = function
| hd :: tl ->
Expand All @@ -97,12 +90,13 @@ let rec sequencer_main_func num_beats tones device bpm _ =
(function
| None -> ()
| Some event ->
let { Midi.Scale.note; volume } = Play.event_to_note tones event in
Midi.(
let { Midi.Scale.note; _ } = Play.event_to_note tones event in
(* Midi.(
write_output device
[ message_on ~note ~timestamp:0l ~volume ~channel:0 () ]);
[ message_on ~note ~timestamp:0l ~volume ~channel:0 () ]); *)
(*FIXME: don't sleep, but use a timestamp*)
Unix.sleepf (60. /. Float.of_int bpm /. Float.of_int n))
(* Unix.sleepf (60. /. Float.of_int bpm /. Float.of_int n)) *)
Queue.push (note, 24/n) queue)
interesting_stuff;
Mutex.lock event_table_lock;
Hashtbl.clear event_table;
Expand All @@ -111,15 +105,17 @@ let rec sequencer_main_func num_beats tones device bpm _ =
if Atomic.get Watchdog.terminate then
()
else
sequencer_main_func (num_beats + 1) tones device bpm ()
sequencer_main_func (num_beats + 1) tones device bpm queue ()

let tracing (bpm : int) device child_alive path_pid tones =
let queue = Queue.create () in
let polling_domain = Domain.spawn (polling_main_func path_pid) in
let sequencer_domain =
Domain.spawn (sequencer_main_func 1 tones device bpm)
Domain.spawn (sequencer_main_func 1 tones device bpm queue)
in
let watchdog_domain = Domain.spawn (Watchdog.watchdog_func child_alive) in
List.iter Domain.join [ watchdog_domain; polling_domain; sequencer_domain ]
let clock_domain = Domain.spawn (Read_clock.clock_domain_main (Read_clock.External 5) device queue) in
List.iter Domain.join [ watchdog_domain; polling_domain; sequencer_domain; clock_domain ]

let bpm = Arg.(value & opt int 120 & info [ "bpm"; "--bpm" ] ~docv:"BPM")
let stat_play bpm = Play.play ~tracing:(tracing bpm)
Expand Down

0 comments on commit 75a425a

Please sign in to comment.