Skip to content

Commit

Permalink
miniaudio: Add support for enumerating input devices
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Oct 21, 2024
1 parent 231de5f commit 0cdd3d8
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 18 deletions.
32 changes: 24 additions & 8 deletions auxiliary/libs/signalflow_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ def run_list_output_device_names(backend_name: str = None):
print(" - %s" % name)


def run_list_output_backend_names():
output_backend_names = AudioGraph.get_output_backend_names()
def run_list_input_device_names(backend_name: str = None):
input_device_names = AudioGraph.get_input_device_names(backend_name)
print("Available input device names:")
for name in input_device_names:
print(" - %s" % name)

def run_list_backend_names():
backend_names = AudioGraph.get_backend_names()
print("Available output backend names:")
for name in output_backend_names:
for name in backend_names:
print(" - %s" % name)


Expand Down Expand Up @@ -129,8 +135,16 @@ def main():
# Command: list-output-device-names
# --------------------------------------------------------------------------------
list_output_device_names = subparsers.add_parser('list-output-device-names', help='list available output devices')
list_output_device_names.add_argument('--output-backend-name', type=str,
help='name of output backend to use (default: system default backend)',
list_output_device_names.add_argument('--backend-name', type=str,
help='name of audio backend to use (default: system default backend)',
default=None)

# --------------------------------------------------------------------------------
# Command: list-input-device-names
# --------------------------------------------------------------------------------
list_input_device_names = subparsers.add_parser('list-input-device-names', help='list available input devices')
list_input_device_names.add_argument('--backend-name', type=str,
help='name of audio backend to use (default: system default backend)',
default=None)

# --------------------------------------------------------------------------------
Expand Down Expand Up @@ -168,9 +182,11 @@ def main():
elif args.command == 'test':
run_test(args.frequency, args.gain, args.output_backend_name, args.output_device_name)
elif args.command == 'list-output-device-names':
run_list_output_device_names(args.output_backend_name)
elif args.command == 'list-output-backend-names':
run_list_output_backend_names()
run_list_output_device_names(args.backend_name)
elif args.command == 'list-input-device-names':
run_list_input_device_names(args.backend_name)
elif args.command == 'list-backend-names':
run_list_backend_names()
elif args.command == 'list-midi-output-device-names':
run_list_midi_output_device_names()
elif args.command == 'list-midi-input-device-names':
Expand Down
10 changes: 9 additions & 1 deletion source/include/signalflow/core/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,21 @@ class AudioGraph
*--------------------------------------------------------------------------------*/
static std::list<std::string> get_output_device_names(std::string backend_name = "");

/**--------------------------------------------------------------------------------
* Returns a list of available audio I/O input devices.
*
* @return The list of device names.
*
*--------------------------------------------------------------------------------*/
static std::list<std::string> get_input_device_names(std::string backend_name = "");

/**--------------------------------------------------------------------------------
* Returns a list of available audio I/O output backends.
*
* @return The list of backend names.
*
*--------------------------------------------------------------------------------*/
static std::list<std::string> get_output_backend_names();
static std::list<std::string> get_backend_names();

/**--------------------------------------------------------------------------------
* Schedule a node for rendering without connecting the node to the graph's output.
Expand Down
3 changes: 2 additions & 1 deletion source/include/signalflow/node/io/output/miniaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class AudioOut : public AudioOut_Abstract
virtual void destroy() override;

static std::list<std::string> get_output_device_names(std::string backend_name = "");
static std::list<std::string> get_output_backend_names();
static std::list<std::string> get_input_device_names(std::string backend_name = "");
static std::list<std::string> get_backend_names();

private:
/*--------------------------------------------------------------------------------
Expand Down
10 changes: 8 additions & 2 deletions source/src/core/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,15 @@ std::list<std::string> AudioGraph::get_output_device_names(std::string backend_n
}

// static
std::list<std::string> AudioGraph::get_output_backend_names()
std::list<std::string> AudioGraph::get_input_device_names(std::string backend_name)
{
return AudioOut::get_output_backend_names();
return AudioOut::get_input_device_names(backend_name);
}

// static
std::list<std::string> AudioGraph::get_backend_names()
{
return AudioOut::get_backend_names();
}

NodeRef AudioGraph::add_node(NodeRef node)
Expand Down
38 changes: 33 additions & 5 deletions source/src/node/io/output/miniaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,15 @@ std::list<std::string> AudioOut::get_output_device_names(std::string backend_nam
ma_result rv;
ma_device_info *playback_devices;
ma_uint32 playback_device_count;
ma_device_info *capture_devices;
ma_uint32 capture_device_count;
ma_context context;

AudioOut::init_context(&context, backend_name);

rv = ma_context_get_devices(&context,
&playback_devices,
&playback_device_count,
&capture_devices,
&capture_device_count);
NULL,
NULL);
if (rv != MA_SUCCESS)
{
throw audio_io_exception("miniaudio: Failure querying audio devices");
Expand All @@ -268,7 +266,37 @@ std::list<std::string> AudioOut::get_output_device_names(std::string backend_nam
return device_names;
}

std::list<std::string> AudioOut::get_output_backend_names()
std::list<std::string> AudioOut::get_input_device_names(std::string backend_name)
{
std::list<std::string> device_names;

ma_result rv;
ma_device_info *capture_devices;
ma_uint32 capture_device_count;
ma_context context;

AudioOut::init_context(&context, backend_name);

rv = ma_context_get_devices(&context,
NULL,
NULL,
&capture_devices,
&capture_device_count);
if (rv != MA_SUCCESS)
{
throw audio_io_exception("miniaudio: Failure querying audio devices");
}
for (unsigned int i = 0; i < capture_device_count; i++)
{
device_names.push_back(std::string(capture_devices[i].name));
}

ma_context_uninit(&context);

return device_names;
}

std::list<std::string> AudioOut::get_backend_names()
{
std::list<std::string> backend_names;
ma_backend enabled_backends[MA_BACKEND_COUNT];
Expand Down
8 changes: 7 additions & 1 deletion source/src/python/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ void init_python_graph(py::module &m)
return AudioGraph::get_output_device_names(backend_name_str);
},
"backend_name"_a = "", R"pbdoc(list[str]: List the available output device names.)pbdoc")
.def_static("get_output_backend_names", &AudioGraph::get_output_backend_names, R"pbdoc(list[str]: List the available output backend names.)pbdoc")
.def_static(
"get_input_device_names", [](py::object backend_name) {
std::string backend_name_str = backend_name.is_none() ? "" : backend_name.cast<std::string>();
return AudioGraph::get_input_device_names(backend_name_str);
},
"backend_name"_a = "", R"pbdoc(list[str]: List the available input device names.)pbdoc")
.def_static("get_backend_names", &AudioGraph::get_backend_names, R"pbdoc(list[str]: List the available audio backend names.)pbdoc")

/*--------------------------------------------------------------------------------
* Methods
Expand Down

0 comments on commit 0cdd3d8

Please sign in to comment.