2016-12-20 16:37:36 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2016-12-20 16:37:36 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Source.hxx"
|
|
|
|
#include "MusicChunk.hxx"
|
2018-01-01 18:48:34 +01:00
|
|
|
#include "filter/Filter.hxx"
|
|
|
|
#include "filter/Prepared.hxx"
|
2016-12-20 16:37:36 +01:00
|
|
|
#include "filter/plugins/ReplayGainFilterPlugin.hxx"
|
2019-06-17 11:10:33 +02:00
|
|
|
#include "pcm/Mix.hxx"
|
2016-12-26 20:02:15 +01:00
|
|
|
#include "thread/Mutex.hxx"
|
2016-12-20 16:37:36 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
|
|
|
#include "util/RuntimeError.hxx"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-02-01 13:47:16 +01:00
|
|
|
AudioOutputSource::AudioOutputSource() noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-27 11:42:14 +01:00
|
|
|
AudioOutputSource::~AudioOutputSource() noexcept = default;
|
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
AudioFormat
|
2017-06-08 13:19:34 +02:00
|
|
|
AudioOutputSource::Open(const AudioFormat audio_format, const MusicPipe &_pipe,
|
2016-12-20 16:37:36 +01:00
|
|
|
PreparedFilter *prepared_replay_gain_filter,
|
|
|
|
PreparedFilter *prepared_other_replay_gain_filter,
|
2017-12-27 11:21:43 +01:00
|
|
|
PreparedFilter &prepared_filter)
|
2016-12-20 16:37:36 +01:00
|
|
|
{
|
|
|
|
assert(audio_format.IsValid());
|
|
|
|
|
2018-11-07 00:17:48 +01:00
|
|
|
if (!IsOpen() || &_pipe != &pipe.GetPipe()) {
|
|
|
|
current_chunk = nullptr;
|
2016-12-20 16:37:36 +01:00
|
|
|
pipe.Init(_pipe);
|
2018-11-07 00:17:48 +01:00
|
|
|
}
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
/* (re)open the filter */
|
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (filter && audio_format != in_audio_format)
|
2016-12-20 16:37:36 +01:00
|
|
|
/* the filter must be reopened on all input format
|
|
|
|
changes */
|
|
|
|
CloseFilter();
|
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (filter == nullptr)
|
2016-12-20 16:37:36 +01:00
|
|
|
/* open the filter */
|
|
|
|
OpenFilter(audio_format,
|
|
|
|
prepared_replay_gain_filter,
|
|
|
|
prepared_other_replay_gain_filter,
|
|
|
|
prepared_filter);
|
|
|
|
|
|
|
|
in_audio_format = audio_format;
|
2018-01-02 09:59:22 +01:00
|
|
|
return filter->GetOutAudioFormat();
|
2016-12-20 16:37:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-08 14:44:49 +02:00
|
|
|
AudioOutputSource::Close() noexcept
|
2016-12-20 16:37:36 +01:00
|
|
|
{
|
|
|
|
assert(in_audio_format.IsValid());
|
|
|
|
in_audio_format.Clear();
|
|
|
|
|
2017-01-03 10:49:09 +01:00
|
|
|
Cancel();
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
CloseFilter();
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:39:00 +01:00
|
|
|
void
|
2017-05-08 14:44:49 +02:00
|
|
|
AudioOutputSource::Cancel() noexcept
|
2017-01-11 15:39:00 +01:00
|
|
|
{
|
|
|
|
current_chunk = nullptr;
|
|
|
|
pipe.Cancel();
|
2017-01-11 15:38:55 +01:00
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (replay_gain_filter)
|
|
|
|
replay_gain_filter->Reset();
|
2017-01-11 15:38:55 +01:00
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (other_replay_gain_filter)
|
|
|
|
other_replay_gain_filter->Reset();
|
2017-01-11 15:38:55 +01:00
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (filter)
|
|
|
|
filter->Reset();
|
2017-01-11 15:39:00 +01:00
|
|
|
}
|
|
|
|
|
2016-12-20 16:37:36 +01:00
|
|
|
void
|
|
|
|
AudioOutputSource::OpenFilter(AudioFormat audio_format,
|
|
|
|
PreparedFilter *prepared_replay_gain_filter,
|
|
|
|
PreparedFilter *prepared_other_replay_gain_filter,
|
2017-12-27 11:21:43 +01:00
|
|
|
PreparedFilter &prepared_filter)
|
2016-12-20 16:37:36 +01:00
|
|
|
try {
|
|
|
|
assert(audio_format.IsValid());
|
|
|
|
|
|
|
|
/* the replay_gain filter cannot fail here */
|
2019-07-08 19:49:58 +02:00
|
|
|
if (prepared_other_replay_gain_filter) {
|
|
|
|
other_replay_gain_serial = 0;
|
|
|
|
other_replay_gain_filter =
|
|
|
|
prepared_other_replay_gain_filter->Open(audio_format);
|
|
|
|
}
|
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (prepared_replay_gain_filter) {
|
2017-01-23 17:55:04 +01:00
|
|
|
replay_gain_serial = 0;
|
2018-01-02 09:59:22 +01:00
|
|
|
replay_gain_filter =
|
2016-12-20 16:37:36 +01:00
|
|
|
prepared_replay_gain_filter->Open(audio_format);
|
|
|
|
|
2019-07-08 19:49:58 +02:00
|
|
|
audio_format = replay_gain_filter->GetOutAudioFormat();
|
|
|
|
|
|
|
|
assert(replay_gain_filter->GetOutAudioFormat() ==
|
|
|
|
other_replay_gain_filter->GetOutAudioFormat());
|
2017-01-23 17:55:04 +01:00
|
|
|
}
|
2016-12-20 16:37:36 +01:00
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
filter = prepared_filter.Open(audio_format);
|
2016-12-20 16:37:36 +01:00
|
|
|
} catch (...) {
|
|
|
|
CloseFilter();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-05-08 14:44:49 +02:00
|
|
|
AudioOutputSource::CloseFilter() noexcept
|
2016-12-20 16:37:36 +01:00
|
|
|
{
|
2018-01-02 09:59:22 +01:00
|
|
|
replay_gain_filter.reset();
|
|
|
|
other_replay_gain_filter.reset();
|
|
|
|
filter.reset();
|
2016-12-20 16:37:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstBuffer<void>
|
|
|
|
AudioOutputSource::GetChunkData(const MusicChunk &chunk,
|
2018-01-02 09:59:22 +01:00
|
|
|
Filter *current_replay_gain_filter,
|
2016-12-20 16:37:36 +01:00
|
|
|
unsigned *replay_gain_serial_p)
|
|
|
|
{
|
|
|
|
assert(!chunk.IsEmpty());
|
|
|
|
assert(chunk.CheckFormat(in_audio_format));
|
|
|
|
|
|
|
|
ConstBuffer<void> data(chunk.data, chunk.length);
|
|
|
|
|
|
|
|
assert(data.size % in_audio_format.GetFrameSize() == 0);
|
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
if (!data.empty() && current_replay_gain_filter != nullptr) {
|
|
|
|
replay_gain_filter_set_mode(*current_replay_gain_filter,
|
2016-12-20 16:37:36 +01:00
|
|
|
replay_gain_mode);
|
|
|
|
|
2018-01-02 22:03:38 +01:00
|
|
|
if (chunk.replay_gain_serial != *replay_gain_serial_p) {
|
2018-01-02 09:59:22 +01:00
|
|
|
replay_gain_filter_set_info(*current_replay_gain_filter,
|
2016-12-20 16:37:36 +01:00
|
|
|
chunk.replay_gain_serial != 0
|
|
|
|
? &chunk.replay_gain_info
|
|
|
|
: nullptr);
|
|
|
|
*replay_gain_serial_p = chunk.replay_gain_serial;
|
|
|
|
}
|
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
data = current_replay_gain_filter->FilterPCM(data);
|
2016-12-20 16:37:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstBuffer<void>
|
|
|
|
AudioOutputSource::FilterChunk(const MusicChunk &chunk)
|
|
|
|
{
|
2018-01-02 09:59:22 +01:00
|
|
|
auto data = GetChunkData(chunk, replay_gain_filter.get(),
|
2016-12-20 16:37:36 +01:00
|
|
|
&replay_gain_serial);
|
2017-11-10 19:24:33 +01:00
|
|
|
if (data.empty())
|
2016-12-20 16:37:36 +01:00
|
|
|
return data;
|
|
|
|
|
|
|
|
/* cross-fade */
|
|
|
|
|
|
|
|
if (chunk.other != nullptr) {
|
|
|
|
auto other_data = GetChunkData(*chunk.other,
|
2018-01-02 09:59:22 +01:00
|
|
|
other_replay_gain_filter.get(),
|
2016-12-20 16:37:36 +01:00
|
|
|
&other_replay_gain_serial);
|
2017-11-10 19:24:33 +01:00
|
|
|
if (other_data.empty())
|
2016-12-20 16:37:36 +01:00
|
|
|
return data;
|
|
|
|
|
|
|
|
/* if the "other" chunk is longer, then that trailer
|
|
|
|
is used as-is, without mixing; it is part of the
|
|
|
|
"next" song being faded in, and if there's a rest,
|
|
|
|
it means cross-fading ends here */
|
|
|
|
|
|
|
|
if (data.size > other_data.size)
|
|
|
|
data.size = other_data.size;
|
|
|
|
|
|
|
|
float mix_ratio = chunk.mix_ratio;
|
|
|
|
if (mix_ratio >= 0)
|
|
|
|
/* reverse the mix ratio (because the
|
|
|
|
arguments to pcm_mix() are reversed), but
|
|
|
|
only if the mix ratio is non-negative; a
|
|
|
|
negative mix ratio is a MixRamp special
|
|
|
|
case */
|
2020-03-16 07:33:21 +01:00
|
|
|
mix_ratio = 1.0f - mix_ratio;
|
2016-12-20 16:37:36 +01:00
|
|
|
|
|
|
|
void *dest = cross_fade_buffer.Get(other_data.size);
|
|
|
|
memcpy(dest, other_data.data, other_data.size);
|
|
|
|
if (!pcm_mix(cross_fade_dither, dest, data.data, data.size,
|
|
|
|
in_audio_format.format,
|
|
|
|
mix_ratio))
|
|
|
|
throw FormatRuntimeError("Cannot cross-fade format %s",
|
|
|
|
sample_format_to_string(in_audio_format.format));
|
|
|
|
|
|
|
|
data.data = dest;
|
|
|
|
data.size = other_data.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply filter chain */
|
|
|
|
|
2018-01-02 09:59:22 +01:00
|
|
|
return filter->FilterPCM(data);
|
2016-12-20 16:37:36 +01:00
|
|
|
}
|
2016-12-26 13:53:22 +01:00
|
|
|
|
|
|
|
bool
|
2016-12-26 20:02:15 +01:00
|
|
|
AudioOutputSource::Fill(Mutex &mutex)
|
2016-12-26 13:53:22 +01:00
|
|
|
{
|
|
|
|
if (current_chunk != nullptr && pending_tag == nullptr &&
|
2017-11-10 19:24:33 +01:00
|
|
|
pending_data.empty())
|
2019-08-27 19:04:51 +02:00
|
|
|
DropCurrentChunk();
|
2016-12-26 13:53:22 +01:00
|
|
|
|
|
|
|
if (current_chunk != nullptr)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
current_chunk = pipe.Get();
|
|
|
|
if (current_chunk == nullptr)
|
|
|
|
return false;
|
|
|
|
|
2017-12-20 14:43:29 +01:00
|
|
|
pending_tag = current_chunk->tag.get();
|
2016-12-26 13:53:22 +01:00
|
|
|
|
|
|
|
try {
|
2016-12-26 20:02:15 +01:00
|
|
|
/* release the mutex while the filter runs, because
|
|
|
|
that may take a while */
|
|
|
|
const ScopeUnlock unlock(mutex);
|
|
|
|
|
2016-12-26 13:53:22 +01:00
|
|
|
pending_data = pending_data.FromVoid(FilterChunk(*current_chunk));
|
|
|
|
} catch (...) {
|
|
|
|
current_chunk = nullptr;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioOutputSource::ConsumeData(size_t nbytes) noexcept
|
|
|
|
{
|
|
|
|
pending_data.skip_front(nbytes);
|
|
|
|
|
2017-11-10 19:24:33 +01:00
|
|
|
if (pending_data.empty())
|
2019-08-27 19:04:51 +02:00
|
|
|
DropCurrentChunk();
|
2016-12-26 13:53:22 +01:00
|
|
|
}
|
2018-01-01 19:22:45 +01:00
|
|
|
|
|
|
|
ConstBuffer<void>
|
|
|
|
AudioOutputSource::Flush()
|
|
|
|
{
|
|
|
|
return filter
|
|
|
|
? filter->Flush()
|
|
|
|
: nullptr;
|
|
|
|
}
|