move audio converter helper

This commit is contained in:
RadWolfie 2020-05-14 19:09:19 -05:00
parent 95e8642970
commit 9871b25fa8
6 changed files with 59 additions and 30 deletions

View File

@ -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"

View File

@ -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 <cstdint>
// 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<int32_t>(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<uint32_t>(exp((pitch / 4096.0f) * log(2)) * 48000.0f);
}

View File

@ -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));
}

View File

@ -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<int32_t>(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<uint32_t>(exp((pitch / 4096.0f) * log(2)) * 48000.0f);
}
} // end of namespace XTL

View File

@ -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<class T>
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<class T>
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<uint8_t>(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

View File

@ -26,6 +26,7 @@
#include "Cxbx.h"
#include "core\hle\DSOUND\XbDSoundTypes.h"
#include "common/audio/converter.hpp"
namespace XTL {