(********************************************************************)
(*  OCaml code from the book ``Learn Programming with OCaml''       *)
(*  See https://usr.lmf.cnrs.fr/lpo/                                *)
(*                                                                  *)
(*  Sylvain Conchon and Jean-Christophe Filliâtre                   *)
(*  Copyright 2025 Université Paris-Saclay and CNRS                 *)
(*                                                                  *)
(*  Openly licensed via CC BY SA 4.0                                *)
(*  See https://creativecommons.org/licenses/by-sa/4.0/deed.en      *)
(********************************************************************)

(* Program 14 on page 105
   Playing a Musical Score *)

type note = Do | Re | Mi | Fa | Sol | La | Si
type pitch = { note : note; octave : int }
type duration = Half | Quarter
type symbol = Note of pitch * duration | Rest of duration
type score = { symbols : symbol list;  metronome : int }

let frequency { note = n; octave = o} =
  let f0 =
    match n with
      | Do -> 33
      | Re -> 37
      | Mi -> 41
      | Fa -> 44
      | Sol -> 49
      | La -> 55
      | Si -> 62
  in
  f0 * truncate (2. ** float o)

let millisecondes d t =
  let quarter = 60000 / t in
  match d with
    | Half -> quarter * 2
    | Quarter -> quarter

let sound t s =
  match s with
    | Note (p, d) ->
        let f = frequency p in
        Graphics.sound f (millisecondes d t)
    | Rest r ->
        Graphics.sound 0 (millisecondes r t)

let play_score { symbols = l; metronome = t } =
    List.iter (sound t) l

This document was generated using caml2html