summaryrefslogtreecommitdiff
path: root/src/audio/sampman_oal.cpp
diff options
context:
space:
mode:
authorSergeanur <s.anureev@yandex.ua>2020-07-29 15:52:17 +0300
committerGitHub <noreply@github.com>2020-07-29 15:52:17 +0300
commit57d5782588af765a12337aa9f17dd474d3713e9e (patch)
tree30e82f920b5c3ab2c24a652d13df68609f7aaa80 /src/audio/sampman_oal.cpp
parente13d80a4ebfdaa89de3ad2eb6dc9ec6fbc1d3132 (diff)
parente259bb6609fc319f202016b3019e7ea8f3c3b5d2 (diff)
Merge pull request #587 from Sergeanur/opus
OPUS support for OpenAL
Diffstat (limited to 'src/audio/sampman_oal.cpp')
-rw-r--r--src/audio/sampman_oal.cpp58
1 files changed, 51 insertions, 7 deletions
diff --git a/src/audio/sampman_oal.cpp b/src/audio/sampman_oal.cpp
index 49ce5230..0e173152 100644
--- a/src/audio/sampman_oal.cpp
+++ b/src/audio/sampman_oal.cpp
@@ -27,6 +27,9 @@
#include "MusicManager.h"
#include "Frontend.h"
#include "Timer.h"
+#ifdef AUDIO_OPUS
+#include <opusfile.h>
+#endif
//TODO: fix eax3 reverb
//TODO: max channals
@@ -69,7 +72,11 @@ char SampleBankDescFilename[] = "audio/sfx.SDT";
char SampleBankDataFilename[] = "audio/sfx.RAW";
FILE *fpSampleDescHandle;
+#ifdef AUDIO_OPUS
+OggOpusFile *fpSampleDataHandle;
+#else
FILE *fpSampleDataHandle;
+#endif
bool bSampleBankLoaded [MAX_SAMPLEBANKS];
int32 nSampleBankDiscStartOffset [MAX_SAMPLEBANKS];
int32 nSampleBankSize [MAX_SAMPLEBANKS];
@@ -444,6 +451,8 @@ int8 cSampleManager::GetCurrent3DProviderIndex(void)
int8 cSampleManager::SetCurrent3DProvider(uint8 nProvider)
{
ASSERT( nProvider < m_nNumberOfProviders );
+ if (nProvider >= m_nNumberOfProviders)
+ nProvider = 0;
int savedprovider = curprovider;
if ( nProvider < m_nNumberOfProviders )
@@ -754,12 +763,27 @@ cSampleManager::LoadSampleBank(uint8 nBank)
return false;
}
+#ifdef AUDIO_OPUS
+ int samplesRead = 0;
+ int samplesSize = nSampleBankSize[nBank] / 2;
+ op_pcm_seek(fpSampleDataHandle, 0);
+ while (samplesSize > 0) {
+ int size = op_read(fpSampleDataHandle, (opus_int16 *)(nSampleBankMemoryStartAddress[nBank] + samplesRead), samplesSize, NULL);
+ if (size <= 0) {
+ // huh?
+ //assert(0);
+ break;
+ }
+ samplesRead += size*2;
+ samplesSize -= size;
+ }
+#else
if ( fseek(fpSampleDataHandle, nSampleBankDiscStartOffset[nBank], SEEK_SET) != 0 )
return false;
if ( fread((void *)nSampleBankMemoryStartAddress[nBank], 1, nSampleBankSize[nBank], fpSampleDataHandle) != nSampleBankSize[nBank] )
return false;
-
+#endif
bSampleBankLoaded[nBank] = true;
return true;
@@ -851,13 +875,28 @@ cSampleManager::LoadPedComment(uint32 nComment)
}
}
}
-
+
+#ifdef AUDIO_OPUS
+ int samplesRead = 0;
+ int samplesSize = m_aSamples[nComment].nSize / 2;
+ op_pcm_seek(fpSampleDataHandle, m_aSamples[nComment].nOffset / 2);
+ while (samplesSize > 0) {
+ int size = op_read(fpSampleDataHandle, (opus_int16 *)(nSampleBankMemoryStartAddress[SAMPLEBANK_PED] + PED_BLOCKSIZE * nCurrentPedSlot + samplesRead),
+ samplesSize, NULL);
+ if (size <= 0) {
+ return false;
+ }
+ samplesRead += size * 2;
+ samplesSize -= size;
+ }
+#else
if ( fseek(fpSampleDataHandle, m_aSamples[nComment].nOffset, SEEK_SET) != 0 )
return false;
if ( fread((void *)(nSampleBankMemoryStartAddress[SAMPLEBANK_PED] + PED_BLOCKSIZE*nCurrentPedSlot), 1, m_aSamples[nComment].nSize, fpSampleDataHandle) != m_aSamples[nComment].nSize )
return false;
-
+
+#endif
nPedSlotSfx[nCurrentPedSlot] = nComment;
alBufferData(pedBuffers[nCurrentPedSlot],
@@ -1387,7 +1426,7 @@ cSampleManager::InitialiseSampleBanks(void)
fpSampleDescHandle = fopen(SampleBankDescFilename, "rb");
if ( fpSampleDescHandle == NULL )
return false;
-
+#ifndef AUDIO_OPUS
fpSampleDataHandle = fopen(SampleBankDataFilename, "rb");
if ( fpSampleDataHandle == NULL )
{
@@ -1400,9 +1439,14 @@ cSampleManager::InitialiseSampleBanks(void)
fseek(fpSampleDataHandle, 0, SEEK_END);
int32 _nSampleDataEndOffset = ftell(fpSampleDataHandle);
rewind(fpSampleDataHandle);
-
+#else
+ int e;
+ fpSampleDataHandle = op_open_file(SampleBankDataFilename, &e);
+#endif
fread(m_aSamples, sizeof(tSample), TOTAL_AUDIO_SAMPLES, fpSampleDescHandle);
-
+#ifdef AUDIO_OPUS
+ int32 _nSampleDataEndOffset = m_aSamples[TOTAL_AUDIO_SAMPLES - 1].nOffset + m_aSamples[TOTAL_AUDIO_SAMPLES - 1].nSize;
+#endif
fclose(fpSampleDescHandle);
fpSampleDescHandle = NULL;
@@ -1420,7 +1464,7 @@ cSampleManager::InitialiseSampleBanks(void)
nSampleBankSize[SAMPLEBANK_MAIN] = nSampleBankDiscStartOffset[SAMPLEBANK_PED] - nSampleBankDiscStartOffset[SAMPLEBANK_MAIN];
nSampleBankSize[SAMPLEBANK_PED] = _nSampleDataEndOffset - nSampleBankDiscStartOffset[SAMPLEBANK_PED];
-
+
return true;
}