Skip to content
This repository has been archived by the owner on Oct 16, 2021. It is now read-only.

Commit

Permalink
Refactor sound (part 2) (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyanpasu64 authored Oct 9, 2019
2 parents d1d961c + c67c1cd commit 5d5bf47
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 40 deletions.
25 changes: 0 additions & 25 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Source/APU/APU.CPP
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ bool CAPU::SetupSound(int SampleRate, int NrChannels, int Machine) // // //
return true;
}

void CAPU::AddTime(int32_t Cycles)
void CAPU::AddCycles(int32_t Cycles)
{
if (Cycles < 0)
return;
Expand Down
2 changes: 1 addition & 1 deletion Source/APU/APU.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CAPU {

void Reset();
void Process();
void AddTime(int32_t Cycles);
void AddCycles(int32_t Cycles);

void SetExternalSound(uint8_t Chip);
void Write(uint16_t Address, uint8_t Value); // // //
Expand Down
3 changes: 3 additions & 0 deletions Source/APU/Mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ void CMixer::AddValue(int ChanID, int Chip, int Value, int AbsValue, int FrameCy
StoreChannelLevel(ChanID, AbsValue);
m_iChannels[ChanID] = Value;

// Unless otherwise notes, Value is already a delta.
switch (Chip) {
case SNDCHIP_NONE:
// Value == AbsValue.
switch (ChanID) {
case CHANID_SQUARE1:
case CHANID_SQUARE2:
Expand All @@ -411,6 +413,7 @@ void CMixer::AddValue(int ChanID, int Chip, int Value, int AbsValue, int FrameCy
MixFDS(Value, FrameCycles);
break;
case SNDCHIP_MMC5:
// Value == AbsValue.
MixMMC5(Delta, FrameCycles);
break;
case SNDCHIP_VRC6:
Expand Down
2 changes: 1 addition & 1 deletion Source/ChannelHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ int CChannelHandler::LimitVolume(int Volume) const // // //

void CChannelHandler::AddCycles(int count)
{
m_pSoundGen->AddCycles(count);
m_pSoundGen->AddCyclesUnlessEndOfFrame(count);
}

void CChannelHandler::WriteRegister(uint16_t Reg, uint8_t Value)
Expand Down
4 changes: 2 additions & 2 deletions Source/DirectSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ buffer_event_t CDSoundChannel::WaitForSyncEvent(DWORD dwTimeout) const
case WAIT_OBJECT_0: // External event
return BUFFER_CUSTOM_EVENT;
case WAIT_OBJECT_0 + 1: // DirectSound buffer
return (GetWriteBlock() == m_iCurrentWriteBlock) ? BUFFER_OUT_OF_SYNC : BUFFER_IN_SYNC;
return (GetWritableBlock() == m_iCurrentWriteBlock) ? BUFFER_OUT_OF_SYNC : BUFFER_IN_SYNC;
case WAIT_TIMEOUT: // Timeout
return BUFFER_TIMEOUT;
}
Expand All @@ -388,7 +388,7 @@ int CDSoundChannel::GetPlayBlock() const
return (PlayPos / m_iBlockSize);
}

int CDSoundChannel::GetWriteBlock() const
int CDSoundChannel::GetWritableBlock() const
{
// Return the block where the write pos is
DWORD PlayPos, WritePos;
Expand Down
11 changes: 10 additions & 1 deletion Source/DirectSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ class CDSoundChannel

private:
int GetPlayBlock() const;
int GetWriteBlock() const;

/*!
https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ee418062(v%3Dvs.85)
The write cursor is the point in the buffer ahead of which it is safe to write data to the buffer.
Data should not be written to the part of the buffer after the play cursor and before the write cursor.
Data should not be written to the blocks in (?, write cursor's block].
Otherwise, you risk writing before the write cursor, which will not be played back properly.
*/
int GetWritableBlock() const;

void AdvanceWritePointer();

Expand Down
21 changes: 13 additions & 8 deletions Source/SoundGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "MIDI.h"
#include "ChannelFactory.h" // // // test
#include "DetuneTable.h" // // //
#include <iostream>

// 1kHz test tone
//#define AUDIO_TEST
Expand Down Expand Up @@ -1394,15 +1395,15 @@ void CSoundGen::ResetAPU()
m_pAPU->ClearSample(); // // //
}

void CSoundGen::AddCycles(int Count)
void CSoundGen::AddCyclesUnlessEndOfFrame(int Count)
{
// Called from player thread
ASSERT(GetCurrentThreadId() == m_nThreadID);

// Add APU cycles
Count = std::min(Count, m_iUpdateCycles - m_iConsumedCycles);
m_iConsumedCycles += Count;
m_pAPU->AddTime(Count);
m_pAPU->AddCycles(Count);
}

uint8_t CSoundGen::GetReg(int Chip, int Reg) const
Expand Down Expand Up @@ -2119,18 +2120,20 @@ void CSoundGen::UpdateAPU()
CSingleLock l(&m_csAPULock); // // //
if (l.Lock()) {
// Update APU channel registers
unsigned int LastChip = SNDCHIP_NONE; // // // 050B
unsigned int PrevChip = SNDCHIP_NONE; // // // 050B
for (int i = 0; i < CHANNELS; ++i) {
if (m_pChannels[i] != NULL) {
m_pChannels[i]->RefreshChannel();
m_pChannels[i]->FinishTick(); // // //
unsigned int Chip = m_pTrackerChannels[i]->GetChip();
if (m_pDocument->ExpansionEnabled(Chip)) {
int Delay = (Chip == LastChip) ? 150 : 250;
AddCycles(Delay);
LastChip = Chip;
int Delay = (Chip == PrevChip) ? 150 : 250;

AddCyclesUnlessEndOfFrame(Delay);
m_pAPU->Process();

PrevChip = Chip;
}
m_pAPU->Process();
}
}
#ifdef WRITE_VGM // // //
Expand All @@ -2142,8 +2145,10 @@ void CSoundGen::UpdateAPU()
if (m_iConsumedCycles > m_iUpdateCycles) {
throw std::runtime_error("overflowed vblank!");
}
m_pAPU->AddTime(m_iUpdateCycles - m_iConsumedCycles);

m_pAPU->AddCycles(m_iUpdateCycles - m_iConsumedCycles);
m_pAPU->Process();

l.Unlock();
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SoundGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class CSoundGen : public CWinThread, IAudioCallback
// Used by channels

/** Add cycles to model execution time (capped at vblank). */
void AddCycles(int Count);
void AddCyclesUnlessEndOfFrame(int Count);

// Other
uint8_t GetReg(int Chip, int Reg) const;
Expand Down

0 comments on commit 5d5bf47

Please sign in to comment.