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 1 commit
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
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 @@ -25,7 +25,8 @@ 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()); };
protected:
std::shared_ptr<IMixer> m_mixer;
std::unique_ptr<files::WAVFile> m_out;
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 All @@ -23,6 +25,7 @@ namespace HyperSonicDrivers::audio::sdl2

void Renderer::renderBuffer(IAudioStream* stream)
{
// TODO: the loop can be done passing the IAudioDriver and track
if (m_buf.empty())
{
m_out->save_prepare(stream->getRate(), stream->isStereo());
Raffaello marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -32,4 +35,25 @@ 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)
{
// TODO: this can be incorporated in renderBuffer as soon is doing the internal loop checking stream is ended.

// 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());
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ namespace HyperSonicDrivers::audio::sdl2

void renderBuffer(IAudioStream* stream) override;
using IRenderer::renderBuffer;
void renderFlush(IAudioStream* stream) override;
using IRenderer::renderFlush;
};
}
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 Down
Binary file not shown.
Binary file not shown.
Loading