Skip to content

Commit

Permalink
Minor.
Browse files Browse the repository at this point in the history
  • Loading branch information
smimram committed Oct 16, 2024
1 parent 63cfda5 commit 0572bd6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
44 changes: 22 additions & 22 deletions src/MIDI.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ type event =
(* Tempo is in microseconds per quarter. *)
let samples_of_delta samplerate division tempo delta =
match division with
| Ticks_per_quarter tpq ->
(* These computations sometimes overflow on 32 bits. *)
let tpq = Int64.of_int tpq in
let tempo = Int64.of_int tempo in
let tps = Int64.of_int samplerate in
let ten = Int64.of_int 1000000 in
let delta = Int64.of_int delta in
let ( * ) = Int64.mul in
let ( / ) = Int64.div in
Int64.to_int (delta * tempo / tpq * tps / ten)
| SMPTE (fps, res) -> samplerate * delta / (fps * res)
| Ticks_per_quarter tpq ->
(* These computations sometimes overflow on 32 bits. *)
let tpq = Int64.of_int tpq in
let tempo = Int64.of_int tempo in
let tps = Int64.of_int samplerate in
let ten = Int64.of_int 1000000 in
let delta = Int64.of_int delta in
let ( * ) = Int64.mul in
let ( / ) = Int64.div in
Int64.to_int (delta * tempo / tpq * tps / ten)
| SMPTE (fps, res) -> samplerate * delta / (fps * res)

(*
let delta_of_samples samplerate division tempo samples =
Expand Down Expand Up @@ -100,17 +100,17 @@ let encode_event chan e =
let coi = char_of_int in
let bof = byte_of_float in
(match e with
| Note_off (n, v) ->
Bytes.set s 0 (coi ((0x8 lsl 4) + chan));
Bytes.set s 1 (coi n);
Bytes.set s 2 (bof v)
| Note_on (n, v) ->
Bytes.set s 0 (coi ((0x9 lsl 4) + chan));
Bytes.set s 1 (coi n);
Bytes.set s 2 (bof v)
| _ ->
(* TODO *)
assert false);
| Note_off (n, v) ->
Bytes.set s 0 (coi ((0x8 lsl 4) + chan));
Bytes.set s 1 (coi n);
Bytes.set s 2 (bof v)
| Note_on (n, v) ->
Bytes.set s 0 (coi ((0x9 lsl 4) + chan));
Bytes.set s 1 (coi n);
Bytes.set s 2 (bof v)
| _ ->
(* TODO *)
assert false);
Bytes.to_string s

type buffer = {
Expand Down
13 changes: 9 additions & 4 deletions src/MIDI.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type event =
| Key_signature of int * bool
| Custom of string

(** {2 Buffers} *)

(** A MIDI buffer. *)
type buffer

Expand All @@ -84,15 +86,19 @@ val add : buffer -> int -> buffer -> int -> int -> unit
val clear_all : buffer -> unit
val insert : buffer -> int * event -> unit

(** Multitrack buffers. *)
module Multitrack : sig
(** A multitrack buffer. *)
type t = buffer array
type buffer = t

(** Channels. *)
val channels : buffer -> int

(** Duration. *)
val duration : buffer -> int

(** Create a multitrack MIDI buffer with given number of channels and length
in samples. *)
(** Create a multitrack MIDI buffer with given number of channels and length in samples. *)
val create : int -> int -> buffer

val clear : ?channel:int -> buffer -> int -> int -> unit
Expand All @@ -102,8 +108,7 @@ module IO : sig
module Reader : sig
class type t =
object
(** Read data at with given samplerate for events, in a given track, with a
given length in samples. *)
(** Read data at with given samplerate for events, in a given track, with a given length in samples. *)
method read : int -> Multitrack.buffer -> int -> int -> int

(** Close the stream. *)
Expand Down

0 comments on commit 0572bd6

Please sign in to comment.