2025-08-12 11:29:24 -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 "CBufferItem.h"
|
|
|
|
|
|
2012-08-31 12:08:41 -07:00
|
|
|
#if CONFIG2_AUDIO
|
|
|
|
|
|
2025-08-12 11:29:24 -07:00
|
|
|
#include "soundmanager/ISoundManager.h"
|
2012-08-31 12:08:41 -07:00
|
|
|
#include "soundmanager/SoundManager.h"
|
2025-10-10 11:49:37 -07:00
|
|
|
#include "soundmanager/data/SoundData.h"
|
2012-08-14 17:10:44 -07:00
|
|
|
|
2025-08-12 11:29:24 -07:00
|
|
|
#include <AL/al.h>
|
|
|
|
|
#include <cstddef>
|
2019-06-06 12:30:48 -07:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2025-10-10 11:49:37 -07:00
|
|
|
CBufferItem::CBufferItem(CSoundData* sndData)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
|
|
|
|
ResetVars();
|
|
|
|
|
if (InitOpenAL())
|
|
|
|
|
Attach(sndData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CBufferItem::~CBufferItem()
|
|
|
|
|
{
|
2013-04-17 20:24:20 -07:00
|
|
|
Stop();
|
|
|
|
|
ReleaseOpenALBuffer();
|
2013-03-01 06:22:28 -08:00
|
|
|
}
|
|
|
|
|
|
2012-08-31 12:08:41 -07:00
|
|
|
|
2013-04-17 20:24:20 -07:00
|
|
|
void CBufferItem::ReleaseOpenALBuffer()
|
2013-03-01 06:22:28 -08:00
|
|
|
{
|
2013-04-17 20:24:20 -07:00
|
|
|
if ( m_ALSource == 0 )
|
|
|
|
|
return;
|
|
|
|
|
|
2013-03-01 06:22:28 -08:00
|
|
|
int num_processed;
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-03-01 06:22:28 -08:00
|
|
|
alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-04-17 20:24:20 -07:00
|
|
|
|
2013-03-01 06:22:28 -08:00
|
|
|
if (num_processed > 0)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2013-04-17 20:24:20 -07:00
|
|
|
ALuint* al_buf = new ALuint[num_processed];
|
|
|
|
|
alSourceUnqueueBuffers(m_ALSource, num_processed, al_buf);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-04-17 20:24:20 -07:00
|
|
|
delete[] al_buf;
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
2013-06-10 17:05:57 -07:00
|
|
|
alSourcei(m_ALSource, AL_BUFFER, 0);
|
2013-06-06 04:13:57 -07:00
|
|
|
((CSoundManager*)g_SoundManager)->ReleaseALSource(m_ALSource);
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-04-26 16:13:11 -07:00
|
|
|
|
2013-04-17 20:24:20 -07:00
|
|
|
m_ALSource = 0;
|
2013-03-01 06:22:28 -08:00
|
|
|
}
|
2012-08-14 17:10:44 -07:00
|
|
|
|
|
|
|
|
bool CBufferItem::IdleTask()
|
|
|
|
|
{
|
2013-03-01 06:22:28 -08:00
|
|
|
if ( m_ALSource == 0 )
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-08-14 17:10:44 -07:00
|
|
|
HandleFade();
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2013-03-01 06:22:28 -08:00
|
|
|
if (m_LastPlay)
|
|
|
|
|
{
|
2019-06-06 12:30:48 -07:00
|
|
|
std::lock_guard<std::mutex> lock(m_ItemMutex);
|
2013-03-01 06:22:28 -08:00
|
|
|
int proc_state;
|
2013-04-25 03:48:34 -07:00
|
|
|
alGetSourcei(m_ALSource, AL_SOURCE_STATE, &proc_state);
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2021-01-22 04:50:05 -08:00
|
|
|
m_ShouldBePlaying = (proc_state != AL_STOPPED && proc_state != AL_INITIAL && proc_state != AL_PAUSED);
|
|
|
|
|
return m_ShouldBePlaying;
|
2013-03-01 06:22:28 -08:00
|
|
|
}
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2013-03-01 06:22:28 -08:00
|
|
|
if (GetLooping())
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2013-03-01 06:22:28 -08:00
|
|
|
int num_processed;
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-03-01 06:22:28 -08:00
|
|
|
alGetSourcei(m_ALSource, AL_BUFFERS_PROCESSED, &num_processed);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-03-01 06:22:28 -08:00
|
|
|
for (int i = 0; i < num_processed; i++)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2013-04-17 20:24:20 -07:00
|
|
|
ALuint al_buf;
|
|
|
|
|
alSourceUnqueueBuffers(m_ALSource, 1, &al_buf);
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2013-04-17 20:24:20 -07:00
|
|
|
alSourceQueueBuffers(m_ALSource, 1, &al_buf);
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-10 11:49:37 -07:00
|
|
|
void CBufferItem::Attach(CSoundData* itemData)
|
2012-08-14 17:10:44 -07:00
|
|
|
{
|
2025-10-10 11:49:37 -07:00
|
|
|
if ( m_ALSource == 0 )
|
2013-03-01 06:22:28 -08:00
|
|
|
return;
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2025-10-10 11:49:37 -07:00
|
|
|
if (m_SoundData != NULL)
|
|
|
|
|
{
|
|
|
|
|
CSoundData::ReleaseSoundData(m_SoundData);
|
|
|
|
|
m_SoundData = 0;
|
|
|
|
|
}
|
2015-02-02 05:44:06 -08:00
|
|
|
AL_CHECK;
|
2025-10-10 11:49:37 -07:00
|
|
|
if (itemData != NULL)
|
|
|
|
|
{
|
|
|
|
|
m_SoundData = itemData->IncrementCount();
|
|
|
|
|
alSourceQueueBuffers(m_ALSource, m_SoundData->GetBufferCount(),(const ALuint *) m_SoundData->GetBufferPtr());
|
|
|
|
|
AL_CHECK;
|
|
|
|
|
}
|
2012-08-14 17:10:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CBufferItem::SetLooping(bool loops)
|
|
|
|
|
{
|
|
|
|
|
m_Looping = loops;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-31 12:08:41 -07:00
|
|
|
#endif // CONFIG2_AUDIO
|