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.
|
|
|
|
//
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AEGISUB
|
|
|
|
//
|
|
|
|
// Website: http://aegisub.cellosoft.com
|
|
|
|
// Contact: mailto:jiifurusu@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_OPENAL
|
|
|
|
|
|
|
|
|
2007-05-10 23:08:30 +02:00
|
|
|
///////////
|
|
|
|
// Headers
|
|
|
|
#include <wx/wxprec.h>
|
2008-03-07 22:48:36 +01:00
|
|
|
#include "audio_player_manager.h"
|
2008-03-07 22:47:20 +01:00
|
|
|
#include "audio_provider_manager.h"
|
2007-05-10 23:08:30 +02:00
|
|
|
#include "utils.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "frame_main.h"
|
2008-03-05 03:43:01 +01:00
|
|
|
#include "audio_player_openal.h"
|
2007-05-10 23:08:30 +02:00
|
|
|
#include "options.h"
|
2008-03-05 21:16:31 +01:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
#include <al.h>
|
|
|
|
#include <alc.h>
|
|
|
|
#elif defined(__APPLE__)
|
2007-09-12 01:22:26 +02:00
|
|
|
#include <OpenAL/AL.h>
|
|
|
|
#include <OpenAL/ALC.h>
|
|
|
|
#else
|
2008-03-05 21:16:31 +01:00
|
|
|
#include <AL/al.h>
|
|
|
|
#include <AL/alc.h>
|
2007-09-12 01:22:26 +02:00
|
|
|
#endif
|
2007-05-10 23:08:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Auto-link to OpenAL lib for MSVC
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma comment(lib, "openal32.lib")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
|
|
|
OpenALPlayer::OpenALPlayer()
|
|
|
|
{
|
|
|
|
volume = 1.0f;
|
|
|
|
open = false;
|
|
|
|
playing = false;
|
|
|
|
start_frame = cur_frame = end_frame = bpf = 0;
|
|
|
|
provider = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Destructor
|
|
|
|
OpenALPlayer::~OpenALPlayer()
|
|
|
|
{
|
|
|
|
CloseStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Open stream
|
|
|
|
void OpenALPlayer::OpenStream()
|
|
|
|
{
|
|
|
|
CloseStream();
|
|
|
|
|
|
|
|
// Get provider
|
|
|
|
provider = GetProvider();
|
|
|
|
bpf = provider->GetChannels() * provider->GetBytesPerSample();
|
|
|
|
|
|
|
|
// Open device
|
|
|
|
device = alcOpenDevice(0);
|
|
|
|
if (!device) {
|
|
|
|
throw _T("Failed opening default OpenAL device");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create context
|
|
|
|
context = alcCreateContext(device, 0);
|
|
|
|
if (!context) {
|
|
|
|
alcCloseDevice(device);
|
|
|
|
throw _T("Failed creating OpenAL context");
|
|
|
|
}
|
|
|
|
if (!alcMakeContextCurrent(context)) {
|
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
|
|
|
throw _T("Failed selecting OpenAL context");
|
|
|
|
}
|
|
|
|
// Clear error code
|
|
|
|
alGetError();
|
|
|
|
|
|
|
|
// Generate buffers
|
|
|
|
alGenBuffers(num_buffers, buffers);
|
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
|
|
|
throw _T("Error generating OpenAL buffers");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate source
|
|
|
|
alGenSources(1, &source);
|
|
|
|
if (alGetError() != AL_NO_ERROR) {
|
|
|
|
alDeleteBuffers(num_buffers, buffers);
|
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
|
|
|
throw _T("Error generating OpenAL source");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine buffer length
|
|
|
|
samplerate = provider->GetSampleRate();
|
2007-05-11 00:31:49 +02:00
|
|
|
buffer_length = samplerate / num_buffers / 2; // buffers for half a second of audio
|
2007-05-10 23:08:30 +02:00
|
|
|
|
|
|
|
// Now ready
|
|
|
|
open = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// Close stream
|
|
|
|
void OpenALPlayer::CloseStream()
|
|
|
|
{
|
|
|
|
if (!open) return;
|
|
|
|
|
|
|
|
Stop();
|
|
|
|
|
|
|
|
alDeleteSources(1, &source);
|
|
|
|
alDeleteBuffers(num_buffers, buffers);
|
|
|
|
alcDestroyContext(context);
|
|
|
|
alcCloseDevice(device);
|
|
|
|
|
|
|
|
// No longer working
|
|
|
|
open = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////
|
|
|
|
// Play
|
2007-08-31 16:11:35 +02: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
|
|
|
|
|
|
|
// Update timer
|
|
|
|
if (displayTimer && !displayTimer->IsRunning()) displayTimer->Start(15);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////
|
|
|
|
// Stop
|
|
|
|
void OpenALPlayer::Stop(bool timerToo)
|
|
|
|
{
|
|
|
|
if (!open) return;
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (timerToo && displayTimer) {
|
|
|
|
displayTimer->Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OpenALPlayer::FillBuffers(ALsizei count)
|
|
|
|
{
|
|
|
|
wxLogDebug(_T("FillBuffers: count=%d, buffers_free=%d"), count, buffers_free);
|
|
|
|
if (count > buffers_free) count = buffers_free;
|
|
|
|
if (count < 1) count = 1;
|
|
|
|
|
|
|
|
// Get memory to hold sound buffers
|
|
|
|
void *data = malloc(buffer_length * bpf);
|
|
|
|
|
|
|
|
// Do the actual filling/queueing
|
|
|
|
ALuint bufid = buf_first_free;
|
|
|
|
while (count > 0) {
|
|
|
|
ALsizei fill_len = buffer_length;
|
2008-06-15 14:43:56 +02:00
|
|
|
if (fill_len > (ALsizei)(end_frame - cur_frame)) fill_len = (ALsizei)(end_frame - cur_frame);
|
2007-05-10 23:08:30 +02:00
|
|
|
wxLogDebug(_T("buffer_length=%d, fill_len=%d, end_frame-cur_frame=%d"), buffer_length, fill_len, end_frame-cur_frame);
|
|
|
|
|
|
|
|
if (fill_len > 0)
|
|
|
|
// Get fill_len frames of audio
|
|
|
|
provider->GetAudioWithVolume(data, cur_frame, fill_len, volume);
|
|
|
|
if (fill_len < buffer_length)
|
|
|
|
// And zerofill the rest
|
|
|
|
memset((char*)data+(fill_len*bpf), 0, (buffer_length-fill_len)*bpf);
|
|
|
|
|
|
|
|
cur_frame += fill_len;
|
|
|
|
|
|
|
|
alBufferData(buffers[bufid], AL_FORMAT_MONO16, data, buffer_length*bpf, samplerate);
|
|
|
|
alSourceQueueBuffers(source, 1, &buffers[bufid]); // FIXME: collect buffer handles and queue all at once instead of one at a time?
|
|
|
|
if (++bufid >= num_buffers) bufid = 0;
|
|
|
|
count--; buffers_free--;
|
|
|
|
}
|
|
|
|
buf_first_free = bufid;
|
|
|
|
|
|
|
|
// Free buffer memory again
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OpenALPlayer::Notify()
|
|
|
|
{
|
|
|
|
ALsizei newplayed;
|
|
|
|
alGetSourcei(source, AL_BUFFERS_PROCESSED, &newplayed);
|
|
|
|
|
|
|
|
wxLogDebug(_T("OpenAL Player notify: buffers_played=%d, newplayed=%d, playeddiff=%d"), buffers_played, newplayed);
|
|
|
|
|
|
|
|
if (newplayed > 0) {
|
|
|
|
// Reclaim buffers
|
|
|
|
ALuint *bufs = new ALuint[newplayed];
|
|
|
|
ALsizei i = 0;
|
|
|
|
while (i < newplayed) {
|
|
|
|
bufs[i++] = buffers[buf_first_queued];
|
|
|
|
if (++buf_first_queued >= num_buffers) buf_first_queued = 0;
|
|
|
|
}
|
|
|
|
alSourceUnqueueBuffers(source, newplayed, bufs);
|
|
|
|
delete[] 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogDebug(_T("frames played=%d, num frames=%d"), (buffers_played - num_buffers) * buffer_length, 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
|
2007-05-10 23:08:30 +02:00
|
|
|
if ((buffers_played - num_buffers) * buffer_length > (ALsizei)(end_frame - start_frame)) {
|
2007-05-11 00:31:49 +02:00
|
|
|
// Then stop
|
2007-05-10 23:08:30 +02:00
|
|
|
Stop(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool OpenALPlayer::IsPlaying()
|
|
|
|
{
|
|
|
|
return playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Set end
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
// Set current position
|
2007-08-31 16:11:35 +02:00
|
|
|
void OpenALPlayer::SetCurrentPosition(int64_t pos)
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
cur_frame = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t OpenALPlayer::GetStartPosition()
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
return start_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t OpenALPlayer::GetEndPosition()
|
2007-05-10 23:08:30 +02:00
|
|
|
{
|
|
|
|
return end_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////
|
|
|
|
// Get current position
|
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();
|
|
|
|
return buffers_played * buffer_length + start_frame + extra * samplerate / 1000;
|
2007-05-10 23:08:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#endif // WITH_OPENAL
|
|
|
|
|