package tiny_libs

  1. Overview
  2. Docs
From-scratch libraries for teaching: graphics, audio, compression, crypto, networking and more

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.3.6.tar.gz
md5=7c636383d146d30ac6f2fa234a6253c8
sha512=c79f3823c5f8f57e5038eb640d487c61168b84aa07c61999d6622ef9fd0c890e2b03b4c6a7cdbbe9352a49e25dda00ac7bb14693cee8e3d7beeed251351a2af0

doc/src/tiny_libs.audio_mod/Paula.ml.html

Source file Paula.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(* Claude Code
 *
 * Copyright (C) 2026 Yoann Padioleau
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * (LGPL) as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *)

(* See Paula.mli *)

type reading = Hold | Linear | Cubic

let readings = [ Hold; Linear; Cubic ]
let reading_name = function Hold -> "hold (Paula's)" | Linear -> "linear" | Cubic -> "cubic"

(* the sample, where in it (in bytes, fractional), the loop, the period,
 * the volume; silent when [sample] is empty *)
type t = {
  mutable sample : Signal.t;
  mutable pos : float;
  mutable loop_start : int;
  mutable loop_length : int;
  mutable period : int;
  mutable volume : int;
}

let create () : t = { sample = [||]; pos = 0.; loop_start = 0; loop_length = 0; period = 428; volume = 64 }

let trigger (c : t) (sample : Signal.t) ~(loop_start : int) ~(loop_length : int) ~(offset : int) : unit =
  c.sample <- sample;
  c.pos <- float_of_int offset;
  c.loop_start <- loop_start;
  c.loop_length <- loop_length

let set_period (c : t) (p : int) : unit = if p > 0 then c.period <- p
let set_volume (c : t) (v : int) : unit = c.volume <- max 0 (min 64 v)
let stop (c : t) : unit = c.sample <- [||]
let playing (c : t) : bool = Array.length c.sample > 0

let next (reading : reading) (c : t) : float =
  let n = Array.length c.sample in
  if n = 0 then 0.
  else
    (* past the loop's end, back by its length; past the sample's end
     * with no loop, silent *)
    let loop_end = c.loop_start + c.loop_length in
    if c.loop_length > 0 && c.pos >= float_of_int loop_end then c.pos <- c.pos -. float_of_int c.loop_length;
    if c.pos >= float_of_int n then (
      c.sample <- [||];
      0.)
    else
      let x =
        match reading with
        | Hold -> c.sample.(int_of_float c.pos)
        | Linear -> Resample.read Linear c.sample c.pos
        | Cubic -> Resample.read Cubic c.sample c.pos
      in
      c.pos <- c.pos +. (Mod.paula_clock /. float_of_int c.period /. float_of_int Signal.rate);
      x *. float_of_int c.volume /. 64.