CrossFade.cxx 4.16 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "AudioFormat.hxx"
24
#include "util/NumberParser.hxx"
25 26
#include "util/Domain.hxx"
#include "Log.hxx"
27

28 29
#include <cmath>

30
#include <assert.h>
31

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

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

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

54
		ramp_list = endptr + 1;
55 56

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

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

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

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

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

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

87
	return FloatDuration(-1);
88
}
89

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

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

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

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

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

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

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

	return chunks;
}