2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 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"
|
2008-08-26 08:27:09 +02:00
|
|
|
#include "crossfade.h"
|
2009-01-07 18:06:22 +01:00
|
|
|
#include "pcm_mix.h"
|
2009-03-03 22:23:25 +01:00
|
|
|
#include "chunk.h"
|
2008-11-02 16:55:43 +01:00
|
|
|
#include "audio_format.h"
|
2008-11-02 17:07:13 +01:00
|
|
|
#include "tag.h"
|
2008-08-26 08:27:09 +02:00
|
|
|
|
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>
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "crossfade"
|
|
|
|
|
2010-03-22 02:24:24 +01:00
|
|
|
#ifdef G_OS_WIN32
|
2010-05-18 23:55:48 +02:00
|
|
|
static char *
|
|
|
|
strtok_r(char *str, const char *delim, G_GNUC_UNUSED char **saveptr)
|
|
|
|
{
|
|
|
|
return strtok(str, delim);
|
|
|
|
}
|
2010-03-22 02:24:24 +01:00
|
|
|
#endif
|
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
static float mixramp_interpolate(char *ramp_list, float required_db)
|
|
|
|
{
|
|
|
|
float db, secs, last_db = nan(""), last_secs = 0;
|
|
|
|
char *ramp_str, *save_str = NULL;
|
|
|
|
|
|
|
|
/* 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 tokens out of the input string. */
|
|
|
|
ramp_str = strtok_r(ramp_list, " ", &save_str);
|
|
|
|
|
|
|
|
/* Tell strtok to continue next time round. */
|
|
|
|
ramp_list = NULL;
|
|
|
|
|
|
|
|
/* Parse the dB value. */
|
|
|
|
if (NULL == ramp_str) {
|
|
|
|
return nan("");
|
|
|
|
}
|
|
|
|
db = (float)atof(ramp_str);
|
|
|
|
|
|
|
|
/* Parse the time. */
|
|
|
|
ramp_str = strtok_r(NULL, ";", &save_str);
|
|
|
|
if (NULL == ramp_str) {
|
|
|
|
return nan("");
|
|
|
|
}
|
|
|
|
secs = (float)atof(ramp_str);
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
if (isnan(last_db)) {
|
|
|
|
return secs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, interpolate linearly. */
|
|
|
|
secs = last_secs + (required_db - last_db) * (secs - last_secs) / (db - last_db);
|
|
|
|
return secs;
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2008-08-26 08:27:09 +02:00
|
|
|
unsigned cross_fade_calc(float duration, float total_time,
|
2010-03-21 18:21:47 +01:00
|
|
|
float mixramp_db, float mixramp_delay,
|
2010-05-08 09:19:44 +02:00
|
|
|
float replay_gain_db, float replay_gain_prev_db,
|
2010-03-21 18:21:47 +01:00
|
|
|
char *mixramp_start, char *mixramp_prev_end,
|
2008-09-07 19:19:55 +02:00
|
|
|
const struct audio_format *af,
|
2009-02-10 18:51:29 +01:00
|
|
|
const struct audio_format *old_format,
|
2008-08-26 08:27:09 +02:00
|
|
|
unsigned max_chunks)
|
|
|
|
{
|
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 */
|
|
|
|
!audio_format_equals(af, old_format))
|
2008-08-26 08:27:09 +02:00
|
|
|
return 0;
|
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
assert(duration >= 0);
|
2009-11-15 15:39:00 +01:00
|
|
|
assert(audio_format_valid(af));
|
2008-08-26 08:27:09 +02:00
|
|
|
|
2010-03-21 18:21:47 +01:00
|
|
|
chunks_f = (float)audio_format_time_to_size(af) / (float)CHUNK_SIZE;
|
|
|
|
|
|
|
|
if (isnan(mixramp_delay) || !(mixramp_start) || !(mixramp_prev_end)) {
|
|
|
|
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);
|
2010-03-21 18:21:47 +01:00
|
|
|
if (!isnan(mixramp_overlap) && (mixramp_delay <= mixramp_overlap)) {
|
|
|
|
chunks = (chunks_f * (mixramp_overlap - mixramp_delay));
|
|
|
|
g_debug("will overlap %d chunks, %fs", chunks,
|
|
|
|
mixramp_overlap - mixramp_delay);
|
|
|
|
}
|
|
|
|
}
|
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;
|
2010-03-21 18:21:47 +01:00
|
|
|
g_warning("audio_buffer_size too small for computed MixRamp overlap");
|
|
|
|
}
|
2008-08-26 08:27:09 +02:00
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|