From 78629c077e299827db2a4e038e5ee50a4a9d22c4 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 18 Feb 2022 17:37:26 -0500 Subject: [PATCH 01/19] Make all branches build again --- .github/workflows/cmake.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 1539212f5..174a49bc2 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -3,8 +3,6 @@ name: CMake on: push: - branches: - - master paths: - src/** @@ -15,8 +13,6 @@ on: - "!**/Makefile*" pull_request: - branches: - - master paths: - src/** From 6d57d031fa94cb362d20eed37bcb258f54842435 Mon Sep 17 00:00:00 2001 From: Jasmine Iwanek Date: Fri, 18 Feb 2022 17:38:16 -0500 Subject: [PATCH 02/19] Update cmake.yml --- .github/workflows/cmake.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 174a49bc2..4bc84e91c 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -3,7 +3,6 @@ name: CMake on: push: - paths: - src/** - "**/CMakeLists.txt" @@ -13,7 +12,6 @@ on: - "!**/Makefile*" pull_request: - paths: - src/** - "**/CMakeLists.txt" From 09716ae28434c955b75c38c6d49c22c32cd23ce5 Mon Sep 17 00:00:00 2001 From: Cacodemon345 Date: Sat, 19 Feb 2022 15:35:10 +0600 Subject: [PATCH 03/19] FAudio audio backend --- CMakeLists.txt | 1 + src/CMakeLists.txt | 7 +- src/sound/CMakeLists.txt | 10 +- src/sound/faudio.cpp | 219 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 src/sound/faudio.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index baebe22b8..38df9646e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,6 +115,7 @@ set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) option(RELEASE "Release build" OFF) option(DYNAREC "Dynamic recompiler" ON) option(OPENAL "OpenAL" ON) +option(FAUDIO "FAudio" OFF) option(FLUIDSYNTH "FluidSynth" ON) option(MUNT "MUNT" ON) option(DINPUT "DirectInput" OFF) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 704024067..ba0cffc7d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -86,7 +86,12 @@ if(APPLE) target_link_libraries(86Box Freetype::Freetype) endif() -if(OPENAL) +if(FAUDIO) + find_package(FAudio REQUIRED) + target_link_libraries(86Box FAudio::FAudio) +endif() + +if(OPENAL AND NOT FAUDIO) if(VCPKG_TOOLCHAIN) find_package(OpenAL CONFIG REQUIRED) elseif(MINGW) diff --git a/src/sound/CMakeLists.txt b/src/sound/CMakeLists.txt index 8879b0378..bddd1776e 100644 --- a/src/sound/CMakeLists.txt +++ b/src/sound/CMakeLists.txt @@ -19,9 +19,13 @@ add_library(snd OBJECT sound.c snd_opl.c snd_opl_nuked.c snd_resid.cc snd_azt2316a.c snd_cms.c snd_cs423x.c snd_gus.c snd_sb.c snd_sb_dsp.c snd_emu8k.c snd_mpu401.c snd_sn76489.c snd_ssi2001.c snd_wss.c snd_ym7128.c) -if(OPENAL) +if(OPENAL OR FAUDIO) target_compile_definitions(snd PRIVATE USE_OPENAL) - target_sources(snd PRIVATE openal.c) + if (FAUDIO) + target_sources(snd PRIVATE faudio.cpp) + else() + target_sources(snd PRIVATE openal.c) + endif() endif() if(FLUIDSYNTH) @@ -60,4 +64,4 @@ if(TANDY_ISA) endif() add_subdirectory(resid-fp) -target_link_libraries(86Box resid-fp) \ No newline at end of file +target_link_libraries(86Box resid-fp) diff --git a/src/sound/faudio.cpp b/src/sound/faudio.cpp new file mode 100644 index 000000000..246ec0b67 --- /dev/null +++ b/src/sound/faudio.cpp @@ -0,0 +1,219 @@ +/* + * 86Box A hypervisor and IBM PC system emulator that specializes in + * running old operating systems and software designed for IBM + * PC systems and compatibles from 1981 through fairly recent + * system designs based on the PCI bus. + * + * This file is part of the 86Box distribution. + * + * Interface to the FAudio audio processing library. + * + * + * + * Authors: Cacodemon345 + * + * Copyright 2022 Cacodemon345. + */ +#include +#include +#include +#include +#include +#include +#include + +extern "C" +{ +#include <86box/86box.h> +#include <86box/sound.h> +#include <86box/midi.h> + +static int midi_freq = 44100; +static int midi_buf_size = 4410; +static int initialized = 0; +static FAudio* faudio = nullptr; +static FAudioMasteringVoice* mastervoice = nullptr; +static FAudioSourceVoice* srcvoice = nullptr; +static FAudioSourceVoice* srcvoicemidi = nullptr; +static FAudioSourceVoice* srcvoicecd = nullptr; + +#define FREQ 48000 +#define BUFLEN SOUNDBUFLEN + +static void FAUDIOCALL +onBufferFinished( + FAudioVoiceCallback *callback, + void *pBufferContext) +{ + if (sound_is_float) delete[] (float*)(pBufferContext); + else delete[] (int16_t*)(pBufferContext); + +} + +static FAudioVoiceCallback callbacks = +{ + onBufferFinished +}; + +void +inital() +{ + if (FAudioCreate(&faudio, 0, FAUDIO_DEFAULT_PROCESSOR)) + { + return; + } + + if (FAudio_CreateMasteringVoice(faudio, &mastervoice, 2, FREQ, 0, 0, nullptr)) + { + FAudio_Release(faudio); + faudio = nullptr; + return; + } + FAudioWaveFormatEx fmt; + fmt.nChannels = 2; + if (sound_is_float) + { + fmt.wFormatTag = FAUDIO_FORMAT_IEEE_FLOAT; + fmt.wBitsPerSample = 32; + } + else + { + fmt.wFormatTag = FAUDIO_FORMAT_PCM; + fmt.wBitsPerSample = 16; + } + fmt.nSamplesPerSec = FREQ; + fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; + fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; + fmt.cbSize = 0; + if (FAudio_CreateSourceVoice(faudio, &srcvoice, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr)) + { + FAudioVoice_DestroyVoice(mastervoice); + FAudio_Release(faudio); + faudio = nullptr; + mastervoice = nullptr; + return; + } + fmt.nSamplesPerSec = CD_FREQ; + fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; + fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; + FAudio_CreateSourceVoice(faudio, &srcvoicecd, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr); + FAudioVoice_SetVolume(srcvoice, 1, FAUDIO_COMMIT_NOW); + FAudioSourceVoice_Start(srcvoice, 0, FAUDIO_COMMIT_NOW); + FAudioSourceVoice_Start(srcvoicecd, 0, FAUDIO_COMMIT_NOW); + auto mdn = midi_device_get_internal_name(midi_device_current); + if (strcmp(mdn, "none") && strcmp(mdn, SYSTEM_MIDI_INTERNAL_NAME)) + { + fmt.nSamplesPerSec = midi_freq; + fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; + fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; + FAudio_CreateSourceVoice(faudio, &srcvoicemidi, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr); + FAudioSourceVoice_Start(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); + } + initialized = 1; +} + +void +closeal() +{ + if (!initialized) return; + initialized = 0; + FAudioSourceVoice_Stop(srcvoice, 0, FAUDIO_COMMIT_NOW); + FAudioSourceVoice_FlushSourceBuffers(srcvoice); + FAudioSourceVoice_Stop(srcvoicecd, 0, FAUDIO_COMMIT_NOW); + FAudioSourceVoice_FlushSourceBuffers(srcvoicecd); + if (srcvoicemidi) + { + FAudioSourceVoice_Stop(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); + FAudioSourceVoice_FlushSourceBuffers(srcvoicemidi); + FAudioVoice_DestroyVoice(srcvoicemidi); + } + FAudioVoice_DestroyVoice(srcvoice); + FAudioVoice_DestroyVoice(srcvoicecd); + FAudioVoice_DestroyVoice(mastervoice); + FAudio_Release(faudio); + srcvoice = srcvoicecd = srcvoicemidi = nullptr; + mastervoice = nullptr; + faudio = nullptr; +} + +void +givealbuffer_common(void *buf, FAudioSourceVoice* sourcevoice, size_t buflen) +{ + if (!initialized) return; + + FAudioVoice_SetVolume(mastervoice, pow(10.0, (double)sound_gain / 20.0), FAUDIO_COMMIT_NOW); + FAudioBuffer buffer{}; + buffer.Flags = 0; + if (sound_is_float) + { + buffer.pAudioData = (uint8_t*)new float[buflen]; + buffer.AudioBytes = (buflen) * sizeof(float); + } + else + { + buffer.pAudioData = (uint8_t*)new int16_t[buflen]; + buffer.AudioBytes = (buflen) * sizeof(int16_t); + } + if (buffer.pAudioData == nullptr) + { + fatal("faudio: Out Of Memory!"); + } + memcpy((void*)buffer.pAudioData, buf, buffer.AudioBytes); + buffer.PlayBegin = buffer.PlayLength = 0; + buffer.PlayLength = buflen >> 1; + buffer.pContext = (void*)buffer.pAudioData; + FAudioSourceVoice_SubmitSourceBuffer(sourcevoice, &buffer, nullptr); +} + +void +givealbuffer(void *buf) +{ + givealbuffer_common(buf, srcvoice, BUFLEN << 1); +} + +void +givealbuffer_cd(void *buf) +{ + if (srcvoicecd) givealbuffer_common(buf, srcvoicecd, CD_BUFLEN << 1); +} + +void +al_set_midi(int freq, int buf_size) +{ + midi_freq = freq; + midi_buf_size = buf_size; + + if (initialized && srcvoicemidi) + { + FAudioSourceVoice_Stop(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); + FAudioSourceVoice_FlushSourceBuffers(srcvoicemidi); + FAudioVoice_DestroyVoice(srcvoicemidi); + srcvoicemidi = nullptr; + FAudioWaveFormatEx fmt; + fmt.nChannels = 2; + if (sound_is_float) + { + fmt.wFormatTag = FAUDIO_FORMAT_IEEE_FLOAT; + fmt.wBitsPerSample = 32; + } + else + { + fmt.wFormatTag = FAUDIO_FORMAT_PCM; + fmt.wBitsPerSample = 16; + } + fmt.nSamplesPerSec = midi_freq; + fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; + fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; + fmt.cbSize = 0; + FAudio_CreateSourceVoice(faudio, &srcvoicemidi, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr); + FAudioSourceVoice_Start(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); + } +} + +void +givealbuffer_midi(void *buf, uint32_t size) +{ + givealbuffer_common(buf, srcvoicemidi, size); +} + +} \ No newline at end of file From a625f372375cf049b5f60ea065c4d6067fde512b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sat, 19 Feb 2022 12:59:30 +0100 Subject: [PATCH 04/19] Add EditorConfig for JSON files --- .editorconfig | 4 ++++ vcpkg.json | 66 +++++++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/.editorconfig b/.editorconfig index 047c9d67c..1cb9087ec 100644 --- a/.editorconfig +++ b/.editorconfig @@ -32,3 +32,7 @@ indent_size = 4 [*.cmake] indent_style = space indent_size = 4 + +[*.json] +indent_style = space +indent_size = 4 diff --git a/vcpkg.json b/vcpkg.json index 85295f1cf..2e01d1c1f 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,35 +1,35 @@ { - "name": "86box", - "version-string": "3.1", - "homepage": "https://86box.net/", - "documentation": "http://86box.readthedocs.io/", - "license": "GPL-2.0-or-later", - "dependencies": [ - "freetype", - "libpng", - "openal-soft", - "sdl2", - "rtmidi" - ], - "features": { - "qt-ui": { - "description": "Qt User Interface", - "dependencies": [ - "qt5-base", - "qt5-translations" - ] - }, - "munt": { - "description": "Roland MT-32 emulation", - "dependencies": [ - "libmt32emu" - ] - }, - "slirp": { - "description": "Slirp network support", - "dependencies": [ - "libslirp" - ] - } - } + "name": "86box", + "version-string": "3.1", + "homepage": "https://86box.net/", + "documentation": "http://86box.readthedocs.io/", + "license": "GPL-2.0-or-later", + "dependencies": [ + "freetype", + "libpng", + "openal-soft", + "sdl2", + "rtmidi" + ], + "features": { + "qt-ui": { + "description": "Qt User Interface", + "dependencies": [ + "qt5-base", + "qt5-translations" + ] + }, + "munt": { + "description": "Roland MT-32 emulation", + "dependencies": [ + "libmt32emu" + ] + }, + "slirp": { + "description": "Slirp network support", + "dependencies": [ + "libslirp" + ] + } + } } From e03a0c446ece4fdb16323ffbf1d33f98b2264982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sat, 19 Feb 2022 16:20:20 +0100 Subject: [PATCH 05/19] Fix Mac bundles (hopefully finally) --- src/qt/CMakeLists.txt | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index d48f17115..94a7dadd9 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -202,9 +202,8 @@ macro(install_qt5_plugin _qt_plugin_name _qt_plugins_var _prefix) get_filename_component(_qt_plugin_type "${_qt_plugin_path}" PATH) get_filename_component(_qt_plugin_type "${_qt_plugin_type}" NAME) set(_qt_plugin_dest "${_prefix}/PlugIns/${_qt_plugin_type}") - install(FILES "${_qt_plugin_path}" - DESTINATION "${_qt_plugin_dest}") - list(APPEND ${_qt_plugins_var} "${_qt_plugin_dest}/${_qt_plugin_file}") + install(FILES "${_qt_plugin_path}" DESTINATION "${_qt_plugin_dest}") + list(APPEND ${_qt_plugins_var} "\${CMAKE_INSTALL_PREFIX_ABSOLUTE}/${_qt_plugin_dest}/${_qt_plugin_file}") else() message(FATAL_ERROR "QT plugin ${_qt_plugin_name} not found") endif() @@ -245,11 +244,7 @@ if (APPLE AND CMAKE_MACOSX_BUNDLE) install(CODE " include(BundleUtilities) get_filename_component(CMAKE_INSTALL_PREFIX_ABSOLUTE \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX} ABSOLUTE) - foreach(PLUGIN ${QT_PLUGINS}) - get_filename_component(PLUGIN_ABSOLUTE \${PLUGIN} ABSOLUTE BASE_DIR \${CMAKE_INSTALL_PREFIX_ABSOLUTE}) - list(APPEND QT_PLUGINS_ABSOLUTE \${PLUGIN_ABSOLUTE}) - endforeach() - fixup_bundle(\"\${CMAKE_INSTALL_PREFIX_ABSOLUTE}/86Box.app\" \"${QT_PLUGINS_ABSOLUTE}\" \"${DIRS}\")") + fixup_bundle(\"\${CMAKE_INSTALL_PREFIX_ABSOLUTE}/86Box.app\" \"${QT_PLUGINS}\" \"${DIRS}\")") endif() if (UNIX AND NOT APPLE) From 72a7c9cdcf5b36bdf5fecad3d2c29bb6551d8b81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sat, 19 Feb 2022 21:59:30 +0100 Subject: [PATCH 06/19] Rename `faudio.cpp` to `xaudio2.c` --- src/sound/{faudio.cpp => xaudio2.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/sound/{faudio.cpp => xaudio2.c} (100%) diff --git a/src/sound/faudio.cpp b/src/sound/xaudio2.c similarity index 100% rename from src/sound/faudio.cpp rename to src/sound/xaudio2.c From 3101d74658d1e65e83abf86de4f4170b6a2fbe10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 00:41:28 +0100 Subject: [PATCH 07/19] Add XAudio2 support to the FAudio sound backend --- CMakeLists.txt | 3 +- src/CMakeLists.txt | 24 +---- src/include/FAudio_compat.h | 106 +++++++++++++++++++ src/sound/CMakeLists.txt | 36 ++++++- src/sound/xaudio2.c | 202 +++++++++++++++++++++++------------- 5 files changed, 270 insertions(+), 101 deletions(-) create mode 100644 src/include/FAudio_compat.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ff59cbc6b..0aa4c7b56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,8 +114,7 @@ set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) # ------ ----------- ---- option(RELEASE "Release build" OFF) option(DYNAREC "Dynamic recompiler" ON) -option(OPENAL "OpenAL" ON) -option(FAUDIO "FAudio" OFF) +option(OPENAL "OpenAL" OFF) option(FLUIDSYNTH "FluidSynth" ON) option(MUNT "MUNT" ON) option(DINPUT "DirectInput" OFF) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba0cffc7d..674346065 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,7 +43,7 @@ if(VNC) add_compile_definitions(USE_VNC) add_library(vnc OBJECT vnc.c vnc_keymap.c) target_link_libraries(86Box vnc vncserver) - if (WIN32) + if(WIN32) target_link_libraries(86Box ws2_32) endif() endif() @@ -86,28 +86,6 @@ if(APPLE) target_link_libraries(86Box Freetype::Freetype) endif() -if(FAUDIO) - find_package(FAudio REQUIRED) - target_link_libraries(86Box FAudio::FAudio) -endif() - -if(OPENAL AND NOT FAUDIO) - if(VCPKG_TOOLCHAIN) - find_package(OpenAL CONFIG REQUIRED) - elseif(MINGW) - find_package(OpenAL MODULE REQUIRED) - else() - find_package(OpenAL REQUIRED) - endif() - - if(TARGET OpenAL::OpenAL) - target_link_libraries(86Box OpenAL::OpenAL) - else() - include_directories(${OPENAL_INCLUDE_DIR}) - target_link_libraries(86Box ${OPENAL_LIBRARY}) - endif() -endif() - find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) if(STATIC_BUILD AND TARGET SDL2::SDL2-static) diff --git a/src/include/FAudio_compat.h b/src/include/FAudio_compat.h new file mode 100644 index 000000000..eb901a184 --- /dev/null +++ b/src/include/FAudio_compat.h @@ -0,0 +1,106 @@ +/* map xaudio2 API to faudio API */ +typedef uint32_t HRESULT; +typedef uint32_t UINT32; +typedef uint32_t DWORD; +typedef uint8_t BOOL; + +#define WINAPI FAUDIOCALL + +#define TRUE 1 +#define FALSE 0 + +#define S_OK 0 +#define XAUDIO2_E_INVALID_CALL FAUDIO_E_INVALID_CALL + +#define XAUDIO2_DEFAULT_PROCESSOR FAUDIO_DEFAULT_PROCESSOR +#define XAUDIO2_COMMIT_NOW FAUDIO_COMMIT_NOW +#define XAUDIO2_END_OF_STREAM FAUDIO_END_OF_STREAM + +#define WAVE_FORMAT_PCM FAUDIO_FORMAT_PCM +#define WAVE_FORMAT_IEEE_FLOAT FAUDIO_FORMAT_IEEE_FLOAT + +#define AudioCategory_GameEffects FAudioStreamCategory_GameEffects + +#define GlobalDefaultDevice FAudioGlobalDefaultDevice +#define NotDefaultDevice FAudioNotDefaultDevice + +#define XAudio2Create FAudioCreate + +typedef FAudioBuffer XAUDIO2_BUFFER; +typedef FAudioDeviceDetails XAUDIO2_DEVICE_DETAILS; +typedef FAudioEffectChain XAUDIO2_EFFECT_CHAIN; +typedef FAudioEffectDescriptor XAUDIO2_EFFECT_DESCRIPTOR; +typedef FAudioVoiceDetails XAUDIO2_VOICE_DETAILS; +typedef FAudioVoiceDetails XAUDIO27_VOICE_DETAILS; +typedef FAudioVoiceState XAUDIO2_VOICE_STATE; +typedef FAudioWaveFormatEx WAVEFORMATEX; +typedef FAudioPerformanceData XAUDIO2_PERFORMANCE_DATA; + +typedef FAudioEngineCallback IXAudio2EngineCallback; +typedef FAudioVoiceCallback IXAudio2VoiceCallback; + +typedef FAPO IXAPO; + +typedef FAudio IXAudio27; +#define IXAudio27_CreateMasteringVoice FAudio_CreateMasteringVoice +#define IXAudio27_CreateSourceVoice FAudio_CreateSourceVoice +#define IXAudio27_CreateSubmixVoice FAudio_CreateSubmixVoice +#define IXAudio27_GetDeviceCount FAudio_GetDeviceCount +#define IXAudio27_GetDeviceDetails FAudio_GetDeviceDetails +#define IXAudio27_GetPerformanceData FAudio_GetPerformanceData +#define IXAudio27_Initialize FAudio_Initialize +#define IXAudio27_RegisterForCallbacks FAudio_RegisterForCallbacks +#define IXAudio27_Release FAudio_Release +#define IXAudio27_StartEngine FAudio_StartEngine +#define IXAudio27_StopEngine FAudio_StopEngine +#define IXAudio27_UnregisterForCallbacks FAudio_UnregisterForCallbacks + +typedef FAudio IXAudio2; +#define IXAudio2_CreateMasteringVoice FAudio_CreateMasteringVoice8 +#define IXAudio2_CreateSourceVoice FAudio_CreateSourceVoice +#define IXAudio2_CreateSubmixVoice FAudio_CreateSubmixVoice +#define IXAudio2_GetPerformanceData FAudio_GetPerformanceData +#define IXAudio2_RegisterForCallbacks FAudio_RegisterForCallbacks +#define IXAudio2_Release FAudio_Release +#define IXAudio2_StartEngine FAudio_StartEngine +#define IXAudio2_StopEngine FAudio_StopEngine +#define IXAudio2_UnregisterForCallbacks FAudio_UnregisterForCallbacks + +typedef FAudioMasteringVoice IXAudio2MasteringVoice; +#define IXAudio2MasteringVoice_DestroyVoice FAudioVoice_DestroyVoice +#define IXAudio2MasteringVoice_GetChannelMask FAudioMasteringVoice_GetChannelMask +#define IXAudio2MasteringVoice_SetEffectChain FAudioVoice_SetEffectChain +#define IXAudio2MasteringVoice_SetVolume FAudioVoice_SetVolume + +typedef FAudioSourceVoice IXAudio27SourceVoice; +#define IXAudio27SourceVoice_DestroyVoice FAudioVoice_DestroyVoice +#define IXAudio27SourceVoice_ExitLoop FAudioSourceVoice_ExitLoop +#define IXAudio27SourceVoice_FlushSourceBuffers FAudioSourceVoice_FlushSourceBuffers +#define IXAudio27SourceVoice_GetState(a,b) FAudioSourceVoice_GetState(a,b,0) +#define IXAudio27SourceVoice_GetVoiceDetails FAudioVoice_GetVoiceDetails +#define IXAudio27SourceVoice_SetChannelVolumes FAudioVoice_SetChannelVolumes +#define IXAudio27SourceVoice_SetSourceSampleRate FAudioSourceVoice_SetSourceSampleRate +#define IXAudio27SourceVoice_Start FAudioSourceVoice_Start +#define IXAudio27SourceVoice_Stop FAudioSourceVoice_Stop +#define IXAudio27SourceVoice_SubmitSourceBuffer FAudioSourceVoice_SubmitSourceBuffer + +typedef FAudioSourceVoice IXAudio2SourceVoice; +#define IXAudio2SourceVoice_DestroyVoice FAudioVoice_DestroyVoice +#define IXAudio2SourceVoice_ExitLoop FAudioSourceVoice_ExitLoop +#define IXAudio2SourceVoice_FlushSourceBuffers FAudioSourceVoice_FlushSourceBuffers +#define IXAudio2SourceVoice_GetState FAudioSourceVoice_GetState +#define IXAudio2SourceVoice_GetVoiceDetails FAudioVoice_GetVoiceDetails +#define IXAudio2SourceVoice_SetChannelVolumes FAudioVoice_SetChannelVolumes +#define IXAudio2SourceVoice_SetSourceSampleRate FAudioSourceVoice_SetSourceSampleRate +#define IXAudio2SourceVoice_SetVolume FAudioVoice_SetVolume +#define IXAudio2SourceVoice_Start FAudioSourceVoice_Start +#define IXAudio2SourceVoice_Stop FAudioSourceVoice_Stop +#define IXAudio2SourceVoice_SubmitSourceBuffer FAudioSourceVoice_SubmitSourceBuffer + +typedef FAudioSubmixVoice IXAudio27SubmixVoice; +#define IXAudio27SubmixVoice_GetVoiceDetails FAudioVoice_GetVoiceDetails +#define IXAudio27SubmixVoice_DestroyVoice FAudioVoice_DestroyVoice + +typedef FAudioSubmixVoice IXAudio2SubmixVoice; +#define IXAudio2SubmixVoice_GetVoiceDetails FAudioVoice_GetVoiceDetails +#define IXAudio2SubmixVoice_DestroyVoice FAudioVoice_DestroyVoice \ No newline at end of file diff --git a/src/sound/CMakeLists.txt b/src/sound/CMakeLists.txt index bddd1776e..968f87ef8 100644 --- a/src/sound/CMakeLists.txt +++ b/src/sound/CMakeLists.txt @@ -19,12 +19,38 @@ add_library(snd OBJECT sound.c snd_opl.c snd_opl_nuked.c snd_resid.cc snd_azt2316a.c snd_cms.c snd_cs423x.c snd_gus.c snd_sb.c snd_sb_dsp.c snd_emu8k.c snd_mpu401.c snd_sn76489.c snd_ssi2001.c snd_wss.c snd_ym7128.c) -if(OPENAL OR FAUDIO) - target_compile_definitions(snd PRIVATE USE_OPENAL) - if (FAUDIO) - target_sources(snd PRIVATE faudio.cpp) +if(OPENAL) + if(VCPKG_TOOLCHAIN) + find_package(OpenAL CONFIG REQUIRED) + elseif(MINGW) + find_package(OpenAL MODULE REQUIRED) else() - target_sources(snd PRIVATE openal.c) + find_package(OpenAL REQUIRED) + endif() + + if(TARGET OpenAL::OpenAL) + target_link_libraries(86Box OpenAL::OpenAL) + else() + include_directories(${OPENAL_INCLUDE_DIR}) + target_link_libraries(86Box ${OPENAL_LIBRARY}) + endif() + + target_sources(snd PRIVATE openal.c) +else() + if(WIN32) + option(FAUDIO "Use FAudio instead of XAudio2" OFF) + endif() + + target_sources(snd PRIVATE xaudio2.c) + + if(NOT WIN32 OR FAUDIO) + find_package(PkgConfig REQUIRED) + + # Use FAudio, a reimplementation of XAudio2 + pkg_check_modules(FAUDIO REQUIRED IMPORTED_TARGET FAudio) + target_link_libraries(86Box PkgConfig::FAUDIO) + + set_property(SOURCE xaudio2.c PROPERTY COMPILE_DEFINITIONS USE_FAUDIO) endif() endif() diff --git a/src/sound/xaudio2.c b/src/sound/xaudio2.c index 246ec0b67..8f3329c3b 100644 --- a/src/sound/xaudio2.c +++ b/src/sound/xaudio2.c @@ -6,7 +6,7 @@ * * This file is part of the 86Box distribution. * - * Interface to the FAudio audio processing library. + * Interface to the XAudio2 audio processing library. * * * @@ -20,95 +20,152 @@ #include #include #include -#include -extern "C" -{ +#if defined(_WIN32) && !defined(USE_FAUDIO) +#define COBJMACROS +#include +#else +#include +#include +#endif + #include <86box/86box.h> #include <86box/sound.h> #include <86box/midi.h> +#include <86box/plat_dynld.h> + +#if defined(_WIN32) && !defined(USE_FAUDIO) +static void *xaudio2_handle = NULL; +static HRESULT (WINAPI *pXAudio2Create)(IXAudio2** ppXAudio2, uint32_t Flags, XAUDIO2_PROCESSOR XAudio2Processor); +static dllimp_t xaudio2_imports[] = { + { "XAudio2Create", &pXAudio2Create }, + { NULL, NULL }, +}; +#define XAudio2Create pXAudio2Create +#endif static int midi_freq = 44100; static int midi_buf_size = 4410; static int initialized = 0; -static FAudio* faudio = nullptr; -static FAudioMasteringVoice* mastervoice = nullptr; -static FAudioSourceVoice* srcvoice = nullptr; -static FAudioSourceVoice* srcvoicemidi = nullptr; -static FAudioSourceVoice* srcvoicecd = nullptr; +static IXAudio2 *xaudio2 = NULL; +static IXAudio2MasteringVoice *mastervoice = NULL; +static IXAudio2SourceVoice *srcvoice = NULL; +static IXAudio2SourceVoice *srcvoicemidi = NULL; +static IXAudio2SourceVoice *srcvoicecd = NULL; #define FREQ 48000 #define BUFLEN SOUNDBUFLEN -static void FAUDIOCALL -onBufferFinished( - FAudioVoiceCallback *callback, - void *pBufferContext) +static void WINAPI OnVoiceProcessingPassStart(IXAudio2VoiceCallback *callback, uint32_t bytesRequired) {} +static void WINAPI OnVoiceProcessingPassEnd(IXAudio2VoiceCallback *callback) {} +static void WINAPI OnStreamEnd(IXAudio2VoiceCallback *callback) {} +static void WINAPI OnBufferStart(IXAudio2VoiceCallback *callback, void *pBufferContext) {} +static void WINAPI OnLoopEnd(IXAudio2VoiceCallback *callback, void *pBufferContext) {} +static void WINAPI OnVoiceError(IXAudio2VoiceCallback *callback, void *pBufferContext, HRESULT error) {} + +static void WINAPI +OnBufferEnd(IXAudio2VoiceCallback *callback, void *pBufferContext) { - if (sound_is_float) delete[] (float*)(pBufferContext); - else delete[] (int16_t*)(pBufferContext); - + free(pBufferContext); } -static FAudioVoiceCallback callbacks = +#if defined(_WIN32) && !defined(USE_FAUDIO) +static IXAudio2VoiceCallbackVtbl callbacksVtbl = +#else +static FAudioVoiceCallback callbacks = +#endif { - onBufferFinished + .OnVoiceProcessingPassStart = OnVoiceProcessingPassStart, + .OnVoiceProcessingPassEnd = OnVoiceProcessingPassEnd, + .OnStreamEnd = OnStreamEnd, + .OnBufferStart = OnBufferStart, + .OnBufferEnd = OnBufferEnd, + .OnLoopEnd = OnLoopEnd, + .OnVoiceError = OnVoiceError }; +#if defined(_WIN32) && !defined(USE_FAUDIO) +static IXAudio2VoiceCallback callbacks = { &callbacksVtbl }; +#endif + void inital() { - if (FAudioCreate(&faudio, 0, FAUDIO_DEFAULT_PROCESSOR)) +#if defined(_WIN32) && !defined(USE_FAUDIO) + if (xaudio2_handle == NULL) { + xaudio2_handle = dynld_module("xaudio2_9.dll", xaudio2_imports); + } + + if (xaudio2_handle == NULL) { + xaudio2_handle = dynld_module("xaudio2_9redist.dll", xaudio2_imports); + } + + if (xaudio2_handle == NULL) { + return; + } +#endif + + if (XAudio2Create(&xaudio2, 0, XAUDIO2_DEFAULT_PROCESSOR)) { return; } - if (FAudio_CreateMasteringVoice(faudio, &mastervoice, 2, FREQ, 0, 0, nullptr)) + if (IXAudio2_CreateMasteringVoice(xaudio2, &mastervoice, 2, FREQ, 0, 0, NULL, 0)) { - FAudio_Release(faudio); - faudio = nullptr; + IXAudio2_Release(xaudio2); + xaudio2 = NULL; return; } - FAudioWaveFormatEx fmt; + + WAVEFORMATEX fmt; fmt.nChannels = 2; + if (sound_is_float) { - fmt.wFormatTag = FAUDIO_FORMAT_IEEE_FLOAT; + fmt.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; fmt.wBitsPerSample = 32; } else { - fmt.wFormatTag = FAUDIO_FORMAT_PCM; + fmt.wFormatTag = WAVE_FORMAT_PCM; fmt.wBitsPerSample = 16; } + fmt.nSamplesPerSec = FREQ; fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; fmt.cbSize = 0; - if (FAudio_CreateSourceVoice(faudio, &srcvoice, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr)) + + if (IXAudio2_CreateSourceVoice(xaudio2, &srcvoice, &fmt, 0, 2.0f, &callbacks, NULL, NULL)) { - FAudioVoice_DestroyVoice(mastervoice); - FAudio_Release(faudio); - faudio = nullptr; - mastervoice = nullptr; + IXAudio2MasteringVoice_DestroyVoice(mastervoice); + IXAudio2_Release(xaudio2); + xaudio2 = NULL; + mastervoice = NULL; return; } + fmt.nSamplesPerSec = CD_FREQ; fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; - FAudio_CreateSourceVoice(faudio, &srcvoicecd, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr); - FAudioVoice_SetVolume(srcvoice, 1, FAUDIO_COMMIT_NOW); - FAudioSourceVoice_Start(srcvoice, 0, FAUDIO_COMMIT_NOW); - FAudioSourceVoice_Start(srcvoicecd, 0, FAUDIO_COMMIT_NOW); - auto mdn = midi_device_get_internal_name(midi_device_current); + + IXAudio2_CreateSourceVoice(xaudio2, &srcvoicecd, &fmt, 0, 2.0f, &callbacks, NULL, NULL); + + IXAudio2SourceVoice_SetVolume(srcvoice, 1, XAUDIO2_COMMIT_NOW); + IXAudio2SourceVoice_Start(srcvoice, 0, XAUDIO2_COMMIT_NOW); + IXAudio2SourceVoice_Start(srcvoicecd, 0, XAUDIO2_COMMIT_NOW); + + char *mdn = midi_device_get_internal_name(midi_device_current); + if (strcmp(mdn, "none") && strcmp(mdn, SYSTEM_MIDI_INTERNAL_NAME)) - { + { fmt.nSamplesPerSec = midi_freq; fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; - FAudio_CreateSourceVoice(faudio, &srcvoicemidi, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr); - FAudioSourceVoice_Start(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); + IXAudio2_CreateSourceVoice(xaudio2, &srcvoicemidi, &fmt, 0, 2.0f, &callbacks, NULL, NULL); + IXAudio2SourceVoice_Start(srcvoicemidi, 0, XAUDIO2_COMMIT_NOW); } + initialized = 1; } @@ -117,52 +174,57 @@ closeal() { if (!initialized) return; initialized = 0; - FAudioSourceVoice_Stop(srcvoice, 0, FAUDIO_COMMIT_NOW); - FAudioSourceVoice_FlushSourceBuffers(srcvoice); - FAudioSourceVoice_Stop(srcvoicecd, 0, FAUDIO_COMMIT_NOW); - FAudioSourceVoice_FlushSourceBuffers(srcvoicecd); + IXAudio2SourceVoice_Stop(srcvoice, 0, XAUDIO2_COMMIT_NOW); + IXAudio2SourceVoice_FlushSourceBuffers(srcvoice); + IXAudio2SourceVoice_Stop(srcvoicecd, 0, XAUDIO2_COMMIT_NOW); + IXAudio2SourceVoice_FlushSourceBuffers(srcvoicecd); if (srcvoicemidi) { - FAudioSourceVoice_Stop(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); - FAudioSourceVoice_FlushSourceBuffers(srcvoicemidi); - FAudioVoice_DestroyVoice(srcvoicemidi); + IXAudio2SourceVoice_Stop(srcvoicemidi, 0, XAUDIO2_COMMIT_NOW); + IXAudio2SourceVoice_FlushSourceBuffers(srcvoicemidi); + IXAudio2SourceVoice_DestroyVoice(srcvoicemidi); } - FAudioVoice_DestroyVoice(srcvoice); - FAudioVoice_DestroyVoice(srcvoicecd); - FAudioVoice_DestroyVoice(mastervoice); - FAudio_Release(faudio); - srcvoice = srcvoicecd = srcvoicemidi = nullptr; - mastervoice = nullptr; - faudio = nullptr; + IXAudio2SourceVoice_DestroyVoice(srcvoice); + IXAudio2SourceVoice_DestroyVoice(srcvoicecd); + IXAudio2MasteringVoice_DestroyVoice(mastervoice); + IXAudio2_Release(xaudio2); + srcvoice = srcvoicecd = srcvoicemidi = NULL; + mastervoice = NULL; + xaudio2 = NULL; + +#if defined(_WIN32) && !defined(USE_FAUDIO) + dynld_close(xaudio2_handle); + xaudio2_handle = NULL; +#endif } void -givealbuffer_common(void *buf, FAudioSourceVoice* sourcevoice, size_t buflen) +givealbuffer_common(void *buf, IXAudio2SourceVoice* sourcevoice, size_t buflen) { if (!initialized) return; - FAudioVoice_SetVolume(mastervoice, pow(10.0, (double)sound_gain / 20.0), FAUDIO_COMMIT_NOW); - FAudioBuffer buffer{}; + IXAudio2MasteringVoice_SetVolume(mastervoice, pow(10.0, (double)sound_gain / 20.0), XAUDIO2_COMMIT_NOW); + XAUDIO2_BUFFER buffer = {0}; buffer.Flags = 0; if (sound_is_float) { - buffer.pAudioData = (uint8_t*)new float[buflen]; + buffer.pAudioData = calloc(buflen, sizeof(float)); buffer.AudioBytes = (buflen) * sizeof(float); } else { - buffer.pAudioData = (uint8_t*)new int16_t[buflen]; + buffer.pAudioData = calloc(buflen, sizeof(int16_t)); buffer.AudioBytes = (buflen) * sizeof(int16_t); } - if (buffer.pAudioData == nullptr) + if (buffer.pAudioData == NULL) { - fatal("faudio: Out Of Memory!"); + fatal("xaudio2: Out Of Memory!"); } memcpy((void*)buffer.pAudioData, buf, buffer.AudioBytes); buffer.PlayBegin = buffer.PlayLength = 0; buffer.PlayLength = buflen >> 1; buffer.pContext = (void*)buffer.pAudioData; - FAudioSourceVoice_SubmitSourceBuffer(sourcevoice, &buffer, nullptr); + IXAudio2SourceVoice_SubmitSourceBuffer(sourcevoice, &buffer, NULL); } void @@ -185,28 +247,28 @@ al_set_midi(int freq, int buf_size) if (initialized && srcvoicemidi) { - FAudioSourceVoice_Stop(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); - FAudioSourceVoice_FlushSourceBuffers(srcvoicemidi); - FAudioVoice_DestroyVoice(srcvoicemidi); - srcvoicemidi = nullptr; - FAudioWaveFormatEx fmt; + IXAudio2SourceVoice_Stop(srcvoicemidi, 0, XAUDIO2_COMMIT_NOW); + IXAudio2SourceVoice_FlushSourceBuffers(srcvoicemidi); + IXAudio2SourceVoice_DestroyVoice(srcvoicemidi); + srcvoicemidi = NULL; + WAVEFORMATEX fmt; fmt.nChannels = 2; if (sound_is_float) { - fmt.wFormatTag = FAUDIO_FORMAT_IEEE_FLOAT; + fmt.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; fmt.wBitsPerSample = 32; } else { - fmt.wFormatTag = FAUDIO_FORMAT_PCM; + fmt.wFormatTag = WAVE_FORMAT_PCM; fmt.wBitsPerSample = 16; } fmt.nSamplesPerSec = midi_freq; fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8; fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nBlockAlign; fmt.cbSize = 0; - FAudio_CreateSourceVoice(faudio, &srcvoicemidi, &fmt, 0, 2.0f, &callbacks, nullptr, nullptr); - FAudioSourceVoice_Start(srcvoicemidi, 0, FAUDIO_COMMIT_NOW); + IXAudio2_CreateSourceVoice(xaudio2, &srcvoicemidi, &fmt, 0, 2.0f, &callbacks, NULL, NULL); + IXAudio2SourceVoice_Start(srcvoicemidi, 0, XAUDIO2_COMMIT_NOW); } } @@ -215,5 +277,3 @@ givealbuffer_midi(void *buf, uint32_t size) { givealbuffer_common(buf, srcvoicemidi, size); } - -} \ No newline at end of file From 6a63ae21f86ebdb782b81bc5fd37107926b8f22f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 00:46:24 +0100 Subject: [PATCH 08/19] Remove the `USE_OPENAL` define as the reason for its introduction is gone --- src/86box.c | 2 -- src/sound/midi_fluidsynth.c | 8 -------- src/sound/midi_mt32.c | 8 -------- src/sound/sound.c | 7 +------ 4 files changed, 1 insertion(+), 24 deletions(-) diff --git a/src/86box.c b/src/86box.c index 9647a8a6e..2e49184ef 100644 --- a/src/86box.c +++ b/src/86box.c @@ -941,9 +941,7 @@ pc_reset_hard_close(void) scsi_disk_close(); -#ifdef USE_OPENAL closeal(); -#endif video_reset_close(); diff --git a/src/sound/midi_fluidsynth.c b/src/sound/midi_fluidsynth.c index ed3e2d535..14093efb4 100644 --- a/src/sound/midi_fluidsynth.c +++ b/src/sound/midi_fluidsynth.c @@ -40,10 +40,8 @@ enum fluid_interp { }; -#ifdef USE_OPENAL extern void givealbuffer_midi(void *buf, uint32_t size); extern void al_set_midi(int freq, int buf_size); -#endif static void *fluidsynth_handle; /* handle to FluidSynth DLL */ @@ -152,9 +150,7 @@ static void fluidsynth_thread(void *param) buf_pos += buf_size; if (buf_pos >= data->buf_size) { -#ifdef USE_OPENAL givealbuffer_midi(data->buffer, data->buf_size / sizeof(float)); -#endif buf_pos = 0; } } @@ -167,9 +163,7 @@ static void fluidsynth_thread(void *param) buf_pos += buf_size; if (buf_pos >= data->buf_size) { -#ifdef USE_OPENAL givealbuffer_midi(data->buffer_int16, data->buf_size / sizeof(int16_t)); -#endif buf_pos = 0; } } @@ -322,9 +316,7 @@ void* fluidsynth_init(const device_t *info) data->buffer_int16 = malloc(data->buf_size); } -#ifdef USE_OPENAL al_set_midi(data->samplerate, data->buf_size); -#endif dev = malloc(sizeof(midi_device_t)); memset(dev, 0, sizeof(midi_device_t)); diff --git a/src/sound/midi_mt32.c b/src/sound/midi_mt32.c index 00c26b392..e1812a733 100644 --- a/src/sound/midi_mt32.c +++ b/src/sound/midi_mt32.c @@ -14,10 +14,8 @@ #include <86box/midi.h> -#ifdef USE_OPENAL extern void givealbuffer_midi(void *buf, uint32_t size); extern void al_set_midi(int freq, int buf_size); -#endif static void display_mt32_message(void *instance_data, const char *message); @@ -193,9 +191,7 @@ static void mt32_thread(void *param) buf_pos += bsize; if (buf_pos >= buf_size) { -#ifdef USE_OPENAL givealbuffer_midi(buffer, buf_size / sizeof(float)); -#endif buf_pos = 0; } } @@ -207,9 +203,7 @@ static void mt32_thread(void *param) buf_pos += bsize; if (buf_pos >= buf_size) { -#ifdef USE_OPENAL givealbuffer_midi(buffer_int16, buf_size / sizeof(int16_t)); -#endif buf_pos = 0; } } @@ -261,9 +255,7 @@ void* mt32emu_init(char *control_rom, char *pcm_rom) mt32emu_set_reversed_stereo_enabled(context, device_get_config_int("reversed_stereo")); mt32emu_set_nice_amp_ramp_enabled(context, device_get_config_int("nice_ramp")); -#ifdef USE_OPENAL al_set_midi(samplerate, buf_size); -#endif dev = malloc(sizeof(midi_device_t)); memset(dev, 0, sizeof(midi_device_t)); diff --git a/src/sound/sound.c b/src/sound/sound.c index e12b38bcb..4beab94ba 100644 --- a/src/sound/sound.c +++ b/src/sound/sound.c @@ -347,12 +347,10 @@ sound_cd_thread(void *param) } } -#ifdef USE_OPENAL if (sound_is_float) givealbuffer_cd(cd_out_buffer); else givealbuffer_cd(cd_out_buffer_int16); -#endif } } @@ -465,12 +463,10 @@ sound_poll(void *priv) } } -#ifdef USE_OPENAL if (sound_is_float) givealbuffer(outbuffer_ex); else givealbuffer(outbuffer_ex_int16); -#endif if (cd_thread_enable) { cd_buf_update--; @@ -499,9 +495,8 @@ sound_reset(void) midi_device_init(); midi_in_device_init(); -#ifdef USE_OPENAL + inital(); -#endif timer_add(&sound_poll_timer, sound_poll, NULL, 1); From 0f3c2232ad9f759a7d0b246da2801c73ac68ab26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 00:48:39 +0100 Subject: [PATCH 09/19] Add XAudio2 support to the legacy Makefile --- src/win/Makefile.mingw | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/win/Makefile.mingw b/src/win/Makefile.mingw index 3f0b3a3b6..35b06795f 100644 --- a/src/win/Makefile.mingw +++ b/src/win/Makefile.mingw @@ -173,7 +173,7 @@ ifndef DINPUT DINPUT := n endif ifndef OPENAL -OPENAL := y +OPENAL := n endif ifndef FLUIDSYNTH FLUIDSYNTH := y @@ -373,9 +373,6 @@ else endif endif -ifeq ($(OPENAL), y) -OPTS += -DUSE_OPENAL -endif ifeq ($(FLUIDSYNTH), y) OPTS += -DUSE_FLUIDSYNTH FSYNTHOBJ := midi_fluidsynth.o @@ -645,7 +642,6 @@ PRINTOBJ := png.o prt_cpmap.o \ prt_escp.o prt_text.o prt_ps.o SNDOBJ := sound.o \ - openal.o \ snd_opl.o snd_opl_nuked.o \ snd_resid.o \ convolve.o convolve-sse.o envelope.o extfilt.o \ @@ -736,6 +732,12 @@ else PLATOBJ += win_joystick_rawinput.o endif +ifeq ($(OPENAL), y) + SNDOBJ += openal.o +else + SNDOBJ += xaudio2.o +endif + OBJ := $(MAINOBJ) $(CPUOBJ) $(CHIPSETOBJ) $(MCHOBJ) $(DEVOBJ) $(MEMOBJ) \ $(FDDOBJ) $(GAMEOBJ) $(CDROMOBJ) $(ZIPOBJ) $(MOOBJ) $(HDDOBJ) $(MINIVHDOBJ) \ $(NETOBJ) $(PRINTOBJ) $(SCSIOBJ) $(SIOOBJ) $(SNDOBJ) $(VIDOBJ) $(VOODOOOBJ) \ From 5f0453746fe6851eec763d7b1a5c69ee847b216d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 00:51:53 +0100 Subject: [PATCH 10/19] Add FAudio to the Mac and Linux GitHub build actions dependencies --- .github/workflows/cmake.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml index 4bc84e91c..2e4753927 100644 --- a/.github/workflows/cmake.yml +++ b/.github/workflows/cmake.yml @@ -252,7 +252,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install dependencies - run: sudo apt update && sudo apt install gcc-11 g++-11 libfreetype-dev libsdl2-dev libpng-dev libopenal-dev libc6-dev librtmidi-dev qtbase5-dev qttools5-dev + run: sudo apt update && sudo apt install gcc-11 g++-11 libfreetype-dev libsdl2-dev libpng-dev libopenal-dev libc6-dev librtmidi-dev qtbase5-dev qttools5-dev libfaudio-dev - name: Configure CMake run: >- cmake -S . -B build @@ -316,7 +316,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install dependencies - run: brew install freetype sdl2 libpng openal-soft rtmidi qt@5 + run: brew install freetype sdl2 libpng openal-soft rtmidi qt@5 faudio - name: Configure CMake run: >- cmake -S . -B build From 4f4ed24e8d3ede5fd21be98a3c45c54c506e5805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 01:01:14 +0100 Subject: [PATCH 11/19] Try to find FAudio manually if no pkgconfig is found for it --- src/sound/CMakeLists.txt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sound/CMakeLists.txt b/src/sound/CMakeLists.txt index 968f87ef8..ef753cd6f 100644 --- a/src/sound/CMakeLists.txt +++ b/src/sound/CMakeLists.txt @@ -47,8 +47,16 @@ else() find_package(PkgConfig REQUIRED) # Use FAudio, a reimplementation of XAudio2 - pkg_check_modules(FAUDIO REQUIRED IMPORTED_TARGET FAudio) - target_link_libraries(86Box PkgConfig::FAUDIO) + pkg_check_modules(FAUDIO IMPORTED_TARGET FAudio) + if(FAUDIO_FOUND) + target_link_libraries(86Box PkgConfig::FAUDIO) + else() + find_path(FAUDIO_INCLUDE_DIR NAMES "FAudio.h") + find_library(FAUDIO_LIBRARY FAudio) + + include_directories(${FAUDIO_INCLUDE_DIR}) + target_link_libraries(86Box ${FAUDIO_LIBRARY}) + endif() set_property(SOURCE xaudio2.c PROPERTY COMPILE_DEFINITIONS USE_FAUDIO) endif() From 8f5981bbfd192f4d7a79dba4156cef8dbc0310eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 01:14:47 +0100 Subject: [PATCH 12/19] Jenkins: Disable FAudio for now --- .ci/build.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.ci/build.sh b/.ci/build.sh index a791521f5..493f42789 100644 --- a/.ci/build.sh +++ b/.ci/build.sh @@ -298,6 +298,9 @@ EOF # Link against the system libslirp instead of compiling ours. cmake_flags_extra="$cmake_flags_extra -D SLIRP_EXTERNAL=ON" + + # Use OpenAL for Linux builds before FAudio builds are set up. + cmake_flags_extra="$cmake_flags_extra -D OPENAL=ON" fi # Clean workspace. From bd32abf94e0c4a33beb9fa4a651199bd983b6575 Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 20 Feb 2022 01:22:28 +0100 Subject: [PATCH 13/19] Attempt to make the main thread wait for network mutex. --- src/include/86box/plat.h | 1 + src/network/network.c | 2 +- src/thread.cpp | 10 ++++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/include/86box/plat.h b/src/include/86box/plat.h index c60e6d922..8c0651709 100644 --- a/src/include/86box/plat.h +++ b/src/include/86box/plat.h @@ -203,6 +203,7 @@ extern void thread_destroy_event(event_t *arg); extern mutex_t *thread_create_mutex(void); extern void thread_close_mutex(mutex_t *arg); +extern int thread_test_mutex(mutex_t *arg); extern int thread_wait_mutex(mutex_t *arg); extern int thread_release_mutex(mutex_t *mutex); diff --git a/src/network/network.c b/src/network/network.c index fad9eacbc..48e3b928b 100644 --- a/src/network/network.c +++ b/src/network/network.c @@ -348,7 +348,7 @@ network_rx_queue(void *priv) { int ret = 1; - if (network_rx_pause) { + if (network_rx_pause || !thread_test_mutex(network_mutex)) { timer_on_auto(&network_rx_queue_timer, 0.762939453125 * 2.0 * 128.0); return; } diff --git a/src/thread.cpp b/src/thread.cpp index 4134befba..1003dd871 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -37,6 +37,16 @@ thread_create_mutex(void) return mutex; } +int +thread_test_mutex(mutex_t *_mutex) +{ + if (_mutex == nullptr) + return 0; + + auto mutex = reinterpret_cast(_mutex); + return mutex->try_lock() ? 1 : 0; +} + int thread_wait_mutex(mutex_t *_mutex) { From 8ec062e1ecadba46fd3018a1286ab5d01aacb75e Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 20 Feb 2022 01:30:37 +0100 Subject: [PATCH 14/19] And of course, release the mutex as well after we're done. --- src/network/network.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/network/network.c b/src/network/network.c index 48e3b928b..44408a873 100644 --- a/src/network/network.c +++ b/src/network/network.c @@ -371,6 +371,8 @@ network_rx_queue(void *priv) network_queue_advance(0); network_busy(0); + + network_wait(0); } From 623b38975c0717714603d904a6cdcb8d138018b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Hrdli=C4=8Dka?= Date: Sun, 20 Feb 2022 01:40:27 +0100 Subject: [PATCH 15/19] Don't link OpenAL on legacy makefile if disabled --- src/win/Makefile.mingw | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/win/Makefile.mingw b/src/win/Makefile.mingw index 35b06795f..1f894f12c 100644 --- a/src/win/Makefile.mingw +++ b/src/win/Makefile.mingw @@ -746,8 +746,7 @@ ifdef EXOBJ OBJ += $(EXOBJ) endif -LIBS := -mwindows -lcomctl32 \ - -lopenal -lole32 +LIBS := -mwindows -lcomctl32 -lole32 ifeq ($(VNC), y) LIBS += $(VNCLIB) -lws2_32 @@ -762,7 +761,10 @@ ifeq ($(ARM64), y) LIBS += -lgcc endif ifeq ($(DINPUT), y) - LIBS += -ldinput8 + LIBS += -ldinput8 +endif +ifeq ($(OPENAL), y) + LIBS += -lopenal endif LIBS += -static From 8ac721d395900ba815db33bc5d7871a61f4f6436 Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 20 Feb 2022 01:41:31 +0100 Subject: [PATCH 16/19] Fixed linking with Makefile.mingw. --- src/win/Makefile.mingw | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/win/Makefile.mingw b/src/win/Makefile.mingw index 35b06795f..380fa658d 100644 --- a/src/win/Makefile.mingw +++ b/src/win/Makefile.mingw @@ -746,13 +746,12 @@ ifdef EXOBJ OBJ += $(EXOBJ) endif -LIBS := -mwindows -lcomctl32 \ - -lopenal -lole32 +LIBS := -mwindows -lcomctl32 -lSDL2 -lrtmidi -limagehlp -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lversion -luuid ifeq ($(VNC), y) LIBS += $(VNCLIB) -lws2_32 endif -LIBS += -lpng -lz -lwsock32 -lshell32 -liphlpapi -lpsapi -lSDL2 -limm32 -lhid -lsetupapi -loleaut32 -luxtheme -lversion -lrtmidi -lwinmm -static -lstdc++ +LIBS += -lpng -lz -lwsock32 -liphlpapi -lpsapi -lhid -lsetupapi -luxtheme -static -lstdc++ ifneq ($(X64), y) ifneq ($(ARM64), y) LIBS += -Wl,--large-address-aware From b4c1e801c5978982885bfe1f0b4f6c09c725a3dc Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 20 Feb 2022 02:00:23 +0100 Subject: [PATCH 17/19] Some more network changes. --- src/network/net_pcap.c | 10 ++++++---- src/network/net_slirp.c | 11 +++++++---- src/network/network.c | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/network/net_pcap.c b/src/network/net_pcap.c index 7008d1442..94f050f76 100644 --- a/src/network/net_pcap.c +++ b/src/network/net_pcap.c @@ -183,8 +183,10 @@ poll_thread(void *arg) /* Wait for a poll request. */ network_poll(); - if (pcap == NULL) + if (pcap == NULL) { + network_wait(0); break; + } if (network_get_wait() || (poll_card->set_link_state && poll_card->set_link_state(poll_card->priv)) || (poll_card->wait && poll_card->wait(poll_card->priv))) data = NULL; @@ -213,12 +215,12 @@ poll_thread(void *arg) if (tx) network_do_tx(); + /* Release ownership of the device. */ + network_wait(0); + /* If we did not get anything, wait a while. */ if (!tx) thread_wait_event(evt, 10); - - /* Release ownership of the device. */ - network_wait(0); } /* No longer needed. */ diff --git a/src/network/net_slirp.c b/src/network/net_slirp.c index b4b46bbe7..ab672cb3f 100644 --- a/src/network/net_slirp.c +++ b/src/network/net_slirp.c @@ -366,7 +366,10 @@ poll_thread(void *arg) network_poll(); /* Stop processing if asked to. */ - if (slirp->stop) break; + if (slirp->stop) { + network_wait(0); + break; + } /* See if there is any work. */ slirp_tic(slirp); @@ -377,12 +380,12 @@ poll_thread(void *arg) if (tx) network_do_tx(); + /* Release ownership of the queue. */ + network_wait(0); + /* If we did not get anything, wait a while. */ if (!tx) thread_wait_event(evt, 10); - - /* Release ownership of the queue. */ - network_wait(0); } /* No longer needed. */ diff --git a/src/network/network.c b/src/network/network.c index 44408a873..b99f32adf 100644 --- a/src/network/network.c +++ b/src/network/network.c @@ -204,9 +204,13 @@ network_wait(uint8_t wait) void network_poll(void) { + network_wait(0); + while (poll_data.busy) thread_wait_event(poll_data.wake_poll_thread, -1); + network_wait(1); + thread_reset_event(poll_data.wake_poll_thread); } From f06736ed9a9e127a65f0e884d4047de474820cff Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 20 Feb 2022 02:01:56 +0100 Subject: [PATCH 18/19] And more. --- src/network/network.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/network/network.c b/src/network/network.c index b99f32adf..7f4e83514 100644 --- a/src/network/network.c +++ b/src/network/network.c @@ -352,13 +352,13 @@ network_rx_queue(void *priv) { int ret = 1; + netpkt_t *pkt = NULL; + if (network_rx_pause || !thread_test_mutex(network_mutex)) { timer_on_auto(&network_rx_queue_timer, 0.762939453125 * 2.0 * 128.0); return; } - netpkt_t *pkt = NULL; - network_busy(1); network_queue_get(0, &pkt); From 7928bc8c1cd525131a97535bae9be53135f28bce Mon Sep 17 00:00:00 2001 From: OBattler Date: Sun, 20 Feb 2022 02:03:42 +0100 Subject: [PATCH 19/19] And even more. --- src/network/net_pcap.c | 2 ++ src/network/net_slirp.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/network/net_pcap.c b/src/network/net_pcap.c index 94f050f76..cfa4837dc 100644 --- a/src/network/net_pcap.c +++ b/src/network/net_pcap.c @@ -180,8 +180,10 @@ poll_thread(void *arg) /* Request ownership of the device. */ network_wait(1); +#if 0 /* Wait for a poll request. */ network_poll(); +#endif if (pcap == NULL) { network_wait(0); diff --git a/src/network/net_slirp.c b/src/network/net_slirp.c index ab672cb3f..aeb28ad37 100644 --- a/src/network/net_slirp.c +++ b/src/network/net_slirp.c @@ -362,8 +362,10 @@ poll_thread(void *arg) /* Request ownership of the queue. */ network_wait(1); +#if 0 /* Wait for a poll request. */ network_poll(); +#endif /* Stop processing if asked to. */ if (slirp->stop) {