2006-12-23 03:31:34 +01:00
|
|
|
// Copyright (c) 2006, Rodrigo Braz Monteiro
|
|
|
|
// 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_dsound.cpp
|
|
|
|
/// @brief Old DirectSound-based audio output
|
|
|
|
/// @ingroup audio_output
|
|
|
|
///
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2007-12-31 07:46:22 +01:00
|
|
|
#ifdef WITH_DIRECTSOUND
|
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
#include <libaegisub/log.h>
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
#include "audio_controller.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_player_dsound.h"
|
|
|
|
#include "frame_main.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "utils.h"
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
DirectSoundPlayer::DirectSoundPlayer(AudioProvider *provider)
|
|
|
|
: AudioPlayer(provider)
|
|
|
|
, playing(false)
|
|
|
|
, volume(1.0f)
|
|
|
|
, offset(0)
|
|
|
|
, bufSize(0)
|
|
|
|
, playPos(0)
|
|
|
|
, startPos(0)
|
|
|
|
, endPos(0)
|
|
|
|
, startTime(0)
|
|
|
|
, directSound(0)
|
|
|
|
, buffer(0)
|
|
|
|
, thread(0)
|
|
|
|
{
|
2006-12-23 03:31:34 +01:00
|
|
|
// Initialize the DirectSound object
|
|
|
|
HRESULT res;
|
2012-11-13 17:51:01 +01:00
|
|
|
res = DirectSoundCreate8(&DSDEVID_DefaultPlayback,&directSound,nullptr); // TODO: support selecting audio device
|
2012-01-08 02:33:39 +01:00
|
|
|
if (FAILED(res)) throw agi::AudioPlayerOpenError("Failed initializing DirectSound", 0);
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Set DirectSound parameters
|
2011-12-22 22:17:40 +01:00
|
|
|
directSound->SetCooperativeLevel((HWND)wxGetApp().frame->GetHandle(),DSSCL_PRIORITY);
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Create the wave format structure
|
|
|
|
WAVEFORMATEX waveFormat;
|
|
|
|
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
waveFormat.nSamplesPerSec = provider->GetSampleRate();
|
|
|
|
waveFormat.nChannels = provider->GetChannels();
|
|
|
|
waveFormat.wBitsPerSample = provider->GetBytesPerSample() * 8;
|
|
|
|
waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
|
|
|
|
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
|
2007-10-18 04:27:55 +02:00
|
|
|
waveFormat.cbSize = sizeof(waveFormat);
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Create the buffer initializer
|
2007-07-28 15:59:32 +02:00
|
|
|
int aim = waveFormat.nAvgBytesPerSec * 15/100; // 150 ms buffer
|
2006-12-23 03:31:34 +01:00
|
|
|
int min = DSBSIZE_MIN;
|
|
|
|
int max = DSBSIZE_MAX;
|
2010-12-31 22:03:03 +01:00
|
|
|
bufSize = std::min(std::max(min,aim),max);
|
2006-12-23 03:31:34 +01:00
|
|
|
DSBUFFERDESC desc;
|
|
|
|
desc.dwSize = sizeof(DSBUFFERDESC);
|
2007-10-18 04:27:55 +02:00
|
|
|
desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
|
2006-12-23 03:31:34 +01:00
|
|
|
desc.dwBufferBytes = bufSize;
|
|
|
|
desc.dwReserved = 0;
|
|
|
|
desc.lpwfxFormat = &waveFormat;
|
|
|
|
desc.guid3DAlgorithm = GUID_NULL;
|
|
|
|
|
|
|
|
// Create the buffer
|
|
|
|
IDirectSoundBuffer *buf;
|
2012-11-13 17:51:01 +01:00
|
|
|
res = directSound->CreateSoundBuffer(&desc,&buf,nullptr);
|
2012-01-08 02:33:39 +01:00
|
|
|
if (res != DS_OK) throw agi::AudioPlayerOpenError("Failed creating DirectSound buffer", 0);
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Copy interface to buffer
|
|
|
|
res = buf->QueryInterface(IID_IDirectSoundBuffer8,(LPVOID*) &buffer);
|
2012-01-08 02:33:39 +01:00
|
|
|
if (res != S_OK) throw agi::AudioPlayerOpenError("Failed casting interface to IDirectSoundBuffer8", 0);
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Set data
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
DirectSoundPlayer::~DirectSoundPlayer() {
|
2006-12-23 03:31:34 +01:00
|
|
|
Stop();
|
|
|
|
|
2007-04-20 01:38:54 +02:00
|
|
|
// Unref the DirectSound buffer
|
|
|
|
if (buffer) {
|
|
|
|
buffer->Release();
|
2012-11-13 17:51:01 +01:00
|
|
|
buffer = nullptr;
|
2007-04-20 01:38:54 +02:00
|
|
|
}
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2007-04-20 01:38:54 +02:00
|
|
|
// Unref the DirectSound object
|
|
|
|
if (directSound) {
|
|
|
|
directSound->Release();
|
2012-11-13 17:51:01 +01:00
|
|
|
directSound = nullptr;
|
2007-04-20 01:38:54 +02:00
|
|
|
}
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Fill buffer
|
|
|
|
/// @param fill
|
|
|
|
/// @return
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2007-04-20 18:27:18 +02:00
|
|
|
bool DirectSoundPlayer::FillBuffer(bool fill) {
|
2007-04-20 01:38:54 +02:00
|
|
|
if (playPos >= endPos) return false;
|
|
|
|
|
2006-12-23 03:31:34 +01:00
|
|
|
// Variables
|
2007-04-20 18:27:18 +02:00
|
|
|
HRESULT res;
|
2006-12-23 03:31:34 +01:00
|
|
|
void *ptr1, *ptr2;
|
|
|
|
unsigned long int size1, size2;
|
|
|
|
int bytesps = provider->GetBytesPerSample();
|
|
|
|
|
|
|
|
// To write length
|
2007-04-20 18:27:18 +02:00
|
|
|
int toWrite = 0;
|
|
|
|
if (fill) {
|
|
|
|
toWrite = bufSize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DWORD bufplay;
|
2012-11-13 17:51:01 +01:00
|
|
|
res = buffer->GetCurrentPosition(&bufplay, nullptr);
|
2007-04-20 18:27:18 +02:00
|
|
|
if (FAILED(res)) return false;
|
|
|
|
toWrite = (int)bufplay - (int)offset;
|
|
|
|
if (toWrite < 0) toWrite += bufSize;
|
|
|
|
}
|
|
|
|
if (toWrite == 0) return true;
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2007-04-25 02:35:10 +02:00
|
|
|
// Make sure we only get as many samples as are available
|
|
|
|
if (playPos + toWrite/bytesps > endPos) {
|
|
|
|
toWrite = (endPos - playPos) * bytesps;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're going to fill the entire buffer (ie. at start of playback) start by zeroing it out
|
2007-07-28 15:59:32 +02:00
|
|
|
// If it's not zeroed out we might have a playback selection shorter than the buffer
|
|
|
|
// and then everything after the playback selection will be junk, which we don't want played.
|
2007-04-25 02:35:10 +02:00
|
|
|
if (fill) {
|
|
|
|
RetryClear:
|
|
|
|
res = buffer->Lock(0, bufSize, &ptr1, &size1, &ptr2, &size2, 0);
|
|
|
|
if (res == DSERR_BUFFERLOST) {
|
|
|
|
buffer->Restore();
|
|
|
|
goto RetryClear;
|
|
|
|
}
|
|
|
|
memset(ptr1, 0, size1);
|
|
|
|
memset(ptr2, 0, size2);
|
|
|
|
buffer->Unlock(ptr1, size1, ptr2, size2);
|
|
|
|
}
|
|
|
|
|
2006-12-23 03:31:34 +01:00
|
|
|
// Lock buffer
|
2007-04-20 18:27:18 +02:00
|
|
|
RetryLock:
|
|
|
|
if (fill) {
|
|
|
|
res = buffer->Lock(offset, toWrite, &ptr1, &size1, &ptr2, &size2, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = buffer->Lock(offset, toWrite, &ptr1, &size1, &ptr2, &size2, 0);//DSBLOCK_FROMWRITECURSOR);
|
|
|
|
}
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Buffer lost?
|
|
|
|
if (res == DSERR_BUFFERLOST) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("audio/player/dsound1") << "lost dsound buffer";
|
2006-12-23 03:31:34 +01:00
|
|
|
buffer->Restore();
|
2007-04-20 18:27:18 +02:00
|
|
|
goto RetryLock;
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error
|
2007-04-20 01:38:54 +02:00
|
|
|
if (FAILED(res)) return false;
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Convert size to number of samples
|
|
|
|
unsigned long int count1 = size1 / bytesps;
|
|
|
|
unsigned long int count2 = size2 / bytesps;
|
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D_IF(count1, "audio/player/dsound1") << "DS fill: " << (unsigned long)playPos << " -> " << (unsigned long)playPos+count1;
|
|
|
|
LOG_D_IF(count2, "audio/player/dsound1") << "DS fill: " << (unsigned long)playPos+count1 << " -> " << (unsigned long)playPos+count1+count2;
|
|
|
|
LOG_D_IF(!count1 && !count2, "audio/player/dsound1") << "DS fill: nothing";
|
2007-07-28 15:59:32 +02:00
|
|
|
|
2006-12-23 03:31:34 +01:00
|
|
|
// Get source wave
|
2007-04-20 18:27:18 +02:00
|
|
|
if (count1) provider->GetAudioWithVolume(ptr1, playPos, count1, volume);
|
|
|
|
if (count2) provider->GetAudioWithVolume(ptr2, playPos+count1, count2, volume);
|
|
|
|
playPos += count1+count2;
|
|
|
|
|
2006-12-23 03:31:34 +01:00
|
|
|
// Unlock
|
2007-04-20 18:27:18 +02:00
|
|
|
buffer->Unlock(ptr1,count1*bytesps,ptr2,count2*bytesps);
|
2007-04-20 01:38:54 +02:00
|
|
|
|
2007-04-22 18:03:28 +02:00
|
|
|
// Update offset
|
|
|
|
offset = (offset + count1*bytesps + count2*bytesps) % bufSize;
|
|
|
|
|
2007-07-28 15:59:32 +02:00
|
|
|
return playPos < endPos;
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Play
|
|
|
|
/// @param start
|
|
|
|
/// @param count
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
void DirectSoundPlayer::Play(int64_t start,int64_t count) {
|
2006-12-23 03:31:34 +01:00
|
|
|
// Make sure that it's stopped
|
|
|
|
Stop();
|
2007-04-20 01:38:54 +02:00
|
|
|
// The thread is now guaranteed dead
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2007-04-20 18:27:18 +02:00
|
|
|
HRESULT res;
|
|
|
|
|
2007-04-20 01:38:54 +02:00
|
|
|
// We sure better have a buffer
|
|
|
|
assert(buffer);
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2007-01-05 19:27:15 +01:00
|
|
|
// Set variables
|
|
|
|
startPos = start;
|
|
|
|
endPos = start+count;
|
|
|
|
playPos = start;
|
|
|
|
offset = 0;
|
|
|
|
|
2007-04-20 18:27:18 +02:00
|
|
|
// Fill whole buffer
|
|
|
|
FillBuffer(true);
|
2007-04-20 01:38:54 +02:00
|
|
|
|
2007-07-29 23:00:57 +02:00
|
|
|
DWORD play_flag = 0;
|
2007-10-18 02:33:07 +02:00
|
|
|
if (count*provider->GetBytesPerSample() > bufSize) {
|
2007-07-29 23:00:57 +02:00
|
|
|
// Start thread
|
|
|
|
thread = new DirectSoundPlayerThread(this);
|
|
|
|
thread->Create();
|
|
|
|
thread->Run();
|
|
|
|
play_flag = DSBPLAY_LOOPING;
|
|
|
|
}
|
2006-12-23 03:31:34 +01:00
|
|
|
|
|
|
|
// Play
|
|
|
|
buffer->SetCurrentPosition(0);
|
2007-07-29 23:00:57 +02:00
|
|
|
res = buffer->Play(0,0,play_flag);
|
2008-10-23 00:31:43 +02:00
|
|
|
if (SUCCEEDED(res)) playing = true;
|
2007-04-20 20:07:44 +02:00
|
|
|
startTime = GetTickCount();
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Stop
|
|
|
|
/// @param timerToo
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2012-03-25 06:05:44 +02:00
|
|
|
void DirectSoundPlayer::Stop() {
|
2007-01-05 19:27:15 +01:00
|
|
|
// Stop the thread
|
|
|
|
if (thread) {
|
2007-08-15 21:40:41 +02:00
|
|
|
if (thread->IsAlive()) {
|
|
|
|
thread->Stop();
|
2008-10-23 00:31:43 +02:00
|
|
|
thread->Wait();
|
2007-08-15 21:40:41 +02:00
|
|
|
}
|
2012-11-13 17:51:01 +01:00
|
|
|
thread = nullptr;
|
2007-01-05 19:27:15 +01:00
|
|
|
}
|
2008-10-23 00:31:43 +02:00
|
|
|
// The thread is now guaranteed dead and there are no concurrency problems to worry about
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2008-10-23 00:31:43 +02:00
|
|
|
// Stop
|
|
|
|
if (buffer) buffer->Stop(); // the thread should have done this already
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2007-01-05 19:27:15 +01:00
|
|
|
// Reset variables
|
2008-10-23 00:31:43 +02:00
|
|
|
playing = false;
|
2007-01-05 19:27:15 +01:00
|
|
|
playPos = 0;
|
|
|
|
startPos = 0;
|
|
|
|
endPos = 0;
|
|
|
|
offset = 0;
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Set end
|
|
|
|
/// @param pos
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
void DirectSoundPlayer::SetEndPosition(int64_t pos) {
|
2008-10-23 00:31:43 +02:00
|
|
|
if (playing) endPos = pos;
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Get current position
|
|
|
|
/// @return
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t DirectSoundPlayer::GetCurrentPosition() {
|
2008-10-23 00:31:43 +02:00
|
|
|
// Check if buffer is loaded
|
|
|
|
if (!buffer || !playing) return 0;
|
2006-12-23 03:31:34 +01:00
|
|
|
|
2007-05-11 00:39:17 +02:00
|
|
|
// FIXME: this should be based on not duration played but actual sample being heard
|
|
|
|
// (during vidoeo playback, cur_frame might get changed to resync)
|
2007-04-20 20:07:44 +02:00
|
|
|
DWORD curtime = GetTickCount();
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t tdiff = curtime - startTime;
|
2007-04-20 20:07:44 +02:00
|
|
|
return startPos + tdiff * provider->GetSampleRate() / 1000;
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Thread constructor
|
|
|
|
/// @param par
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2008-10-23 00:31:43 +02:00
|
|
|
DirectSoundPlayerThread::DirectSoundPlayerThread(DirectSoundPlayer *par) : wxThread(wxTHREAD_JOINABLE) {
|
2006-12-23 03:31:34 +01:00
|
|
|
parent = par;
|
2012-11-13 17:51:01 +01:00
|
|
|
stopnotify = CreateEvent(nullptr, true, false, nullptr);
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Thread destructor
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2006-12-23 03:31:34 +01:00
|
|
|
DirectSoundPlayerThread::~DirectSoundPlayerThread() {
|
2007-04-20 01:38:54 +02:00
|
|
|
CloseHandle(stopnotify);
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Thread entry point
|
|
|
|
/// @return
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2006-12-23 03:31:34 +01:00
|
|
|
wxThread::ExitCode DirectSoundPlayerThread::Entry() {
|
2008-01-21 21:56:39 +01:00
|
|
|
CoInitialize(0);
|
|
|
|
|
2007-04-20 18:27:18 +02:00
|
|
|
// Wake up thread every half second to fill buffer as needed
|
|
|
|
// This more or less assumes the buffer is at least one second long
|
2007-07-28 15:59:32 +02:00
|
|
|
while (WaitForSingleObject(stopnotify, 50) == WAIT_TIMEOUT) {
|
2007-04-20 20:07:44 +02:00
|
|
|
if (!parent->FillBuffer(false)) {
|
|
|
|
// FillBuffer returns false when end of stream is reached
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("audio/player/dsound1") << "DS thread hit end of stream";
|
2006-12-23 03:31:34 +01:00
|
|
|
break;
|
2007-04-20 20:07:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now fill buffer with silence
|
|
|
|
DWORD bytesFilled = 0;
|
2007-07-28 15:59:32 +02:00
|
|
|
while (WaitForSingleObject(stopnotify, 50) == WAIT_TIMEOUT) {
|
2007-04-20 20:07:44 +02:00
|
|
|
void *buf1, *buf2;
|
|
|
|
DWORD size1, size2;
|
|
|
|
DWORD playpos;
|
|
|
|
HRESULT res;
|
2012-11-13 17:51:01 +01:00
|
|
|
res = parent->buffer->GetCurrentPosition(&playpos, nullptr);
|
2007-04-20 20:07:44 +02:00
|
|
|
if (FAILED(res)) break;
|
2007-04-22 18:03:28 +02:00
|
|
|
int toWrite = playpos - parent->offset;
|
|
|
|
while (toWrite < 0) toWrite += parent->bufSize;
|
2008-10-23 00:31:43 +02:00
|
|
|
res = parent->buffer->Lock(parent->offset, toWrite, &buf1, &size1, &buf2, &size2, 0);
|
2007-04-20 20:07:44 +02:00
|
|
|
if (FAILED(res)) break;
|
|
|
|
if (size1) memset(buf1, 0, size1);
|
|
|
|
if (size2) memset(buf2, 0, size2);
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D_IF(size1, "audio/player/dsound1") << "DS blnk:" << (unsigned long)parent->playPos+bytesFilled << " -> " << (unsigned long)parent->playPos+bytesFilled+size1;
|
|
|
|
LOG_D_IF(size2, "audio/player/dsound1") << "DS blnk:" << (unsigned long)parent->playPos+bytesFilled+size1 << " -> " << (unsigned long)parent->playPos+bytesFilled+size1+size2;
|
2007-04-20 20:07:44 +02:00
|
|
|
bytesFilled += size1 + size2;
|
2008-10-23 00:31:43 +02:00
|
|
|
parent->buffer->Unlock(buf1, size1, buf2, size2);
|
2007-07-28 15:59:32 +02:00
|
|
|
if (bytesFilled > parent->bufSize) break;
|
2007-04-22 18:03:28 +02:00
|
|
|
parent->offset = (parent->offset + size1 + size2) % parent->bufSize;
|
2006-12-23 03:31:34 +01:00
|
|
|
}
|
|
|
|
|
2007-07-28 15:59:32 +02:00
|
|
|
WaitForSingleObject(stopnotify, 150);
|
2007-04-22 18:03:28 +02:00
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("audio/player/dsound1") << "DS thread dead";
|
2007-04-20 18:27:18 +02:00
|
|
|
|
2008-10-23 00:31:43 +02:00
|
|
|
parent->playing = false;
|
|
|
|
parent->buffer->Stop();
|
2008-01-21 21:56:39 +01:00
|
|
|
|
|
|
|
CoUninitialize();
|
2006-12-23 03:31:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2006-12-23 04:55:00 +01:00
|
|
|
|
2012-03-25 06:05:06 +02:00
|
|
|
/// @brief Stop playback thread
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
///
|
2007-04-20 01:38:54 +02:00
|
|
|
void DirectSoundPlayerThread::Stop() {
|
|
|
|
// Increase the stopnotify by one, causing a wait for it to succeed
|
2007-04-20 18:27:18 +02:00
|
|
|
SetEvent(stopnotify);
|
2007-04-20 01:38:54 +02:00
|
|
|
}
|
2007-12-31 07:46:22 +01:00
|
|
|
|
|
|
|
#endif // WITH_DIRECTSOUND
|
2009-07-29 07:43:02 +02:00
|
|
|
|