2025-06-20 10:31:35 -07:00
|
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
|
* This file is part of 0 A.D.
|
2012-08-14 17:10:44 -07:00
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2012-08-14 17:10:44 -07:00
|
|
|
|
* it 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.
|
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2012-08-14 17:10:44 -07:00
|
|
|
|
* 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 received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-14 17:10:44 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "OggData.h"
|
|
|
|
|
|
|
2012-08-31 12:08:41 -07:00
|
|
|
|
#if CONFIG2_AUDIO
|
|
|
|
|
|
|
2025-08-10 10:59:02 -07:00
|
|
|
|
#include "lib/status.h"
|
|
|
|
|
|
#include "lib/types.h"
|
2012-08-31 12:08:41 -07:00
|
|
|
|
#include "ps/Filesystem.h"
|
2012-09-01 15:16:53 -07:00
|
|
|
|
#include "soundmanager/SoundManager.h"
|
2025-08-10 10:59:02 -07:00
|
|
|
|
#include "soundmanager/data/ogg.h"
|
2012-09-01 15:12:28 -07:00
|
|
|
|
|
2025-07-29 16:00:14 -07:00
|
|
|
|
#include <AL/al.h>
|
2025-06-20 12:23:24 -07:00
|
|
|
|
#include <algorithm>
|
2025-07-29 16:00:14 -07:00
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
#include <stdexcept>
|
2025-08-13 08:20:49 -07:00
|
|
|
|
#include <span>
|
2025-06-20 12:23:24 -07:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
2025-06-20 10:56:56 -07:00
|
|
|
|
/*
|
|
|
|
|
|
* Each buffer holds ~0.56 seconds of audio.
|
|
|
|
|
|
* Bytes per second = sampleRate × channels × bytesPerSample.
|
|
|
|
|
|
* For 44100 Hz, 2 channels, 2 bytes per sample, this is 176400 bytes per second.
|
|
|
|
|
|
* Nice multiple of 4096 (system page size)
|
|
|
|
|
|
*/
|
|
|
|
|
|
constexpr int OGG_DEFAULT_BUFFER_SIZE = 98304;
|
|
|
|
|
|
|
2025-07-29 16:00:14 -07:00
|
|
|
|
COggData::COggData(const VfsPath& itemPath)
|
2025-07-11 11:18:40 -07:00
|
|
|
|
: m_Format(0), m_Frequency(0), m_OneShot(false), m_BuffersCount(0)
|
2012-08-14 17:10:44 -07:00
|
|
|
|
{
|
2025-07-29 16:00:14 -07:00
|
|
|
|
if (OpenOggNonstream(g_VFS, itemPath, m_Stream) != INFO::OK)
|
|
|
|
|
|
throw new OggDataError("Can't open Ogg file");
|
|
|
|
|
|
|
|
|
|
|
|
m_FileFinished = false;
|
|
|
|
|
|
|
|
|
|
|
|
SetFormatAndFreq(m_Stream->Format(), m_Stream->SamplingRate());
|
|
|
|
|
|
m_FileName = itemPath;
|
|
|
|
|
|
|
|
|
|
|
|
AL_CHECK;
|
|
|
|
|
|
alGenBuffers(m_Buffer.size(), m_Buffer.data());
|
|
|
|
|
|
|
|
|
|
|
|
ALenum err{alGetError()};
|
|
|
|
|
|
if (err != AL_NO_ERROR)
|
|
|
|
|
|
throw new OggDataError(fmt::format("Failed to create initial buffer. OpenAL error: {}", alGetString(err)));
|
|
|
|
|
|
|
|
|
|
|
|
m_BuffersCount = FetchDataIntoBuffer(m_Buffer.size(), m_Buffer.data());
|
|
|
|
|
|
if (!m_FileFinished)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
m_OneShot = true;
|
|
|
|
|
|
if (m_BuffersCount < OGG_DEFAULT_BUFFER_COUNT)
|
|
|
|
|
|
alDeleteBuffers(OGG_DEFAULT_BUFFER_COUNT - m_BuffersCount, &m_Buffer.at(m_BuffersCount));
|
|
|
|
|
|
|
|
|
|
|
|
AL_CHECK;
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COggData::~COggData()
|
|
|
|
|
|
{
|
2015-02-02 05:44:06 -08:00
|
|
|
|
AL_CHECK;
|
2025-07-11 11:18:40 -07:00
|
|
|
|
if (m_BuffersCount > 0)
|
|
|
|
|
|
alDeleteBuffers(m_BuffersCount, &m_Buffer.at(0));
|
2013-04-17 20:24:20 -07:00
|
|
|
|
|
2015-02-02 05:44:06 -08:00
|
|
|
|
AL_CHECK;
|
2025-07-11 11:18:40 -07:00
|
|
|
|
m_BuffersCount = 0;
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-14 21:18:07 -07:00
|
|
|
|
void COggData::SetFormatAndFreq(ALenum form, ALsizei freq)
|
2012-08-14 17:10:44 -07:00
|
|
|
|
{
|
|
|
|
|
|
m_Format = form;
|
|
|
|
|
|
m_Frequency = freq;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-31 18:52:05 -07:00
|
|
|
|
bool COggData::IsStereo()
|
|
|
|
|
|
{
|
2015-02-02 05:44:06 -08:00
|
|
|
|
return m_Format == AL_FORMAT_STEREO16;
|
2013-05-31 18:52:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-14 17:10:44 -07:00
|
|
|
|
ALsizei COggData::GetBufferCount()
|
|
|
|
|
|
{
|
2025-07-11 11:18:40 -07:00
|
|
|
|
return m_BuffersCount;
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool COggData::IsFileFinished()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_FileFinished;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void COggData::ResetFile()
|
|
|
|
|
|
{
|
2025-06-22 10:02:56 -07:00
|
|
|
|
m_Stream->ResetFile();
|
2012-08-14 17:10:44 -07:00
|
|
|
|
m_FileFinished = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool COggData::IsOneShot()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_OneShot;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int COggData::FetchDataIntoBuffer(int count, ALuint* buffers)
|
|
|
|
|
|
{
|
2025-06-20 12:23:24 -07:00
|
|
|
|
std::vector<u8> PCMOut(OGG_DEFAULT_BUFFER_SIZE);
|
2025-06-22 09:58:44 -07:00
|
|
|
|
int buffersWritten{0};
|
2016-11-23 06:09:58 -08:00
|
|
|
|
|
2025-06-22 09:58:44 -07:00
|
|
|
|
for (int i{0}; i < count && !m_FileFinished; ++i)
|
2015-02-02 05:44:06 -08:00
|
|
|
|
{
|
2025-06-20 12:23:24 -07:00
|
|
|
|
std::fill(PCMOut.begin(), PCMOut.end(), 0);
|
2025-08-13 08:20:49 -07:00
|
|
|
|
const size_t totalRet{m_Stream->GetNextChunk(std::span<u8>(PCMOut))};
|
2025-07-11 11:15:47 -07:00
|
|
|
|
m_FileFinished = m_Stream->AtFileEOF();
|
2025-06-20 12:23:24 -07:00
|
|
|
|
if (totalRet == 0)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
++buffersWritten;
|
|
|
|
|
|
alBufferData(buffers[i], m_Format, PCMOut.data(), static_cast<ALsizei>(totalRet), m_Frequency);
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
2015-02-02 05:44:06 -08:00
|
|
|
|
return buffersWritten;
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ALuint COggData::GetBuffer()
|
|
|
|
|
|
{
|
2025-06-23 09:46:48 -07:00
|
|
|
|
return m_Buffer.at(0);
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ALuint* COggData::GetBufferPtr()
|
|
|
|
|
|
{
|
2025-06-23 09:46:48 -07:00
|
|
|
|
return m_Buffer.data();
|
2012-08-14 17:10:44 -07:00
|
|
|
|
}
|
2012-08-31 12:08:41 -07:00
|
|
|
|
#endif // CONFIG2_AUDIO
|