From bfee5e31170dabd7a5a518f9309b8d4d0e784d7a Mon Sep 17 00:00:00 2001 From: Raffaello Bertini Date: Sat, 11 Nov 2023 11:40:35 +0000 Subject: [PATCH] Rate Converters fix mono/stereo copy value to output buffer (#276) * init * code rev * fix rate converters mono/stereo output buffer copy * update version * fix tests * sonarcloud code rev * windows ci * sonarcloud ci rev --- .github/workflows/ci-windows.yml | 21 ++++++++++++------- .github/workflows/sonarcloud.yml | 4 ++-- CMakeLists.txt | 2 +- .../examples/pcm-example.cpp | 4 ++-- .../audio/converters/CopyRateConverter.hpp | 4 ++-- .../audio/converters/LinearRateConverter.hpp | 8 +++---- .../src/HyperSonicDrivers/files/IPCMFile.cpp | 20 +++++++++--------- .../src/HyperSonicDrivers/files/VOCFile.cpp | 15 +++++++------ .../HyperSonicDrivers/files/TestVOCFile.cpp | 4 ++-- 9 files changed, 46 insertions(+), 36 deletions(-) diff --git a/.github/workflows/ci-windows.yml b/.github/workflows/ci-windows.yml index 572b4549..d3064cad 100644 --- a/.github/workflows/ci-windows.yml +++ b/.github/workflows/ci-windows.yml @@ -8,7 +8,20 @@ name: ci-windows on: - push: + #push: + # paths-ignore: + # - 'doc/**' + # - '.gitignore' + # - '.gitattributes' + # - 'README.md' + # - 'LICENSE' + # - 'wave_generators.r' + # branches-ignore: + # - master + release: + types: [created] + pull_request: + branches: [ master ] paths-ignore: - 'doc/**' - '.gitignore' @@ -16,12 +29,6 @@ on: - 'README.md' - 'LICENSE' - 'wave_generators.r' - branches-ignore: - - master - release: - types: [created] - #pull_request: - # branches: [ master ] #permissions: read-all diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml index 600047e9..54918cff 100644 --- a/.github/workflows/sonarcloud.yml +++ b/.github/workflows/sonarcloud.yml @@ -15,8 +15,8 @@ on: - 'README.md' - 'LICENSE' - 'wave_generators.r' - branches: - - master + #branches: + # - master pull_request: # branches: [ master ] diff --git a/CMakeLists.txt b/CMakeLists.txt index c9a88d75..0ba94995 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) endif() -project ("sdl2-hyper-sonic-drivers" VERSION 0.15.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards") +project ("sdl2-hyper-sonic-drivers" VERSION 0.15.1 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards") include (TestBigEndian) TEST_BIG_ENDIAN(IS_BIG_ENDIAN) if(IS_BIG_ENDIAN) diff --git a/sdl2-hyper-sonic-drivers/examples/pcm-example.cpp b/sdl2-hyper-sonic-drivers/examples/pcm-example.cpp index 18aabc9b..d640781e 100644 --- a/sdl2-hyper-sonic-drivers/examples/pcm-example.cpp +++ b/sdl2-hyper-sonic-drivers/examples/pcm-example.cpp @@ -57,8 +57,8 @@ int main(int argc, char* argv[]) delayMillis(500); - drv.play(wavSound, 150, -127); - drv.play(vocSound, 255, 127); + drv.play(wavSound, 150, 127); + drv.play(vocSound, 255, -127); for (int i = 0, sig = +1; i < 3; i++, sig *= -1) { cout << i << ". playing same sound again reversed balance" << endl; diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/CopyRateConverter.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/CopyRateConverter.hpp index 8d5af722..2a87bf3f 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/CopyRateConverter.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/CopyRateConverter.hpp @@ -47,9 +47,9 @@ namespace HyperSonicDrivers::audio::converters int16_t out1 = (stereo ? *it++ : out0); // output left channel - output_channel(obuf[reverseStereo ? 1 : 0], out0, vol_l); + output_channel(obuf[reverseStereo ? 0 : 1], out0, vol_l); // output right channel - output_channel(obuf[reverseStereo ? 0 : 1], out1, vol_r); + output_channel(obuf[reverseStereo ? 1 : 0], out1, vol_r); obuf += 2; } diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/LinearRateConverter.hpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/LinearRateConverter.hpp index 9cbd4c59..7ea2bac9 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/LinearRateConverter.hpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/audio/converters/LinearRateConverter.hpp @@ -101,7 +101,7 @@ namespace HyperSonicDrivers::audio::converters inLen -= (stereo ? 2 : 1); ilast0 = icur0; icur0 = *inPtr++; - if (stereo) + if constexpr (stereo) { ilast1 = icur1; icur1 = *inPtr++; @@ -114,12 +114,12 @@ namespace HyperSonicDrivers::audio::converters while (opos < fracOneLow && obuf < oend) { // interpolate - int16_t out0 = interpolate(ilast0, icur0, opos); - int16_t out1 = stereo ? interpolate(ilast1, icur1, opos) : out0; + const int16_t out0 = interpolate(ilast0, icur0, opos); + const int16_t out1 = stereo ? interpolate(ilast1, icur1, opos) : out0; // output left channel output_channel(obuf[reverseStereo ? 0 : 1], out0, vol_l); // output right channel - output_channel(obuf[reverseStereo ? 0 : 1], out1, vol_r); + output_channel(obuf[reverseStereo ? 1 : 0], out1, vol_r); obuf += 2; // Increment output position diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/IPCMFile.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/IPCMFile.cpp index 56017c62..e3ff017c 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/IPCMFile.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/IPCMFile.cpp @@ -7,30 +7,30 @@ namespace HyperSonicDrivers::files { void IPCMFile::make_pcm_sound_(const audio::mixer::eChannelGroup group) { + if (m_channels > 2) + utils::throwLogC(std::format("only mono or stereo PCM files are supported (num_channels = {})", m_channels)); + std::shared_ptr data; - uint32_t size = getDataSize(); + uint32_t size = m_dataSize; - switch (getBitsDepth()) + switch (m_bitsDepth) { case 8: - data.reset(audio::converters::convert8to16(getData().get(), size)); + data.reset(audio::converters::convert8to16(m_data.get(), size)); break; case 16: - data = std::reinterpret_pointer_cast(getData()); + data = std::reinterpret_pointer_cast(m_data); size >>= 1; break; default: - utils::throwLogC(std::format("bitsDepth = {}, not supported/implemented", getBitsDepth())); + utils::throwLogC(std::format("bitsDepth = {}, not supported/implemented", m_bitsDepth)); break; } - if (getChannels() > 2) - utils::throwLogC(std::format("only mono or stereo PCM files are supported (num_channels = {})", getChannels())); - m_sound = std::make_shared( group, - getChannels() == 2, - getSampleRate(), + m_channels == 2, + m_sampleRate, size, data ); diff --git a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/VOCFile.cpp b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/VOCFile.cpp index fa8a6857..5f6a4bcb 100644 --- a/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/VOCFile.cpp +++ b/sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/VOCFile.cpp @@ -75,7 +75,7 @@ namespace HyperSonicDrivers::files // timeConstant = 65536 - (256000000 / (channels * sampleRate); // sampleRate = 256000000 / ((65536 - (timeConstant<<8))*channels) m_sampleRate = 256000000L / ((65536 - (timeConstant << 8)) * m_channels); - //_sampleRate = 1000000 / (256 - timeConstant); + //m_sampleRate = 1000000 / (256 - timeConstant); assertValid_(m_sampleRate == (1000000 / (256 - timeConstant))); // pack Method switch (packMethod) @@ -131,10 +131,10 @@ namespace HyperSonicDrivers::files case 7: logW("end loop block not-implemented"); break; - case 8: + case 8: // extended logW("special block 8 not-implemented"); break; - case 9: + case 9: // extended 2 { assertValid_(m_version >= 0x0114); m_sampleRate = db.data[0] + (db.data[1] << 8) + (db.data[2] << 16) + (db.data[3] << 24); @@ -180,16 +180,19 @@ namespace HyperSonicDrivers::files int divisor = 1; if (m_bitsDepth == 16) { - divisor *= 2; + divisor <<= 1; } if (m_channels == 2) { - divisor *= 2; + divisor <<= 1; } - + const int d = buf.size() % divisor; for (int i = 0; i < d; i++) buf.push_back(0); + if (buf.size() % 2 == 1) + buf.push_back(0); + m_dataSize = static_cast(buf.size()); m_data = std::make_shared(m_dataSize); std::memcpy(m_data.get(), buf.data(), sizeof(uint8_t)* m_dataSize); diff --git a/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/files/TestVOCFile.cpp b/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/files/TestVOCFile.cpp index 823f588a..0e9b5d2b 100644 --- a/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/files/TestVOCFile.cpp +++ b/sdl2-hyper-sonic-drivers/test/HyperSonicDrivers/files/TestVOCFile.cpp @@ -46,8 +46,8 @@ namespace HyperSonicDrivers::files VOCFile, VocFileTest, ::testing::Values( - std::make_tuple("../fixtures/VSCREAM1.VOC", audio::mixer::eChannelGroup::Sfx, 8000, 5817, 0x80), - std::make_tuple("../fixtures/DUNE.VOC", audio::mixer::eChannelGroup::Speech, 14705, 15233, 0x83) + std::make_tuple("../fixtures/VSCREAM1.VOC", audio::mixer::eChannelGroup::Sfx, 8000, 5818, 0x80), + std::make_tuple("../fixtures/DUNE.VOC", audio::mixer::eChannelGroup::Speech, 14705, 15234, 0x83) ) );