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

List devices #43

Merged
merged 3 commits into from
Oct 20, 2023
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: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Add the following line to your `deps` in `mix.exs`. Run `mix deps.get`.

This package depends on the [PortAudio](http://portaudio.com/) library. The precompiled build will be pulled and linked automatically. However, should there be any problems, consider installing it manually.

The `mix pa_devices` task prints available audio devices and their IDs, which you can pass to the `Membrane.PortAudio.Source` or `Membrane.PortAudio.Sink`.

### Manual instalation of dependencies
#### Ubuntu

Expand Down
13 changes: 10 additions & 3 deletions bundlex.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ defmodule Membrane.PortAudio.BundlexProject do
"https://github.com/membraneframework-precompiled/precompiled_portaudio/releases/latest/download/portaudio"

case Bundlex.get_target() do
%{os: "linux"} ->
{[{:precompiled, "#{url_prefix}_linux.tar.gz"}, :pkg_config], "portaudio"}

%{architecture: "x86_64", os: "darwin" <> _rest_of_os_name} ->
{[{:precompiled, "#{url_prefix}_macos_intel.tar.gz"}, :pkg_config], "portaudio"}

%{architecture: "aarch64", os: "darwin" <> _rest_of_os_name} ->
{:precompiled, "#{url_prefix}_macos_arm.tar.gz"}

%{architecture: "aarch64", os: "darwin" <> _rest_of_os_name} ->
{[:pkg_config], "portaudio-2.0"}

Expand All @@ -41,6 +41,13 @@ defmodule Membrane.PortAudio.BundlexProject do
sources: ["source.c", "pa_helper.c"],
os_deps: [get_portaudio()],
preprocessor: Unifex
],
pa_devices: [
interface: :nif,
deps: [unifex: :unifex],
sources: ["pa_devices.c"],
os_deps: [get_portaudio()],
preprocessor: Unifex
]
]
end
Expand Down
31 changes: 31 additions & 0 deletions c_src/membrane_portaudio_plugin/pa_devices.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "pa_devices.h"

UNIFEX_TERM list(UnifexEnv *env) {
Pa_Initialize();
int numDevices = Pa_GetDeviceCount();
if (numDevices < 0) {
printf("\nERROR: Pa_CountDevices returned 0x%x\n", numDevices);
return list_result(env);
} else if (numDevices == 0) {
printf("\nNo audio devices found\n");
} else {
printf("\nAvailable audio devices:\n\n");
int default_input_id = Pa_GetDefaultInputDevice();
int default_output_id = Pa_GetDefaultOutputDevice();
for (int i = 0; i < numDevices; i++) {
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(i);
const char *default_str =
i == default_input_id
? " (default input)"
: i == default_output_id ? " (default output)" : "";

printf("%s%s\r\n\tid: %d\r\n\tmax_input_channels: "
"%d\r\n\tmax_output_channels: %d\r\n\n",
device_info->name, default_str, i, device_info->maxInputChannels,
device_info->maxOutputChannels);
}
}

Pa_Terminate();
return list_result(env);
}
2 changes: 2 additions & 0 deletions c_src/membrane_portaudio_plugin/pa_devices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "_generated/pa_devices.h"
#include <portaudio.h>
3 changes: 3 additions & 0 deletions c_src/membrane_portaudio_plugin/pa_devices.spec.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Membrane.PortAudio.Devices

spec list() :: :ok
15 changes: 14 additions & 1 deletion lib/membrane_portaudio.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
defmodule Membrane.PortAudio do
@moduledoc false
@moduledoc """
PortAudio utilities.
"""
use Application

@impl true
Expand All @@ -9,4 +11,15 @@ defmodule Membrane.PortAudio do
opts = [strategy: :one_for_one, name: Membrane.PortAudio]
Supervisor.start_link(children, opts)
end

@doc """
Prints names and ids of available audio devices to stdout.

Corresponding task `mix pa_devices` is available too.
"""
@spec print_devices() :: :ok
def print_devices() do
Application.ensure_all_started(:membrane_portaudio_plugin)
__MODULE__.SyncExecutor.apply(__MODULE__.Devices, :list, [])
end
end
4 changes: 4 additions & 0 deletions lib/membrane_portaudio_plugin/devices.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Membrane.PortAudio.Devices do
@moduledoc false
use Unifex.Loader
end
7 changes: 6 additions & 1 deletion lib/membrane_portaudio_plugin/sink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ defmodule Membrane.PortAudio.Sink do
type: :integer,
spec: integer | :default,
default: :default,
description: "PortAudio sound card id"
description: """
PortAudio device id. Defaults to the default output device.

You can list available devices with `mix pa_devices` or
`Membrane.PortAudio.print_devices/0`.
"""
],
ringbuffer_size: [
type: :integer,
Expand Down
7 changes: 6 additions & 1 deletion lib/membrane_portaudio_plugin/source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ defmodule Membrane.PortAudio.Source do
def_options endpoint_id: [
spec: integer | :default,
default: :default,
description: "PortAudio sound card id"
description: """
PortAudio device id. Defaults to the default input device.

You can list available devices with `mix pa_devices` or
`Membrane.PortAudio.print_devices/0`.
"""
],
portaudio_buffer_size: [
spec: pos_integer,
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Membrane.PortAudio.Mixfile do
elixirc_paths: elixirc_paths(Mix.env()),
deps: deps(),
dialyzer: dialyzer(),
aliases: [pa_devices: "eval 'Membrane.PortAudio.print_devices()'"],

# hex
description: "Raw audio retriever and player based on PortAudio",
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dialyxir": {:hex, :dialyxir, "1.4.1", "a22ed1e7bd3a3e3f197b68d806ef66acb61ee8f57b3ac85fc5d57354c5482a93", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "84b795d6d7796297cca5a3118444b80c7d94f7ce247d49886e7c291e1ae49801"},
"earmark_parser": {:hex, :earmark_parser, "1.4.37", "2ad73550e27c8946648b06905a57e4d454e4d7229c2dafa72a0348c99d8be5f7", [:mix], [], "hexpm", "6b19783f2802f039806f375610faa22da130b8edc21209d0bff47918bb48360e"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"ex_doc": {:hex, :ex_doc, "0.30.6", "5f8b54854b240a2b55c9734c4b1d0dd7bdd41f71a095d42a70445c03cf05a281", [:mix], [{:earmark_parser, "~> 1.4.31", [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", "bd48f2ddacf4e482c727f9293d9498e0881597eae6ddc3d9562bd7923375109f"},
"ex_doc": {:hex, :ex_doc, "0.30.8", "cf3eb2eb32137966aab0929bb3af42773b2d08e2f785a5fee9caabf664082cb3", [:mix], [{:earmark_parser, "~> 1.4.31", [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", "bfb981d8e0a8ab23857e502d611c612ae2c24536dd3b530e741d1d94ea44e6e2"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"finch": {:hex, :finch, "0.16.0", "40733f02c89f94a112518071c0a91fe86069560f5dbdb39f9150042f44dcfb1a", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f660174c4d519e5fec629016054d60edd822cdfe2b7270836739ac2f97735ec5"},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
Expand Down