Skip to content

Commit

Permalink
Reset last metadata on new track by default. Add a source method to
Browse files Browse the repository at this point in the history
change the default.
  • Loading branch information
toots committed Oct 10, 2024
1 parent 4dc4d24 commit aca033a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/core/lang_source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ let source_methods =
"Indicate if a source is ready to stream. This does not mean that the \
source is currently streaming, just that its resources are all properly \
initialized.",
fun (s : Source.source) -> val_fun [] (fun _ -> bool s#is_ready) );
fun s -> val_fun [] (fun _ -> bool s#is_ready) );
( "reset_last_metadata_on_track",
([], ref_t bool_t),
"If `true`, the source's `last_metadata` is reset on each new track. If \
a metadata is present along with the track mark, then it becomes the \
new `last_metadata`, otherwise, `last_metadata becomes `null`.",
fun s ->
reference
(fun () -> bool s#reset_last_metadata_on_track)
(fun b -> s#set_reset_last_metadata_on_track (to_bool b)) );
( "buffered",
([], fun_t [] (list_t (product_t string_t float_t))),
"Length of buffered data.",
Expand Down
9 changes: 9 additions & 0 deletions src/core/source.ml
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ class virtual operator ?(stack = []) ?clock ?(name = "src") sources =
initializer
self#on_before_streaming_cycle (fun () -> on_track_called <- false)

val mutable reset_last_metadata_on_track = Atomic.make true

method reset_last_metadata_on_track =
Atomic.get reset_last_metadata_on_track

method set_reset_last_metadata_on_track =
Atomic.set reset_last_metadata_on_track

val mutable on_track : (Frame.metadata -> unit) List.t = []
method on_track fn = on_track <- fn :: on_track

Expand All @@ -436,6 +444,7 @@ class virtual operator ?(stack = []) ?clock ?(name = "src") sources =
method private execute_on_track buf =
if not on_track_called then (
on_track_called <- true;
if self#reset_last_metadata_on_track then last_metadata <- None;
self#set_last_metadata buf;
let m = Option.value ~default:Frame.Metadata.empty last_metadata in
self#log#debug "calling on_track handlers..";
Expand Down
3 changes: 3 additions & 0 deletions src/core/source.mli
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ class virtual source :
(** The source's last metadata. *)
method last_metadata : Frame.metadata option

method reset_last_metadata_on_track : bool
method set_reset_last_metadata_on_track : bool -> unit

(** Register a callback to be called on new metadata *)
method on_metadata : (Frame.metadata -> unit) -> unit

Expand Down
29 changes: 16 additions & 13 deletions tests/regression/append.liq
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
music = chop(every=1., metadata=[("source", "s1")], sine(amplitude=0.1, 440.))

def next(_) =
s = sine(amplitude=0.1, duration=.5, 880.)
metadata.map(insert_missing=true, fun (_) -> [("source", "s2")], s)
sine(amplitude=0.1, duration=.5, 880.)
end

s = append(music, next)

count_s1 = ref(0)
count_s2 = ref(0)

s.on_metadata(fun (m) -> begin
s = m["source"]
if s == "s1" then
ref.incr(count_s1)
elsif s == "s2" then
ref.incr(count_s2)
end
s.on_track(
fun (m) ->
begin
s = m["source"]
if
s == "s1"
then
ref.incr(count_s1)
else
test.equal(m["source"], "")
ref.incr(count_s2)
end

if count_s1() > 2 and count_s2() > 2 then
test.pass()
end
end)
if count_s1() > 2 and count_s2() > 2 then test.pass() end
end
)

output.dummy(s)

0 comments on commit aca033a

Please sign in to comment.