gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

audioOutputWAV.cpp (1307B)


      1 #include "audioOutputWAV.h"
      2 
      3 #include <array>
      4 #include <chrono>
      5 
      6 namespace mqConsoleLib
      7 {
      8 constexpr uint32_t g_blockSize = 64;
      9 
     10 const std::string g_filename = "mq_output_" + std::to_string(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) + ".wav";
     11 
     12 AudioOutputWAV::AudioOutputWAV(const ProcessCallback& _callback): AudioOutput(_callback), wavWriter(g_filename, 44100)
     13 {
     14 	m_stereoOutput.resize(g_blockSize<<1);
     15 
     16 	m_thread.reset(new std::thread([&]()
     17 	{
     18 		while(!m_terminate)
     19 			threadFunc();
     20 	}));
     21 }
     22 
     23 AudioOutputWAV::~AudioOutputWAV()
     24 {
     25 	m_terminate = true;
     26 	m_thread->join();
     27 	m_thread.reset();
     28 }
     29 
     30 void AudioOutputWAV::threadFunc()
     31 {
     32 	const mqLib::TAudioOutputs* outputs = nullptr;
     33 	m_processCallback(g_blockSize, outputs);
     34 
     35 	if(!outputs)
     36 	{
     37 		std::this_thread::yield();
     38 		return;
     39 	}
     40 
     41 	const auto& outs = *outputs;
     42 
     43 	for(size_t i=0; i<g_blockSize; ++i)
     44 	{
     45 		m_stereoOutput[i<<1] = outs[0][i];
     46 		m_stereoOutput[(i<<1) + 1] = outs[1][i];
     47 
     48 		if(silence && (outs[0][i] || outs[1][i]))
     49 			silence = false;
     50 	}
     51 
     52 	if(!silence)
     53 	{
     54 		// continously write to disk if not silent anymore
     55 		wavWriter.append([&](auto& _dst)
     56 		{
     57 			_dst.reserve(_dst.size() + m_stereoOutput.size());
     58 			for (auto& d : m_stereoOutput)
     59 				_dst.push_back(d);
     60 		}
     61 		);
     62 	}
     63 }
     64 }