2007-05-10 23:08:30 +02:00
|
|
|
// Copyright (c) 2007, Niels Martin Hansen
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the Aegisub Group nor the names of its contributors
|
|
|
|
// may be used to endorse or promote products derived from this software
|
|
|
|
// without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_player_openal.cpp
|
|
|
|
/// @brief OpenAL-based audio output
|
|
|
|
/// @ingroup audio_output
|
|
|
|
///
|
2007-05-10 23:08:30 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_OPENAL
|
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_player_openal.h"
|
2011-11-19 02:14:13 +01:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2008-03-05 21:16:31 +01:00
|
|
|
|
2007-05-10 23:08:30 +02:00
|
|
|
// Auto-link to OpenAL lib for MSVC
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma comment(lib, "openal32.lib")
|
|
|
|
#endif
|
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
DEFINE_SIMPLE_EXCEPTION(OpenALException, agi::AudioPlayerOpenError, "audio/open/player/openal")
|
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
OpenALPlayer::OpenALPlayer(AudioProvider *provider)
|
|
|
|
: AudioPlayer(provider)
|
2011-11-19 02:14:13 +01:00
|
|
|
, playing(false)
|
|
|
|
, volume(1.f)
|
2013-10-23 23:27:38 +02:00
|
|
|
, samplerate(provider->GetSampleRate())
|
|
|
|
, bpf(provider->GetChannels() * provider->GetBytesPerSample())
|
2011-11-19 02:14:13 +01:00
|
|
|
, start_frame(0)
|
|
|
|
, cur_frame(0)
|
|
|
|
, end_frame(0)
|
|
|
|
, device(0)
|
|
|
|
, context(0)
|
2013-10-23 23:27:38 +02:00
|
|
|
, source(0)
|
|
|
|
, buf_first_free(0)
|
|
|
|
, buf_first_queued(0)
|
|
|
|
, buffers_free(0)
|
|
|
|
, buffers_played(0)
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
2011-11-19 02:14:13 +01:00
|
|
|
try {
|
|
|
|
// Open device
|
|
|
|
device = alcOpenDevice(0);
|
2012-01-08 02:33:39 +01:00
|
|
|
if (!device) throw OpenALException("Failed opening default OpenAL device", 0);
|
2011-11-19 02:14:13 +01:00
|
|
|
|
|
|
|
// Create context
|
|
|
|
context = alcCreateContext(device, 0);
|
2012-01-08 02:33:39 +01:00
|
|
|
if (!context) throw OpenALException("Failed creating OpenAL context", 0);
|
|
|
|
if (!alcMakeContextCurrent(context)) throw OpenALException("Failed selecting OpenAL context", 0);
|
2011-11-19 02:14:13 +01:00
|
|
|
|
|
|
|
// Clear error code
|
|
|
|
alGetError();
|
|
|
|
|
|
|
|
// Generate buffers
|
|
|
|
alGenBuffers(num_buffers, buffers);
|
2012-01-08 02:33:39 +01:00
|
|
|
if (alGetError() != AL_NO_ERROR) throw OpenALException("Error generating OpenAL buffers", 0);
|
2011-11-19 02:14:13 +01:00
|
|
|
|
|
|
|
// Generate source
|
|
|
|
alGenSources(1, &source);
|
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
alDeleteBuffers(num_buffers, buffers);
|
2012-01-08 02:33:39 +01:00
|
|
|
throw OpenALException("Error generating OpenAL source", 0);
|
2011-11-19 02:14:13 +01:00
|
|
|
}
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
2011-11-19 02:14:13 +01:00
|
|
|
catch (...)
|
|
|
|
{
|
2007-05-10 23:08:30 +02:00
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
2011-11-19 02:14:13 +01:00
|
|
|
throw;
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine buffer length
|
2011-11-19 02:14:13 +01:00
|
|
|
decode_buffer.resize(samplerate * bpf / num_buffers / 2); // buffers for half a second of audio
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
OpenALPlayer::~OpenALPlayer()
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
alDeleteSources(1, &source);
|
|
|
|
alDeleteBuffers(num_buffers, buffers);
|
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
|
|
|
}
|
|
|
|
|
2011-11-19 02:14:13 +01:00
|
|
|
void OpenALPlayer::Play(int64_t start, int64_t count)
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
if (playing) {
|
|
|
|
// Quick reset
|
|
|
|
playing = false;
|
|
|
|
alSourceStop(source);
|
|
|
|
alSourcei(source, AL_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set params
|
|
|
|
start_frame = start;
|
|
|
|
cur_frame = start;
|
|
|
|
end_frame = start + count;
|
|
|
|
playing = true;
|
|
|
|
|
|
|
|
// Prepare buffers
|
|
|
|
buffers_free = num_buffers;
|
|
|
|
buffers_played = 0;
|
|
|
|
buf_first_free = 0;
|
|
|
|
buf_first_queued = 0;
|
|
|
|
FillBuffers(num_buffers);
|
|
|
|
|
|
|
|
// And go!
|
|
|
|
alSourcePlay(source);
|
|
|
|
wxTimer::Start(100);
|
2007-05-11 00:31:49 +02:00
|
|
|
playback_segment_timer.Start();
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:44 +02:00
|
|
|
void OpenALPlayer::Stop()
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
if (!playing) return;
|
|
|
|
|
|
|
|
// Reset data
|
|
|
|
wxTimer::Stop();
|
|
|
|
playing = false;
|
|
|
|
start_frame = 0;
|
|
|
|
cur_frame = 0;
|
|
|
|
end_frame = 0;
|
|
|
|
|
|
|
|
// Then drop the playback
|
|
|
|
alSourceStop(source);
|
|
|
|
alSourcei(source, AL_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenALPlayer::FillBuffers(ALsizei count)
|
|
|
|
{
|
|
|
|
// Do the actual filling/queueing
|
2011-11-19 02:14:13 +01:00
|
|
|
for (count = mid(1, count, buffers_free); count > 0; --count) {
|
|
|
|
ALsizei fill_len = mid<ALsizei>(0, decode_buffer.size() / bpf, end_frame - cur_frame);
|
2007-05-10 23:08:30 +02:00
|
|
|
|
|
|
|
if (fill_len > 0)
|
|
|
|
// Get fill_len frames of audio
|
2011-11-19 02:14:13 +01:00
|
|
|
provider->GetAudioWithVolume(&decode_buffer[0], cur_frame, fill_len, volume);
|
|
|
|
if ((size_t)fill_len * bpf < decode_buffer.size())
|
2007-05-10 23:08:30 +02:00
|
|
|
// And zerofill the rest
|
2011-11-19 02:14:13 +01:00
|
|
|
memset(&decode_buffer[fill_len * bpf], 0, decode_buffer.size() - fill_len * bpf);
|
2007-05-10 23:08:30 +02:00
|
|
|
|
|
|
|
cur_frame += fill_len;
|
|
|
|
|
2011-11-19 02:14:13 +01:00
|
|
|
alBufferData(buffers[buf_first_free], AL_FORMAT_MONO16, &decode_buffer[0], decode_buffer.size(), samplerate);
|
|
|
|
alSourceQueueBuffers(source, 1, &buffers[buf_first_free]); // FIXME: collect buffer handles and queue all at once instead of one at a time?
|
|
|
|
buf_first_free = (buf_first_free + 1) % num_buffers;
|
|
|
|
--buffers_free;
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenALPlayer::Notify()
|
|
|
|
{
|
|
|
|
ALsizei newplayed;
|
|
|
|
alGetSourcei(source, AL_BUFFERS_PROCESSED, &newplayed);
|
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("player/audio/openal") << "buffers_played=" << buffers_played << " newplayed=" << newplayed;
|
2007-05-10 23:08:30 +02:00
|
|
|
|
|
|
|
if (newplayed > 0) {
|
|
|
|
// Reclaim buffers
|
2011-11-19 02:14:13 +01:00
|
|
|
ALuint bufs[num_buffers];
|
|
|
|
for (ALsizei i = 0; i < newplayed; ++i) {
|
|
|
|
bufs[i] = buffers[buf_first_queued];
|
|
|
|
buf_first_queued = (buf_first_queued + 1) % num_buffers;
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
alSourceUnqueueBuffers(source, newplayed, bufs);
|
|
|
|
buffers_free += newplayed;
|
|
|
|
|
|
|
|
// Update
|
|
|
|
buffers_played += newplayed;
|
2007-05-11 00:31:49 +02:00
|
|
|
playback_segment_timer.Start();
|
2007-05-10 23:08:30 +02:00
|
|
|
|
|
|
|
// Fill more buffers
|
|
|
|
FillBuffers(newplayed);
|
|
|
|
}
|
|
|
|
|
2011-11-19 02:14:13 +01:00
|
|
|
LOG_D("player/audio/openal") << "frames played=" << (buffers_played - num_buffers) * decode_buffer.size() / bpf << " num frames=" << end_frame - start_frame;
|
2007-05-11 00:31:49 +02:00
|
|
|
// Check that all of the selected audio plus one full set of buffers has been queued
|
2012-01-08 02:04:29 +01:00
|
|
|
if ((buffers_played - num_buffers) * (int64_t)decode_buffer.size() > (end_frame - start_frame) * bpf) {
|
2012-03-25 06:05:44 +02:00
|
|
|
Stop();
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
void OpenALPlayer::SetEndPosition(int64_t pos)
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
end_frame = pos;
|
|
|
|
}
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t OpenALPlayer::GetCurrentPosition()
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
2007-05-11 00:39:17 +02:00
|
|
|
// FIXME: this should be based on not duration played but actual sample being heard
|
2008-06-15 14:43:56 +02:00
|
|
|
// (during video playback, cur_frame might get changed to resync)
|
2007-05-11 00:31:49 +02:00
|
|
|
long extra = playback_segment_timer.Time();
|
2011-11-19 02:14:13 +01:00
|
|
|
return buffers_played * decode_buffer.size() / bpf + start_frame + extra * samplerate / 1000;
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#endif // WITH_OPENAL
|