2007-04-22 23:59:35 +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
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Headers
|
|
|
|
#include <wx/wxprec.h>
|
|
|
|
#include "audio_player.h"
|
|
|
|
#include "audio_provider.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "frame_main.h"
|
|
|
|
#include "audio_player.h"
|
|
|
|
#include <alsa/asoundlib.h>
|
2007-04-23 19:01:00 +02:00
|
|
|
#include "options.h"
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
///////////////
|
|
|
|
// Alsa player
|
|
|
|
class AlsaPlayer : public AudioPlayer {
|
2007-04-22 23:59:35 +02:00
|
|
|
private:
|
2007-04-25 00:29:27 +02:00
|
|
|
bool open;
|
|
|
|
volatile bool playing;
|
|
|
|
volatile float volume;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
volatile unsigned long start_frame; // first frame of playback
|
|
|
|
volatile unsigned long cur_frame; // last written frame + 1
|
|
|
|
volatile unsigned long end_frame; // last frame to play
|
|
|
|
unsigned long bpf; // bytes per frame
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
AudioProvider *provider;
|
2007-04-22 23:59:35 +02:00
|
|
|
snd_pcm_t *pcm_handle; // device handle
|
|
|
|
snd_pcm_stream_t stream; // stream direction
|
2007-04-25 00:29:27 +02:00
|
|
|
snd_async_handler_t *pcm_callback;
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
snd_pcm_format_t sample_format;
|
|
|
|
unsigned int rate; // sample rate of audio
|
|
|
|
unsigned int real_rate; // actual sample rate played back
|
|
|
|
int periods; // number of bytes in a frame
|
|
|
|
snd_pcm_uframes_t bufsize; // size of buffer in frames
|
|
|
|
|
|
|
|
void SetUpHardware();
|
2007-04-25 00:29:27 +02:00
|
|
|
void SetUpAsync();
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
static void async_write_handler(snd_async_handler_t *pcm_callback);
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
AlsaPlayer();
|
|
|
|
~AlsaPlayer();
|
|
|
|
|
|
|
|
void OpenStream();
|
|
|
|
void CloseStream();
|
|
|
|
|
|
|
|
void Play(__int64 start,__int64 count);
|
|
|
|
void Stop(bool timerToo=true);
|
|
|
|
bool IsPlaying();
|
|
|
|
|
|
|
|
__int64 GetStartPosition();
|
|
|
|
__int64 GetEndPosition();
|
|
|
|
__int64 GetCurrentPosition();
|
|
|
|
void SetEndPosition(__int64 pos);
|
|
|
|
void SetCurrentPosition(__int64 pos);
|
|
|
|
|
|
|
|
void SetVolume(double vol) { volume = vol; }
|
|
|
|
double GetVolume() { return volume; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////
|
|
|
|
// Factory
|
|
|
|
class AlsaPlayerFactory : public AudioPlayerFactory {
|
|
|
|
public:
|
|
|
|
AudioPlayer *CreatePlayer() { return new AlsaPlayer(); }
|
|
|
|
AlsaPlayerFactory() : AudioPlayerFactory(_T("alsa")) {}
|
|
|
|
} registerAlsaPlayer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Constructor
|
2007-04-25 00:29:27 +02:00
|
|
|
AlsaPlayer::AlsaPlayer()
|
|
|
|
{
|
2007-04-22 23:59:35 +02:00
|
|
|
volume = 1.0f;
|
2007-04-25 00:29:27 +02:00
|
|
|
open = false;
|
|
|
|
playing = false;
|
|
|
|
start_frame = cur_frame = end_frame = bpf = 0;
|
|
|
|
provider = 0;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Destructor
|
2007-04-25 00:29:27 +02:00
|
|
|
AlsaPlayer::~AlsaPlayer()
|
|
|
|
{
|
2007-04-22 23:59:35 +02:00
|
|
|
CloseStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
// Open stream
|
2007-04-25 00:29:27 +02:00
|
|
|
void AlsaPlayer::OpenStream()
|
|
|
|
{
|
2007-04-22 23:59:35 +02:00
|
|
|
CloseStream();
|
|
|
|
|
|
|
|
// Get provider
|
2007-04-25 00:29:27 +02:00
|
|
|
provider = GetProvider();
|
|
|
|
bpf = provider->GetChannels() * provider->GetBytesPerSample();
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
// We want playback
|
|
|
|
stream = SND_PCM_STREAM_PLAYBACK;
|
2007-04-25 00:29:27 +02:00
|
|
|
// And get a device name
|
2007-04-23 19:01:00 +02:00
|
|
|
wxString device = Options.AsText(_T("Audio Alsa Device"));
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
// Open device for blocking access
|
2007-04-25 00:29:27 +02:00
|
|
|
if (snd_pcm_open(&pcm_handle, device.mb_str(wxConvUTF8), stream, 0) < 0) { // supposedly we don't want SND_PCM_ASYNC even for async playback
|
|
|
|
throw _T("Error opening specified PCM device");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SetUpHardware();
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Register async handler
|
|
|
|
SetUpAsync();
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Now ready
|
|
|
|
open = true;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
void AlsaPlayer::SetUpHardware()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
// Allocate params structure
|
|
|
|
snd_pcm_hw_params_t *hwparams;
|
|
|
|
snd_pcm_hw_params_malloc(&hwparams);
|
|
|
|
|
2007-04-22 23:59:35 +02:00
|
|
|
// Get hardware params
|
|
|
|
if (snd_pcm_hw_params_any(pcm_handle, hwparams) < 0) {
|
|
|
|
throw _T("Error setting up default PCM device");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set stream format
|
|
|
|
if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
|
|
|
|
throw _T("Could not set interleaved stream format");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sample format
|
2007-04-25 00:29:27 +02:00
|
|
|
switch (provider->GetBytesPerSample()) {
|
|
|
|
case 1:
|
|
|
|
sample_format = SND_PCM_FORMAT_S8;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sample_format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw _T("Can only handle 8 and 16 bit sound");
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
if (snd_pcm_hw_params_set_format(pcm_handle, hwparams, sample_format) < 0) {
|
|
|
|
throw _T("Could not set sample format");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sample rate
|
|
|
|
rate = provider->GetSampleRate();
|
|
|
|
real_rate = rate;
|
|
|
|
if (snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &real_rate, 0) < 0) {
|
|
|
|
throw _T("Could not set sample rate");
|
|
|
|
}
|
|
|
|
if (rate != real_rate) {
|
|
|
|
wxLogDebug(_T("Could not set ideal sample rate %d, using %d instead"), rate, real_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set number of channels
|
|
|
|
if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, provider->GetChannels()) < 0) {
|
|
|
|
throw _T("Could not set number of channels");
|
|
|
|
}
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Set periods (number of bytes ideally written at a time)
|
|
|
|
// Somewhat arbitrary for now (256 frames)
|
|
|
|
periods = provider->GetChannels() * provider->GetBytesPerSample() * 256;
|
2007-04-22 23:59:35 +02:00
|
|
|
if (snd_pcm_hw_params_set_periods(pcm_handle, hwparams, periods, 0) < 0) {
|
|
|
|
throw _T("Could not set periods");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set buffer size
|
|
|
|
snd_pcm_uframes_t wanted_bufsize = rate;
|
|
|
|
bufsize = wanted_bufsize;
|
|
|
|
if (snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &bufsize) < 0) {
|
|
|
|
throw _T("Could not set buffer size");
|
|
|
|
}
|
|
|
|
if (bufsize != wanted_bufsize) {
|
|
|
|
wxLogDebug(_T("Couldn't get wanted buffer size of %u, got %u instead"), wanted_bufsize, bufsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply parameters
|
|
|
|
if (snd_pcm_hw_params(pcm_handle, hwparams) < 0) {
|
|
|
|
throw _T("Failed applying sound hardware settings");
|
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
|
|
|
|
// And free memory again
|
|
|
|
snd_pcm_hw_params_free(hwparams);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
void AlsaPlayer::SetUpAsync()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
// Prepare software params struct
|
|
|
|
snd_pcm_sw_params_t *sw_params;
|
|
|
|
snd_pcm_sw_params_malloc (&sw_params);
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Get current parameters
|
|
|
|
if (snd_pcm_sw_params_current(pcm_handle, sw_params) < 0) {
|
|
|
|
throw _T("Couldn't get current SW params");
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// How full the buffer must be before playback begins
|
|
|
|
if (snd_pcm_sw_params_set_start_threshold(pcm_handle, sw_params, bufsize/2) < 0) {
|
|
|
|
throw _T("Failed setting start threshold");
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// The the largest write guaranteed never to block
|
|
|
|
if (snd_pcm_sw_params_set_avail_min(pcm_handle, sw_params, bufsize/4) < 0) {
|
|
|
|
throw _T("Failed setting min available buffer");
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Apply settings
|
|
|
|
if (snd_pcm_sw_params(pcm_handle, sw_params) < 0) {
|
|
|
|
throw _T("Failed applying SW params");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// And free struct again
|
|
|
|
snd_pcm_sw_params_free(sw_params);
|
|
|
|
|
|
|
|
// Attach async handler
|
|
|
|
if (snd_async_add_pcm_handler(&pcm_callback, pcm_handle, async_write_handler, this) < 0) {
|
|
|
|
throw _T("Failed attaching async handler");
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
////////////////
|
|
|
|
// Close stream
|
|
|
|
void AlsaPlayer::CloseStream()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
if (!open) return;
|
|
|
|
|
|
|
|
Stop();
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Remove async handler
|
|
|
|
snd_async_del_handler(pcm_callback);
|
|
|
|
|
|
|
|
// Close device
|
|
|
|
snd_pcm_close(pcm_handle);
|
|
|
|
|
|
|
|
// No longer working
|
|
|
|
open = false;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
////////
|
|
|
|
// Play
|
|
|
|
void AlsaPlayer::Play(__int64 start,__int64 count)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
if (playing) {
|
|
|
|
// Quick reset
|
|
|
|
playing = false;
|
|
|
|
snd_pcm_drop(pcm_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set params
|
|
|
|
start_frame = start;
|
|
|
|
cur_frame = start;
|
|
|
|
end_frame = start + count;
|
|
|
|
playing = true;
|
|
|
|
|
|
|
|
// Prepare a bit
|
|
|
|
snd_pcm_prepare (pcm_handle);
|
|
|
|
async_write_handler(pcm_callback);
|
|
|
|
|
|
|
|
// And go!
|
|
|
|
snd_pcm_start(pcm_handle);
|
|
|
|
|
|
|
|
// Update timer
|
|
|
|
if (displayTimer && !displayTimer->IsRunning()) displayTimer->Start(15);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
////////
|
|
|
|
// Stop
|
|
|
|
void AlsaPlayer::Stop(bool timerToo)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
if (!open) return;
|
|
|
|
if (!playing) return;
|
|
|
|
|
|
|
|
// Reset data
|
|
|
|
playing = false;
|
|
|
|
start_frame = 0;
|
|
|
|
cur_frame = 0;
|
|
|
|
end_frame = 0;
|
|
|
|
|
|
|
|
// Then drop the playback
|
|
|
|
snd_pcm_drop(pcm_handle);
|
|
|
|
|
|
|
|
if (timerToo && displayTimer) {
|
|
|
|
displayTimer->Stop();
|
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
bool AlsaPlayer::IsPlaying()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
return playing;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
///////////
|
|
|
|
// Set end
|
|
|
|
void AlsaPlayer::SetEndPosition(__int64 pos)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
end_frame = pos;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
////////////////////////
|
|
|
|
// Set current position
|
|
|
|
void AlsaPlayer::SetCurrentPosition(__int64 pos)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
cur_frame = pos;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
__int64 AlsaPlayer::GetStartPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
|
|
|
return start_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
__int64 AlsaPlayer::GetEndPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
|
|
|
return end_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
////////////////////////
|
|
|
|
// Get current position
|
|
|
|
__int64 AlsaPlayer::GetCurrentPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
return cur_frame; // FIXME
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
void AlsaPlayer::async_write_handler(snd_async_handler_t *pcm_callback)
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-25 00:29:27 +02:00
|
|
|
// TODO: check for broken pipes in here and restore as needed
|
|
|
|
AlsaPlayer *player = (AlsaPlayer*)snd_async_handler_get_callback_private(pcm_callback);
|
|
|
|
|
|
|
|
if (player->cur_frame >= player->end_frame + player->rate) {
|
|
|
|
// More than a second past end of stream
|
|
|
|
snd_pcm_drain(player->pcm_handle);
|
|
|
|
player->playing = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_sframes_t frames = snd_pcm_avail_update(player->pcm_handle);
|
|
|
|
|
|
|
|
if (player->cur_frame >= player->end_frame) {
|
|
|
|
// Past end of stream, add some silence
|
|
|
|
void *buf = calloc(frames, player->bpf);
|
|
|
|
snd_pcm_writei(player->pcm_handle, buf, frames);
|
|
|
|
free(buf);
|
|
|
|
player->cur_frame += frames;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *buf = malloc(frames * player->bpf);
|
|
|
|
player->provider->GetAudioWithVolume(buf, player->cur_frame, frames, player->volume);
|
|
|
|
snd_pcm_writei(player->pcm_handle, buf, frames);
|
|
|
|
free(buf);
|
|
|
|
player->cur_frame += frames;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|