From aca033a38934309ca07ef45f60d75d64c90f5e42 Mon Sep 17 00:00:00 2001 From: Romain Beauxis Date: Thu, 10 Oct 2024 13:38:15 -0500 Subject: [PATCH] Reset last metadata on new track by default. Add a source method to change the default. --- src/core/lang_source.ml | 11 ++++++++++- src/core/source.ml | 9 +++++++++ src/core/source.mli | 3 +++ tests/regression/append.liq | 29 ++++++++++++++++------------- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/core/lang_source.ml b/src/core/lang_source.ml index eb6c3c630a..53fe0231ac 100644 --- a/src/core/lang_source.ml +++ b/src/core/lang_source.ml @@ -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.", diff --git a/src/core/source.ml b/src/core/source.ml index a34dcc9da6..2b75c74235 100644 --- a/src/core/source.ml +++ b/src/core/source.ml @@ -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 @@ -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.."; diff --git a/src/core/source.mli b/src/core/source.mli index 8dfda5e6bc..71cc42efaa 100644 --- a/src/core/source.mli +++ b/src/core/source.mli @@ -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 diff --git a/tests/regression/append.liq b/tests/regression/append.liq index fc353283a2..468248e2fa 100644 --- a/tests/regression/append.liq +++ b/tests/regression/append.liq @@ -1,8 +1,7 @@ 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) @@ -10,17 +9,21 @@ 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)