2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2013-01-04 10:31:59 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-08-26 08:27:09 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-08-26 08:27:09 +02:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-04 10:31:59 +01:00
|
|
|
#include "CrossFade.hxx"
|
2013-01-04 10:16:16 +01:00
|
|
|
#include "MusicChunk.hxx"
|
2013-08-03 21:00:50 +02:00
|
|
|
#include "AudioFormat.hxx"
|
2013-10-26 13:13:16 +02:00
|
|
|
#include "util/NumberParser.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "util/Domain.hxx"
|
|
|
|
#include "Log.hxx"
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-01-07 08:53:08 +01:00
|
|
|
#include <cmath>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
2010-03-21 18:21:47 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
static constexpr Domain cross_fade_domain("cross_fade");
|
|
|
|
|
2013-10-26 13:13:16 +02:00
|
|
|
gcc_pure
|
|
|
|
static float
|
|
|
|
mixramp_interpolate(const char *ramp_list, float required_db)
|
2010-05-18 23:55:48 +02:00
|
|
|
{
|
2013-10-26 13:13:16 +02:00
|
|
|
float last_db = nan(""), last_secs = 0;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
/* ramp_list is a string of pairs of dBs and seconds that describe the
|
|
|
|
* volume profile. Delimiters are semi-colons between pairs and spaces
|
|
|
|
* between the dB and seconds of a pair.
|
|
|
|
* The dB values must be monotonically increasing for this to work. */
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Parse the dB value. */
|
2013-10-26 13:13:16 +02:00
|
|
|
char *endptr;
|
|
|
|
const float db = ParseFloat(ramp_list, &endptr);
|
|
|
|
if (endptr == ramp_list || *endptr != ' ')
|
2013-10-26 13:14:16 +02:00
|
|
|
break;
|
2013-10-26 13:16:10 +02:00
|
|
|
|
2013-10-26 13:13:16 +02:00
|
|
|
ramp_list = endptr + 1;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
/* Parse the time. */
|
2013-10-26 13:13:16 +02:00
|
|
|
float secs = ParseFloat(ramp_list, &endptr);
|
|
|
|
if (endptr == ramp_list || (*endptr != ';' && *endptr != 0))
|
2013-10-26 13:14:16 +02:00
|
|
|
break;
|
2013-10-26 13:16:10 +02:00
|
|
|
|
2013-10-26 13:13:16 +02:00
|
|
|
ramp_list = endptr;
|
|
|
|
if (*ramp_list == ';')
|
|
|
|
++ramp_list;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
|
|
|
/* Check for exact match. */
|
|
|
|
if (db == required_db) {
|
|
|
|
return secs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save if too quiet. */
|
|
|
|
if (db < required_db) {
|
|
|
|
last_db = db;
|
|
|
|
last_secs = secs;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If required db < any stored value, use the least. */
|
2013-01-07 08:53:08 +01:00
|
|
|
if (std::isnan(last_db))
|
2010-03-21 18:21:47 +01:00
|
|
|
return secs;
|
|
|
|
|
|
|
|
/* Finally, interpolate linearly. */
|
|
|
|
secs = last_secs + (required_db - last_db) * (secs - last_secs) / (db - last_db);
|
|
|
|
return secs;
|
|
|
|
}
|
2013-10-26 13:14:16 +02:00
|
|
|
|
|
|
|
return nan("");
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2013-10-26 13:53:08 +02:00
|
|
|
unsigned
|
2013-10-29 00:14:27 +01:00
|
|
|
CrossFadeSettings::Calculate(float total_time,
|
|
|
|
float replay_gain_db, float replay_gain_prev_db,
|
|
|
|
const char *mixramp_start, const char *mixramp_prev_end,
|
|
|
|
const AudioFormat af,
|
|
|
|
const AudioFormat old_format,
|
|
|
|
unsigned max_chunks) const
|
2008-08-26 08:27:09 +02:00
|
|
|
{
|
2010-03-21 18:21:47 +01:00
|
|
|
unsigned int chunks = 0;
|
|
|
|
float chunks_f;
|
|
|
|
float mixramp_overlap;
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
if (duration < 0 || duration >= total_time ||
|
2009-02-10 18:51:29 +01:00
|
|
|
/* we can't crossfade when the audio formats are different */
|
2013-08-03 21:00:50 +02:00
|
|
|
af != old_format)
|
2008-08-26 08:27:09 +02:00
|
|
|
return 0;
|
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
assert(duration >= 0);
|
2013-08-03 21:00:50 +02:00
|
|
|
assert(af.IsValid());
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2013-08-03 21:00:50 +02:00
|
|
|
chunks_f = (float)af.GetTimeToSize() / (float)CHUNK_SIZE;
|
2010-03-21 18:21:47 +01:00
|
|
|
|
2013-10-30 16:50:34 +01:00
|
|
|
if (mixramp_delay <= 0 || !mixramp_start || !mixramp_prev_end) {
|
2010-03-21 18:21:47 +01:00
|
|
|
chunks = (chunks_f * duration + 0.5);
|
|
|
|
} else {
|
2010-05-08 09:19:44 +02:00
|
|
|
/* Calculate mixramp overlap. */
|
|
|
|
mixramp_overlap = mixramp_interpolate(mixramp_start, mixramp_db - replay_gain_db)
|
|
|
|
+ mixramp_interpolate(mixramp_prev_end, mixramp_db - replay_gain_prev_db);
|
2013-01-07 08:53:08 +01:00
|
|
|
if (!std::isnan(mixramp_overlap) &&
|
|
|
|
mixramp_delay <= mixramp_overlap) {
|
2010-03-21 18:21:47 +01:00
|
|
|
chunks = (chunks_f * (mixramp_overlap - mixramp_delay));
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(cross_fade_domain,
|
|
|
|
"will overlap %d chunks, %fs", chunks,
|
|
|
|
mixramp_overlap - mixramp_delay);
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
if (chunks > max_chunks) {
|
2008-08-26 08:27:09 +02:00
|
|
|
chunks = max_chunks;
|
2013-09-27 22:31:24 +02:00
|
|
|
LogWarning(cross_fade_domain,
|
|
|
|
"audio_buffer_size too small for computed MixRamp overlap");
|
2010-03-21 18:21:47 +01:00
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|