summaryrefslogtreecommitdiff
blob: b2d276beab8a3921e397e6922501848067489d02 (plain)
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
--- Slim/Player/ReplayGain.pm.orig	2014-09-27 15:15:59.976908870 +0100
+++ Slim/Player/ReplayGain.pm	2014-09-27 15:14:48.202343905 +0100
@@ -74,10 +74,7 @@
 	return preventClipping( $track->replay_gain(), $track->replay_peak() );
 }
 
-# Based on code from James Sutula's Dynamic Transition Updater plugin,
-# this method determines whether tracks at a given offset from each
-# other in the playlist are similarly adjacent within the same album.
-sub trackAlbumMatch {
+sub findTracksByIndex {
 	my $class  = shift;
 	my $client = shift;
 	my $offset = shift;
@@ -114,10 +111,24 @@
 	# Get the track objects
 	my $current_url   = Slim::Player::Playlist::song($client, $current_index);
 	my $current_track = Slim::Schema->objectForUrl({ 'url' => $current_url, 'create' => 1, 'readTags' => 1 });
-	
+
 	my $compare_url   = Slim::Player::Playlist::song($client, $compare_index);
 	my $compare_track = Slim::Schema->objectForUrl({ 'url' => $compare_url, 'create' => 1, 'readTags' => 1 });
 
+	return ($current_track, $compare_track);
+}
+
+# Based on code from James Sutula's Dynamic Transition Updater plugin,
+# this method determines whether tracks at a given offset from each
+# other in the playlist are similarly adjacent within the same album.
+sub trackAlbumMatch {
+	my $class  = shift;
+	my $client = shift;
+	my $offset = shift;
+
+	my ($current_track, $compare_track) = $class->findTracksByIndex($client, $offset);
+	return if (!$current_track || !$compare_track);
+
 	if (!blessed($current_track) || !blessed($compare_track)) {
 
 		logError("Couldn't find object for track: [$current_track] or [$compare_track] !");
@@ -178,18 +189,88 @@
 	return 0;
 }
 
+# Identify whether the sample rates match between two tracks in a
+# client playlist. This is modelled after the trackAlbumMatch function
+# above.
+sub trackSampleRateMatch {
+	my $class  = shift;
+	my $client = shift;
+	my $offset = shift;
+
+	my ($current_track, $compare_track) = $class->findTracksByIndex($client, $offset);
+	return if (!$current_track || !$compare_track);
+
+	if (!blessed($current_track) || !blessed($compare_track)) {
+
+		logError("Couldn't find object for track: [$current_track] or [$compare_track] !");
+
+		return 0;
+	}
+
+	if (!$current_track->can('samplerate') || !$compare_track->can('samplerate')) {
+
+		logError("Couldn't a find valid object for track: [$current_track] or [$compare_track] !");
+
+		return 0;
+	}
+
+	# For remote tracks, get metadata from the protocol handler
+	if ( $current_track->remote ) {
+	  if ( !$compare_track->remote ) {
+			# Other track is not remote, fail
+			return;
+		}
+
+		my $current_meta = {};
+		my $compare_meta = {};
+
+		my $current_handler = Slim::Player::ProtocolHandlers->handlerForURL( $current_track->url );
+		my $compare_handler = Slim::Player::ProtocolHandlers->handlerForURL( $compare_track->url );
+
+		if ( $current_handler && $current_handler->can('getMetadataFor') ) {
+			$current_meta = $current_handler->getMetadataFor( $client, $current_track->url );
+		}
+
+		if ( $compare_handler && $compare_handler->can('getMetadataFor') ) {
+			$compare_meta = $compare_handler->getMetadataFor( $client, $compare_track->url );
+		}
+
+		if (   $current_meta->{samplerate}
+			&& $compare_meta->{samplerate}
+			&& $current_meta->{samplerate} eq $compare_meta->{samplerate}
+		) {
+			# Sample rate metadata matches
+			return 1;
+		}
+		else {
+			return;
+		}
+	}
+
+	# Check sample rates match
+	my $compare_rate = $compare_track->samplerate;
+	my $current_rate = $current_track->samplerate;
+	if ($compare_rate && $current_rate &&
+		($compare_rate == $current_rate)) {
+
+		return 1;
+	}
+
+	return 0;
+}
+
 # Bug 5119
 # Reduce the gain value if necessary to avoid clipping
 sub preventClipping {
 	my ( $gain, $peak ) = @_;
-	
+
 	if ( defined $peak && defined $gain && $peak > 0 ) {
 		my $noclip = -20 * ( log($peak) / log(10) );
 		if ( $noclip < $gain ) {
 			return $noclip;
 		}
 	}
-	
+
 	return $gain;
 }
 
--- Slim/Player/Squeezebox.pm.orig	2014-09-27 15:15:55.008731322 +0100
+++ Slim/Player/Squeezebox.pm	2014-09-27 15:15:24.451639324 +0100
@@ -994,19 +994,30 @@
 			}
 		}
 
+		# Don't do transitions if the sample rates of the two
+		# songs differ. This avoids some unpleasant white
+		# noise from (at least) the Squeezebox Touch when
+		# using the analogue outputs. This might be bug#1884.
+		if (!Slim::Player::ReplayGain->trackSampleRateMatch($master, -1)
+		    ||
+		    !Slim::Player::ReplayGain->trackSampleRateMatch($master, 1)) {
+			main::INFOLOG && $log->info('Overriding transition due to differing sample rates');
+			$transitionType = 0;
+		 }
+
 	}
-	
+
 	if ($transitionDuration > $client->maxTransitionDuration()) {
 		$transitionDuration = $client->maxTransitionDuration();
 	}
-	
+
 	if ( main::INFOLOG && $log->is_info ) {
 		$log->info(sprintf(
 			"Starting decoder with format: %s flags: 0x%x autostart: %s buffer threshold: %s output threshold: %s samplesize: %s samplerate: %s endian: %s channels: %s",
 			$formatbyte, $flags, $autostart, $bufferThreshold, $outputThreshold, $pcmsamplesize, $pcmsamplerate, $pcmendian, $pcmchannels,
 		));
 	}
-	
+
 	my $frame = pack 'aaaaaaaCCCaCCCNnN', (
 		's',	# command
 		$autostart,