2007-04-22 03:23:25 +02:00
|
|
|
// Copyright (c) 2005-2007, Rodrigo Braz Monteiro
|
2006-02-25 07:04:46 +01: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/
|
2006-02-25 07:04:46 +01:00
|
|
|
//
|
2011-11-28 21:01:46 +01:00
|
|
|
// $Id: audio_player_portaudio.cpp 5897 2011-11-20 03:43:52Z plorkyeran $
|
2009-07-29 07:43:02 +02:00
|
|
|
|
|
|
|
/// @file audio_player_portaudio.cpp
|
|
|
|
/// @brief PortAudio v18-based audio output
|
|
|
|
/// @ingroup audio_output
|
|
|
|
///
|
2006-02-25 07:04:46 +01:00
|
|
|
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#ifdef WITH_PORTAUDIO
|
|
|
|
|
2010-06-09 01:21:39 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
|
2008-03-05 03:43:01 +01:00
|
|
|
#include "audio_player_portaudio.h"
|
2012-01-08 02:33:39 +01:00
|
|
|
|
|
|
|
#include "audio_controller.h"
|
|
|
|
#include "compat.h"
|
2011-11-28 21:01:58 +01:00
|
|
|
#include "include/aegisub/audio_provider.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "main.h"
|
2006-02-25 07:04:46 +01:00
|
|
|
#include "utils.h"
|
2007-04-22 03:23:25 +02:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
DEFINE_SIMPLE_EXCEPTION(PortAudioError, agi::AudioPlayerOpenError, "audio/player/open/portaudio")
|
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
// Uncomment to enable extremely spammy debug logging
|
2009-06-10 06:45:57 +02:00
|
|
|
//#define PORTAUDIO_DEBUG
|
2007-04-22 03:23:25 +02:00
|
|
|
|
2012-01-23 22:14:32 +01:00
|
|
|
/// Order that the host APIs should be tried if there are multiple available
|
|
|
|
static const PaHostApiTypeId pa_host_api_priority[] = {
|
|
|
|
// No WDMKS or ASIO as they don't support shared mode (and WDMKS is pretty broken)
|
|
|
|
paWASAPI,
|
|
|
|
paDirectSound,
|
|
|
|
paMME,
|
|
|
|
|
|
|
|
paCoreAudio,
|
|
|
|
#ifdef __APPLE__
|
|
|
|
paAL,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
paALSA,
|
|
|
|
paOSS
|
|
|
|
};
|
|
|
|
static const size_t pa_host_api_priority_count = sizeof(pa_host_api_priority) / sizeof(pa_host_api_priority[0]);
|
|
|
|
|
|
|
|
PortAudioPlayer::PortAudioPlayer()
|
|
|
|
: volume(1.0f)
|
|
|
|
, pa_start(0.0)
|
|
|
|
{
|
2012-01-08 02:04:44 +01:00
|
|
|
PaError err = Pa_Initialize();
|
2009-06-10 06:45:57 +02:00
|
|
|
|
2012-01-08 02:04:44 +01:00
|
|
|
if (err != paNoError)
|
2012-03-12 00:51:10 +01:00
|
|
|
throw PortAudioError(std::string("Failed opening PortAudio: ") + Pa_GetErrorText(err), 0);
|
2006-02-25 07:04:46 +01:00
|
|
|
|
2012-01-23 22:14:32 +01:00
|
|
|
// Build a list of host API-specific devices we can use
|
|
|
|
// Some host APIs may not support all audio formats, so build a priority
|
|
|
|
// list of host APIs for each device rather than just always using the best
|
|
|
|
for (size_t i = 0; i < pa_host_api_priority_count; ++i) {
|
|
|
|
PaHostApiIndex host_idx = Pa_HostApiTypeIdToHostApiIndex(pa_host_api_priority[i]);
|
|
|
|
if (host_idx >= 0)
|
|
|
|
GatherDevices(host_idx);
|
|
|
|
}
|
|
|
|
GatherDevices(Pa_GetDefaultHostApi());
|
|
|
|
|
|
|
|
if (devices.empty())
|
|
|
|
throw PortAudioError("No PortAudio output devices found", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PortAudioPlayer::GatherDevices(PaHostApiIndex host_idx) {
|
|
|
|
const PaHostApiInfo *host_info = Pa_GetHostApiInfo(host_idx);
|
|
|
|
if (!host_info) return;
|
|
|
|
|
|
|
|
for (int host_device_idx = 0; host_device_idx < host_info->deviceCount; ++host_device_idx) {
|
|
|
|
PaDeviceIndex real_idx = Pa_HostApiDeviceIndexToDeviceIndex(host_idx, host_device_idx);
|
|
|
|
if (real_idx < 0) continue;
|
|
|
|
|
|
|
|
const PaDeviceInfo *device_info = Pa_GetDeviceInfo(real_idx);
|
|
|
|
if (!device_info) continue;
|
|
|
|
if (device_info->maxOutputChannels <= 0) continue;
|
|
|
|
|
|
|
|
// MME truncates device names so check for prefix rather than exact match
|
|
|
|
std::map<std::string, DeviceVec>::iterator dev_it = devices.lower_bound(device_info->name);
|
|
|
|
if (dev_it == devices.end() || dev_it->first.find(device_info->name) != 0) {
|
|
|
|
devices[device_info->name];
|
|
|
|
--dev_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_it->second.push_back(real_idx);
|
|
|
|
if (real_idx == host_info->defaultOutputDevice)
|
|
|
|
default_device.push_back(real_idx);
|
|
|
|
}
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PortAudioPlayer::~PortAudioPlayer() {
|
2012-01-08 02:04:44 +01:00
|
|
|
Pa_Terminate();
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
|
|
|
|
2009-06-10 06:45:57 +02:00
|
|
|
void PortAudioPlayer::OpenStream() {
|
2012-01-23 22:14:32 +01:00
|
|
|
DeviceVec *device_ids = 0;
|
2011-11-28 21:01:58 +01:00
|
|
|
std::string device_name = OPT_GET("Player/Audio/PortAudio/Device Name")->GetString();
|
|
|
|
|
2012-01-23 22:14:32 +01:00
|
|
|
if (devices.count(device_name)) {
|
|
|
|
device_ids = &devices[device_name];
|
|
|
|
LOG_D("audio/player/portaudio") << "using config device: " << device_name;
|
2011-11-28 21:01:58 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:14:32 +01:00
|
|
|
if (!device_ids || device_ids->empty()) {
|
|
|
|
device_ids = &default_device;
|
|
|
|
LOG_D("audio/player/portaudio") << "using default output device";
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
|
|
|
|
2012-01-23 22:14:32 +01:00
|
|
|
std::string error;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < device_ids->size(); ++i) {
|
|
|
|
const PaDeviceInfo *device_info = Pa_GetDeviceInfo((*device_ids)[i]);
|
|
|
|
PaStreamParameters pa_output_p;
|
|
|
|
pa_output_p.device = (*device_ids)[i];
|
|
|
|
pa_output_p.channelCount = provider->GetChannels();
|
|
|
|
pa_output_p.sampleFormat = paInt16;
|
|
|
|
pa_output_p.suggestedLatency = device_info->defaultLowOutputLatency;
|
|
|
|
pa_output_p.hostApiSpecificStreamInfo = NULL;
|
|
|
|
|
|
|
|
LOG_D("audio/player/portaudio") << "OpenStream:"
|
|
|
|
<< " output channels: " << pa_output_p.channelCount
|
|
|
|
<< " latency: " << pa_output_p.suggestedLatency
|
|
|
|
<< " sample rate: " << provider->GetSampleRate()
|
|
|
|
<< " sample format: " << pa_output_p.sampleFormat;
|
|
|
|
|
|
|
|
PaError err = Pa_OpenStream(&stream, NULL, &pa_output_p, provider->GetSampleRate(), 0, paPrimeOutputBuffersUsingStreamCallback, paCallback, this);
|
|
|
|
|
|
|
|
if (err == paNoError) {
|
|
|
|
LOG_D("audo/player/portaudio") << "Using device " << pa_output_p.device << " " << device_info->name << " " << Pa_GetHostApiInfo(device_info->hostApi)->name;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const PaHostErrorInfo *pa_err = Pa_GetLastHostErrorInfo();
|
|
|
|
LOG_D_IF(pa_err->errorCode != 0, "audio/player/portaudio") << "HostError: API: " << pa_err->hostApiType << ", " << pa_err->errorText << ", " << pa_err->errorCode;
|
|
|
|
LOG_D("audio/player/portaudio") << "Failed initializing PortAudio stream with error: " << Pa_GetErrorText(err);
|
|
|
|
error += Pa_GetErrorText(err);
|
|
|
|
error += " ";
|
|
|
|
}
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
2012-01-23 22:14:32 +01:00
|
|
|
|
|
|
|
throw PortAudioError("Failed initializing PortAudio stream: " + error, 0);
|
2009-06-10 06:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PortAudioPlayer::CloseStream() {
|
2009-08-21 22:08:58 +02:00
|
|
|
Stop(false);
|
|
|
|
Pa_CloseStream(stream);
|
2009-06-10 06:45:57 +02:00
|
|
|
}
|
2006-02-25 07:04:46 +01:00
|
|
|
|
2009-06-10 06:45:57 +02:00
|
|
|
void PortAudioPlayer::paStreamFinishedCallback(void *userData) {
|
2011-11-28 21:01:46 +01:00
|
|
|
PortAudioPlayer *player = (PortAudioPlayer *) userData;
|
2006-02-25 07:04:46 +01:00
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
if (player->displayTimer)
|
2009-06-10 06:45:57 +02:00
|
|
|
player->displayTimer->Stop();
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("audio/player/portaudio") << "stopping stream";
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
void PortAudioPlayer::Play(int64_t start_sample, int64_t count) {
|
|
|
|
current = start_sample;
|
|
|
|
start = start_sample;
|
|
|
|
end = start_sample + count;
|
2006-02-25 07:04:46 +01:00
|
|
|
|
|
|
|
// Start playing
|
2009-08-21 22:08:58 +02:00
|
|
|
if (!IsPlaying()) {
|
2011-11-28 21:01:46 +01:00
|
|
|
PaError err = Pa_SetStreamFinishedCallback(stream, paStreamFinishedCallback);
|
2009-06-10 06:45:57 +02:00
|
|
|
if (err != paNoError) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("audio/player/portaudio") << "could not set FinishedCallback";
|
2009-06-10 06:45:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = Pa_StartStream(stream);
|
2006-02-25 07:04:46 +01:00
|
|
|
if (err != paNoError) {
|
2010-06-09 01:21:39 +02:00
|
|
|
LOG_D("audio/player/portaudio") << "error playing stream";
|
2006-02-25 07:04:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 21:01:46 +01:00
|
|
|
pa_start = Pa_GetStreamTime(stream);
|
2006-02-25 07:04:46 +01:00
|
|
|
|
|
|
|
// Update timer
|
2011-11-28 21:01:46 +01:00
|
|
|
if (displayTimer && !displayTimer->IsRunning())
|
|
|
|
displayTimer->Start(15);
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PortAudioPlayer::Stop(bool timerToo) {
|
2011-11-28 21:01:46 +01:00
|
|
|
Pa_StopStream(stream);
|
2006-02-25 07:04:46 +01:00
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
if (timerToo && displayTimer)
|
2006-02-25 07:04:46 +01:00
|
|
|
displayTimer->Stop();
|
|
|
|
}
|
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
int PortAudioPlayer::paCallback(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
|
|
|
|
PaStreamCallbackFlags statusFlags, void *userData)
|
|
|
|
{
|
|
|
|
PortAudioPlayer *player = (PortAudioPlayer *)userData;
|
2009-06-10 06:45:57 +02:00
|
|
|
|
|
|
|
#ifdef PORTAUDIO_DEBUG
|
2011-11-28 21:01:46 +01:00
|
|
|
LOG_D("audio/player/portaudio") << "psCallback:"
|
|
|
|
<< " current: " << player->current
|
|
|
|
<< " start: " << player->start
|
|
|
|
<< " pa_start: " << player->pa_start
|
|
|
|
<< " currentTime: " << timeInfo->currentTime
|
|
|
|
<< " AdcTime: " << timeInfo->inputBufferAdcTime
|
|
|
|
<< " DacTime: " << timeInfo->outputBufferDacTime
|
|
|
|
<< " framesPerBuffer: " << framesPerBuffer
|
|
|
|
<< " CPU: " << Pa_GetStreamCpuLoad(player->stream);
|
2007-04-05 18:39:49 +02:00
|
|
|
#endif
|
|
|
|
|
2009-06-10 06:45:57 +02:00
|
|
|
// Calculate how much left
|
2011-11-28 21:01:46 +01:00
|
|
|
int64_t lenAvailable = std::min<int64_t>(player->end - player->current, framesPerBuffer);
|
2009-06-10 06:45:57 +02:00
|
|
|
|
|
|
|
// Play something
|
|
|
|
if (lenAvailable > 0) {
|
2011-11-28 21:01:46 +01:00
|
|
|
player->GetProvider()->GetAudioWithVolume(outputBuffer, player->current, lenAvailable, player->GetVolume());
|
2009-06-10 06:45:57 +02:00
|
|
|
|
|
|
|
// Set play position
|
2011-11-28 21:01:46 +01:00
|
|
|
player->current += lenAvailable;
|
2009-06-10 06:45:57 +02:00
|
|
|
|
|
|
|
// Continue as normal
|
|
|
|
return 0;
|
2006-02-25 09:26:29 +01:00
|
|
|
}
|
2009-06-10 06:45:57 +02:00
|
|
|
|
|
|
|
// Abort stream and stop the callback.
|
|
|
|
return paAbort;
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
int64_t PortAudioPlayer::GetCurrentPosition() {
|
2009-08-21 22:08:58 +02:00
|
|
|
if (!IsPlaying()) return 0;
|
2009-06-10 06:45:57 +02:00
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
PaTime pa_time = Pa_GetStreamTime(stream);
|
|
|
|
int64_t real = (pa_time - pa_start) * provider->GetSampleRate() + start;
|
2009-06-10 06:45:57 +02:00
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
// If portaudio isn't giving us time info then estimate based on buffer fill and current latency
|
|
|
|
if (pa_time == 0 && pa_start == 0)
|
|
|
|
real = current - Pa_GetStreamInfo(stream)->outputLatency * provider->GetSampleRate();
|
2009-06-10 06:45:57 +02:00
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
#ifdef PORTAUDIO_DEBUG
|
|
|
|
LOG_D("audio/player/portaudio") << "GetCurrentPosition:"
|
|
|
|
<< " pa_time: " << pa_time
|
|
|
|
<< " start: " << start
|
|
|
|
<< " current: " << current
|
|
|
|
<< " pa_start: " << pa_start
|
|
|
|
<< " real: " << real
|
|
|
|
<< " diff: " << pa_time - pa_start;
|
2009-06-10 06:45:57 +02:00
|
|
|
#endif
|
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
return real;
|
2006-02-25 07:04:46 +01:00
|
|
|
}
|
2006-12-23 15:15:04 +01:00
|
|
|
|
2011-11-28 21:01:46 +01:00
|
|
|
wxArrayString PortAudioPlayer::GetOutputDevices() {
|
|
|
|
wxArrayString list;
|
2011-11-28 21:01:58 +01:00
|
|
|
list.push_back("Default");
|
|
|
|
|
2012-01-23 22:14:32 +01:00
|
|
|
try {
|
|
|
|
PortAudioPlayer player;
|
|
|
|
|
|
|
|
for (std::map<std::string, DeviceVec>::iterator it = player.devices.begin(); it != player.devices.end(); ++it)
|
|
|
|
list.push_back(lagi_wxString(it->first));
|
|
|
|
}
|
|
|
|
catch (PortAudioError const&) {
|
|
|
|
// No output devices, just return the list with only Default
|
2009-06-10 06:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2009-08-21 22:08:58 +02:00
|
|
|
bool PortAudioPlayer::IsPlaying() {
|
2011-11-28 21:01:46 +01:00
|
|
|
return !!Pa_IsStreamActive(stream);
|
2009-08-21 22:08:58 +02:00
|
|
|
}
|
|
|
|
|
2008-01-21 21:57:20 +01:00
|
|
|
#endif // WITH_PORTAUDIO
|