Skip to content

Commit

Permalink
Rate Converters fix mono/stereo copy value to output buffer (#276)
Browse files Browse the repository at this point in the history
* init

* code rev

* fix rate converters mono/stereo output buffer copy

* update version

* fix tests

* sonarcloud code rev

* windows ci

* sonarcloud ci rev
  • Loading branch information
Raffaello authored Nov 11, 2023
1 parent 5940002 commit bfee5e3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 36 deletions.
21 changes: 14 additions & 7 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@

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'
- '.gitattributes'
- 'README.md'
- 'LICENSE'
- 'wave_generators.r'
branches-ignore:
- master
release:
types: [created]
#pull_request:
# branches: [ master ]

#permissions: read-all

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ on:
- 'README.md'
- 'LICENSE'
- 'wave_generators.r'
branches:
- master
#branches:
# - master

pull_request:
# branches: [ master ]
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sdl2-hyper-sonic-drivers/examples/pcm-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/IPCMFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ namespace HyperSonicDrivers::files
{
void IPCMFile::make_pcm_sound_(const audio::mixer::eChannelGroup group)
{
if (m_channels > 2)
utils::throwLogC<std::runtime_error>(std::format("only mono or stereo PCM files are supported (num_channels = {})", m_channels));

std::shared_ptr<int16_t[]> 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<int16_t[]>(getData());
data = std::reinterpret_pointer_cast<int16_t[]>(m_data);
size >>= 1;
break;
default:
utils::throwLogC<std::invalid_argument>(std::format("bitsDepth = {}, not supported/implemented", getBitsDepth()));
utils::throwLogC<std::invalid_argument>(std::format("bitsDepth = {}, not supported/implemented", m_bitsDepth));
break;
}

if (getChannels() > 2)
utils::throwLogC<std::runtime_error>(std::format("only mono or stereo PCM files are supported (num_channels = {})", getChannels()));

m_sound = std::make_shared<audio::PCMSound>(
group,
getChannels() == 2,
getSampleRate(),
m_channels == 2,
m_sampleRate,
size,
data
);
Expand Down
15 changes: 9 additions & 6 deletions sdl2-hyper-sonic-drivers/src/HyperSonicDrivers/files/VOCFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<uint32_t>(buf.size());
m_data = std::make_shared<uint8_t[]>(m_dataSize);
std::memcpy(m_data.get(), buf.data(), sizeof(uint8_t)* m_dataSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);

Expand Down

0 comments on commit bfee5e3

Please sign in to comment.