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

Return devices #58

Merged
merged 5 commits into from
Jul 10, 2024
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
56 changes: 39 additions & 17 deletions c_src/membrane_portaudio_plugin/pa_devices.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
#include "pa_devices.h"

UNIFEX_TERM list(UnifexEnv *env) {
UNIFEX_TERM list(UnifexEnv *env)
{
Pa_Initialize();
int numDevices = Pa_GetDeviceCount();
if (numDevices < 0) {
device devices[numDevices];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if numDevices is negative? :P Also, I'd allocate this on the heap just in case numDevices is big for some reason

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");
return list_result(env, devices, numDevices);
}
else if (numDevices == 0)
{
return list_result(env, devices, numDevices);
}
else
{
int default_input_id = Pa_GetDefaultInputDevice();
int default_output_id = Pa_GetDefaultOutputDevice();
for (int i = 0; i < numDevices; i++) {
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);
if (i == default_input_id)
{
devices[i].default_device = DEFAULT_DEVICE_INPUT;
}
else if (i == default_output_id)
{
devices[i].default_device = DEFAULT_DEVICE_OUTPUT;
}
else
{
devices[i].default_device = DEFAULT_DEVICE_FALSE;
}

devices[i].name = malloc(strlen(device_info->name) + 1);
if (device_info->name != NULL)
{
strcpy(devices[i].name, device_info->name);
}
Comment on lines +38 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you allocate anything on the heap, you should free it. In this case, after the call to list_result. However, unifex does malloc and memcpy when converting to an erlang term, so it seems redundant.


devices[i].id = i;
devices[i].max_output_channels = device_info->maxOutputChannels;
devices[i].max_input_channels = device_info->maxInputChannels;
devices[i].default_sample_rate = device_info->defaultSampleRate;
}
}

Pa_Terminate();
return list_result(env);
return list_result(env, devices, numDevices);
}
14 changes: 13 additions & 1 deletion c_src/membrane_portaudio_plugin/pa_devices.spec.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
module Membrane.PortAudio.Devices

spec list() :: :ok
type(
device :: %Membrane.PortAudio.Device{
id: int,
name: string,
max_input_channels: int,
max_output_channels: int,
default_sample_rate: float,
default_device: default_device
}
)

type(default_device :: false | :input | :output)
spec list() :: devices :: [device]
2 changes: 2 additions & 0 deletions lib/membrane_portaudio.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ defmodule Membrane.PortAudio do
@spec print_devices() :: :ok
def print_devices() do
Application.ensure_all_started(:membrane_portaudio_plugin)

__MODULE__.SyncExecutor.apply(__MODULE__.Devices, :list, [])
|> IO.inspect()
Comment on lines 21 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add list_devices/0 here too, the existing one is in a private module and not guarded with the sync executor

end
end
10 changes: 10 additions & 0 deletions lib/membrane_portaudio_plugin/device.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Membrane.PortAudio.Device do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a piece of doc would be great

defstruct [
:id,
:name,
:max_input_channels,
:max_output_channels,
:is_default,
:default_sample_rate
]
end