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

Commit

Permalink
Merge pull request #56 from nyanpasu64/n163-level-bugfix
Browse files Browse the repository at this point in the history
Properly reset N163 level when switching documents
  • Loading branch information
nyanpasu64 authored Apr 22, 2018
2 parents 1c45fd8 + 8871b61 commit 1a27757
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
23 changes: 14 additions & 9 deletions Source/FamiTrackerDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ BOOL CFamiTrackerDoc::OnNewDocument()
if (!CDocument::OnNewDocument())
return FALSE;

// CFamiTrackerDoc::OnNewDocument calls CDocument::OnNewDocument() and CreateEmpty(). Both call DeleteContents.
// Opening files doesn't call CDocument::OnNewDocument() but calls CreateEmpty(). Only the latter calls DeleteContents.
CreateEmpty();

return TRUE;
Expand Down Expand Up @@ -400,7 +402,7 @@ void CFamiTrackerDoc::DeleteContents()
m_iExpansionChip = SNDCHIP_NONE;
m_iVibratoStyle = VIBRATO_OLD;
m_bLinearPitch = DEFAULT_LINEAR_PITCH;
N163LevelOffset = 0;
SetN163LevelOffset(0);

m_iChannelsAvailable = CHANNELS_DEFAULT;
m_iSpeedSplitPoint = DEFAULT_SPEED_SPLIT_POINT;
Expand Down Expand Up @@ -452,6 +454,9 @@ void CFamiTrackerDoc::SetModifiedFlag(BOOL bModified)

void CFamiTrackerDoc::CreateEmpty()
{
// CFamiTrackerDoc::OnNewDocument calls CDocument::OnNewDocument() and CreateEmpty(). Both call DeleteContents.
// OpenDocument() doesn't call CDocument::OnNewDocument() but calls CreateEmpty(). Only the latter calls DeleteContents.

m_csDocumentLock.Lock();

// Allocate first song
Expand All @@ -461,7 +466,7 @@ void CFamiTrackerDoc::CreateEmpty()
// Auto-select new style vibrato for new modules
m_iVibratoStyle = VIBRATO_NEW;
m_bLinearPitch = DEFAULT_LINEAR_PITCH;
N163LevelOffset = 0;
SetN163LevelOffset(0);

m_iNamcoChannels = 0; // // //

Expand Down Expand Up @@ -1308,7 +1313,7 @@ BOOL CFamiTrackerDoc::OpenDocument(LPCTSTR lpszPathName)
// Auto-select old style vibrato for old files
m_iVibratoStyle = VIBRATO_OLD;
m_bLinearPitch = false;
N163LevelOffset = 0;
SetN163LevelOffset(0);
}
else {
if (!OpenDocumentNew(OpenFile))
Expand Down Expand Up @@ -1356,7 +1361,7 @@ BOOL CFamiTrackerDoc::OpenDocumentOld(CFile *pOpenFile)

m_iVibratoStyle = VIBRATO_OLD;
m_bLinearPitch = false;
N163LevelOffset = 0;
SetN163LevelOffset(0);

// // // local structs
struct {
Expand Down Expand Up @@ -2613,7 +2618,7 @@ bool CFamiTrackerDoc::WriteBlock_ParamsExtra(CDocumentFile *pDocFile, const int

// FTM import ////

CFamiTrackerDoc *CFamiTrackerDoc::LoadImportFile(LPCTSTR lpszPathName) const
CFamiTrackerDoc *CFamiTrackerDoc::LoadImportFile(LPCTSTR lpszPathName)
{
// Import a module as new subtunes
CFamiTrackerDoc *pImported = new CFamiTrackerDoc();
Expand Down Expand Up @@ -4283,14 +4288,14 @@ void CFamiTrackerDoc::SetLinearPitch(bool Enable)

// N163 Volume Offset
int CFamiTrackerDoc::GetN163LevelOffset() const {
return N163LevelOffset;
return _N163LevelOffset;
}

// DocumentPropertiesChanged calls GetN163LevelOffset and updates synth if modified.
void CFamiTrackerDoc::SetN163LevelOffset(int offset) {
if (N163LevelOffset != offset) {
if (_N163LevelOffset != offset) {
ModifyIrreversible();
N163LevelOffset = offset;
theApp.LoadSoundConfig();
_N163LevelOffset = offset;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/FamiTrackerDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class CFamiTrackerDoc : public CDocument, public CFTMComponentInterface
bool HasLastLoadFailed() const;

// Import
CFamiTrackerDoc* LoadImportFile(LPCTSTR lpszPathName) const;
static CFamiTrackerDoc* LoadImportFile(LPCTSTR lpszPathName);
bool ImportInstruments(CFamiTrackerDoc *pImported, int *pInstTable);
bool ImportGrooves(CFamiTrackerDoc *pImported, int *pGrooveMap); // // //
bool ImportDetune(CFamiTrackerDoc *pImported); // // //
Expand Down Expand Up @@ -512,7 +512,7 @@ class CFamiTrackerDoc : public CDocument, public CFTMComponentInterface
unsigned int m_iNamcoChannels;
vibrato_t m_iVibratoStyle; // 0 = old style, 1 = new style
bool m_bLinearPitch;
int N163LevelOffset;
int _N163LevelOffset;

machine_t m_iMachine; // // // NTSC / PAL
unsigned int m_iEngineSpeed; // Refresh rate
Expand Down
2 changes: 1 addition & 1 deletion Source/ModuleImportDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void CModuleImportDlg::OnBnClickedCancel()

bool CModuleImportDlg::LoadFile(CString Path, CFamiTrackerDoc *pDoc)
{
m_pImportedDoc = pDoc->LoadImportFile(Path);
m_pImportedDoc = CFamiTrackerDoc::LoadImportFile(Path);

// Check if load failed
if (m_pImportedDoc == NULL)
Expand Down
25 changes: 23 additions & 2 deletions Source/SoundGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ CSoundGen::CSoundGen() :
m_pSequencePlayPos(NULL),
m_iSequencePlayPos(0),
m_iSequenceTimeout(0),
m_iBPMCachePosition(0) // // //
m_iBPMCachePosition(0), // // //
currN163LevelOffset(0)
{
TRACE("SoundGen: Object created\n");

Expand Down Expand Up @@ -362,8 +363,20 @@ CChannelHandler *CSoundGen::GetChannel(int Index) const
return m_pChannels[Index];
}

/*
INVARIANT: called whenever any document is created or changes.
CREATION:
CreateEmpty() calls DocumentPropertiesChanged.
OpenDocument() calls DocumentPropertiesChanged.
PRECONDITION: pDocument is not null.
PROPERTY: if pDocument is main document, linear pitch is synced.
RESULT: linear pitch is correct.
*/
void CSoundGen::DocumentPropertiesChanged(CFamiTrackerDoc *pDocument)
{
if (pDocument != m_pDocument)
return;
ASSERT(pDocument != NULL);

SetupVibratoTable(pDocument->GetVibratoStyle()); // // //
Expand Down Expand Up @@ -529,6 +542,12 @@ void CSoundGen::DocumentPropertiesChanged(CFamiTrackerDoc *pDocument)
}

m_iSpeedSplitPoint = pDocument->GetSpeedSplitPoint();

if (currN163LevelOffset != pDocument->GetN163LevelOffset()) {
// Player thread calls OnLoadSettings() which calls ResetAudioDevice()
// Why are GetCurrentThreadId and GetCurrentThread used interchangably?
LoadSettings();
}
}

//
Expand Down Expand Up @@ -738,14 +757,16 @@ bool CSoundGen::ResetAudioDevice()
if (!m_pAPU->SetupSound(SampleRate, 1, (m_iMachineType == NTSC) ? MACHINE_NTSC : MACHINE_PAL))
return false;

currN163LevelOffset = m_pDocument->GetN163LevelOffset();

m_pAPU->SetChipLevel(CHIP_LEVEL_APU1, float(pSettings->ChipLevels.iLevelAPU1 / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_APU2, float(pSettings->ChipLevels.iLevelAPU2 / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_VRC6, float(pSettings->ChipLevels.iLevelVRC6 / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_VRC7, float(pSettings->ChipLevels.iLevelVRC7 / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_MMC5, float(pSettings->ChipLevels.iLevelMMC5 / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_FDS, float(pSettings->ChipLevels.iLevelFDS / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_N163, float(
(pSettings->ChipLevels.iLevelN163 + m_pDocument->GetN163LevelOffset())/ 10.0f));
(pSettings->ChipLevels.iLevelN163 + currN163LevelOffset) / 10.0f));
m_pAPU->SetChipLevel(CHIP_LEVEL_S5B, float(pSettings->ChipLevels.iLevelS5B / 10.0f));
/*
m_pAPU->SetChipLevel(SNDCHIP_NONE, 0);//pSettings->ChipLevels.iLevel2A03);
Expand Down
1 change: 1 addition & 0 deletions Source/SoundGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class CSoundGen : public CWinThread, IAudioCallback
CDSoundChannel *m_pDSoundChannel;
CVisualizerWnd *m_pVisualizerWnd;
CAPU *m_pAPU;
int currN163LevelOffset;

const CDSample *m_pPreviewSample;

Expand Down

0 comments on commit 1a27757

Please sign in to comment.