1 /* This file is part of Clementine.
2    Copyright 2010, David Sansome <me@davidsansome.com>
3 
4    Clementine is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    Clementine is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 // Copyright: (C) 2003 Mark Kretschmann
19 //           (C) 2004,2005 Max Howell, <max.howell@methylblue.com>
20 // License:   See COPYING
21 
22 #include "enginebase.h"
23 #include "core/timeconstants.h"
24 
25 #include <cmath>
26 
27 #include <QSettings>
28 
29 const char* Engine::Base::kSettingsGroup = "Player";
30 
Base()31 Engine::Base::Base()
32     : volume_(50),
33       beginning_nanosec_(0),
34       end_nanosec_(0),
35       scope_(kScopeSize),
36       fadeout_enabled_(true),
37       fadeout_duration_nanosec_(2 * kNsecPerSec),  // 2s
38       crossfade_enabled_(true),
39       autocrossfade_enabled_(false),
40       crossfade_same_album_(false),
41       next_background_stream_id_(0),
42       about_to_end_emitted_(false) {}
43 
~Base()44 Engine::Base::~Base() {}
45 
Load(const QUrl & url,TrackChangeFlags,bool force_stop_at_end,quint64 beginning_nanosec,qint64 end_nanosec)46 bool Engine::Base::Load(const QUrl& url, TrackChangeFlags,
47                         bool force_stop_at_end, quint64 beginning_nanosec,
48                         qint64 end_nanosec) {
49   Q_UNUSED(force_stop_at_end);
50 
51   url_ = url;
52   beginning_nanosec_ = beginning_nanosec;
53   end_nanosec_ = end_nanosec;
54 
55   about_to_end_emitted_ = false;
56   return true;
57 }
58 
SetVolume(uint value)59 void Engine::Base::SetVolume(uint value) {
60   volume_ = value;
61 
62   SetVolumeSW(MakeVolumeLogarithmic(value));
63 }
64 
MakeVolumeLogarithmic(uint volume)65 uint Engine::Base::MakeVolumeLogarithmic(uint volume) {
66   // We're using a logarithmic function to make the volume ramp more natural.
67   return static_cast<uint>(100 -
68                            100.0 * std::log10((100 - volume) * 0.09 + 1.0));
69 }
70 
ReloadSettings()71 void Engine::Base::ReloadSettings() {
72   QSettings s;
73   s.beginGroup(kSettingsGroup);
74 
75   fadeout_enabled_ = s.value("FadeoutEnabled", true).toBool();
76   fadeout_duration_nanosec_ =
77       s.value("FadeoutDuration", 2000).toLongLong() * kNsecPerMsec;
78   crossfade_enabled_ = s.value("CrossfadeEnabled", true).toBool();
79   autocrossfade_enabled_ = s.value("AutoCrossfadeEnabled", false).toBool();
80   crossfade_same_album_ = !s.value("NoCrossfadeSameAlbum", true).toBool();
81   fadeout_pause_enabled_ = s.value("FadeoutPauseEnabled", false).toBool();
82   fadeout_pause_duration_nanosec_ =
83       s.value("FadeoutPauseDuration", 250).toLongLong() * kNsecPerMsec;
84 }
85 
EmitAboutToEnd()86 void Engine::Base::EmitAboutToEnd() {
87   if (about_to_end_emitted_) return;
88 
89   about_to_end_emitted_ = true;
90   emit TrackAboutToEnd();
91 }
92 
AddBackgroundStream(const QUrl & url)93 int Engine::Base::AddBackgroundStream(const QUrl& url) { return -1; }
94 
Play(const QUrl & u,TrackChangeFlags c,bool force_stop_at_end,quint64 beginning_nanosec,qint64 end_nanosec)95 bool Engine::Base::Play(const QUrl& u, TrackChangeFlags c,
96                         bool force_stop_at_end, quint64 beginning_nanosec,
97                         qint64 end_nanosec) {
98   if (!Load(u, c, force_stop_at_end, beginning_nanosec, end_nanosec))
99     return false;
100 
101   return Play(0);
102 }
103