Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renderer Flush and BufferFlush methods #284

Merged
merged 11 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.17.1 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
project ("sdl2-hyper-sonic-drivers" VERSION 0.18.0 DESCRIPTION "SDL2 based Hyper-Sonic Drivers for emulating old soundcards")
include (TestBigEndian)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
Expand Down
62 changes: 5 additions & 57 deletions sdl2-hyper-sonic-drivers/sdl2-hyper-sonic-drivers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,61 +272,6 @@ int song()
}
*/

//int renderMixer()
//{
// using namespace audio::scummvm;
// using namespace hardware::opl::scummvm;
// using namespace hardware::opl;
// using namespace drivers::westwood;
//
// audio::DiskRendererMixerManager mixerManager(44100);
// mixerManager.init();
// mixerManager.startRecording("test.dat");
//
// std::shared_ptr<Mixer> mixer = mixerManager.getMixer();
//
// //spdlog::set_level(spdlog::level::debug);
// auto opl = OPLFactory::create(OplEmulator::NUKED, OplType::OPL3, mixer);
// auto pOpl = dynamic_cast<EmulatedOPL*>( opl.get());
// //auto opl = std::make_shared<hardware::opl::mame::MameOPL>(mixer);
// std::shared_ptr<files::westwood::ADLFile> adlFile = std::make_shared<files::westwood::ADLFile>("test/fixtures/DUNE0.ADL");
//
// ADLDriver adlDrv(opl, adlFile);
// adlDrv.play(4, 0xFF);
// int samples = -1;
// int totSamples = 0;
// bool isPlaying = adlDrv.isPlaying();
// do
// {
// // TODO review, but is dumping the data
// int16_t buf[1024];
//
// samples = pOpl->readBuffer(buf, 1024);
// mixerManager.callbackHandler(reinterpret_cast<uint8_t*>(buf), samples * 2);
// totSamples += samples;
// isPlaying = adlDrv.isPlaying();
// //spdlog::info("isPlaying? {}", isPlaying);
// } while (isPlaying);
//
// //spdlog::info("TotSamples={} --- space require={} ({}KB) [{}MB]", totSamples, totSamples * sizeof(int16_t), totSamples * sizeof(int16_t) / 1024, totSamples * sizeof(int16_t) / 1024 / 1024);
//
// while (!mixer->isReady()) {
// //spdlog::info("mixer not ready");
// utils::delayMillis(100);
// }
//
// utils::delayMillis(1000);
// while (adlDrv.isPlaying())
// {
// //spdlog::info("is playing");
// utils::delayMillis(100);
//
// }
//
// //spdlog::info("renderer quitting...");
//
// return 0;
//}

void rendererMIDI()
{
Expand All @@ -349,7 +294,7 @@ void rendererMIDI()

//audio::sdl2::Renderer r(44100, 1024);

//r.setOutputFile("renderer_midi.wav");
//r.openOutputFile("renderer_midi.wav");

//auto mixer = r.getMixer();
//auto op2f = files::dmx::OP2File("test/fixtures/GENMIDI.OP2");
Raffaello marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -363,7 +308,7 @@ void rendererMIDI()
//while (mid_drv.isPlaying())
// r.renderBuffer(eo);

//r.releaseOutputFile();
//r.closeOutputFile();

//files::WAVFile w("renderer.wav");
//auto sound = w.getSound();
Expand Down Expand Up @@ -483,6 +428,9 @@ int main(int argc, char* argv[])
//midi_adlib_mus_op2_file();
//midi_adlib_xmi();

//rendererMIDI();
return 0;
Raffaello marked this conversation as resolved.
Show resolved Hide resolved

SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO);

int numAudioDevices = SDL_GetNumAudioDevices(0);
Raffaello marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <HyperSonicDrivers/audio/IAudioStream.hpp>
#include <HyperSonicDrivers/hardware/opl/OPL.hpp>
#include <HyperSonicDrivers/devices/IDevice.hpp>
#include <HyperSonicDrivers/drivers/IAudioDriver.hpp>
#include <HyperSonicDrivers/files/WAVFile.hpp>

namespace HyperSonicDrivers::audio
Expand All @@ -25,7 +26,11 @@ namespace HyperSonicDrivers::audio

virtual void renderBuffer(IAudioStream* stream) = 0;
inline void renderBuffer(const std::shared_ptr<devices::IDevice>& device) { renderBuffer(device->getHardware()->getAudioStream().get()); };
virtual void renderFlush(IAudioStream* stream) = 0;
inline void renderFlush(const std::shared_ptr<devices::IDevice>& device) { renderFlush(device->getHardware()->getAudioStream().get()); };

virtual void renderBuffer(IAudioStream* stream, drivers::IAudioDriver& drv, const int track) = 0;
inline void renderBuffer(const std::shared_ptr<devices::IDevice>& device, drivers::IAudioDriver& drv, const int track) { renderBuffer(device->getHardware()->getAudioStream().get(), drv, track); };
protected:
std::shared_ptr<IMixer> m_mixer;
std::unique_ptr<files::WAVFile> m_out;
Raffaello marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <HyperSonicDrivers/audio/sdl2/Renderer.hpp>
#include <HyperSonicDrivers/audio/sdl2/Mixer.hpp>
#include <HyperSonicDrivers/utils/ILogger.hpp>
#include <algorithm>
#include <ranges>


namespace HyperSonicDrivers::audio::sdl2
Expand Down Expand Up @@ -32,4 +34,34 @@ namespace HyperSonicDrivers::audio::sdl2
const size_t read = stream->readBuffer(m_buf.data(), m_buf.size());
m_out->save_streaming(m_buf.data(), read);
}

void Renderer::renderFlush(IAudioStream* stream)
{
// safety check
if (m_buf.empty())
{
m_out->save_prepare(stream->getRate(), stream->isStereo());
m_buf.resize(m_mixer->buffer_size);
}

while (true)
{
const size_t read = stream->readBuffer(m_buf.data(), m_buf.size());
const bool silenced = std::ranges::all_of(m_buf, [](const auto i) { return i == 0; });
if (silenced)
return;

m_out->save_streaming(m_buf.data(), read);
}
}
Raffaello marked this conversation as resolved.
Show resolved Hide resolved

void Renderer::renderBuffer(IAudioStream* stream, drivers::IAudioDriver& drv, const int track)
{
drv.play(track);
while (drv.isPlaying())
renderBuffer(stream);

renderFlush(stream);
closeOutputFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ namespace HyperSonicDrivers::audio::sdl2

void renderBuffer(IAudioStream* stream) override;
using IRenderer::renderBuffer;
void renderFlush(IAudioStream* stream) override;
using IRenderer::renderFlush;

void renderBuffer(IAudioStream* stream, drivers::IAudioDriver& drv, const int track) override;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ namespace HyperSonicDrivers::audio::sdl2
std::filesystem::remove(rfile);

ASSERT_FALSE(std::filesystem::exists(rfile));

{
audio::sdl2::Renderer r(freq, 1024);
r.openOutputFile(rfile);
Raffaello marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -66,6 +65,7 @@ namespace HyperSonicDrivers::audio::sdl2
while (drv1.isPlaying())
r.renderBuffer(opl);

r.renderFlush(opl);
r.closeOutputFile();
}

Expand All @@ -92,6 +92,42 @@ namespace HyperSonicDrivers::audio::sdl2
std::make_tuple<>("sbpro2_dosbox", 44100, eDeviceName::SbPro2, OplEmulator::DOS_BOX)
)
);

TEST_P(RendererTest, render_wav2)
{
const std::string exp_renderer = "../fixtures/test_renderer_" + test_name + ".wav";
const std::string rfile = "../fixtures/test_renderer_" + test_name + "_out2.wav";

if (std::filesystem::exists(rfile))
std::filesystem::remove(rfile);

ASSERT_FALSE(std::filesystem::exists(rfile));
{
audio::sdl2::Renderer r(freq, 1024);
r.openOutputFile(rfile);

auto drv1 = drivers::westwood::ADLDriver(opl, eChannelGroup::Music);
auto af = std::make_shared<files::westwood::ADLFile>("../fixtures/DUNE0.ADL");
drv1.setADLFile(af);

r.renderBuffer(opl, drv1, 4);
}

files::WAVFile w(rfile);
auto sound = w.getSound();
files::WAVFile wexp(exp_renderer);
auto exp_sound = wexp.getSound();

ASSERT_EQ(sound->dataSize, exp_sound->dataSize);
ASSERT_EQ(sound->freq, exp_sound->freq);
ASSERT_EQ(sound->stereo, exp_sound->stereo);
EXPECT_EQ(sound->freq, freq);
EXPECT_EQ(sound->stereo, opl->getHardware()->isStereo());
for (uint32_t i = 0; i < sound->dataSize; i++)
{
EXPECT_EQ(sound->data[i], exp_sound->data[i]);
}
}
}

int main(int argc, char** argv)
Expand Down
Binary file not shown.
Binary file not shown.
Loading