Skip to content

Commit

Permalink
Merge pull request #41 from membraneframework/fix/live-silence-proces…
Browse files Browse the repository at this point in the history
…sing

Fix handling silence
  • Loading branch information
mat-hek authored Jul 7, 2023
2 parents 38bd3eb + 1ca4ab2 commit cc8fd98
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
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.15.1"}
{:membrane_audio_mix_plugin, "~> 0.15.2"}
```

## Description
Expand Down
39 changes: 13 additions & 26 deletions lib/membrane_audio_mixer/clip_preventing_adder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,22 @@ defmodule Membrane.AudioMixer.ClipPreventingAdder do
split_fun = if state.is_wave_positive, do: &(&1 >= 0), else: &(&1 <= 0)
{values, rest} = Enum.split_while(values, split_fun)

silence? = Enum.all?(values, fn value -> value == 0 end)
if rest != [] or is_last_wave do
buffer = [buffer | get_iodata(values, state)] |> IO.iodata_to_binary()

cond do
rest != [] or is_last_wave ->
buffer = [buffer | get_iodata(values, state)] |> IO.iodata_to_binary()

state =
state
|> Map.put(:is_wave_positive, !state.is_wave_positive)
|> Map.put(:queue, [])

if is_last_wave && rest == [] do
{buffer, state}
else
add_values(rest, is_last_wave, state, buffer)
end
state =
state
|> Map.put(:is_wave_positive, !state.is_wave_positive)
|> Map.put(:queue, [])

silence? ->
{values_to_raw_sample(values, state.stream_format), state}

true ->
state = %__MODULE__{state | queue: state.queue ++ values}
if rest == [] do
{buffer, state}
else
add_values(rest, is_last_wave, state, buffer)
end
else
state = %__MODULE__{state | queue: state.queue ++ values}
{buffer, state}
end
end

Expand All @@ -104,10 +97,4 @@ defmodule Membrane.AudioMixer.ClipPreventingAdder do
end

defp do_scale(values, coefficient), do: Enum.map(values, &trunc(&1 * coefficient))

defp values_to_raw_sample(values, stream_format) do
values
|> Enum.map(fn value -> RawAudio.value_to_sample(value, stream_format) end)
|> IO.iodata_to_binary()
end
end
17 changes: 15 additions & 2 deletions lib/membrane_live_audio_mixer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Membrane.LiveAudioMixer do
- If false, overflow will be clipped to the maximal value of the sample in
the format. See `Membrane.AudioMixer.Adder`.
""",
default: true
default: false
],
native_mixer: [
spec: boolean(),
Expand Down Expand Up @@ -101,6 +101,15 @@ defmodule Membrane.LiveAudioMixer do

@impl true
def handle_init(_ctx, options) do
# TODO: native and prevent_clipping adder enqueue silence.
# in live mixer we want to immediately return added audio
if options.native_mixer or options.prevent_clipping do
Membrane.Logger.warn("""
Leaving options prevent_clipping and native_mixer as defaults is recommended.
In other case silence will be enqueued by mixer and send only when there will be some sound"
""")
end

cond do
options.native_mixer && !options.prevent_clipping ->
raise "Invalid element options, for native mixer only clipping preventing one is available"
Expand Down Expand Up @@ -231,7 +240,11 @@ defmodule Membrane.LiveAudioMixer do
{[], payload, state}
end

{[buffer: {:output, %Buffer{payload: payload}}] ++ actions, state}
if payload == <<>> do
{actions, state}
else
{[buffer: {:output, %Buffer{payload: payload}}] ++ actions, state}
end
end

@impl true
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.15.1"
@version "0.15.2"
@github_url "https://github.com/membraneframework/membrane_audio_mix_plugin"

def project do
Expand Down

0 comments on commit cc8fd98

Please sign in to comment.