package mm
The mm library contains high-level APIs to create and manipulate multimedia streams (audio, video, MIDI)
Install
Dune Dependency
Authors
Maintainers
Sources
v0.8.6.tar.gz
md5=003b6e873fe6158dda4627bb674fb57b
sha512=c486e8eaa5dd25a2629c9486c4048ffa2cdeae9e56f73bc8d01a86413038dd3473ebd383abb06f08a2a24a78a335f22aede98ac92436e42a9c6eb1a856f92dab
doc/src/mm.midi/synth.ml.html
Source file synth.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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
(* * Copyright 2011 The Savonet Team * * This file is part of ocaml-mm. * * ocaml-mm is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * ocaml-mm is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with ocaml-mm; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * As a special exception to the GNU Library General Public License, you may * link, statically or dynamically, a "work that uses the Library" with a publicly * distributed version of the Library to produce an executable file containing * portions of the Library, and distribute that executable file under terms of * your choice, without any of the additional requirements listed in clause 6 * of the GNU Library General Public License. * By "a publicly distributed version of the Library", we mean either the unmodified * Library as distributed by The Savonet Team, or a modified version of the Library that is * distributed under the conditions defined in clause 3 of the GNU Library General * Public License. This exception does not however invalidate any other reasons why * the executable file might be covered by the GNU Library General Public License. * *) open Mm_audio class type t = object method set_volume : float -> unit method note_on : int -> float -> unit method note_off : int -> float -> unit method fill_add : Audio.buffer -> int -> int -> unit method play_add : MIDI.buffer -> int -> Audio.buffer -> int -> int -> unit method play : MIDI.buffer -> int -> Audio.buffer -> int -> int -> unit method reset : unit end type synth = t type note = { note : int; volume : float; generator : Audio.Generator.t } class virtual base = object (self) method virtual private generator : float -> float -> Audio.Generator.t val mutable vol : float = 1. method set_volume v = vol <- v val mutable notes : note list = [] method note_on n v = let note = { note = n; volume = v; (* TODO: we could want to change the volume after a not has begun to be played *) generator = self#generator (Audio.Note.freq n) (v *. vol); } in notes <- note :: notes method note_off n _ = (* TODO: remove only one note *) (* TODO: merge the two iterations on the list *) List.iter (fun note -> if note.note = n then note.generator#release) notes; notes <- List.filter (fun note -> not note.generator#dead) notes method fill_add buf ofs len = List.iter (fun note -> note.generator#fill_add buf ofs len) notes method private fill buf ofs len = Audio.clear buf ofs len; self#fill_add buf ofs len method private event = function | MIDI.Note_off (n, v) -> self#note_off n v | MIDI.Note_on (n, v) -> self#note_on n v | MIDI.Control_change (0x7, v) -> self#set_volume (float v /. 127.) | _ -> () (* TODO: add offset for evs *) method play_add evs eofs buf bofs len = let rec play o evs ofs = match evs with | (t, _) :: _ when t >= eofs + len -> () | (t, _) :: tl when t < eofs -> play t tl ofs | (t, e) :: tl -> let delta = t - max eofs o in self#fill_add buf (bofs + ofs) delta; self#event e; play t tl (ofs + delta) | [] -> self#fill_add buf (bofs + ofs) (len - o) in play 0 (MIDI.data evs) 0 method play evs eofs buf bofs len = Audio.clear buf bofs len; self#play_add evs eofs buf bofs len method reset = notes <- [] end class create g = object inherit base method private generator f v = g f v end class create_mono g = create (fun f v -> new Audio.Generator.of_mono (g f v)) let might_adsr adsr g = match adsr with None -> g | Some a -> new Audio.Mono.Generator.adsr a g class sine ?adsr sr = create_mono (fun f v -> might_adsr adsr (new Audio.Mono.Generator.sine sr ~volume:v f)) class square ?adsr sr = create_mono (fun f v -> might_adsr adsr (new Audio.Mono.Generator.square sr ~volume:v f)) class saw ?adsr sr = create_mono (fun f v -> might_adsr adsr (new Audio.Mono.Generator.saw sr ~volume:v f)) class monophonic (g : Audio.Generator.t) = object (self) method set_volume v = g#set_volume v method note_on n v = g#set_frequency (Audio.Note.freq n); g#set_volume v method note_off (_ : int) (_ : float) = (* TODO: check for the last note? *) g#release method fill_add = g#fill_add method play_add (_ : MIDI.buffer) (_ : int) (_ : Audio.buffer) (_ : int) (_ : int) : unit = assert false method play evs eofs buf bofs len : unit = self#play_add evs eofs buf bofs len; assert false method reset = g#set_volume 0. end module Multitrack = struct class type t = object method play_add : MIDI.Multitrack.buffer -> int -> Audio.buffer -> int -> int -> unit method play : MIDI.Multitrack.buffer -> int -> Audio.buffer -> int -> int -> unit end class create n (f : int -> synth) = object (self) val synth = Array.init n f method play_add (evs : MIDI.Multitrack.buffer) eofs buf bofs len = for c = 0 to Array.length synth - 1 do synth.(c)#play_add evs.(c) eofs buf bofs len done method play evs eofs buf bofs len = Audio.clear buf bofs len; self#play_add evs eofs buf bofs len end end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>