CrossFade.cxx 4.16 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 "CrossFade.hxx"
21
#include "Chrono.hxx"
22
#include "MusicChunk.hxx"
23
#include "pcm/AudioFormat.hxx"
24
#include "util/NumberParser.hxx"
25
#include "util/Domain.hxx"
26
#include "util/Math.hxx"
27
#include "Log.hxx"
28

29
#include <cassert>
30

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

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

	/* 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. */

46
	while (true) {
47
		/* Parse the dB value. */
48 49 50
		char *endptr;
		const float db = ParseFloat(ramp_list, &endptr);
		if (endptr == ramp_list || *endptr != ' ')
51
			break;
52

53
		ramp_list = endptr + 1;
54 55

		/* Parse the time. */
56
		FloatDuration duration{ParseFloat(ramp_list, &endptr)};
57
		if (endptr == ramp_list || (*endptr != ';' && *endptr != 0))
58
			break;
59

60 61 62
		ramp_list = endptr;
		if (*ramp_list == ';')
			++ramp_list;
63 64

		/* Check for exact match. */
65
		if (db == required_db) {
66
			return duration;
67 68 69 70 71
		}

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

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

		/* Finally, interpolate linearly. */
82 83
		duration = last_duration + (required_db - last_db) * (duration - last_duration) / (db - last_db);
		return duration;
84
	}
85

86
	return FloatDuration(-1);
87
}
88

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

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

106
	assert(duration > FloatDuration::zero());
107
	assert(af.IsValid());
108

109 110
	const auto chunk_duration =
		af.SizeToTime<FloatDuration>(sizeof(MusicChunk::data));
111

112 113
	if (mixramp_delay <= FloatDuration::zero() ||
	    !mixramp_start || !mixramp_prev_end) {
114
		chunks = lround(duration / chunk_duration);
115
	} else {
116
		/* Calculate mixramp overlap. */
117
		const auto mixramp_overlap_current =
118 119
			mixramp_interpolate(mixramp_start,
					    mixramp_db - replay_gain_db);
120
		const auto mixramp_overlap_prev =
121 122
			mixramp_interpolate(mixramp_prev_end,
					    mixramp_db - replay_gain_prev_db);
123
		const auto mixramp_overlap =
124 125
			mixramp_overlap_current + mixramp_overlap_prev;

126 127
		if (mixramp_overlap_current >= FloatDuration::zero() &&
		    mixramp_overlap_prev >= FloatDuration::zero() &&
128
		    mixramp_delay <= mixramp_overlap) {
129 130
			chunks = lround((mixramp_overlap - mixramp_delay)
					/ chunk_duration);
131 132
			FormatDebug(cross_fade_domain,
				    "will overlap %d chunks, %fs", chunks,
133
				    (mixramp_overlap - mixramp_delay).count());
134 135
		}
	}
136

137
	if (chunks > max_chunks) {
138
		chunks = max_chunks;
139 140
		LogWarning(cross_fade_domain,
			   "audio_buffer_size too small for computed MixRamp overlap");
141
	}
142 143 144

	return chunks;
}