2011-06-12 02:45:02 +02:00
|
|
|
// Copyright (c) 2011, Niels Martin Hansen
|
2007-04-22 23:59:35 +02:00
|
|
|
// 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_alsa.cpp
|
|
|
|
/// @brief ALSA-based audio output
|
|
|
|
/// @ingroup audio_output
|
|
|
|
///
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_ALSA
|
2014-03-24 15:54:22 +01:00
|
|
|
#include "include/aegisub/audio_player.h"
|
2012-01-08 02:33:39 +01:00
|
|
|
|
|
|
|
#include "audio_controller.h"
|
2010-05-24 03:46:04 +02:00
|
|
|
#include "compat.h"
|
2007-04-22 23:59:35 +02:00
|
|
|
#include "frame_main.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2011-11-07 06:24:46 +01:00
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
#include <libaegisub/audio/provider.h>
|
2013-10-24 22:17:53 +02:00
|
|
|
#include <libaegisub/log.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2013-10-24 22:17:53 +02:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
#include <atomic>
|
2011-06-12 02:45:02 +02:00
|
|
|
#include <algorithm>
|
2014-05-23 05:13:08 +02:00
|
|
|
#include <boost/scope_exit.hpp>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
2014-03-24 15:54:22 +01:00
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#include <memory>
|
2014-05-23 05:13:08 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
// X11 is the bestest
|
|
|
|
#undef None
|
2014-03-24 15:54:22 +01:00
|
|
|
|
|
|
|
namespace {
|
2014-05-23 05:13:08 +02:00
|
|
|
enum class Message {
|
|
|
|
None,
|
|
|
|
Start,
|
|
|
|
Stop,
|
|
|
|
Close
|
|
|
|
};
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 20:37:51 +02:00
|
|
|
using clock = std::chrono::steady_clock;
|
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
class AlsaPlayer final : public AudioPlayer {
|
|
|
|
std::mutex mutex;
|
|
|
|
std::condition_variable cond;
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
std::string device_name = OPT_GET("Player/Audio/ALSA/Device")->GetString();
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
Message message = Message::None;
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
std::atomic<bool> playing{false};
|
|
|
|
std::atomic<double> volume{1.0};
|
|
|
|
int64_t start_position = 0;
|
|
|
|
std::atomic<int64_t> end_position{0};
|
2022-08-18 22:44:19 +02:00
|
|
|
bool fallback_mono16 = false; // whether to convert to 16 bit mono. FIXME: more flexible conversion
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 20:37:51 +02:00
|
|
|
std::mutex position_mutex;
|
2014-03-24 15:54:22 +01:00
|
|
|
int64_t last_position = 0;
|
2014-05-23 20:37:51 +02:00
|
|
|
clock::time_point last_position_time;
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 18:19:19 +02:00
|
|
|
std::vector<char> decode_buffer;
|
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
std::thread thread;
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2022-08-18 22:44:19 +02:00
|
|
|
snd_pcm_format_t GetPCMFormat(const agi::AudioProvider *provider);
|
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
void PlaybackThread();
|
2014-03-24 15:54:22 +01:00
|
|
|
|
2014-05-23 20:37:51 +02:00
|
|
|
void UpdatePlaybackPosition(snd_pcm_t *pcm, int64_t position)
|
|
|
|
{
|
|
|
|
snd_pcm_sframes_t delay;
|
|
|
|
if (snd_pcm_delay(pcm, &delay) == 0)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> playback_lock;
|
|
|
|
last_position = position - delay;
|
|
|
|
last_position_time = clock::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 15:54:22 +01:00
|
|
|
public:
|
2014-07-09 16:22:49 +02:00
|
|
|
AlsaPlayer(agi::AudioProvider *provider);
|
2014-03-24 15:54:22 +01:00
|
|
|
~AlsaPlayer();
|
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
void Play(int64_t start, int64_t count) override;
|
|
|
|
void Stop() override;
|
|
|
|
bool IsPlaying() override { return playing; }
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
void SetVolume(double vol) override { volume = vol; }
|
|
|
|
int64_t GetEndPosition() override { return end_position; }
|
|
|
|
int64_t GetCurrentPosition() override;
|
|
|
|
void SetEndPosition(int64_t pos) override;
|
2011-06-12 02:45:02 +02:00
|
|
|
};
|
2007-04-26 20:34:36 +02:00
|
|
|
|
2022-08-18 22:44:19 +02:00
|
|
|
snd_pcm_format_t AlsaPlayer::GetPCMFormat(const agi::AudioProvider *provider) {
|
|
|
|
if (provider->AreSamplesFloat()) {
|
|
|
|
switch (provider->GetBytesPerSample()) {
|
|
|
|
case 4:
|
|
|
|
return SND_PCM_FORMAT_FLOAT_LE;
|
|
|
|
case 8:
|
|
|
|
return SND_PCM_FORMAT_FLOAT64_LE;
|
|
|
|
default:
|
|
|
|
fallback_mono16 = true;
|
|
|
|
return SND_PCM_FORMAT_S16_LE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (provider->GetBytesPerSample()) {
|
|
|
|
case 1:
|
|
|
|
return SND_PCM_FORMAT_U8;
|
|
|
|
case 2:
|
|
|
|
return SND_PCM_FORMAT_S16_LE;
|
|
|
|
case 3:
|
|
|
|
return SND_PCM_FORMAT_S24_LE;
|
|
|
|
case 4:
|
|
|
|
return SND_PCM_FORMAT_S32_LE;
|
|
|
|
case 8:
|
|
|
|
return SND_PCM_FORMAT_S32_LE;
|
|
|
|
default:
|
|
|
|
fallback_mono16 = true;
|
|
|
|
return SND_PCM_FORMAT_S16_LE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
void AlsaPlayer::PlaybackThread()
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
snd_pcm_t *pcm = nullptr;
|
|
|
|
if (snd_pcm_open(&pcm, device_name.c_str(), SND_PCM_STREAM_PLAYBACK, 0) != 0)
|
|
|
|
return;
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "opened pcm";
|
2014-05-23 05:13:08 +02:00
|
|
|
BOOST_SCOPE_EXIT_ALL(&) { snd_pcm_close(pcm); };
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
do_setup:
|
2022-08-18 22:44:19 +02:00
|
|
|
snd_pcm_format_t pcm_format = GetPCMFormat(provider);
|
2011-06-12 02:45:02 +02:00
|
|
|
if (snd_pcm_set_params(pcm,
|
|
|
|
pcm_format,
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED,
|
2022-08-18 22:44:19 +02:00
|
|
|
fallback_mono16 ? 1 : provider->GetChannels(),
|
2014-05-23 05:13:08 +02:00
|
|
|
provider->GetSampleRate(),
|
2011-06-12 02:45:02 +02:00
|
|
|
1, // allow resample
|
|
|
|
100*1000 // 100 milliseconds latency
|
|
|
|
) != 0)
|
2014-05-23 05:13:08 +02:00
|
|
|
return;
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "set pcm params";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2022-08-18 22:44:19 +02:00
|
|
|
size_t framesize = fallback_mono16 ? sizeof(int16_t) : provider->GetChannels() * provider->GetBytesPerSample();
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
while (true)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
|
|
|
// Wait for condition to trigger
|
2014-05-23 05:13:08 +02:00
|
|
|
while (message != Message::Start)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
cond.wait(lock, [&] { return message != Message::None; });
|
|
|
|
if (message == Message::Close)
|
|
|
|
return;
|
|
|
|
if (message == Message::Start && end_position > start_position)
|
|
|
|
break;
|
|
|
|
// Not playing, so don't need to stop...
|
|
|
|
message = Message::None;
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2014-05-23 05:13:08 +02:00
|
|
|
message = Message::None;
|
2011-06-12 02:45:02 +02:00
|
|
|
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "starting playback";
|
2014-05-23 05:13:08 +02:00
|
|
|
int64_t position = start_position;
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
// Initial buffer-fill
|
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
auto avail = std::min(snd_pcm_avail(pcm), (snd_pcm_sframes_t)(end_position-position));
|
2014-05-23 18:19:19 +02:00
|
|
|
decode_buffer.resize(avail * framesize);
|
2022-08-18 22:44:19 +02:00
|
|
|
if (fallback_mono16) {
|
|
|
|
provider->GetInt16MonoAudioWithVolume(reinterpret_cast<int16_t*>(decode_buffer.data()), position, avail, volume);
|
|
|
|
} else {
|
|
|
|
provider->GetAudioWithVolume(decode_buffer.data(), position, avail, volume);
|
|
|
|
}
|
2014-05-23 20:37:51 +02:00
|
|
|
|
2014-05-23 04:51:55 +02:00
|
|
|
snd_pcm_sframes_t written = 0;
|
|
|
|
while (written <= 0)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2014-05-23 18:19:19 +02:00
|
|
|
written = snd_pcm_writei(pcm, decode_buffer.data(), avail);
|
2014-05-23 04:51:55 +02:00
|
|
|
if (written == -ESTRPIPE)
|
|
|
|
snd_pcm_recover(pcm, written, 0);
|
|
|
|
else if (written <= 0)
|
|
|
|
{
|
|
|
|
LOG_D("audio/player/alsa") << "error filling buffer";
|
2014-05-23 05:13:08 +02:00
|
|
|
return;
|
2014-05-23 04:51:55 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2014-05-23 04:51:55 +02:00
|
|
|
position += written;
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start playback
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "initial buffer filled, hitting start";
|
2011-06-12 02:45:02 +02:00
|
|
|
snd_pcm_start(pcm);
|
|
|
|
|
2014-05-23 20:37:51 +02:00
|
|
|
UpdatePlaybackPosition(pcm, position);
|
2014-05-23 05:13:08 +02:00
|
|
|
playing = true;
|
|
|
|
BOOST_SCOPE_EXIT_ALL(&) { playing = false; };
|
|
|
|
while (true)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
|
|
|
// Sleep a bit, or until an event
|
2014-05-23 05:13:08 +02:00
|
|
|
cond.wait_for(lock, std::chrono::milliseconds{25});
|
|
|
|
|
|
|
|
if (message == Message::Close)
|
|
|
|
{
|
|
|
|
snd_pcm_drop(pcm);
|
|
|
|
return;
|
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
// Check for stop signal
|
2014-05-23 05:13:08 +02:00
|
|
|
if (message == Message::Stop || message == Message::Start)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "playback loop, stop signal";
|
2011-06-12 02:45:02 +02:00
|
|
|
snd_pcm_drop(pcm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill buffer
|
2012-10-15 06:37:14 +02:00
|
|
|
snd_pcm_sframes_t tmp_pcm_avail = snd_pcm_avail(pcm);
|
2012-05-15 16:06:39 +02:00
|
|
|
if (tmp_pcm_avail == -EPIPE)
|
2012-02-20 19:22:43 +01:00
|
|
|
{
|
|
|
|
if (snd_pcm_recover(pcm, -EPIPE, 1) < 0)
|
|
|
|
{
|
|
|
|
LOG_D("audio/player/alsa") << "failed to recover from underrun";
|
2014-05-23 05:13:08 +02:00
|
|
|
return;
|
2012-02-20 19:22:43 +01:00
|
|
|
}
|
2012-05-15 16:06:39 +02:00
|
|
|
tmp_pcm_avail = snd_pcm_avail(pcm);
|
|
|
|
}
|
2014-05-23 05:13:08 +02:00
|
|
|
auto avail = std::min(tmp_pcm_avail, (snd_pcm_sframes_t)(end_position-position));
|
2012-05-15 16:06:39 +02:00
|
|
|
if (avail < 0)
|
|
|
|
continue;
|
2014-05-23 04:51:55 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2014-05-23 18:19:19 +02:00
|
|
|
decode_buffer.resize(avail * framesize);
|
2022-08-18 22:44:19 +02:00
|
|
|
if (fallback_mono16) {
|
|
|
|
provider->GetInt16MonoAudioWithVolume(reinterpret_cast<int16_t*>(decode_buffer.data()), position, avail, volume);
|
|
|
|
} else {
|
|
|
|
provider->GetAudioWithVolume(decode_buffer.data(), position, avail, volume);
|
|
|
|
}
|
2014-05-23 04:51:55 +02:00
|
|
|
snd_pcm_sframes_t written = 0;
|
|
|
|
while (written <= 0)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2014-05-23 18:19:19 +02:00
|
|
|
written = snd_pcm_writei(pcm, decode_buffer.data(), avail);
|
2014-05-23 04:51:55 +02:00
|
|
|
if (written == -ESTRPIPE || written == -EPIPE)
|
|
|
|
snd_pcm_recover(pcm, written, 0);
|
|
|
|
else if (written == 0)
|
|
|
|
break;
|
|
|
|
else if (written < 0)
|
|
|
|
{
|
|
|
|
LOG_D("audio/player/alsa") << "error filling buffer, written=" << written;
|
2014-05-23 05:13:08 +02:00
|
|
|
return;
|
2014-05-23 04:51:55 +02:00
|
|
|
}
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2014-05-23 04:51:55 +02:00
|
|
|
position += written;
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
|
|
|
|
2014-05-23 20:37:51 +02:00
|
|
|
UpdatePlaybackPosition(pcm, position);
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
// Check for end of playback
|
2014-05-23 05:13:08 +02:00
|
|
|
if (position >= end_position)
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "playback loop, past end, draining";
|
2011-06-12 02:45:02 +02:00
|
|
|
snd_pcm_drain(pcm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-05-23 05:13:08 +02:00
|
|
|
|
|
|
|
playing = false;
|
2012-02-20 19:22:37 +01:00
|
|
|
LOG_D("audio/player/alsa") << "out of playback loop";
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
switch (snd_pcm_state(pcm))
|
|
|
|
{
|
|
|
|
case SND_PCM_STATE_OPEN:
|
|
|
|
// no clue what could have happened here, but start over
|
|
|
|
goto do_setup;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_SETUP:
|
|
|
|
// we lost the preparedness?
|
|
|
|
snd_pcm_prepare(pcm);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_DISCONNECTED:
|
|
|
|
// lost device, close the handle and return error
|
2014-05-23 05:13:08 +02:00
|
|
|
return;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
default:
|
|
|
|
// everything else should either be fine or impossible (here)
|
|
|
|
break;
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
AlsaPlayer::AlsaPlayer(agi::AudioProvider *provider) try
|
2012-03-20 00:31:33 +01:00
|
|
|
: AudioPlayer(provider)
|
2014-05-23 05:13:08 +02:00
|
|
|
, thread(&AlsaPlayer::PlaybackThread, this)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
}
|
|
|
|
catch (std::system_error const&) {
|
2014-07-09 16:22:49 +02:00
|
|
|
throw AudioPlayerOpenError("AlsaPlayer: Creating the playback thread failed");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2012-03-20 00:31:33 +01:00
|
|
|
AlsaPlayer::~AlsaPlayer()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2011-06-12 02:45:02 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
message = Message::Close;
|
|
|
|
cond.notify_all();
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2014-05-23 05:13:08 +02:00
|
|
|
thread.join();
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2011-06-12 02:45:02 +02:00
|
|
|
void AlsaPlayer::Play(int64_t start, int64_t count)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
message = Message::Start;
|
|
|
|
start_position = start;
|
|
|
|
end_position = start + count;
|
|
|
|
cond.notify_all();
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2012-03-25 06:05:44 +02:00
|
|
|
void AlsaPlayer::Stop()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
message = Message::Stop;
|
|
|
|
cond.notify_all();
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
void AlsaPlayer::SetEndPosition(int64_t pos)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2014-05-23 05:13:08 +02:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
end_position = pos;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t AlsaPlayer::GetCurrentPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2011-06-12 02:45:02 +02:00
|
|
|
int64_t lastpos;
|
2014-05-23 20:37:51 +02:00
|
|
|
clock::time_point lasttime;
|
2011-06-12 02:45:02 +02:00
|
|
|
|
|
|
|
{
|
2014-05-23 20:37:51 +02:00
|
|
|
std::unique_lock<std::mutex> playback_lock;
|
2014-05-23 05:13:08 +02:00
|
|
|
lastpos = last_position;
|
|
|
|
lasttime = last_position_time;
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
|
|
|
|
2014-05-23 20:37:51 +02:00
|
|
|
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(clock::now() - lasttime).count();
|
|
|
|
return lastpos + ms * provider->GetSampleRate() / 1000;
|
2014-03-24 15:54:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 16:22:49 +02:00
|
|
|
std::unique_ptr<AudioPlayer> CreateAlsaPlayer(agi::AudioProvider *provider, wxWindow *)
|
2014-03-24 15:54:22 +01:00
|
|
|
{
|
2014-04-23 22:53:24 +02:00
|
|
|
return agi::make_unique<AlsaPlayer>(provider);
|
2011-06-12 02:45:02 +02:00
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2022-08-18 22:44:19 +02:00
|
|
|
#endif // WITH_ALSA
|