Skip to content

Commit

Permalink
More work on miniaudio: List device/backend names
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Oct 20, 2024
1 parent e32549e commit 348179d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
24 changes: 0 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,6 @@ add_library(signalflow SHARED ${SRC})
#-------------------------------------------------------------------------------
add_compile_definitions(SIGNALFLOW_VERSION="${SIGNALFLOW_VERSION}")

#-------------------------------------------------------------------------------
# Dependencies
#-------------------------------------------------------------------------------

set(SOUNDIO_BUILD_DIR "" CACHE PATH "Path to built SoundIO library (will use find_library if blank)")

if (SOUNDIO_BUILD_DIR)
set(SOUNDIO_INCLUDE_DIR "${SOUNDIO_BUILD_DIR}/.." CACHE PATH "Path to SoundIO include directory (ignored if SOUNDIO_BUILD_DIR is blank")
add_definitions(-DHAVE_SOUNDIO)
target_link_libraries(signalflow "${SOUNDIO_BUILD_DIR}/x64-Debug/soundio.lib")
include_directories(signalflow "${SOUNDIO_BUILD_DIR}/$<CONFIG>/")
include_directories(signalflow "${SOUNDIO_INCLUDE_DIR}/")
else()
find_library(SOUNDIO soundio)
if (SOUNDIO)
message("Found libsoundio")
add_definitions(-DHAVE_SOUNDIO)
target_link_libraries(signalflow ${SOUNDIO})
else()
message(SEND_ERROR "Couldn't find libsoundio")
endif()
endif()


set(SNDFILE_BUILD_DIR "" CACHE PATH "Path to build sndfile library (will use find_library if blank)")

if (SNDFILE_BUILD_DIR)
Expand Down
1 change: 1 addition & 0 deletions source/include/signalflow/node/io/output/miniaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AudioOut_MiniAudio : public AudioOut_Abstract
private:
std::string backend_name;
std::string device_name;
ma_context context;
ma_device device;
};

Expand Down
2 changes: 0 additions & 2 deletions source/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ set(SRC ${SRC}
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/fold.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/processors/wetdry.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/io/input/abstract.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/io/input/soundio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/io/output/abstract.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/io/output/dummy.cpp
# ${CMAKE_CURRENT_SOURCE_DIR}/node/io/output/soundio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/io/output/miniaudio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/add.cpp
${CMAKE_CURRENT_SOURCE_DIR}/node/operators/amplitude-to-decibels.cpp
Expand Down
70 changes: 58 additions & 12 deletions source/src/node/io/output/miniaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#define MINIAUDIO_IMPLEMENTATION
#include "signalflow/node/io/output/miniaudio-library.h"

#ifdef HAVE_SOUNDIO

#include "signalflow/core/graph.h"

#include <algorithm>
Expand Down Expand Up @@ -86,7 +84,6 @@ int AudioOut_MiniAudio::init()
ma_uint32 playback_device_count;
ma_device_info *capture_devices;
ma_uint32 capture_device_count;
ma_context context;
ma_result rv;

if (ma_context_init(NULL, 0, NULL, &context) != MA_SUCCESS)
Expand All @@ -106,16 +103,18 @@ int AudioOut_MiniAudio::init()
{
for (int i = 0; i < playback_device_count; i++)
{
printf(" - (%d) %s\n", i, playback_devices[i].name);
if (strcmp(playback_devices[i].name, device_name.c_str()) == 0)
if (strncmp(playback_devices[i].name, device_name.c_str(), strlen(device_name.c_str())) == 0)
{
if (selected_device_index != -1)
{
throw std::runtime_error("More than one audio device found matching name '" + device_name + "'");
}
selected_device_index = i;
}
}
if (selected_device_index == -1)
{
printf("Couldn't find device\n");
return -1;
throw std::runtime_error("No audio device found matching name '" + device_name + "'");
}

config.playback.pDeviceID = &playback_devices[selected_device_index].id;
Expand Down Expand Up @@ -190,19 +189,66 @@ int AudioOut_MiniAudio::destroy()

std::list<std::string> AudioOut_MiniAudio::get_output_device_names()
{
return {};
std::list<std::string> device_names;

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

rv = ma_context_init(NULL, 0, NULL, &context);
if (rv != MA_SUCCESS)
{
throw std::runtime_error("miniaudio: Failure initialising audio context");
}

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

return device_names;
}

int AudioOut_MiniAudio::get_default_output_device_index()
{
return 0;
// TODO: Is this even used?
return -1;
}

std::list<std::string> AudioOut_MiniAudio::get_output_backend_names()
{
return {};
std::list<std::string> backend_names;
ma_backend enabled_backends[MA_BACKEND_COUNT];
size_t enabled_backend_count;
ma_result rv;

rv = ma_get_enabled_backends(enabled_backends, MA_BACKEND_COUNT, &enabled_backend_count);
if (rv != MA_SUCCESS)
{
throw std::runtime_error("miniaudio: Failure querying backend devices");
}
for (int i = 0; i < enabled_backend_count; i++)
{
std::string backend_name = std::string(ma_get_backend_name(enabled_backends[i]));
if (backend_name != "Custom" && backend_name != "Null")
{
backend_names.push_back(backend_name);
}
}

return backend_names;
}

} // namespace signalflow

#endif
3 changes: 0 additions & 3 deletions source/src/python/nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ void init_python_nodes(py::module &m)
/*--------------------------------------------------------------------------------
* Node subclasses
*-------------------------------------------------------------------------------*/
py::class_<AudioIn, Node, NodeRefTemplate<AudioIn>>(m, "AudioIn", "Audio input")
.def(py::init<int>(), "num_channels"_a = 1);

py::class_<AudioOut_Abstract, Node, NodeRefTemplate<AudioOut_Abstract>>(m, "AudioOut_Abstract", "Abstract audio output");

py::class_<AudioOut_Dummy, Node, NodeRefTemplate<AudioOut_Dummy>>(m, "AudioOut_Dummy", "Dummy audio output for offline processing")
Expand Down

0 comments on commit 348179d

Please sign in to comment.