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
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_OPENAL
|
2014-03-24 15:54:22 +01:00
|
|
|
#include "include/aegisub/audio_player.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
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
#include <libaegisub/audio/provider.h>
|
2014-03-24 15:54:22 +01:00
|
|
|
#include <libaegisub/log.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2014-03-24 15:54:22 +01:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <al.h>
|
|
|
|
#include <alc.h>
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
#include <OpenAL/al.h>
|
|
|
|
#include <OpenAL/alc.h>
|
|
|
|
#else
|
|
|
|
#include <AL/al.h>
|
|
|
|
#include <AL/alc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <wx/timer.h>
|
|
|
|
|
2007-05-10 23:08:30 +02:00
|
|
|
// Auto-link to OpenAL lib for MSVC
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma comment(lib, "openal32.lib")
|
|
|
|
#endif
|
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
namespace {
|
|
|
|
class OpenALPlayer final : public AudioPlayer, wxTimer {
|
|
|
|
/// Number of OpenAL buffers to use
|
|
|
|
static const ALsizei num_buffers = 8;
|
|
|
|
|
|
|
|
bool playing = false; ///< Is audio currently playing?
|
|
|
|
|
|
|
|
float volume = 1.f; ///< Current audio volume
|
|
|
|
ALsizei samplerate; ///< Sample rate of the audio
|
|
|
|
int bpf; ///< Bytes per frame
|
|
|
|
|
|
|
|
int64_t start_frame = 0; ///< First frame of playbacka
|
|
|
|
int64_t cur_frame = 0; ///< Next frame to write to playback buffers
|
|
|
|
int64_t end_frame = 0; ///< Last frame to play
|
|
|
|
|
|
|
|
ALCdevice *device = nullptr; ///< OpenAL device handle
|
|
|
|
ALCcontext *context = nullptr; ///< OpenAL sound context
|
|
|
|
ALuint buffers[num_buffers]; ///< OpenAL sound buffers
|
|
|
|
ALuint source = 0; ///< OpenAL playback source
|
|
|
|
|
|
|
|
/// Index into buffers, first free (unqueued) buffer to be filled
|
|
|
|
ALsizei buf_first_free = 0;
|
|
|
|
|
|
|
|
/// Index into buffers, first queued (non-free) buffer
|
|
|
|
ALsizei buf_first_queued = 0;
|
|
|
|
|
|
|
|
/// Number of free buffers
|
|
|
|
ALsizei buffers_free = 0;
|
|
|
|
|
|
|
|
/// Number of buffers which have been fully played since playback was last started
|
|
|
|
ALsizei buffers_played = 0;
|
|
|
|
|
|
|
|
wxStopWatch playback_segment_timer;
|
|
|
|
|
|
|
|
/// Buffer to decode audio into
|
|
|
|
std::vector<char> decode_buffer;
|
|
|
|
|
|
|
|
/// Fill count OpenAL buffers
|
|
|
|
void FillBuffers(ALsizei count);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// wxTimer override to periodically fill available buffers
|
|
|
|
void Notify() override;
|
|
|
|
|
|
|
|
public:
|
2014-07-09 16:22:49 +02:00
|
|
|
OpenALPlayer(agi::AudioProvider *provider);
|
2014-03-24 15:54:22 +01:00
|
|
|
~OpenALPlayer();
|
|
|
|
|
|
|
|
void Play(int64_t start,int64_t count) override;
|
|
|
|
void Stop() override;
|
|
|
|
bool IsPlaying() override { return playing; }
|
|
|
|
|
|
|
|
int64_t GetEndPosition() override { return end_frame; }
|
|
|
|
int64_t GetCurrentPosition() override;
|
|
|
|
void SetEndPosition(int64_t pos) override;
|
|
|
|
|
|
|
|
void SetVolume(double vol) override { volume = vol; }
|
|
|
|
};
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
OpenALPlayer::OpenALPlayer(agi::AudioProvider *provider)
|
2012-03-20 00:31:33 +01:00
|
|
|
: AudioPlayer(provider)
|
2013-10-23 23:27:38 +02:00
|
|
|
, samplerate(provider->GetSampleRate())
|
|
|
|
, bpf(provider->GetChannels() * provider->GetBytesPerSample())
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
2011-11-19 02:14:13 +01:00
|
|
|
try {
|
|
|
|
// Open device
|
2013-11-21 18:13:36 +01:00
|
|
|
device = alcOpenDevice(nullptr);
|
2014-07-09 16:22:49 +02:00
|
|
|
if (!device) throw AudioPlayerOpenError("Failed opening default OpenAL device");
|
2011-11-19 02:14:13 +01:00
|
|
|
|
|
|
|
// Create context
|
2013-11-21 18:13:36 +01:00
|
|
|
context = alcCreateContext(device, nullptr);
|
2014-07-09 16:22:49 +02:00
|
|
|
if (!context) throw AudioPlayerOpenError("Failed creating OpenAL context");
|
|
|
|
if (!alcMakeContextCurrent(context)) throw AudioPlayerOpenError("Failed selecting OpenAL context");
|
2011-11-19 02:14:13 +01:00
|
|
|
|
|
|
|
// Clear error code
|
|
|
|
alGetError();
|
|
|
|
|
|
|
|
// Generate buffers
|
|
|
|
alGenBuffers(num_buffers, buffers);
|
2014-07-09 16:22:49 +02:00
|
|
|
if (alGetError() != AL_NO_ERROR) throw AudioPlayerOpenError("Error generating OpenAL buffers");
|
2011-11-19 02:14:13 +01:00
|
|
|
|
|
|
|
// Generate source
|
|
|
|
alGenSources(1, &source);
|
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
alDeleteBuffers(num_buffers, buffers);
|
2014-07-09 16:22:49 +02:00
|
|
|
throw AudioPlayerOpenError("Error generating OpenAL source");
|
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
|
|
|
}
|
2014-03-24 15:54:22 +01:00
|
|
|
}
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
std::unique_ptr<AudioPlayer> CreateOpenALPlayer(agi::AudioProvider *provider, wxWindow *)
|
2014-03-24 15:54:22 +01:00
|
|
|
{
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<OpenALPlayer>(provider);
|
2014-03-24 15:54:22 +01:00
|
|
|
}
|
2007-05-10 23:08:30 +02:00
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#endif // WITH_OPENAL
|