From b0db9272211960f52afb7cf80472045a0b69694d Mon Sep 17 00:00:00 2001 From: Dale Whinham Date: Thu, 1 Dec 2022 13:51:41 +0000 Subject: [PATCH] Add ability to reverse global stereo output Closes #307. --- CHANGELOG.md | 4 ++++ include/config.def | 1 + sdcard/mt32-pi.cfg | 7 +++++++ src/mt32pi.cpp | 23 +++++++++++++++++++---- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1153c5d..cb81023 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Ability to reverse global left and right audio channels (new configuration file option) to work around broken hardware (issue #307). + ### Changed - Update to libmt32emu v2.7.0. diff --git a/include/config.def b/include/config.def index 01cc73c..aed5624 100644 --- a/include/config.def +++ b/include/config.def @@ -53,6 +53,7 @@ BEGIN_SECTION(audio) CFG(output_device, TAudioOutputDevice, AudioOutputDevice, TAudioOutputDevice::PWM ) CFG(sample_rate, int, AudioSampleRate, 48000 ) CFG(chunk_size, int, AudioChunkSize, 256 ) +CFG(reversed_stereo, bool, AudioReversedStereo, false ) END_SECTION BEGIN_SECTION(control) diff --git a/sdcard/mt32-pi.cfg b/sdcard/mt32-pi.cfg index a94f311..78cecf5 100644 --- a/sdcard/mt32-pi.cfg +++ b/sdcard/mt32-pi.cfg @@ -147,6 +147,13 @@ sample_rate = 48000 # Values: 2-2048 (256*) chunk_size = 256 +# Set whether the stereo channels should be swapped or not. +# +# Use this option to work around wrongly-wired audio hardware. +# +# Values: on, off* +reversed_stereo = off + # ----------------------------------------------------------------------------- # Control options # ----------------------------------------------------------------------------- diff --git a/src/mt32pi.cpp b/src/mt32pi.cpp index 6ace8e8..d39aa5b 100644 --- a/src/mt32pi.cpp +++ b/src/mt32pi.cpp @@ -562,6 +562,7 @@ void CMT32Pi::AudioTask() // Circle's "fast path" for I2S 24-bit really expects 32-bit samples const bool bI2S = m_pConfig->AudioOutputDevice == CConfig::TAudioOutputDevice::I2S; + const bool bReversedStereo = m_pConfig->AudioReversedStereo; const u8 nBytesPerSample = bI2S ? sizeof(s32) : (sizeof(s8) * 3); const u8 nBytesPerFrame = 2 * nBytesPerSample; @@ -578,11 +579,25 @@ void CMT32Pi::AudioTask() m_pCurrentSynth->Render(FloatBuffer, nFrames); - // Convert to signed 24-bit integers - for (size_t i = 0; i < nFrames * nChannels; ++i) + if (bReversedStereo) { - s32* const pSample = reinterpret_cast(IntBuffer + i * nBytesPerSample); - *pSample = FloatBuffer[i] * Sample24BitMax; + // Convert to signed 24-bit integers with channel swap + for (size_t i = 0; i < nFrames * nChannels; i += nChannels) + { + s32* const pLeftSample = reinterpret_cast(IntBuffer + i * nBytesPerSample); + s32* const pRightSample = reinterpret_cast(IntBuffer + (i + 1) * nBytesPerSample); + *pLeftSample = FloatBuffer[i + 1] * Sample24BitMax; + *pRightSample = FloatBuffer[i] * Sample24BitMax; + } + } + else + { + // Convert to signed 24-bit integers + for (size_t i = 0; i < nFrames * nChannels; ++i) + { + s32* const pSample = reinterpret_cast(IntBuffer + i * nBytesPerSample); + *pSample = FloatBuffer[i] * Sample24BitMax; + } } const int nResult = m_pSound->Write(IntBuffer, nWriteBytes);