From 9871b25fa87c9ca656b862b8e5033e0d1e9bf295 Mon Sep 17 00:00:00 2001 From: RadWolfie Date: Thu, 14 May 2020 19:09:19 -0500 Subject: [PATCH] move audio converter helper --- CMakeLists.txt | 1 + src/common/audio/converter.hpp | 51 +++++++++++++++++++ .../DSOUND/DirectSound/DirectSoundInline.hpp | 6 +-- src/core/hle/DSOUND/XbDSoundTypes.h | 24 --------- .../hle/DSOUND/common/XbInternalDSVoice.cpp | 6 +-- .../hle/DSOUND/common/XbInternalStruct.hpp | 1 + 6 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 src/common/audio/converter.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4297f44ad..dbb211c5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,6 +130,7 @@ file (GLOB CXBXR_HEADER_GUIv1 # Emulator (module) file (GLOB CXBXR_HEADER_EMU "${CXBXR_ROOT_DIR}/src/common/AddressRanges.h" + "${CXBXR_ROOT_DIR}/src/common/audio/converter.hpp" "${CXBXR_ROOT_DIR}/src/common/util/gloffscreen/glextensions.h" "${CXBXR_ROOT_DIR}/src/common/util/gloffscreen/gloffscreen.h" "${CXBXR_ROOT_DIR}/src/common/XADPCM.h" diff --git a/src/common/audio/converter.hpp b/src/common/audio/converter.hpp new file mode 100644 index 000000000..9c45e12bb --- /dev/null +++ b/src/common/audio/converter.hpp @@ -0,0 +1,51 @@ +// ****************************************************************** +// * +// * This file is part of the Cxbx project. +// * +// * Cxbx and Cxbe are free software; you can redistribute them +// * and/or modify them under the terms of the GNU General Public +// * License as published by the Free Software Foundation; either +// * version 2 of the license, or (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have recieved a copy of the GNU General Public License +// * along with this program; see the file COPYING. +// * If not, write to the Free Software Foundation, Inc., +// * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// * +// * (c) 2020 RadWolfie +// * +// * All rights reserved +// * +// ****************************************************************** +#pragma once + +#include + +// Convert frequency to pitch helper +static inline int32_t converter_freq2pitch(uint32_t freq) { + // NOTE: pitch = 0 is equal to 48 KHz. + /* For research purpose of how to convert frequency to pitch and back to frequency. + // Edit hertz variable to see the result. + float hertz = 12000.0f; + + float hertzRatio = 48000.0f; + float pitchRatio = 4096.0f; + + // Convert hertz to pitch + float pitch = log2(hertz / hertzRatio) * pitchRatio; + + // Convert pitch to hertz + hertz = exp((pitch / pitchRatio) * log(2)) * hertzRatio;*/ + return static_cast(log2(freq / 48000.0f) * 4096.0f); +} + +// Convert pitch to frequency helper +static inline uint32_t converter_pitch2freq(int32_t pitch) { + //* See research documentation above for conversion example. + return static_cast(exp((pitch / 4096.0f) * log(2)) * 48000.0f); +} diff --git a/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp b/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp index 714a28219..8376df2f6 100644 --- a/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp +++ b/src/core/hle/DSOUND/DirectSound/DirectSoundInline.hpp @@ -427,7 +427,7 @@ static inline void DSoundBufferTransferSettings( } // if sync current frequency used (then use pitch only). - uint32_t freq = XTL::converter_pitch2freq(Xb_Voice->GetPitch()); + uint32_t freq = converter_pitch2freq(Xb_Voice->GetPitch()); pDSBufferNew->SetFrequency(freq); pDSBufferOld->GetVolume(&lVolume); @@ -1158,7 +1158,7 @@ static inline HRESULT HybridDirectSoundBuffer_SetFrequency( { HRESULT hRet = S_OK; - int32_t pitch = XTL::converter_freq2pitch((dwFrequency!=0 ? dwFrequency : Xb_Voice->GetFrequencyDefault())); + int32_t pitch = converter_freq2pitch((dwFrequency!=0 ? dwFrequency : Xb_Voice->GetFrequencyDefault())); hRet = HybridDirectSoundBuffer_SetPitch(pDSBuffer, pitch, Xb_Voice); @@ -1374,7 +1374,7 @@ static inline HRESULT HybridDirectSoundBuffer_SetPitch( Xb_Voice->SetPitch(lPitch); // Convert pitch back to frequency - uint32_t setFrequency = XTL::converter_pitch2freq(lPitch); + uint32_t setFrequency = converter_pitch2freq(lPitch); RETURN_RESULT_CHECK(pDSBuffer->SetFrequency(setFrequency)); } diff --git a/src/core/hle/DSOUND/XbDSoundTypes.h b/src/core/hle/DSOUND/XbDSoundTypes.h index b86fdef36..62ea3a782 100644 --- a/src/core/hle/DSOUND/XbDSoundTypes.h +++ b/src/core/hle/DSOUND/XbDSoundTypes.h @@ -369,30 +369,6 @@ struct X_DSVOICEPROPS { LONG lI3DL2DirectVolume; LONG lI3DL2RoomVolume; }; - -// Convert frequency to pitch helper -static inline int32_t converter_freq2pitch(uint32_t freq) { - // NOTE: pitch = 0 is equal to 48 KHz. - /* For research purpose of how to convert frequency to pitch and back to frequency. - // Edit hertz variable to see the result. - float hertz = 12000.0f; - - float hertzRatio = 48000.0f; - float pitchRatio = 4096.0f; - - // Convert hertz to pitch - float pitch = log2(hertz / hertzRatio) * pitchRatio; - - // Convert pitch to hertz - hertz = exp((pitch / pitchRatio) * log(2)) * hertzRatio;*/ - return static_cast(log2(freq / 48000.0f) * 4096.0f); -} - -// Convert pitch to frequency helper -static inline uint32_t converter_pitch2freq(int32_t pitch) { - //* See research documentation above for conversion example. - return static_cast(exp((pitch / 4096.0f) * log(2)) * 48000.0f); -} } // end of namespace XTL diff --git a/src/core/hle/DSOUND/common/XbInternalDSVoice.cpp b/src/core/hle/DSOUND/common/XbInternalDSVoice.cpp index 44de79958..bbbe49cbf 100644 --- a/src/core/hle/DSOUND/common/XbInternalDSVoice.cpp +++ b/src/core/hle/DSOUND/common/XbInternalDSVoice.cpp @@ -58,7 +58,7 @@ void SetFormat_4034_lower(T& settings, XTL::audio_format format) if (format.audio_codec == WAVE_FORMAT_XBOX_ADPCM) { settings.p_audio_format->wSamplesPerBlock = 64; } - settings.pitch = XTL::converter_freq2pitch(format.nSamplesPerSec); + settings.pitch = converter_freq2pitch(format.nSamplesPerSec); } template void SetFormat_4039_only(T& settings, XTL::audio_format format) @@ -68,7 +68,7 @@ void SetFormat_4039_only(T& settings, XTL::audio_format format) settings.cbSize = format.cbSize; settings.nSamplesPerSec_default = format.nSamplesPerSec; settings.bitsPerSample = format.bitsPerSample; - settings.pitch = XTL::converter_freq2pitch(format.nSamplesPerSec); + settings.pitch = converter_freq2pitch(format.nSamplesPerSec); } template void SetFormat_4134_upper(T& settings, XTL::audio_format format) @@ -78,7 +78,7 @@ void SetFormat_4134_upper(T& settings, XTL::audio_format format) settings.cbSize = static_cast(format.cbSize); settings.nSamplesPerSec_default = format.nSamplesPerSec; settings.bitsPerSample = format.bitsPerSample; - settings.pitch = XTL::converter_freq2pitch(format.nSamplesPerSec); + settings.pitch = converter_freq2pitch(format.nSamplesPerSec); } // Interface for get frequency diff --git a/src/core/hle/DSOUND/common/XbInternalStruct.hpp b/src/core/hle/DSOUND/common/XbInternalStruct.hpp index d8389db0b..1368ebb30 100644 --- a/src/core/hle/DSOUND/common/XbInternalStruct.hpp +++ b/src/core/hle/DSOUND/common/XbInternalStruct.hpp @@ -26,6 +26,7 @@ #include "Cxbx.h" #include "core\hle\DSOUND\XbDSoundTypes.h" +#include "common/audio/converter.hpp" namespace XTL {