From 0572bd66b8c68976fd43b7c28c654498399e77c2 Mon Sep 17 00:00:00 2001 From: Samuel Mimram Date: Wed, 16 Oct 2024 17:05:22 +0200 Subject: [PATCH] Minor. --- src/MIDI.ml | 44 ++++++++++++++++++++++---------------------- src/MIDI.mli | 13 +++++++++---- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/MIDI.ml b/src/MIDI.ml index 8862bbb8..88b69e49 100644 --- a/src/MIDI.ml +++ b/src/MIDI.ml @@ -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 = @@ -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 = { diff --git a/src/MIDI.mli b/src/MIDI.mli index e85f4bac..16f6127d 100644 --- a/src/MIDI.mli +++ b/src/MIDI.mli @@ -63,6 +63,8 @@ type event = | Key_signature of int * bool | Custom of string +(** {2 Buffers} *) + (** A MIDI buffer. *) type buffer @@ -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 @@ -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. *)