Skip to content

Commit

Permalink
adds a way to auto selects audio device
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed Aug 4, 2024
1 parent 1b6e333 commit 35d2c3f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 10 deletions.
41 changes: 38 additions & 3 deletions cpp/worldline/audio_output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,46 @@ DLL_API ou_audio_context_t* ou_init_audio_device(
return result;
}

DLL_API ou_audio_context_t* ou_init_audio_device_auto(
ou_audio_data_callback_t callback) {
ou_audio_context_t* result = new ou_audio_context_t();
if (result == NULL) {
return NULL;
}

ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.format = ma_format_f32;
config.playback.channels = 2;
config.sampleRate = 44100;
g_data_callback = callback;
config.dataCallback = data_callback;
config.pUserData = result;

if (ma_device_init(NULL, &config, &result->device) != MA_SUCCESS) {
delete result;
return NULL;
}

return result;
}

DLL_API const char* ou_get_audio_device_api(ou_audio_context_t* context) {
ma_backend backend = context->device.pContext->backend;
return ma_get_backend_name(backend);
}

DLL_API const char* ou_get_audio_device_name(ou_audio_context_t* context) {
return context->device.playback.name;
}

DLL_API int ou_free_audio_device(ou_audio_context_t* context) {
bool release_context = !context->device.isOwnerOfContext;
ma_device_uninit(&context->device);
ma_result result = ma_context_uninit(&context->context);
if (result != MA_SUCCESS) {
return result;
if (release_context) {
ma_result result = ma_context_uninit(&context->context);
if (result != MA_SUCCESS) {
return result;
}
}
delete context;
return 0;
Expand Down
19 changes: 14 additions & 5 deletions cpp/worldline/audio_output.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef WORLDLINE_AUDIO_OUTPUT_H
#define WORLDLINE_AUDIO_OUTPUT_H

#include <stdint.h>
#include <cstdint>

#include "miniaudio.h"

Expand All @@ -15,17 +15,17 @@
extern "C" {
#endif

typedef struct {
struct ou_audio_device_info_t {
char* name;
uint64_t id;
char* api;
uint32_t api_id;
} ou_audio_device_info_t;
};

typedef struct {
struct ou_audio_context_t {
ma_context context;
ma_device device;
} ou_audio_context_t;
};

typedef void (*ou_audio_data_callback_t)(float* buffer, uint32_t channels,
uint32_t frame_count);
Expand All @@ -39,6 +39,15 @@ DLL_API void ou_free_audio_device_infos(ou_audio_device_info_t* device_infos,
DLL_API ou_audio_context_t* ou_init_audio_device(
uint32_t api_id, uint64_t id, ou_audio_data_callback_t callback);

DLL_API ou_audio_context_t* ou_init_audio_device_auto(
ou_audio_data_callback_t callback);

DLL_API const char* ou_get_audio_device_api(ou_audio_context_t* context);

// On windows returns string of local code page, except for WASAPI which returns UTF-8.
// On other platforms returns UTF-8.
DLL_API const char* ou_get_audio_device_name(ou_audio_context_t* context);

DLL_API int ou_free_audio_device(ou_audio_context_t* context);

DLL_API int ou_audio_device_start(ou_audio_context_t* context);
Expand Down
18 changes: 16 additions & 2 deletions cpp/worldline/audio_output_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,26 @@ TEST(MiniAudioTest, Playback) {
<< " API: " << device_infos[i].api
<< " API Id: " << device_infos[i].api_id << " Index: " << i
<< " ID: " << absl::Hex(device_infos[i].id);
api_id = device_infos[i].api_id;
id = device_infos[i].id;
if (i == 0) {
api_id = device_infos[i].api_id;
id = device_infos[i].id;
}
}
ou_free_audio_device_infos(device_infos, count);

ou_audio_context_t* context = ou_init_audio_device(api_id, id, &white_noise);
LOG(INFO) << "Device API: " << ou_get_audio_device_api(context);
LOG(INFO) << "Device Name: " << ou_get_audio_device_name(context);
ou_audio_device_start(context);
absl::SleepFor(absl::Seconds(1));
ou_audio_device_stop(context);
ou_free_audio_device(context);

absl::SleepFor(absl::Seconds(1));

context = ou_init_audio_device_auto(&white_noise);
LOG(INFO) << "Auto Device API: " << ou_get_audio_device_api(context);
LOG(INFO) << "Auto Device Name: " << ou_get_audio_device_name(context);
ou_audio_device_start(context);
absl::SleepFor(absl::Seconds(1));
ou_audio_device_stop(context);
Expand Down

0 comments on commit 35d2c3f

Please sign in to comment.