LCOV - code coverage report
Current view: top level - source/soundmanager/data - OggData.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 61 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 13 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "OggData.h"
      21             : 
      22             : #if CONFIG2_AUDIO
      23             : 
      24             : #include "lib/status.h"
      25             : #include "ps/CLogger.h"
      26             : #include "ps/Filesystem.h"
      27             : #include "soundmanager/SoundManager.h"
      28             : #include "soundmanager/data/ogg.h"
      29             : 
      30             : #include <algorithm>
      31             : #include <cstddef>
      32             : #include <span>
      33             : #include <vector>
      34             : 
      35             : /*
      36             : * Each buffer holds ~0.56 seconds of audio.
      37             : * Bytes per second = sampleRate × channels × bytesPerSample.
      38             : * For 44100 Hz, 2 channels, 2 bytes per sample, this is 176400 bytes per second.
      39             : * Nice multiple of 4096 (system page size)
      40             : */
      41             : constexpr int OGG_DEFAULT_BUFFER_SIZE = 98304;
      42             : 
      43           0 : COggData::COggData()
      44           0 :     : m_Format(0), m_Frequency(0), m_OneShot(false), m_BuffersCount(0)
      45             : {
      46           0 : }
      47             : 
      48           0 : COggData::~COggData()
      49             : {
      50           0 :     AL_CHECK;
      51           0 :     if (m_BuffersCount > 0)
      52           0 :         alDeleteBuffers(m_BuffersCount, &m_Buffer.at(0));
      53             : 
      54           0 :     AL_CHECK;
      55           0 :     m_BuffersCount = 0;
      56           0 : }
      57             : 
      58           0 : void COggData::SetFormatAndFreq(ALenum form, ALsizei freq)
      59             : {
      60           0 :     m_Format = form;
      61           0 :     m_Frequency = freq;
      62           0 : }
      63             : 
      64           0 : bool COggData::IsStereo()
      65             : {
      66           0 :     return m_Format == AL_FORMAT_STEREO16;
      67             : }
      68             : 
      69           0 : bool COggData::InitOggFile(const VfsPath& itemPath)
      70             : {
      71           0 :     if (OpenOggNonstream(g_VFS, itemPath, m_Stream) != INFO::OK)
      72             :         return false;
      73             : 
      74           0 :     m_FileFinished = false;
      75             : 
      76           0 :     SetFormatAndFreq(m_Stream->Format(), m_Stream->SamplingRate());
      77           0 :     SetFileName(itemPath);
      78             : 
      79           0 :     AL_CHECK;
      80           0 :     alGenBuffers(m_Buffer.size(), m_Buffer.data());
      81             : 
      82           0 :     ALenum err{alGetError()};
      83           0 :     if (err != AL_NO_ERROR)
      84             :     {
      85           0 :         LOGERROR("Failed to create initial buffer. OpenAL error: {}\n", alGetString(err));
      86           0 :         return false;
      87             :     }
      88             : 
      89           0 :     m_BuffersCount = FetchDataIntoBuffer(m_Buffer.size(), m_Buffer.data());
      90           0 :     if (m_FileFinished)
      91             :     {
      92           0 :         m_OneShot = true;
      93           0 :         if (m_BuffersCount < OGG_DEFAULT_BUFFER_COUNT)
      94           0 :             alDeleteBuffers(OGG_DEFAULT_BUFFER_COUNT - m_BuffersCount, &m_Buffer.at(m_BuffersCount));
      95             :     }
      96           0 :     AL_CHECK;
      97             : 
      98           0 :     return true;
      99             : }
     100             : 
     101           0 : ALsizei COggData::GetBufferCount()
     102             : {
     103           0 :     return m_BuffersCount;
     104             : }
     105             : 
     106           0 : bool COggData::IsFileFinished()
     107             : {
     108           0 :     return m_FileFinished;
     109             : }
     110             : 
     111           0 : void COggData::ResetFile()
     112             : {
     113           0 :     m_Stream->ResetFile();
     114           0 :     m_FileFinished = false;
     115           0 : }
     116             : 
     117           0 : bool COggData::IsOneShot()
     118             : {
     119           0 :     return m_OneShot;
     120             : }
     121             : 
     122           0 : int COggData::FetchDataIntoBuffer(int count, ALuint* buffers)
     123             : {
     124           0 :     std::vector<std::uint8_t> PCMOut(OGG_DEFAULT_BUFFER_SIZE);
     125           0 :     int buffersWritten{0};
     126             : 
     127           0 :     for (int i{0}; i < count && !m_FileFinished; ++i)
     128             :     {
     129           0 :         std::fill(PCMOut.begin(), PCMOut.end(), 0);
     130           0 :         const size_t totalRet{m_Stream->GetNextChunk(std::span<std::uint8_t>(PCMOut))};
     131           0 :         m_FileFinished = m_Stream->AtFileEOF();
     132           0 :         if (totalRet == 0)
     133           0 :             continue;
     134             : 
     135           0 :         ++buffersWritten;
     136           0 :         alBufferData(buffers[i], m_Format, PCMOut.data(), static_cast<ALsizei>(totalRet), m_Frequency);
     137             :     }
     138           0 :     return buffersWritten;
     139           0 : }
     140             : 
     141             : 
     142           0 : ALuint COggData::GetBuffer()
     143             : {
     144           0 :     return m_Buffer.at(0);
     145             : }
     146             : 
     147           0 : ALuint* COggData::GetBufferPtr()
     148             : {
     149           0 :     return m_Buffer.data();
     150             : }
     151             : 
     152             : #endif // CONFIG2_AUDIO

Generated by: LCOV version 1.16