Skip to content

Commit 1748180

Browse files
committed
improve examples, fix tests
1 parent c48cf05 commit 1748180

13 files changed

+101
-198
lines changed

examples/README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
# Membrane G.711 FFmpeg plugin examples
22

33
To run, execute `elixir <example>`, e.g. `elixir decode_example.exs`.
4-
5-
## A-law examples
6-
- `encode_example.exs` - Encodes a raw audio file to G.711 A-law format
7-
- `decode_example.exs` - Decodes a G.711 A-law file to raw audio
8-
9-
## μ-law examples
10-
- `encode_mulaw_example.exs` - Encodes a raw audio file to G.711 μ-law format
11-
- `decode_mulaw_example.exs` - Decodes a G.711 μ-law file to raw audio
4+
You can choose between PCMA (A-law, default) and PCMU (μ-law) with `--encoding PCMA|PCMU` option.

examples/decode_example.exs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Decoding example
22
#
3-
# The following pipeline takes a G.711 A-law file and decodes it to the raw audio.
3+
# The following pipeline takes a G.711 file and decodes it to the raw audio.
4+
# Use `--encoding` option to choose between `ALAW` (default) or `ULAW` variants
45

56
Logger.configure(level: :info)
67

78
Mix.install([
89
{:membrane_g711_ffmpeg_plugin,
910
path: __DIR__ |> Path.join("..") |> Path.expand(), override: true},
1011
:membrane_file_plugin,
11-
:req
12+
:membrane_hackney_plugin
1213
])
1314

1415
g711_alaw =
@@ -22,18 +23,29 @@ defmodule Decoding.Pipeline do
2223
use Membrane.Pipeline
2324

2425
@impl true
25-
def handle_init(_ctx, _opts) do
26+
def handle_init(_ctx, opts) do
27+
encoding = opts[:encoding]
28+
29+
{name, ext} =
30+
case encoding do
31+
:PCMA -> {"pcma", "al"}
32+
:PCMU -> {"pcmu", "ul"}
33+
end
34+
35+
url =
36+
"https://raw.githubusercontent.com/membraneframework/static/gh-pages/samples/beep-#{name}-8kHz-mono.#{ext}"
37+
2638
structure =
27-
child(:source, %Membrane.File.Source{chunk_size: 40_960, location: "input.al"})
28-
|> child(:decoder, Membrane.G711.FFmpeg.Decoder)
39+
child(:source, %Membrane.Hackney.Source{location: url})
40+
|> child(:decoder, %Membrane.G711.FFmpeg.Decoder{encoding: encoding})
2941
|> child(:sink, %Membrane.File.Sink{location: "output.raw"})
3042

3143
{[spec: structure], %{}}
3244
end
3345

3446
@impl true
3547
def handle_element_end_of_stream(:sink, _pad, _ctx, state) do
36-
{[terminate: :shutdown], state}
48+
{[terminate: :normal], state}
3749
end
3850

3951
@impl true
@@ -42,8 +54,13 @@ defmodule Decoding.Pipeline do
4254
end
4355
end
4456

57+
{opts, _rest} = OptionParser.parse!(System.argv(), strict: [encoding: :string])
58+
encoding = opts |> Keyword.get(:encoding, "PCMA") |> String.upcase() |> String.to_existing_atom()
59+
4560
# Start and monitor the pipeline
46-
{:ok, _supervisor_pid, pipeline_pid} = Membrane.Pipeline.start_link(Decoding.Pipeline)
61+
{:ok, _supervisor_pid, pipeline_pid} =
62+
Membrane.Pipeline.start_link(Decoding.Pipeline, encoding: encoding)
63+
4764
ref = Process.monitor(pipeline_pid)
4865

4966
# Wait for the pipeline to finish

examples/decode_mulaw_example.exs

Lines changed: 0 additions & 49 deletions
This file was deleted.

examples/encode_example.exs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Encoding example
22
#
33
# The following pipeline takes a raw audio file and encodes it as G.711 A-law.
4+
# Use `--encoding` option to choose between `ALAW` (default) or `ULAW` variants
45

56
Logger.configure(level: :info)
67

@@ -10,39 +11,43 @@ Mix.install([
1011
:membrane_raw_audio_parser_plugin,
1112
:membrane_raw_audio_format,
1213
:membrane_file_plugin,
13-
:req
14+
:membrane_hackney_plugin
1415
])
1516

16-
raw_audio =
17-
Req.get!(
18-
"https://raw.githubusercontent.com/membraneframework/static/gh-pages/samples/beep-s16le-8kHz-mono.raw"
19-
).body
20-
21-
File.write!("input.raw", raw_audio)
22-
2317
defmodule Encoding.Pipeline do
2418
use Membrane.Pipeline
2519

2620
@impl true
27-
def handle_init(_ctx, _opts) do
21+
def handle_init(_ctx, opts) do
22+
encoding = opts[:encoding]
23+
24+
ext =
25+
case encoding do
26+
:PCMA -> "al"
27+
:PCMU -> "ul"
28+
end
29+
30+
url =
31+
"https://raw.githubusercontent.com/membraneframework/static/gh-pages/samples/beep-s16le-8kHz-mono.raw"
32+
2833
structure =
29-
child(:source, %Membrane.File.Source{chunk_size: 40_960, location: "input.raw"})
34+
child(:source, %Membrane.Hackney.Source{location: url})
3035
|> child(:parser, %Membrane.RawAudioParser{
3136
stream_format: %Membrane.RawAudio{
3237
sample_format: :s16le,
3338
sample_rate: 8000,
3439
channels: 1
3540
}
3641
})
37-
|> child(:encoder, Membrane.G711.FFmpeg.Encoder)
38-
|> child(:sink, %Membrane.File.Sink{location: "output.al"})
42+
|> child(:encoder, %Membrane.G711.FFmpeg.Encoder{encoding: encoding})
43+
|> child(:sink, %Membrane.File.Sink{location: "output.#{ext}"})
3944

4045
{[spec: structure], %{}}
4146
end
4247

4348
@impl true
4449
def handle_element_end_of_stream(:sink, _pad, _ctx, state) do
45-
{[terminate: :shutdown], state}
50+
{[terminate: :normal], state}
4651
end
4752

4853
@impl true
@@ -51,12 +56,16 @@ defmodule Encoding.Pipeline do
5156
end
5257
end
5358

59+
{opts, _rest} = OptionParser.parse!(System.argv(), strict: [encoding: :string])
60+
encoding = opts |> Keyword.get(:encoding, "PCMA") |> String.upcase() |> String.to_existing_atom()
61+
5462
# Start and monitor the pipeline
55-
{:ok, _supervisor_pid, pipeline_pid} = Membrane.Pipeline.start_link(Encoding.Pipeline)
63+
{:ok, _supervisor_pid, pipeline_pid} =
64+
Membrane.Pipeline.start_link(Encoding.Pipeline, encoding: encoding)
65+
5666
ref = Process.monitor(pipeline_pid)
5767

5868
# Wait for the pipeline to finish
5969
receive do
60-
{:DOWN, ^ref, :process, _pipeline_pid, _reason} ->
61-
System.stop()
70+
{:DOWN, ^ref, :process, _pipeline_pid, _reason} -> :ok
6271
end

examples/encode_mulaw_example.exs

Lines changed: 0 additions & 62 deletions
This file was deleted.

lib/membrane_g711_ffmpeg/decoder.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,23 @@ defmodule Membrane.G711.FFmpeg.Decoder do
1414
alias Membrane.G711.FFmpeg.Common
1515
alias Membrane.{G711, RawAudio, RemoteStream}
1616

17-
def_options(
18-
encoding: [
19-
spec: :PCMA | :PCMU,
20-
description: "G.711 encoding to decode (A-law or μ-law)",
21-
default: :PCMA
22-
]
23-
)
24-
25-
def_input_pad(:input,
17+
def_options encoding: [
18+
spec: :PCMA | :PCMU,
19+
description: "G.711 encoding to decode (A-law or μ-law)",
20+
default: :PCMA
21+
]
22+
23+
def_input_pad :input,
2624
flow_control: :auto,
2725
accepted_format:
2826
any_of(%RemoteStream{}, %G711{encoding: encoding} when encoding in [:PCMA, :PCMU])
29-
)
3027

31-
def_output_pad(:output,
28+
def_output_pad :output,
3229
flow_control: :auto,
3330
accepted_format: %RawAudio{
3431
channels: G711.num_channels(),
3532
sample_rate: G711.sample_rate()
3633
}
37-
)
3834

3935
@impl true
4036
def handle_init(_ctx, opts) do

lib/membrane_g711_ffmpeg/encoder.ex

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,23 @@ defmodule Membrane.G711.FFmpeg.Encoder do
1919
alias Membrane.G711.FFmpeg.Common
2020
alias Membrane.{G711, RawAudio}
2121

22-
def_options(
23-
encoding: [
24-
spec: :PCMA | :PCMU,
25-
description: "G.711 encoding to use (A-law or μ-law)",
26-
default: :PCMA
27-
]
28-
)
29-
30-
def_input_pad(:input,
22+
def_options encoding: [
23+
spec: :PCMA | :PCMU,
24+
description: "G.711 encoding to use (A-law or μ-law)",
25+
default: :PCMA
26+
]
27+
28+
def_input_pad :input,
3129
flow_control: :auto,
3230
accepted_format: %RawAudio{
3331
channels: G711.num_channels(),
3432
sample_rate: G711.sample_rate(),
3533
sample_format: :s16le
3634
}
37-
)
3835

39-
def_output_pad(:output,
36+
def_output_pad :output,
4037
flow_control: :auto,
4138
accepted_format: %G711{encoding: encoding} when encoding in [:PCMA, :PCMU]
42-
)
4339

4440
@impl true
4541
def handle_init(_ctx, opts) do

0 commit comments

Comments
 (0)