mirror of https://github.com/odrling/Aegisub
parent
36f19a9d42
commit
5330c541ca
|
@ -48,7 +48,6 @@
|
||||||
///////////////
|
///////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
DirectSoundPlayer::DirectSoundPlayer() {
|
DirectSoundPlayer::DirectSoundPlayer() {
|
||||||
playing = false;
|
|
||||||
volume = 1.0f;
|
volume = 1.0f;
|
||||||
playPos = 0;
|
playPos = 0;
|
||||||
startPos = 0;
|
startPos = 0;
|
||||||
|
@ -261,7 +260,6 @@ void DirectSoundPlayer::Play(int64_t start,int64_t count) {
|
||||||
// Play
|
// Play
|
||||||
buffer->SetCurrentPosition(0);
|
buffer->SetCurrentPosition(0);
|
||||||
res = buffer->Play(0,0,play_flag);
|
res = buffer->Play(0,0,play_flag);
|
||||||
if (SUCCEEDED(res)) playing = true;
|
|
||||||
startTime = GetTickCount();
|
startTime = GetTickCount();
|
||||||
|
|
||||||
// Update timer
|
// Update timer
|
||||||
|
@ -276,18 +274,15 @@ void DirectSoundPlayer::Stop(bool timerToo) {
|
||||||
if (thread) {
|
if (thread) {
|
||||||
if (thread->IsAlive()) {
|
if (thread->IsAlive()) {
|
||||||
thread->Stop();
|
thread->Stop();
|
||||||
// The thread is detached so it kills/deletes itself after being stopped
|
|
||||||
//thread->Wait();
|
|
||||||
}
|
}
|
||||||
thread = NULL;
|
thread = NULL;
|
||||||
}
|
}
|
||||||
// The thread is now guaranteed dead and there are no concurrency problems to worry about
|
// The thread should now stop filling the buffer soon enough that we can safely stop the buffer
|
||||||
|
// and possibly start having soomething else filling it soon.
|
||||||
|
|
||||||
// Stop
|
if (buffer) buffer->Stop();
|
||||||
if (buffer) buffer->Stop(); // the thread should have done this already
|
|
||||||
|
|
||||||
// Reset variables
|
// Reset variables
|
||||||
playing = false;
|
|
||||||
playPos = 0;
|
playPos = 0;
|
||||||
startPos = 0;
|
startPos = 0;
|
||||||
endPos = 0;
|
endPos = 0;
|
||||||
|
@ -303,7 +298,7 @@ void DirectSoundPlayer::Stop(bool timerToo) {
|
||||||
///////////
|
///////////
|
||||||
// Set end
|
// Set end
|
||||||
void DirectSoundPlayer::SetEndPosition(int64_t pos) {
|
void DirectSoundPlayer::SetEndPosition(int64_t pos) {
|
||||||
if (playing) endPos = pos;
|
if (IsPlaying()) endPos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -318,8 +313,8 @@ void DirectSoundPlayer::SetCurrentPosition(int64_t pos) {
|
||||||
////////////////////////
|
////////////////////////
|
||||||
// Get current position
|
// Get current position
|
||||||
int64_t DirectSoundPlayer::GetCurrentPosition() {
|
int64_t DirectSoundPlayer::GetCurrentPosition() {
|
||||||
// Check if buffer is loaded
|
// Assume position 0 if we aren't playing currently
|
||||||
if (!buffer || !playing) return 0;
|
if (!IsPlaying()) return 0;
|
||||||
|
|
||||||
// FIXME: this should be based on not duration played but actual sample being heard
|
// FIXME: this should be based on not duration played but actual sample being heard
|
||||||
// (during vidoeo playback, cur_frame might get changed to resync)
|
// (during vidoeo playback, cur_frame might get changed to resync)
|
||||||
|
@ -329,6 +324,20 @@ int64_t DirectSoundPlayer::GetCurrentPosition() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////
|
||||||
|
// Check whether the buffer is playing
|
||||||
|
bool DirectSoundPlayer::IsPlaying() {
|
||||||
|
if (!buffer) return false;
|
||||||
|
|
||||||
|
DWORD status = 0;
|
||||||
|
if (SUCCEEDED(buffer->GetStatus(&status))) {
|
||||||
|
return !!(status & DSBSTATUS_PLAYING);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//////////////////////
|
//////////////////////
|
||||||
// Thread constructor
|
// Thread constructor
|
||||||
DirectSoundPlayerThread::DirectSoundPlayerThread(DirectSoundPlayer *par) : wxThread(wxTHREAD_DETACHED) {
|
DirectSoundPlayerThread::DirectSoundPlayerThread(DirectSoundPlayer *par) : wxThread(wxTHREAD_DETACHED) {
|
||||||
|
@ -350,6 +359,10 @@ wxThread::ExitCode DirectSoundPlayerThread::Entry() {
|
||||||
// Every thread that uses COM must initialise it
|
// Every thread that uses COM must initialise it
|
||||||
CoInitialize(0);
|
CoInitialize(0);
|
||||||
|
|
||||||
|
// Make sure we have the buffer available as long as we're alive
|
||||||
|
parent->buffer->AddRef();
|
||||||
|
IDirectSoundBuffer8 *buffer = parent->buffer;
|
||||||
|
|
||||||
// Wake up thread every half second to fill buffer as needed
|
// Wake up thread every half second to fill buffer as needed
|
||||||
// This more or less assumes the buffer is at least one second long
|
// This more or less assumes the buffer is at least one second long
|
||||||
while (WaitForSingleObject(stopnotify, 50) == WAIT_TIMEOUT) {
|
while (WaitForSingleObject(stopnotify, 50) == WAIT_TIMEOUT) {
|
||||||
|
@ -367,18 +380,18 @@ wxThread::ExitCode DirectSoundPlayerThread::Entry() {
|
||||||
DWORD size1, size2;
|
DWORD size1, size2;
|
||||||
DWORD playpos;
|
DWORD playpos;
|
||||||
HRESULT res;
|
HRESULT res;
|
||||||
res = parent->buffer->GetCurrentPosition(&playpos, NULL);
|
res = buffer->GetCurrentPosition(&playpos, NULL);
|
||||||
if (FAILED(res)) break;
|
if (FAILED(res)) break;
|
||||||
int toWrite = playpos - parent->offset;
|
int toWrite = playpos - parent->offset;
|
||||||
while (toWrite < 0) toWrite += parent->bufSize;
|
while (toWrite < 0) toWrite += parent->bufSize;
|
||||||
res = parent->buffer->Lock(parent->offset, toWrite, &buf1, &size1, &buf2, &size2, 0);
|
res = buffer->Lock(parent->offset, toWrite, &buf1, &size1, &buf2, &size2, 0);
|
||||||
if (FAILED(res)) break;
|
if (FAILED(res)) break;
|
||||||
if (size1) memset(buf1, 0, size1);
|
if (size1) memset(buf1, 0, size1);
|
||||||
if (size2) memset(buf2, 0, size2);
|
if (size2) memset(buf2, 0, size2);
|
||||||
if (size1) wxLogDebug(_T("DS blnk: %05ld -> %05ld"), (unsigned long)parent->playPos+bytesFilled, (unsigned long)parent->playPos+bytesFilled+size1);
|
if (size1) wxLogDebug(_T("DS blnk: %05ld -> %05ld"), (unsigned long)parent->playPos+bytesFilled, (unsigned long)parent->playPos+bytesFilled+size1);
|
||||||
if (size2) wxLogDebug(_T("DS blnk: %05ld => %05ld"), (unsigned long)parent->playPos+bytesFilled+size1, (unsigned long)parent->playPos+bytesFilled+size1+size2);
|
if (size2) wxLogDebug(_T("DS blnk: %05ld => %05ld"), (unsigned long)parent->playPos+bytesFilled+size1, (unsigned long)parent->playPos+bytesFilled+size1+size2);
|
||||||
bytesFilled += size1 + size2;
|
bytesFilled += size1 + size2;
|
||||||
parent->buffer->Unlock(buf1, size1, buf2, size2);
|
buffer->Unlock(buf1, size1, buf2, size2);
|
||||||
if (bytesFilled > parent->bufSize) break;
|
if (bytesFilled > parent->bufSize) break;
|
||||||
parent->offset = (parent->offset + size1 + size2) % parent->bufSize;
|
parent->offset = (parent->offset + size1 + size2) % parent->bufSize;
|
||||||
}
|
}
|
||||||
|
@ -387,8 +400,8 @@ wxThread::ExitCode DirectSoundPlayerThread::Entry() {
|
||||||
|
|
||||||
wxLogDebug(_T("DS thread dead"));
|
wxLogDebug(_T("DS thread dead"));
|
||||||
|
|
||||||
parent->playing = false;
|
// Our owner is responsible for stopping the buffer
|
||||||
parent->buffer->Stop();
|
buffer->Release();
|
||||||
|
|
||||||
// And un-init COM since we inited it
|
// And un-init COM since we inited it
|
||||||
CoUninitialize();
|
CoUninitialize();
|
||||||
|
|
|
@ -90,7 +90,6 @@ class DirectSoundPlayer : public AudioPlayer {
|
||||||
friend class DirectSoundPlayerThread;
|
friend class DirectSoundPlayerThread;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
volatile bool playing;
|
|
||||||
float volume;
|
float volume;
|
||||||
int offset;
|
int offset;
|
||||||
DWORD bufSize;
|
DWORD bufSize;
|
||||||
|
@ -116,7 +115,7 @@ public:
|
||||||
|
|
||||||
void Play(int64_t start,int64_t count);
|
void Play(int64_t start,int64_t count);
|
||||||
void Stop(bool timerToo=true);
|
void Stop(bool timerToo=true);
|
||||||
bool IsPlaying() { return playing; }
|
bool IsPlaying();
|
||||||
|
|
||||||
int64_t GetStartPosition() { return startPos; }
|
int64_t GetStartPosition() { return startPos; }
|
||||||
int64_t GetEndPosition() { return endPos; }
|
int64_t GetEndPosition() { return endPos; }
|
||||||
|
|
Loading…
Reference in New Issue