diff --git a/README.md b/README.md index 0cae187..9778bc6 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ It is a part of [Membrane Multimedia Framework](https://membraneframework.org). Add the following line to your `deps` in `mix.exs`. Run `mix deps.get`. ```elixir - {:membrane_audio_mix_plugin, "~> 0.11.0"} + {:membrane_audio_mix_plugin, "~> 0.11.1"} ``` ## Description diff --git a/lib/membrane_audio_mixer.ex b/lib/membrane_audio_mixer.ex index 5adb4bd..e77f68a 100644 --- a/lib/membrane_audio_mixer.ex +++ b/lib/membrane_audio_mixer.ex @@ -236,7 +236,7 @@ defmodule Membrane.AudioMixer do pad_ref, %Buffer{payload: payload, pts: pts}, false, - _context, + context, %{caps: caps, pads: pads, last_ts_sent: last_ts_sent} = state ) do offset = get_in(pads, [pad_ref, :offset]) @@ -256,7 +256,10 @@ defmodule Membrane.AudioMixer do state end - {{:ok, redemand: :output}, state} + size = byte_size(get_in(state, [:pads, pad_ref, :queue])) + time_frame = RawAudio.frame_size(caps) + + mix_or_redemand(size, time_frame, context, state) end defp do_handle_process( @@ -266,8 +269,6 @@ defmodule Membrane.AudioMixer do context, %{caps: caps, pads: pads} = state ) do - time_frame = RawAudio.frame_size(caps) - {size, pads} = Map.get_and_update( pads, @@ -277,13 +278,8 @@ defmodule Membrane.AudioMixer do end ) - if size >= time_frame do - {actions, state} = mix_and_get_actions(context, %{state | pads: pads}) - actions = if actions == [], do: [redemand: :output], else: actions - {{:ok, actions}, state} - else - {{:ok, redemand: :output}, %{state | pads: pads}} - end + time_frame = RawAudio.frame_size(caps) + mix_or_redemand(size, time_frame, context, %{state | pads: pads}) end @impl true @@ -324,6 +320,16 @@ defmodule Membrane.AudioMixer do ) end + defp mix_or_redemand(size, time_frame, context, state) do + if size >= time_frame do + {actions, state} = mix_and_get_actions(context, state) + actions = if actions == [], do: [redemand: :output], else: actions + {{:ok, actions}, state} + else + {{:ok, redemand: :output}, state} + end + end + defp initialize_mixer_state(nil, _state), do: nil defp initialize_mixer_state(caps, state) do diff --git a/lib/membrane_audio_mixer/clip_preventing_adder.ex b/lib/membrane_audio_mixer/clip_preventing_adder.ex index 93c28f1..03b2333 100644 --- a/lib/membrane_audio_mixer/clip_preventing_adder.ex +++ b/lib/membrane_audio_mixer/clip_preventing_adder.ex @@ -58,8 +58,13 @@ defmodule Membrane.AudioMixer.ClipPreventingAdder do {values, rest} = Enum.split_while(values, split_fun) if !is_last_wave && rest == [] do - state = %__MODULE__{state | queue: state.queue ++ values} - {buffer, state} + if Enum.all?(values, fn value -> value == 0 end) do + {Enum.map(values, &RawAudio.value_to_sample(&1, state.caps)) |> IO.iodata_to_binary(), + state} + else + state = %__MODULE__{state | queue: state.queue ++ values} + {buffer, state} + end else buffer = [buffer | get_iodata(values, state)] |> IO.iodata_to_binary() diff --git a/mix.exs b/mix.exs index 47f2bea..d112ffe 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Membrane.AudioMix.Mixfile do use Mix.Project - @version "0.11.0" + @version "0.11.1" @github_url "https://github.com/membraneframework/membrane_audio_mix_plugin" def project do