-
Notifications
You must be signed in to change notification settings - Fork 2
/
mpd_0.21.xx_selective_resample_mode.patch
297 lines (273 loc) · 9.72 KB
/
mpd_0.21.xx_selective_resample_mode.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# MPD selective resampling (C) 2020 Marcel van de Weert
# https://github.com/bitkeeper/dddac1794build/
#
# License: GPL v2
#
# 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.
#
#
# SELECTIVE_RESAMPLE_MODE = ADHERE_BASE_CLOCK and [UPSAMPLE_ALL | UPSAMPLE_ONLY_48 | UPSAMPLE_ONLY_44]
#
# ADHERE_BASE_CLOCK - If the resampled target rate should be a multiple of the source base rate can be set with this flag.
# UPSAMPLE_ALL - Only upsampling.
# UPSAMPLE_ONLY_48 – only upsampling sources below 88.2kHz.
# UPSAMPLE_ONLY_44 – only upsampling when source is 44.1kHz or below.
#
# Where the flags have the following value:
# ADHERE_BASE_CLOCK = 0x08
# UPSAMPLE_ALL = 0x03
# UPSAMPLE_ONLY_48 = 0x02
# UPSAMPLE_ONLY_44= 0x01
#
diff --git a/src/AudioFormat.cxx b/src/AudioFormat.cxx
index 8d3440266..fe208e66b 100644
--- a/src/AudioFormat.cxx
+++ b/src/AudioFormat.cxx
@@ -22,15 +22,104 @@
#include "util/StringFormat.hxx"
#include <assert.h>
+#include <map>
+
+static const std::map<unsigned, unsigned> lut48to44 = {
+ {768000, 705600 },
+ {384000, 352800 },
+ {192000, 176400 },
+ {96000, 88200 },
+ {48000, 44100 }
+};
+
+static const std::map<unsigned, unsigned> lut44to48 = {
+ {705600, 384000 },
+ {352800, 192000 },
+ {176400, 96000 },
+ {88200, 48000 }
+};
+
+static constexpr unsigned ADHERE_BASE_CLOCK = 0x08;
+static constexpr unsigned UPSAMPLE_MASK = 0x03;
+
+static constexpr unsigned UP_AND_DOWN_SAMPLE = 0x0;
+static constexpr unsigned UPSAMPLE_ALL = 0x03;
+static constexpr unsigned UPSAMPLE_ONLY_44 = 0x01;
+static constexpr unsigned UPSAMPLE_ONLY_48 = 0x02;
+
+
+/**
+ * If source is based on 44.1k get nearest valid 44.1k variant from the target sample rate.
+ * For example if the source is 44.1k and the target is 96k it will return 88.2k.
+ *
+ * param source_rate the samplerate of the current source
+ * param target_rate the samplerate of which the source should be converted to.
+ * return the final sample rate of which the source should be converted to
+ * selective_resample_mode:
+ * 0 - up/down sample to match requisted sample rate (regular behaviour)
+ * 1 - only upsample
+ * 2 - only upsample below 88.2
+ * 3 - up/down sample to match requisted sample rate, adhere base clock (even diverder)
+ * 4 - only upsample, adhere base clock (even diverder)
+ * 5 - only upsample below 88.2, adhere base clock (even diverder)
+ */
+unsigned
+determine_selective_resample_rate(unsigned source_rate, unsigned target_rate, unsigned selective_resample_mode) noexcept;
+
+unsigned
+determine_selective_resample_rate(unsigned source_rate, unsigned target_rate, unsigned selective_resample_mode) noexcept
+{
+ const bool adhere_base_clock = (selective_resample_mode & ADHERE_BASE_CLOCK) == ADHERE_BASE_CLOCK;
+ bool resample = false;
+ unsigned out_sample_rate = source_rate;
+
+ if( target_rate ) {
+ switch(selective_resample_mode & UPSAMPLE_MASK) {
+ case UPSAMPLE_ALL:
+ resample = source_rate < target_rate;
+ break;
+ case UPSAMPLE_ONLY_44:
+ resample = source_rate == 44100;
+ break;
+ case UPSAMPLE_ONLY_48:
+ resample = source_rate < 88200;
+ break;
+ default:
+ resample = true;
+ }
+
+ if( resample ) {
+ if( adhere_base_clock &&
+ source_rate % 44100 == 0 &&
+ target_rate % 48000 == 0 &&
+ lut44to48.find(target_rate) != lut48to44.end() ) {
+ out_sample_rate = lut48to44.find(target_rate)->second;
+ } else if( adhere_base_clock &&
+ source_rate % 48000 == 0 &&
+ target_rate % 44100 == 0 &&
+ lut44to48.find(target_rate) != lut44to48.end() ) {
+ out_sample_rate = lut44to48.find(target_rate)->second;
+ } else {
+ out_sample_rate = target_rate;
+ }
+ }
+ }
+
+ return out_sample_rate;
+}
void
-AudioFormat::ApplyMask(AudioFormat mask) noexcept
+AudioFormat::ApplyMask(AudioFormat mask, unsigned selective_resample_mode) noexcept
{
assert(IsValid());
assert(mask.IsMaskValid());
- if (mask.sample_rate != 0)
- sample_rate = mask.sample_rate;
+ if (mask.sample_rate != 0) {
+ if(selective_resample_mode >= 1)
+ sample_rate = determine_selective_resample_rate(sample_rate, mask.sample_rate, selective_resample_mode);
+ else
+ sample_rate = mask.sample_rate;
+ }
if (mask.format != SampleFormat::UNDEFINED)
format = mask.format;
diff --git a/src/AudioFormat.hxx b/src/AudioFormat.hxx
index 7e411b12a..cedf10580 100644
--- a/src/AudioFormat.hxx
+++ b/src/AudioFormat.hxx
@@ -125,12 +125,12 @@ struct AudioFormat {
return !(*this == other);
}
- void ApplyMask(AudioFormat mask) noexcept;
+ void ApplyMask(AudioFormat mask, unsigned selective_resample_mode = 0) noexcept;
gcc_pure
- AudioFormat WithMask(AudioFormat mask) const noexcept {
+ AudioFormat WithMask(AudioFormat mask, unsigned selective_resample_mode = 0) const noexcept {
AudioFormat result = *this;
- result.ApplyMask(mask);
+ result.ApplyMask(mask, selective_resample_mode);
return result;
}
diff --git a/src/Main.cxx b/src/Main.cxx
index 7ca83c111..9010007a2 100644
--- a/src/Main.cxx
+++ b/src/Main.cxx
@@ -335,6 +335,15 @@ initialize_decoder_and_player(const ConfigData &config,
std::throw_with_nested(FormatRuntimeError("Failed to parse line %i",
param->line));
}
+
+ const unsigned selective_resample_mode =
+ config.GetUnsigned(ConfigOption::SELECTIVE_RESAMPLE_MODE, 0);
+ if(selective_resample_mode > 0 && configured_audio_format == AudioFormat::Undefined() ) {
+ FormatWarning(config_domain, "Missing audio_format, without selective_resample_mode is deactivated.");
+ } else if (selective_resample_mode > 0 && configured_audio_format.sample_rate % 48000 !=0 ) {
+ FormatWarning(config_domain, "For selective_resample_mode the used audio_format should be a multiply of 48kHz.");
+ }
+ partition.pc.SetSelectiveResampleMode( config.GetUnsigned(ConfigOption::SELECTIVE_RESAMPLE_MODE, 0) );
}
inline void
diff --git a/src/config/Option.hxx b/src/config/Option.hxx
index 03da8e378..d51dfdd7d 100644
--- a/src/config/Option.hxx
+++ b/src/config/Option.hxx
@@ -79,6 +79,7 @@ enum class ConfigOption {
DESPOTIFY_USER,
DESPOTIFY_PASSWORD,
DESPOTIFY_HIGH_BITRATE,
+ SELECTIVE_RESAMPLE_MODE,
MAX
};
diff --git a/src/config/Templates.cxx b/src/config/Templates.cxx
index 4da8031d4..a8fe9ecef 100644
--- a/src/config/Templates.cxx
+++ b/src/config/Templates.cxx
@@ -74,6 +74,7 @@ const ConfigTemplate config_param_templates[] = {
{ "despotify_user", false, true },
{ "despotify_password", false, true },
{ "despotify_high_bitrate", false, true },
+ { "selective_resample_mode" }
};
static constexpr unsigned n_config_param_templates =
diff --git a/src/decoder/Control.cxx b/src/decoder/Control.cxx
index 4962e194b..d1781c554 100644
--- a/src/decoder/Control.cxx
+++ b/src/decoder/Control.cxx
@@ -27,10 +27,12 @@
DecoderControl::DecoderControl(Mutex &_mutex, Cond &_client_cond,
const AudioFormat _configured_audio_format,
+ const unsigned _selective_resample_mode,
const ReplayGainConfig &_replay_gain_config) noexcept
:thread(BIND_THIS_METHOD(RunThread)),
mutex(_mutex), client_cond(_client_cond),
configured_audio_format(_configured_audio_format),
+ selective_resample_mode(_selective_resample_mode),
replay_gain_config(_replay_gain_config) {}
DecoderControl::~DecoderControl() noexcept
@@ -61,7 +63,7 @@ DecoderControl::SetReady(const AudioFormat audio_format,
assert(audio_format.IsValid());
in_audio_format = audio_format;
- out_audio_format = audio_format.WithMask(configured_audio_format);
+ out_audio_format = audio_format.WithMask(configured_audio_format, selective_resample_mode);
seekable = _seekable;
total_time = _duration;
diff --git a/src/decoder/Control.hxx b/src/decoder/Control.hxx
index b630a7283..0a6dd2c13 100644
--- a/src/decoder/Control.hxx
+++ b/src/decoder/Control.hxx
@@ -175,6 +175,8 @@ public:
const ReplayGainConfig replay_gain_config;
ReplayGainMode replay_gain_mode = ReplayGainMode::OFF;
+ const unsigned selective_resample_mode;
+
float replay_gain_db = 0;
float replay_gain_prev_db = 0;
@@ -188,6 +190,7 @@ public:
*/
DecoderControl(Mutex &_mutex, Cond &_client_cond,
const AudioFormat _configured_audio_format,
+ const unsigned selective_resample_mode,
const ReplayGainConfig &_replay_gain_config) noexcept;
~DecoderControl() noexcept;
diff --git a/src/player/Control.cxx b/src/player/Control.cxx
index b2042f1e5..5096a0d9f 100644
--- a/src/player/Control.cxx
+++ b/src/player/Control.cxx
@@ -35,7 +35,8 @@ PlayerControl::PlayerControl(PlayerListener &_listener,
buffer_chunks(_buffer_chunks),
configured_audio_format(_configured_audio_format),
thread(BIND_THIS_METHOD(RunThread)),
- replay_gain_config(_replay_gain_config)
+ replay_gain_config(_replay_gain_config),
+ selective_resample_mode(0)
{
}
diff --git a/src/player/Control.hxx b/src/player/Control.hxx
index 542536067..677563462 100644
--- a/src/player/Control.hxx
+++ b/src/player/Control.hxx
@@ -229,6 +229,8 @@ class PlayerControl final : public AudioOutputClient {
const ReplayGainConfig replay_gain_config;
+ unsigned selective_resample_mode;
+
FloatDuration total_play_time = FloatDuration::zero();
public:
@@ -318,6 +320,10 @@ public:
replay_gain_mode = _mode;
}
+ void SetSelectiveResampleMode(unsigned mode) noexcept {
+ selective_resample_mode = mode;
+ }
+
/**
* Like ReadTaggedSong(), but locks and unlocks the object.
*/
diff --git a/src/player/Thread.cxx b/src/player/Thread.cxx
index 286855136..c1ba1cbef 100644
--- a/src/player/Thread.cxx
+++ b/src/player/Thread.cxx
@@ -1153,6 +1153,7 @@ try {
DecoderControl dc(mutex, cond,
configured_audio_format,
+ selective_resample_mode,
replay_gain_config);
dc.StartThread();