CrossFade.cxx 3.87 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "CrossFade.hxx"
22
#include "Chrono.hxx"
23
#include "MusicChunk.hxx"
24
#include "AudioFormat.hxx"
25
#include "util/NumberParser.hxx"
26 27
#include "util/Domain.hxx"
#include "Log.hxx"
28

29
#include <assert.h>
30

31 32
static constexpr Domain cross_fade_domain("cross_fade");

33 34
gcc_pure
static float
35
mixramp_interpolate(const char *ramp_list, float required_db) noexcept
36
{
37 38
	float last_db = 0, last_secs = 0;
	bool have_last = false;
39 40 41 42 43 44 45 46

	/* 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. */
47 48 49
		char *endptr;
		const float db = ParseFloat(ramp_list, &endptr);
		if (endptr == ramp_list || *endptr != ' ')
50
			break;
51

52
		ramp_list = endptr + 1;
53 54

		/* Parse the time. */
55 56
		float secs = ParseFloat(ramp_list, &endptr);
		if (endptr == ramp_list || (*endptr != ';' && *endptr != 0))
57
			break;
58

59 60 61
		ramp_list = endptr;
		if (*ramp_list == ';')
			++ramp_list;
62 63 64 65 66 67 68 69 70 71

		/* Check for exact match. */
		if (db == required_db) {
			return secs;
		}

		/* Save if too quiet. */
		if (db < required_db) {
			last_db = db;
			last_secs = secs;
72
			have_last = true;
73 74 75 76
			continue;
		}

		/* If required db < any stored value, use the least. */
77
		if (!have_last)
78 79 80 81 82 83
			return secs;

		/* Finally, interpolate linearly. */
		secs = last_secs + (required_db - last_db) * (secs - last_secs) / (db - last_db);
		return secs;
	}
84

85
	return -1;
86
}
87

88
unsigned
89
CrossFadeSettings::Calculate(SignedSongTime total_time,
90 91 92 93
			     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,
94
			     unsigned max_chunks) const noexcept
95
{
96 97
	unsigned int chunks = 0;
	float chunks_f;
98

99 100
	if (total_time.IsNegative() ||
	    duration < 0 || duration >= total_time.ToDoubleS() ||
101
	    /* we can't crossfade when the audio formats are different */
102
	    af != old_format)
103 104
		return 0;

105
	assert(duration >= 0);
106
	assert(af.IsValid());
107

108
	chunks_f = (float)af.GetTimeToSize() / (float)CHUNK_SIZE;
109

110
	if (mixramp_delay <= 0 || !mixramp_start || !mixramp_prev_end) {
111 112
		chunks = (chunks_f * duration + 0.5);
	} else {
113
		/* Calculate mixramp overlap. */
114 115 116 117 118 119 120 121 122 123 124
		const float mixramp_overlap_current =
			mixramp_interpolate(mixramp_start,
					    mixramp_db - replay_gain_db);
		const float mixramp_overlap_prev =
			mixramp_interpolate(mixramp_prev_end,
					    mixramp_db - replay_gain_prev_db);
		const float mixramp_overlap =
			mixramp_overlap_current + mixramp_overlap_prev;

		if (mixramp_overlap_current >= 0 &&
		    mixramp_overlap_prev >= 0 &&
125
		    mixramp_delay <= mixramp_overlap) {
126
			chunks = (chunks_f * (mixramp_overlap - mixramp_delay));
127 128 129
			FormatDebug(cross_fade_domain,
				    "will overlap %d chunks, %fs", chunks,
				    mixramp_overlap - mixramp_delay);
130 131
		}
	}
132

133
	if (chunks > max_chunks) {
134
		chunks = max_chunks;
135 136
		LogWarning(cross_fade_domain,
			   "audio_buffer_size too small for computed MixRamp overlap");
137
	}
138 139 140

	return chunks;
}