Skip to content

Commit

Permalink
Adding guards around dumping functions that can be altered from UI th…
Browse files Browse the repository at this point in the history
…read
  • Loading branch information
laoo committed Jan 2, 2024
1 parent 9a2c4a7 commit 31c35f7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
20 changes: 14 additions & 6 deletions WinFelix/WinAudioOut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "ConfigProvider.hpp"
#include "SysConfig.hpp"

WinAudioOut::WinAudioOut() : mWav{}, mNormalizer{ 1.0f / 32768.0f }
WinAudioOut::WinAudioOut() : mWav{}, mNormalizer{ 1.0f / 32768.0f }, mMutex{}
{
CoInitializeEx( NULL, COINIT_MULTITHREADED );

Expand Down Expand Up @@ -104,11 +104,13 @@ WinAudioOut::~WinAudioOut()

void WinAudioOut::setEncoder( std::shared_ptr<IEncoder> pEncoder )
{
std::unique_lock lock{ mMutex };
mEncoder = std::move( pEncoder );
}

void WinAudioOut::setWavOut( std::filesystem::path path )
{
std::unique_lock lock{ mMutex };
if ( path.empty() )
{
if ( mWav )
Expand Down Expand Up @@ -139,6 +141,7 @@ void WinAudioOut::setWavOut( std::filesystem::path path )

bool WinAudioOut::isWavOut() const
{
std::unique_lock lock{ mMutex };
return mWav ? true : false;
}

Expand Down Expand Up @@ -226,11 +229,16 @@ CpuBreakType WinAudioOut::fillBuffer( std::shared_ptr<Core> instance, int64_t re
pfData[i * mMixFormat->nChannels + 1] = mSamplesBuffer[i].right * mNormalizer;
}

if ( mEncoder )
mEncoder->pushAudioBuffer( std::span<float const>( pfData, framesAvailable * mMixFormat->nChannels ) );

if ( mWav )
wav_write( mWav, pfData, framesAvailable );
{
std::unique_lock lock{ mMutex };
if ( mEncoder )
mEncoder->pushAudioBuffer( std::span<float const>( pfData, framesAvailable * mMixFormat->nChannels ) );
}
{
std::unique_lock lock{ mMutex };
if ( mWav )
wav_write( mWav, pfData, framesAvailable );
}

hr = mRenderClient->ReleaseBuffer( framesAvailable, 0 );

Expand Down
1 change: 1 addition & 0 deletions WinFelix/WinAudioOut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class WinAudioOut
std::shared_ptr<IEncoder> mEncoder;
WavFile* mWav;
HANDLE mEvent;
mutable std::mutex mMutex;

double mTimeToSamples;

Expand Down
32 changes: 23 additions & 9 deletions libFelix/Mikey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

Mikey::Mikey( Core & core, ComLynx & comLynx, std::shared_ptr<IVideoSink> videoSink ) : mCore{ core }, mComLynx{ comLynx }, mAccessTick{}, mTimers{}, mAudioChannels{}, mPalette{},
mAttenuation{ 0xff, 0xff, 0xff, 0xff }, mAttenuationLeft{ 0x3c, 0x3c, 0x3c, 0x3c }, mAttenuationRight{ 0x3c, 0x3c, 0x3c, 0x3c }, mDisplayGenerator{ std::make_unique<DisplayGenerator>( std::move( videoSink ) ) },
mParallelPort{ mCore, mComLynx, *mDisplayGenerator }, mDisplayRegs{}, mSuzyDone{}, mPan{ 0xff }, mStereo{}, mSerDat{}, mIRQ{}
mParallelPort{ mCore, mComLynx, *mDisplayGenerator }, mDisplayRegs{}, mSuzyDone{}, mPan{ 0xff }, mStereo{}, mSerDat{}, mIRQ{}, mVGMWriterMutex{}
{
mTimers[0x0] = std::make_unique<TimerCore>( 0x0, [this]( uint64_t tick, bool interrupt )
{
Expand Down Expand Up @@ -280,8 +280,11 @@ SequencedAction Mikey::write( uint16_t address, uint8_t value )
}
else if ( address < 0x40 )
{
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, (uint8_t)address, value );
{
std::unique_lock lock( mVGMWriterMutex );
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, ( uint8_t )address, value );
}
int idx = ( address >> 3 ) & 3;

switch ( address & 0x7 )
Expand Down Expand Up @@ -313,18 +316,27 @@ SequencedAction Mikey::write( uint16_t address, uint8_t value )
mAttenuation[address & 3] = value;
mAttenuationRight[address & 3] = ( value & 0x0f ) << 2;
mAttenuationLeft[address & 3] = ( value & 0xf0 ) >> 2;
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, (uint8_t)address, value );
{
std::unique_lock lock( mVGMWriterMutex );
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, ( uint8_t )address, value );
}
break;
case MPAN:
mPan = value;
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, (uint8_t)address, value );
{
std::unique_lock lock( mVGMWriterMutex );
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, ( uint8_t )address, value );
}
break;
case MSTEREO:
mStereo = value;
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, (uint8_t)address, value );
{
std::unique_lock lock( mVGMWriterMutex );
if ( mVGMWriter )
mVGMWriter->write( mAccessTick, ( uint8_t )address, value );
}
break;
case INTRST:
resetIRQ( value );
Expand Down Expand Up @@ -481,11 +493,13 @@ AudioSample Mikey::sampleAudio( uint64_t tick ) const

void Mikey::setVGMWriter( std::shared_ptr<VGMWriter> writer )
{
std::unique_lock lock( mVGMWriterMutex );
mVGMWriter = std::move( writer );
}

bool Mikey::isVGMWriter() const
{
std::unique_lock lock( mVGMWriterMutex );
return (bool)mVGMWriter;
}

Expand Down
1 change: 1 addition & 0 deletions libFelix/Mikey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Mikey

std::unique_ptr<DisplayGenerator> mDisplayGenerator;
std::shared_ptr<VGMWriter> mVGMWriter;
mutable std::mutex mVGMWriterMutex;

ParallelPort mParallelPort;

Expand Down

0 comments on commit 31c35f7

Please sign in to comment.