Skip to content
Permalink
Browse files

fix audio frequency by set only given frequency value instead of from…

… host function
  • Loading branch information
RadWolfie committed Jan 10, 2020
1 parent 195795b commit a375faba5184605c8ccdd61b23fbb0331e604703
@@ -108,6 +108,7 @@ struct X_CDirectSoundBuffer
DWORD Xb_dwHeadroom;
X_DSENVOLOPEDESC Xb_EnvolopeDesc;
X_DSVOICEPROPS Xb_VoiceProperties;
DWORD Xb_Frequency;
};

//Custom flags (4 bytes support up to 31 shifts,starting from 0)
@@ -260,6 +261,7 @@ class X_CDirectSoundStream
DWORD Xb_dwHeadroom;
X_DSENVOLOPEDESC Xb_EnvolopeDesc;
X_DSVOICEPROPS Xb_VoiceProperties;
DWORD Xb_Frequency;
};

// ******************************************************************
@@ -933,7 +933,7 @@ HRESULT WINAPI XTL::EMUPATCH(IDirectSoundBuffer_SetFormat)
pThis->EmuBufferDesc, pThis->EmuFlags,
pThis->EmuPlayFlags, pThis->EmuDirectSound3DBuffer8,
0, pThis->X_BufferCache, pThis->X_BufferCacheSize,
pThis->Xb_VoiceProperties, xbnullptr);
pThis->Xb_VoiceProperties, xbnullptr, pThis->Xb_Frequency);

return hRet;
}
@@ -953,7 +953,7 @@ HRESULT WINAPI XTL::EMUPATCH(IDirectSoundBuffer_SetFrequency)
LOG_FUNC_ARG(dwFrequency)
LOG_FUNC_END;

HRESULT hRet = HybridDirectSoundBuffer_SetFrequency(pThis->EmuDirectSoundBuffer8, dwFrequency);
HRESULT hRet = HybridDirectSoundBuffer_SetFrequency(pThis->EmuDirectSoundBuffer8, dwFrequency, pThis->Xb_Frequency);

return hRet;
}
@@ -1286,7 +1286,7 @@ HRESULT WINAPI XTL::EMUPATCH(IDirectSoundBuffer_SetPitch)
LOG_FUNC_ARG(lPitch)
LOG_FUNC_END;

HRESULT hRet = HybridDirectSoundBuffer_SetPitch(pThis->EmuDirectSoundBuffer8, lPitch);
HRESULT hRet = HybridDirectSoundBuffer_SetPitch(pThis->EmuDirectSoundBuffer8, lPitch, pThis->Xb_Frequency);

return hRet;
}
@@ -584,7 +584,8 @@ static inline void DSound3DBufferCreate(LPDIRECTSOUNDBUFFER8 pDSBuffer, LPDIRECT
pThis->Xb_VolumeMixbin = 0L; \
pThis->Xb_dwHeadroom = 600; /* default for 2D voice */ \
pThis->Xb_EnvolopeDesc = { 0 }; \
InitVoiceProperties(pThis->Xb_VoiceProperties); /* The rest will initialize in GeneratePCMFormat to GenerateMixBinDefault. */
InitVoiceProperties(pThis->Xb_VoiceProperties); /* The rest will initialize in GeneratePCMFormat to GenerateMixBinDefault. */ \
pThis->Xb_Frequency = XTL_DSXFREQUENCY_ORIGINAL;
//pThis->EmuBufferDesc = { 0 }; // Enable this when become necessary.
/*
pThis->EmuLockPtr1 = xbnullptr; \
@@ -605,20 +606,19 @@ static inline void DSoundBufferTransferSettings(
LPDIRECTSOUNDBUFFER8 &pDSBufferOld,
LPDIRECTSOUNDBUFFER8 &pDSBufferNew,
LPDIRECTSOUND3DBUFFER8 &pDS3DBufferOld,
LPDIRECTSOUND3DBUFFER8 &pDS3DBufferNew)
LPDIRECTSOUND3DBUFFER8 &pDS3DBufferNew,
DWORD Xb_Frequency)
{
DWORD dwFrequency;
LONG lVolume, lPan;
DS3DBUFFER ds3dBuffer;

if (pDSBufferOld == nullptr) {
return;
}

pDSBufferOld->GetVolume(&lVolume);
pDSBufferOld->GetFrequency(&dwFrequency);
pDSBufferNew->SetFrequency(Xb_Frequency);

pDSBufferNew->SetFrequency(dwFrequency);
pDSBufferOld->GetVolume(&lVolume);
pDSBufferNew->SetVolume(lVolume);

if (pDS3DBufferOld != nullptr && pDS3DBufferNew != nullptr) {
@@ -636,7 +636,8 @@ static inline void DSoundBufferReCreate(
DSBUFFERDESC &DSBufferDesc,
LPDIRECTSOUND3DBUFFER8 &pDS3DBuffer,
LPDIRECTSOUNDBUFFER8 &pDSBufferNew,
LPDIRECTSOUND3DBUFFER8 &pDS3DBufferNew) {
LPDIRECTSOUND3DBUFFER8 &pDS3DBufferNew,
DWORD Xb_Frequency) {


DSoundBufferCreate(&DSBufferDesc, pDSBufferNew);
@@ -645,7 +646,7 @@ static inline void DSoundBufferReCreate(
DSound3DBufferCreate(pDSBufferNew, pDS3DBufferNew);
}

DSoundBufferTransferSettings(pDSBuffer, pDSBufferNew, pDS3DBuffer, pDS3DBufferNew);
DSoundBufferTransferSettings(pDSBuffer, pDSBufferNew, pDS3DBuffer, pDS3DBufferNew, Xb_Frequency);
}

static inline void DSoundBufferRelease(
@@ -695,7 +696,7 @@ static inline void DSoundBufferResizeSetSize(
LPDIRECTSOUND3DBUFFER8 pDS3DBufferNew = nullptr;

DSoundBufferReCreate(pThis->EmuDirectSoundBuffer8, pThis->EmuBufferDesc, pThis->EmuDirectSound3DBuffer8,
pDSBufferNew, pDS3DBufferNew);
pDSBufferNew, pDS3DBufferNew, pThis->Xb_Frequency);

// release old buffer
DSoundBufferRelease(pThis->EmuDirectSoundBuffer8, pThis->EmuDirectSound3DBuffer8, refCount);
@@ -781,14 +782,15 @@ static inline void DSoundBufferReplace(
LPDIRECTSOUNDBUFFER8 &pDSBuffer,
DSBUFFERDESC &DSBufferDesc,
DWORD PlayFlags,
LPDIRECTSOUND3DBUFFER8 &pDS3DBuffer)
LPDIRECTSOUND3DBUFFER8 &pDS3DBuffer,
DWORD Xb_Frequency)
{
DWORD refCount, dwPlayCursor, dwStatus;
LPDIRECTSOUNDBUFFER8 pDSBufferNew = nullptr;
LPDIRECTSOUND3DBUFFER8 pDS3DBufferNew = nullptr;

DSoundBufferReCreate(pDSBuffer, DSBufferDesc, pDS3DBuffer,
pDSBufferNew, pDS3DBufferNew);
pDSBufferNew, pDS3DBufferNew, Xb_Frequency);

HRESULT hRet = pDSBuffer->GetStatus(&dwStatus);

@@ -1425,7 +1427,8 @@ static inline HRESULT HybridDirectSoundBuffer_SetFormat(
LPVOID &X_BufferCache,
DWORD &X_BufferCacheSize,
XTL::X_DSVOICEPROPS &Xb_VoiceProperties,
XTL::X_LPDSMIXBINS mixbins_output)
XTL::X_LPDSMIXBINS mixbins_output,
DWORD Xb_Frequency)
{
pDSBuffer->Stop();

@@ -1444,7 +1447,7 @@ static inline HRESULT HybridDirectSoundBuffer_SetFormat(
// Allocate at least 5 second worth of bytes in PCM format.
BufferDesc.dwBufferBytes = BufferDesc.lpwfxFormat->nAvgBytesPerSec * 5;
}
DSoundBufferReplace(pDSBuffer, BufferDesc, dwPlayFlags, pDS3DBuffer);
DSoundBufferReplace(pDSBuffer, BufferDesc, dwPlayFlags, pDS3DBuffer, Xb_Frequency);
}

RETURN_RESULT_CHECK(hRet);
@@ -1454,10 +1457,12 @@ static inline HRESULT HybridDirectSoundBuffer_SetFormat(
//IDirectSoundBuffer
static inline HRESULT HybridDirectSoundBuffer_SetFrequency(
LPDIRECTSOUNDBUFFER8 pDSBuffer,
DWORD dwFrequency)
DWORD dwFrequency,
DWORD &Xb_Frequency)
{
HRESULT hRet = S_OK;

Xb_Frequency = dwFrequency;
hRet = pDSBuffer->SetFrequency(dwFrequency);

RETURN_RESULT_CHECK(hRet);
@@ -1666,15 +1671,13 @@ static inline HRESULT HybridDirectSoundBuffer_SetOutputBuffer(
//IDirectSoundBuffer
static inline HRESULT HybridDirectSoundBuffer_SetPitch(
LPDIRECTSOUNDBUFFER8 pDSBuffer,
LONG lPitch)
LONG lPitch,
DWORD &Xb_Frequency)
{

// Convert pitch back to frequency
if (lPitch == 0) {
lPitch = 48000; // NOTE: pitch = 0 is equal to 48 KHz.
} else {
lPitch = static_cast<LONG>(exp((lPitch / 4096.0f) * log(2)) * 48000.0f);
}
// NOTE: pitch = 0 is equal to 48 KHz.
Xb_Frequency = static_cast<DWORD>(exp((lPitch / 4096.0f) * log(2)) * 48000.0f);

/* For research purpose of how to convert frequency to pitch and back to frequency.
// Edit hertz variable to see the result.
@@ -1689,7 +1692,7 @@ static inline HRESULT HybridDirectSoundBuffer_SetPitch(
// Convert pitch to hertz
hertz = exp((pitch / pitchRatio) * log(2)) * hertzRatio;*/

RETURN_RESULT_CHECK(pDSBuffer->SetFrequency(lPitch));
RETURN_RESULT_CHECK(pDSBuffer->SetFrequency(Xb_Frequency));
}
/*
//Only has one function, this is not a requirement.
@@ -904,7 +904,7 @@ HRESULT WINAPI XTL::EMUPATCH(CDirectSoundStream_SetFormat)
HRESULT hRet = HybridDirectSoundBuffer_SetFormat(pThis->EmuDirectSoundBuffer8, pwfxFormat, pThis->EmuBufferDesc,
pThis->EmuFlags, pThis->EmuPlayFlags, pThis->EmuDirectSound3DBuffer8,
0, pThis->X_BufferCache, pThis->X_BufferCacheSize,
pThis->Xb_VoiceProperties, xbnullptr);
pThis->Xb_VoiceProperties, xbnullptr, pThis->Xb_Frequency);

return hRet;
}
@@ -924,7 +924,7 @@ HRESULT WINAPI XTL::EMUPATCH(CDirectSoundStream_SetFrequency)
LOG_FUNC_ARG(dwFrequency)
LOG_FUNC_END;

HRESULT hRet = HybridDirectSoundBuffer_SetFrequency(pThis->EmuDirectSoundBuffer8, dwFrequency);
HRESULT hRet = HybridDirectSoundBuffer_SetFrequency(pThis->EmuDirectSoundBuffer8, dwFrequency, pThis->Xb_Frequency);

return hRet;
}
@@ -1231,7 +1231,7 @@ HRESULT WINAPI XTL::EMUPATCH(CDirectSoundStream_SetPitch)
LOG_FUNC_ARG(lPitch)
LOG_FUNC_END;

HRESULT hRet = HybridDirectSoundBuffer_SetPitch(pThis->EmuDirectSoundBuffer8, lPitch);
HRESULT hRet = HybridDirectSoundBuffer_SetPitch(pThis->EmuDirectSoundBuffer8, lPitch, pThis->Xb_Frequency);

return hRet;
}
@@ -106,6 +106,11 @@ typedef struct _XDSMIXBINS {
#define X_DSBSTOPEX_RELEASEWAVEFORM 0x00000002
#define X_DSBSTOPEX_ALL (X_DSBSTOPEX_ENVELOPE | X_DSBSTOPEX_RELEASEWAVEFORM)

// Generic frequency range
#define XTL_DSXFREQUENCY_ORIGINAL 0x00000000
//#define XTL_DSGFREQUENCY_MIN 0x00000???
//#define XTL_DSGFREQUENCY_MAX 0x000?????

#define XTL_DSBCAPS_CTRL3D 0x00000010
#define XTL_DSBCAPS_CTRLFREQUENCY 0x00000020
#define XTL_DSBCAPS_CTRLVOLUME 0x00000080

0 comments on commit a375fab

Please sign in to comment.
You can’t perform that action at this time.