Skip to content

Commit

Permalink
Move to membrane_raw_audio_format and update credo | MS-43 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jodlos authored Mar 18, 2022
1 parent fbfee33 commit e33113d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ defmodule Resampling.Pipeline do

alias Membrane.Element.File
alias Membrane.FFmpeg.SWResample.Converter
alias Membrane.Caps.Audio.Raw
alias Membrane.RawAudio

@doc false
@impl true
def handle_init(_) do
children = [
file_src: %File.Source{location: "/tmp/input.raw"},
converter: %Converter{
input_caps: %Raw{channels: 2, format: :s24le, sample_rate: 48_000},
output_caps: %Raw{channels: 2, format: :f32le, sample_rate: 44_100}
input_caps: %RawAudio{channels: 2, sample_format: :s24le, sample_rate: 48_000},
output_caps: %RawAudio{channels: 2, sample_format: :f32le, sample_rate: 44_100}
},
file_sink: %File.Sink{location: "/tmp/output.raw"},
]
Expand Down
2 changes: 1 addition & 1 deletion c_src/membrane_ffmpeg_swresample_plugin/converter.spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ state_type "ConverterState"
# Function creating native handler of converter.
#
# Expects sample format (encoded as integer, using
# `Membrane.Caps.Audio.Raw.Format.serialize/1`), sample rate and number of channels
# `Membrane.RawAudio.SampleFormat.serialize/1`), sample rate and number of channels
# for input and output data, respectively.
#
# Currently supported formats are u8, s16le, s32le, f32le, f64le and s24le (input only)
Expand Down
22 changes: 12 additions & 10 deletions lib/membrane_ffmpeg_swresample_plugin/converter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
"""

use Membrane.Filter

import Mockery.Macro

alias Membrane.Caps.Audio.Raw, as: RawAudio
alias Membrane.Buffer
alias Membrane.{RawAudio, Buffer}
alias Membrane.Caps.Matcher
alias __MODULE__.Native

@supported_caps {RawAudio,
format: Matcher.one_of([:u8, :s16le, :s32le, :f32le, :f64le]),
sample_format: Matcher.one_of([:u8, :s16le, :s32le, :f32le, :f64le]),
channels: Matcher.one_of([1, 2])}

def_output_pad :output, caps: @supported_caps

def_input_pad :input,
demand_unit: :bytes,
caps: [@supported_caps, {RawAudio, format: :s24le, channels: one_of([1, 2])}]
caps: [@supported_caps, {RawAudio, sample_format: :s24le, channels: one_of([1, 2])}]

def_options input_caps: [
type: :caps,
Expand All @@ -37,7 +35,7 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
type: :caps,
spec: RawAudio.t(),
description: """
Audio caps for souce pad (output)
Audio caps for source pad (output)
"""
],
frames_per_buffer: [
Expand Down Expand Up @@ -132,14 +130,18 @@ defmodule Membrane.FFmpeg.SWResample.Converter do
end

defp mk_native(
%RawAudio{format: input_format, sample_rate: input_rate, channels: input_channels},
%RawAudio{format: out_format, sample_rate: out_rate, channels: out_channels}
%RawAudio{
sample_format: input_format,
sample_rate: input_rate,
channels: input_channels
},
%RawAudio{sample_format: out_format, sample_rate: out_rate, channels: out_channels}
) do
mockable(Native).create(
input_format |> RawAudio.Format.serialize(),
input_format |> RawAudio.SampleFormat.serialize(),
input_rate,
input_channels,
out_format |> RawAudio.Format.serialize(),
out_format |> RawAudio.SampleFormat.serialize(),
out_rate,
out_channels
)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Membrane.FFmpeg.SWResample.Mixfile do
defp deps do
[
{:membrane_core, "~> 0.9.0"},
{:membrane_caps_audio_raw, "~> 0.6.0"},
{:membrane_raw_audio_format, "~> 0.8.0"},
{:bunch, "~> 1.3.0"},
{:unifex, "~> 0.7.0"},
{:membrane_common_c, "~> 0.11.0"},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"coerce": {:hex, :coerce, "1.0.1", "211c27386315dc2894ac11bc1f413a0e38505d808153367bd5c6e75a4003d096", [:mix], [], "hexpm", "b44a691700f7a1a15b4b7e2ff1fa30bebd669929ac8aa43cffe9e2f8bf051cf1"},
"credo": {:hex, :credo, "1.6.4", "ddd474afb6e8c240313f3a7b0d025cc3213f0d171879429bf8535d7021d9ad78", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c28f910b61e1ff829bffa056ef7293a8db50e87f2c57a9b5c3f57eee124536b7"},
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
"earmark_parser": {:hex, :earmark_parser, "1.4.21", "7299db854f6d63730c15c8a781862889bb0fbf4432d7c306b3e63ce825d64baa", [:mix], [], "hexpm", "60664e1bdf7a02d8cbec2ac1d5b6fe0a68cf1d749ba955990d647346fac421e4"},
"earmark_parser": {:hex, :earmark_parser, "1.4.23", "1d5f22a2802160fd454404fbf5e8f5d14cd8eb727c63701397b72d8c35267e69", [:mix], [], "hexpm", "2ec13bf14b2f4bbb4a15480970e295eede8bb01087fad6ceca27b724ab8e9d18"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.28.2", "e031c7d1a9fc40959da7bf89e2dc269ddc5de631f9bd0e326cbddf7d8085a9da", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "51ee866993ffbd0e41c084a7677c570d0fc50cb85c6b5e76f8d936d9587fa719"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"membrane_caps_audio_raw": {:hex, :membrane_caps_audio_raw, "0.6.1", "f5772ef28c086d0950b16f85e913b1ea35a4c240cb7d2f42fef3388394b58d5a", [:mix], [{:bimap, "~> 1.1", [hex: :bimap, repo: "hexpm", optional: false]}, {:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:membrane_core, "~> 0.9.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "ff827313b18e38f647264c46b0486682a1fbf84c2e43242cea0f127691c748dd"},
"membrane_common_c": {:hex, :membrane_common_c, "0.11.0", "1706631a80b065725bddb34cf182e17af04aa1a753efc47a0043fa20715ac57b", [:mix], [{:membrane_core, "~> 0.9.0", [hex: :membrane_core, repo: "hexpm", optional: false]}, {:shmex, "~> 0.4.0", [hex: :shmex, repo: "hexpm", optional: false]}, {:unifex, "~> 0.7.0", [hex: :unifex, repo: "hexpm", optional: false]}], "hexpm", "2c0f5a21116466bfa3242fe00aea56097c6336029d1d7558e6bd1dd2c02b4b09"},
"membrane_core": {:hex, :membrane_core, "0.9.0", "91a79636c6b2fcc6ae2e611670cdca33a0b38ff4dabe8fb7c1d56cba426db787", [:mix], [{:bunch, "~> 1.3", [hex: :bunch, repo: "hexpm", optional: false]}, {:qex, "~> 0.3", [hex: :qex, repo: "hexpm", optional: false]}, {:ratio, "~> 2.0", [hex: :ratio, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ab252e7aa944d23fbb22ce0e8bc75b088cfe3ed1c2c7ba203b1cc2fd794a85ed"},
"membrane_raw_audio_format": {:hex, :membrane_raw_audio_format, "0.8.0", "1f73b70a0fb1d56e582823a097d061a364e4781cdfa55810777b2cfdacaa0410", [:mix], [{:bimap, "~> 1.1", [hex: :bimap, repo: "hexpm", optional: false]}, {:bunch, "~> 1.0", [hex: :bunch, repo: "hexpm", optional: false]}, {:membrane_core, "~> 0.9.0", [hex: :membrane_core, repo: "hexpm", optional: false]}], "hexpm", "3194f10acc832da778f085d54f731a7b19be02e4d695480f764cf7d208de6ecf"},
"mockery": {:hex, :mockery, "2.3.1", "a02fd60b10ac9ed37a7a2ecf6786c1f1dd5c75d2b079a60594b089fba32dc087", [:mix], [], "hexpm", "1d0971d88ebf084e962da3f2cfee16f0ea8e04ff73a7710428500d4500b947fa"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"numbers": {:hex, :numbers, "5.2.4", "f123d5bb7f6acc366f8f445e10a32bd403c8469bdbce8ce049e1f0972b607080", [:mix], [{:coerce, "~> 1.0", [hex: :coerce, repo: "hexpm", optional: false]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "eeccf5c61d5f4922198395bf87a465b6f980b8b862dd22d28198c5e6fab38582"},
Expand Down
23 changes: 12 additions & 11 deletions test/membrane_ffmpeg_swresample_plugin/converter_native_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Membrane.FFmpeg.SWResample.Converter.NativeTest do
@moduledoc false
use ExUnit.Case, async: true
alias Membrane.Caps.Audio.Raw
alias Membrane.RawAudio

@module Membrane.FFmpeg.SWResample.Converter.Native

Expand All @@ -17,10 +18,10 @@ defmodule Membrane.FFmpeg.SWResample.Converter.NativeTest do
out_rate <- rates,
out_channels <- channels do
[
input_fmt |> Raw.Format.serialize(),
input_fmt |> RawAudio.SampleFormat.serialize(),
input_rate,
input_channels,
out_fmt |> Raw.Format.serialize(),
out_fmt |> RawAudio.SampleFormat.serialize(),
out_rate,
out_channels
]
Expand All @@ -40,21 +41,21 @@ defmodule Membrane.FFmpeg.SWResample.Converter.NativeTest do
] = input

[
input_fmt |> Raw.Format.serialize(),
input_fmt |> RawAudio.SampleFormat.serialize(),
input_rate,
input_channels,
out_fmt |> Raw.Format.serialize(),
out_fmt |> RawAudio.SampleFormat.serialize(),
out_rate,
out_channels
]
end

defp mk_simple_converter(out_fmt, dst_fmt) do
@module.create(
out_fmt |> Raw.Format.serialize(),
out_fmt |> RawAudio.SampleFormat.serialize(),
48_000,
1,
dst_fmt |> Raw.Format.serialize(),
dst_fmt |> RawAudio.SampleFormat.serialize(),
48_000,
1
)
Expand Down Expand Up @@ -134,10 +135,10 @@ defmodule Membrane.FFmpeg.SWResample.Converter.NativeTest do
for size <- [1234, 5120, 20_480, 384_000] do
{:ok, handle} =
@module.create(
:s16le |> Raw.Format.serialize(),
:s16le |> RawAudio.SampleFormat.serialize(),
48_000,
2,
:u8 |> Raw.Format.serialize(),
:u8 |> RawAudio.SampleFormat.serialize(),
24_000,
1
)
Expand All @@ -154,10 +155,10 @@ defmodule Membrane.FFmpeg.SWResample.Converter.NativeTest do
for n <- [1, 10, 100] do
{:ok, handle} =
@module.create(
:f32le |> Raw.Format.serialize(),
:f32le |> RawAudio.SampleFormat.serialize(),
48_000,
2,
:s16le |> Raw.Format.serialize(),
:s16le |> RawAudio.SampleFormat.serialize(),
44_100,
2
)
Expand Down
20 changes: 10 additions & 10 deletions test/membrane_ffmpeg_swresample_plugin/converter_test.exs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
defmodule Membrane.FFmpeg.SWResample.ConverterTest do
use ExUnit.Case, async: true
use Mockery
alias Membrane.Caps.Audio.Raw
alias Membrane.RawAudio
alias Membrane.FFmpeg.SWResample.Converter

@module Converter
@native Converter.Native

@s16le_caps %Raw{
@s16le_caps %RawAudio{
channels: 2,
format: :s16le,
sample_format: :s16le,
sample_rate: 48_000
}

@u8_caps %Raw{
@u8_caps %RawAudio{
channels: 1,
format: :u8,
sample_format: :u8,
sample_rate: 44_100
}

Expand Down Expand Up @@ -43,10 +43,10 @@ defmodule Membrane.FFmpeg.SWResample.ConverterTest do

assert %{native: :mock_handle} = new_state

input_fmt = @s16le_caps.format |> Raw.Format.serialize()
input_fmt = @s16le_caps.sample_format |> RawAudio.SampleFormat.serialize()
input_rate = @s16le_caps.sample_rate
input_channel = @s16le_caps.channels
out_fmt = @u8_caps.format |> Raw.Format.serialize()
out_fmt = @u8_caps.sample_format |> RawAudio.SampleFormat.serialize()
out_rate = @u8_caps.sample_rate
out_channel = @u8_caps.channels

Expand Down Expand Up @@ -78,10 +78,10 @@ defmodule Membrane.FFmpeg.SWResample.ConverterTest do

assert %{native: :mock_handle} = new_state

input_fmt = @s16le_caps.format |> Raw.Format.serialize()
input_fmt = @s16le_caps.sample_format |> RawAudio.SampleFormat.serialize()
input_rate = @s16le_caps.sample_rate
input_channel = @s16le_caps.channels
out_fmt = @u8_caps.format |> Raw.Format.serialize()
out_fmt = @u8_caps.sample_format |> RawAudio.SampleFormat.serialize()
out_rate = @u8_caps.sample_rate
out_channel = @u8_caps.channels

Expand Down Expand Up @@ -150,7 +150,7 @@ defmodule Membrane.FFmpeg.SWResample.ConverterTest do
assert {{:ok, actions}, ^state} = @module.handle_demand(:output, 2, :buffers, nil, state)

buffers_size =
2 * state.frames_per_buffer * Raw.sample_size(state.input_caps) *
2 * state.frames_per_buffer * RawAudio.sample_size(state.input_caps) *
state.input_caps.channels

assert actions == [demand: {:input, buffers_size}]
Expand Down

0 comments on commit e33113d

Please sign in to comment.