diff options
| author | Osmium Sorcerer <os@sof.beauty> | 2026-03-24 02:56:23 +0000 |
|---|---|---|
| committer | Osmium Sorcerer <os@sof.beauty> | 2026-03-29 22:22:25 +0000 |
| commit | 3f6fb17deddd1b366d16db5a2531c82407ced9db (patch) | |
| tree | 5dd78ee66a67398274d08f598b79f8b663791c1f /src/aoapplication.cpp | |
| parent | 8475cfb7ac19f8698d2b96a0367f4735a016c375 (diff) | |
Rewrite audio engine: replace BASS with miniaudio
SFX and blip players largely remain the same.
For the music player, we now have to implement network streaming
natively, we no longer have a convenient function that did everything
for us. I introduced QNetworkRequest to download the stream in memory
and signal when it's ready to be decoded and played back. The size is
guarded to prevent the client from accidentally downloading terabytes of
audio.
Delete QFutureWatcher, we no longer need it for concurrency. miniaudio
uses a separate audio thread. Network donwloads and communication with
the track name display are handled by Qt signals.
Also, delete an odd "music.txt" feature. Its purpose was specifying
offsets for loops in a text file per track, but it remained obscure and
unused in practice.
Unsupported:
- Large streams, including unbounded ones (radio). We'll need a ring
buffer for that, and a mechanism to write to it from the network and
feed it to the audio thread.
- Effect flags: fade in, fade out, sync pos. Ignored.
- Audio device selection.
Diffstat (limited to 'src/aoapplication.cpp')
| -rw-r--r-- | src/aoapplication.cpp | 92 |
1 files changed, 30 insertions, 62 deletions
diff --git a/src/aoapplication.cpp b/src/aoapplication.cpp index 774816b..ecfd97a 100644 --- a/src/aoapplication.cpp +++ b/src/aoapplication.cpp @@ -26,6 +26,13 @@ AOApplication::AOApplication(QObject *parent) message_handler_context = this; original_message_handler = qInstallMessageHandler(message_handler); + + mus_decoders[0] = ma_decoding_backend_libopus; + mus_decoders[1] = ma_decoding_backend_libvorbis; + mus_decoder_config = ma_decoder_config_init_default(); + mus_decoder_config.pCustomBackendUserData = nullptr; + mus_decoder_config.customBackendCount = 2; + mus_decoder_config.ppCustomBackendVTables = mus_decoders; } AOApplication::~AOApplication() @@ -175,24 +182,6 @@ void AOApplication::call_settings_menu() delete l_dialog; } -// Callback for when BASS device is lost -// Only actually used for music syncs -void CALLBACK AOApplication::BASSreset(HSTREAM handle, DWORD channel, DWORD data, void *user) -{ - Q_UNUSED(handle); - Q_UNUSED(channel); - Q_UNUSED(data); - Q_UNUSED(user); - doBASSreset(); -} - -void AOApplication::doBASSreset() -{ - BASS_Free(); - BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr); - load_bass_plugins(); -} - void AOApplication::server_connected() { qInfo() << "Established connection to server."; @@ -203,35 +192,33 @@ void AOApplication::server_connected() courtroom_loaded = false; } -void AOApplication::initBASS() +void AOApplication::initAudio() { - BASS_SetConfig(BASS_CONFIG_DEV_DEFAULT, 1); - BASS_Free(); - // Change the default audio output device to be the one the user has given - // in his config.ini file for now. - unsigned int a = 0; - BASS_DEVICEINFO info; - - if (Options::getInstance().audioOutputDevice() == "default") + ma_context ctx; + if (ma_context_init(nullptr, 0, nullptr, &ctx) != MA_SUCCESS) { - BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr); - load_bass_plugins(); + qCritical("Failed to initialize audio context."); } - else + // TODO: Support multiple devices + + ma_resource_manager_config rm_config = ma_resource_manager_config_init(); + rm_config.decodedFormat = ma_format_f32; + rm_config.decodedChannels = 2; + rm_config.decodedSampleRate = 48000; + ma_decoding_backend_vtable *decoders[] = {ma_decoding_backend_libopus, ma_decoding_backend_libvorbis}; + rm_config.ppCustomDecodingBackendVTables = decoders; + rm_config.customDecodingBackendCount = sizeof(decoders) / sizeof(decoders[0]); + rm_config.pCustomDecodingBackendUserData = nullptr; + if (ma_resource_manager_init(&rm_config, &audio_rm) != MA_SUCCESS) { - for (a = 0; BASS_GetDeviceInfo(a, &info); a++) - { - if (Options::getInstance().audioOutputDevice() == info.name) - { - BASS_SetDevice(a); - BASS_Init(static_cast<int>(a), 48000, BASS_DEVICE_LATENCY, nullptr, nullptr); - load_bass_plugins(); - qInfo() << info.name << "was set as the default audio output device."; - return; - } - } - BASS_Init(-1, 48000, BASS_DEVICE_LATENCY, nullptr, nullptr); - load_bass_plugins(); + qCritical("Failed to initialize audio resource manager."); + } + + ma_engine_config engine_config = ma_engine_config_init(); + engine_config.pResourceManager = &audio_rm; + if (ma_engine_init(&engine_config, &audio_engine) != MA_SUCCESS) + { + qCritical("Failed to initialize audio engine."); } } @@ -262,22 +249,3 @@ void AOApplication::centerOrMoveWidgetOnPrimaryScreen(QWidget *widget) widget->move(point->x(), point->y()); } } - -#if (defined(_WIN32) || defined(_WIN64)) -void AOApplication::load_bass_plugins() -{ - BASS_PluginLoad("bassopus.dll", 0); -} -#elif defined __APPLE__ -void AOApplication::load_bass_plugins() -{ - BASS_PluginLoad("libbassopus.dylib", 0); -} -#elif (defined(LINUX) || defined(__linux__)) -void AOApplication::load_bass_plugins() -{ - BASS_PluginLoad("libbassopus.so", 0); -} -#else -#error This operating system is unsupported for BASS plugins. -#endif |
