Skip to content

Commit

Permalink
Replace float array with floatarray
Browse files Browse the repository at this point in the history
  • Loading branch information
gridbugs committed Jul 28, 2023
1 parent 12f2c91 commit 17c8497
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/core/dsl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ val chebyshev_high_pass_filter :
float t

val sample_and_hold : float t -> bool t -> float t
val sample_player_mono : float array -> bool t -> float t
val sample_player_mono : floatarray -> bool t -> float t
val bitwise_trigger_sequencer : int -> int t list -> bool t -> bool t list
val delay : 'a t -> time_s:float t -> fill:'a -> 'a t
val clock_delay : float -> bool t -> bool t
Expand Down
10 changes: 6 additions & 4 deletions src/core/modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,19 @@ module Sample_and_hold = struct
end

module Sample_player_mono = struct
type t = { data : float array; trigger : bool Signal.t }
type t = { data : floatarray; trigger : bool Signal.t }

let signal t =
Raw.with_state' ~init:(Array.length t.data) ~f:(fun index ctx ->
Raw.with_state' ~init:(Float.Array.length t.data) ~f:(fun index ctx ->
if Signal.sample t.trigger ctx then (0, 0.0)
else
(* deliberately allow index to be 1 out of bounds to indicate that the sample is finished *)
let sample =
if index < Array.length t.data then Array.get t.data index else 0.0
if index < Float.Array.length t.data then
Float.Array.get t.data index
else 0.0
in
let index = Int.min (index + 1) (Array.length t.data) in
let index = Int.min (index + 1) (Float.Array.length t.data) in
(index, sample))
|> Signal.of_raw
end
Expand Down
2 changes: 1 addition & 1 deletion src/core/modules.mli
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module Sample_and_hold : sig
end

module Sample_player_mono : sig
type t = { data : float array; trigger : bool Signal.t }
type t = { data : floatarray; trigger : bool Signal.t }

val signal : t -> float Signal.t
end
Expand Down
14 changes: 7 additions & 7 deletions src/interactive/window.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ module Visualization_style = struct
end

module Sample_buffer = struct
type t = { samples : float array; mutable next_i : int }
type t = { samples : floatarray; mutable next_i : int }

let create size =
let samples = Array.init size ~f:(Fun.const 0.0) in
let samples = Float.Array.init size (Fun.const 0.0) in
{ samples; next_i = 0 }

let length t = t.next_i
let is_full t = t.next_i == Array.length t.samples
let is_full t = t.next_i == Float.Array.length t.samples

let append_unless_full t sample =
if not (is_full t) then (
Array.set t.samples t.next_i sample;
Float.Array.set t.samples t.next_i sample;
t.next_i <- t.next_i + 1)

let clear t = t.next_i <- 0
Expand All @@ -53,8 +53,8 @@ module Sample_buffer = struct
let rec loop i =
if i >= t.next_i then None
else
let prev = Array.get t.samples (i - 1) in
let current = Array.get t.samples i in
let prev = Float.Array.get t.samples (i - 1) in
let current = Float.Array.get t.samples i in
if prev <= 0.0 && current >= 0.0 then Some i else loop (i + 1)
in
loop 1
Expand All @@ -63,7 +63,7 @@ module Sample_buffer = struct
let rec loop i count =
if i >= t.next_i || count >= max_iterations then ()
else (
f count (Array.get t.samples i);
f count (Float.Array.get t.samples i);
loop (i + stride) (count + 1))
in
loop offset 0
Expand Down
4 changes: 2 additions & 2 deletions src/low-level/llama_low_level.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module Wav = struct
let read_wav_file_mono_exn path =
(* TODO: This array can't be indexed with [Array.get] but values can be
obtained with (say) [Array.iter]. This is possibly a bug in the ocaml
rust bindings for the [float array] special case. *)
rust bindings for the [floatarray] special case. *)
let broken_array = read_wav_file_mono_exn_raw path in
Array.of_list (Array.to_list broken_array)
Float.Array.of_seq (Array.to_seq broken_array)
end

module Output_stream = struct
Expand Down
2 changes: 1 addition & 1 deletion src/low-level/llama_low_level.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module System : sig
end

module Wav : sig
val read_wav_file_mono_exn : string -> float array
val read_wav_file_mono_exn : string -> floatarray
end

module Output_stream : sig
Expand Down

0 comments on commit 17c8497

Please sign in to comment.