Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix demands and mixing #28

Merged
merged 3 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 17 additions & 11 deletions lib/membrane_audio_mixer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/membrane_audio_mixer/clip_preventing_adder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down