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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2007-04-22 23:59:35 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file audio_player_alsa.cpp
|
|
|
|
/// @brief ALSA-based audio output
|
|
|
|
/// @ingroup audio_output
|
|
|
|
///
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_ALSA
|
|
|
|
|
|
|
|
|
2007-04-22 23:59:35 +02:00
|
|
|
///////////
|
|
|
|
// Headers
|
2010-06-09 01:21:39 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_player_alsa.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"
|
2010-05-24 03:46:04 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "compat.h"
|
2007-04-22 23:59:35 +02:00
|
|
|
#include "frame_main.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2007-04-22 23:59:35 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Destructor
|
|
|
|
///
|
2007-04-25 00:29:27 +02:00
|
|
|
AlsaPlayer::~AlsaPlayer()
|
|
|
|
{
|
2007-04-22 23:59:35 +02:00
|
|
|
CloseStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief 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
|
2010-05-21 03:13:36 +02:00
|
|
|
wxString device = lagi_wxString(OPT_GET("Player/Audio/ALSA/Device")->GetString());
|
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
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
///
|
2007-04-25 00:29:27 +02:00
|
|
|
void AlsaPlayer::SetUpHardware()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
2007-04-26 20:34:36 +02:00
|
|
|
int dir;
|
|
|
|
|
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) {
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Error setting up default PCM device");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set stream format
|
|
|
|
if (snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Could not set interleaved stream format");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Can only handle 8 and 16 bit sound");
|
2007-04-25 00:29:27 +02:00
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
if (snd_pcm_hw_params_set_format(pcm_handle, hwparams, sample_format) < 0) {
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Could not set sample format");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
2007-04-26 22:11:48 +02:00
|
|
|
// Ask for resampling
|
|
|
|
if (snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 1) < 0) {
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Couldn't enable resampling");
|
2007-04-26 22:11:48 +02:00
|
|
|
}
|
|
|
|
|
2007-04-22 23:59:35 +02:00
|
|
|
// Set sample rate
|
|
|
|
rate = provider->GetSampleRate();
|
|
|
|
real_rate = rate;
|
|
|
|
if (snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &real_rate, 0) < 0) {
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Could not set sample rate");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D_IF(rate != real_rate, "player/audio/alsa") << "ALSA player: Could not set ideal sample rate " << rate << "using " << real_rate << "instead";
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
// Set number of channels
|
|
|
|
if (snd_pcm_hw_params_set_channels(pcm_handle, hwparams, provider->GetChannels()) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Could not set number of channels";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Could not set number of channels");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
2007-07-18 15:46:38 +02:00
|
|
|
printf("ALSA player: Set sample rate %u (wanted %u)\n", real_rate, rate);
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
// Set buffer size
|
2007-04-26 20:34:36 +02:00
|
|
|
unsigned int wanted_buflen = 1000000; // microseconds
|
|
|
|
buflen = wanted_buflen;
|
|
|
|
if (snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buflen, &dir) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Couldn't set buffer length";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Couldn't set buffer length");
|
2007-04-26 20:34:36 +02:00
|
|
|
}
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D_IF(buflen != wanted_buflen, "player/audio/alsa") << "Couldn't get wanted buffer size of " << wanted_buflen << ", got " << buflen << "instead";
|
|
|
|
|
2007-04-26 20:34:36 +02:00
|
|
|
if (snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Couldn't get buffer size";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Couldn't get buffer size");
|
2007-04-26 20:34:36 +02:00
|
|
|
}
|
2007-07-18 15:46:38 +02:00
|
|
|
printf("ALSA player: Buffer size: %lu\n", bufsize);
|
2007-04-26 20:34:36 +02:00
|
|
|
|
|
|
|
// Set period (number of frames ideally written at a time)
|
|
|
|
// Somewhat arbitrary for now
|
|
|
|
unsigned int wanted_period = bufsize / 4;
|
|
|
|
period_len = wanted_period; // microseconds
|
|
|
|
if (snd_pcm_hw_params_set_period_time_near(pcm_handle, hwparams, &period_len, &dir) < 0) {
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Couldn't set period length");
|
2007-04-26 20:34:36 +02:00
|
|
|
}
|
2010-06-16 08:20:27 +02:00
|
|
|
LOG_D_IF(period_len != wanted_period, "player/audio/alsa") << "Couldn't get wanted period size of " << wanted_period << ", got " << period_len << " instead";
|
2010-06-09 01:21:39 +02:00
|
|
|
|
2007-04-26 20:34:36 +02:00
|
|
|
if (snd_pcm_hw_params_get_period_size(hwparams, &period, &dir) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Couldn't get period size";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Couldn't get period size");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
2007-07-18 15:46:38 +02:00
|
|
|
printf("ALSA player: Period size: %lu\n", period);
|
2007-04-22 23:59:35 +02:00
|
|
|
|
|
|
|
// Apply parameters
|
|
|
|
if (snd_pcm_hw_params(pcm_handle, hwparams) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Failed applying sound hardware settings";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Failed applying sound hardware settings");
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
///
|
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) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Couldn't get current SW params";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Couldn't get current SW params");
|
2007-04-25 00:29:27 +02:00
|
|
|
}
|
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
|
2007-04-26 22:11:48 +02:00
|
|
|
if (snd_pcm_sw_params_set_start_threshold(pcm_handle, sw_params, bufsize - period) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Failed setting start threshold";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Failed setting start threshold");
|
2007-04-25 00:29:27 +02:00
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// The the largest write guaranteed never to block
|
2007-04-26 20:34:36 +02:00
|
|
|
if (snd_pcm_sw_params_set_avail_min(pcm_handle, sw_params, period) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Failed setting min available buffer";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Failed setting min available buffer");
|
2007-04-25 00:29:27 +02:00
|
|
|
}
|
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) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Failed applying SW params";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: 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);
|
|
|
|
|
2010-01-15 23:58:51 +01:00
|
|
|
// Prepare for playback
|
|
|
|
snd_pcm_prepare(pcm_handle);
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
// Attach async handler
|
|
|
|
if (snd_async_add_pcm_handler(&pcm_callback, pcm_handle, async_write_handler, this) < 0) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_E("player/audio/alsa") << "Failed attaching async handler";
|
2007-07-18 15:46:38 +02:00
|
|
|
throw _T("ALSA player: Failed attaching async handler");
|
2007-04-25 00:29:27 +02:00
|
|
|
}
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Close stream
|
|
|
|
/// @return
|
|
|
|
///
|
2007-04-25 00:29:27 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Play
|
|
|
|
/// @param start
|
|
|
|
/// @param count
|
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
void AlsaPlayer::Play(int64_t start,int64_t 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Stop
|
|
|
|
/// @param timerToo
|
|
|
|
/// @return
|
|
|
|
///
|
2007-04-25 00:29:27 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
/// @return
|
|
|
|
///
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Set end
|
|
|
|
/// @param pos
|
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
void AlsaPlayer::SetEndPosition(int64_t 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Set current position
|
|
|
|
/// @param pos
|
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
void AlsaPlayer::SetCurrentPosition(int64_t 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
/// @return
|
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t AlsaPlayer::GetStartPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
|
|
|
return start_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
/// @return
|
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t AlsaPlayer::GetEndPosition()
|
2007-04-22 23:59:35 +02:00
|
|
|
{
|
|
|
|
return end_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief Get current position
|
|
|
|
/// @return
|
|
|
|
///
|
2007-08-31 16:11:35 +02:00
|
|
|
int64_t AlsaPlayer::GetCurrentPosition()
|
2007-04-22 23:59:35 +02: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-26 22:17:20 +02:00
|
|
|
snd_pcm_sframes_t delay = 0;
|
|
|
|
snd_pcm_delay(pcm_handle, &delay); // don't bother catching errors here
|
|
|
|
return cur_frame - delay;
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/// @brief DOCME
|
|
|
|
/// @param pcm_callback
|
|
|
|
///
|
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);
|
2007-11-16 14:15:26 +01:00
|
|
|
|
|
|
|
if (frames == -EPIPE) {
|
|
|
|
snd_pcm_prepare(player->pcm_handle);
|
|
|
|
frames = snd_pcm_avail_update(player->pcm_handle);
|
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
|
2007-04-26 22:11:48 +02:00
|
|
|
// TODO: handle underrun
|
|
|
|
|
2007-04-25 00:29:27 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-04-26 22:11:48 +02:00
|
|
|
void *buf = malloc(player->period * player->bpf);
|
|
|
|
while (frames >= player->period) {
|
2007-05-07 16:22:18 +02:00
|
|
|
unsigned long start = player->cur_frame;
|
2007-04-26 22:11:48 +02:00
|
|
|
player->provider->GetAudioWithVolume(buf, player->cur_frame, player->period, player->volume);
|
2007-11-16 14:15:26 +01:00
|
|
|
int err = snd_pcm_writei(player->pcm_handle, buf, player->period);
|
|
|
|
if(err == -EPIPE) {
|
|
|
|
snd_pcm_prepare(player->pcm_handle);
|
|
|
|
}
|
2007-04-26 22:11:48 +02:00
|
|
|
player->cur_frame += player->period;
|
|
|
|
frames = snd_pcm_avail_update(player->pcm_handle);
|
|
|
|
}
|
2007-04-25 00:29:27 +02:00
|
|
|
free(buf);
|
2007-04-22 23:59:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#endif // WITH_ALSA
|
2009-07-29 07:43:02 +02:00
|
|
|
|
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
|
|
|
|