-
Notifications
You must be signed in to change notification settings - Fork 0
/
Radio.cpp
433 lines (360 loc) · 11.6 KB
/
Radio.cpp
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Copyright 2005-2009 Last.fm Ltd.
- Primarily authored by Max Howell, Jono Cole and Doug Mansell
This file is part of the Last.fm Desktop Application Suite.
lastfm-desktop 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 3 of the License, or
(at your option) any later version.
lastfm-desktop 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.
You should have received a copy of the GNU General Public License
along with lastfm-desktop. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QThread>
#include <QTimer>
#include <cmath>
#include <lastfm/RadioTuner>
#include <phonon/mediaobject.h>
#include <phonon/backendcapabilities.h>
#include "Radio.h"
Radio::Radio( )
: m_audioOutput( 0 ),
m_mediaObject( 0 ),
m_state( Radio::Stopped ),
m_bErrorRecover( false )
{
initRadio();
}
Radio::~Radio()
{
// I'm not confident about the sleep code on Windows --mxcl
#ifndef WIN32
if (m_mediaObject->state() != Phonon::PlayingState)
return;
qreal starting_volume = m_audioOutput->volume();
//sigmoid curve
for (int x = 18; x >= -60; --x)
{
qreal y = x;
y /= 10;
y = qreal(1) / (qreal(1) + std::exp( -y ));
y *= starting_volume;
m_audioOutput->setVolume( y );
struct Thread : QThread { using QThread::msleep; };
Thread::msleep( 7 );
}
#endif
}
// fixme:
// todo:
// note:
// if the station is the same as current station (ie: the user hit stop then start)
// then we *don't* retune. norman is quite emphatic about this. :)
void
Radio::play( const lastfm::RadioStation& station )
{
if (m_state != Stopped)
{
//FIXME filthy! get us to a clean slate
State oldstate = m_state;
m_state = Stopped; //prevents stateChanged() doing anything
stop();
clear();
m_state = oldstate;
}
delete m_tuner;
m_tuner = 0;
if (0 == m_audioOutput) {
if (!initRadio()) {
changeState( Stopped );
return;
}
}
m_station = station;
m_tuner = new lastfm::RadioTuner( m_station );
connect( m_tuner, SIGNAL(title( QString )), SLOT(setStationName( QString )) );
connect( m_tuner, SIGNAL(trackAvailable()), SLOT(enqueue()) );
connect( m_tuner, SIGNAL(error( lastfm::ws::Error )), SLOT(onTunerError( lastfm::ws::Error )) );
changeState( TuningIn );
}
// play this radio station after the current track has finished
void
Radio::playNext( const lastfm::RadioStation& station )
{
if (m_state == Playing)
{
m_station = station;
m_tuner->retune( m_station );
}
}
void
Radio::enqueue()
{
if (m_state == Stopped) {
// this should be impossible. If we are stopped, then the GUI looks
// stopped too, and receiving tracks to play will result in a playing
// radio and a stopped GUI. NICE.
Q_ASSERT( 0 );
return;
}
phononEnqueue();
}
void
Radio::skip()
{
if (!m_mediaObject)
return;
// attempt to refill the phonon queue if it's empty
if (m_mediaObject->queue().isEmpty())
phononEnqueue();
QList<Phonon::MediaSource> q = m_mediaObject->queue();
if (q.size())
{
Phonon::MediaSource source = q.takeFirst();
m_mediaObject->setQueue( q );
m_mediaObject->setCurrentSource( source );
m_mediaObject->play();
}
else if (m_state != Stopped)
{
qDebug() << "queue empty";
// we are still waiting for a playlist to come back from the tuner
m_mediaObject->blockSignals( true ); //don't tell outside world that we stopped
m_mediaObject->stop();
m_mediaObject->setCurrentSource( QUrl() );
m_mediaObject->blockSignals( false );
changeState( TuningIn );
}
}
void
Radio::onTunerError( lastfm::ws::Error e )
{
// otherwise leave things be, we'll stop when we run out of content
if (m_state == TuningIn)
stop();
emit error( e );
}
void
Radio::stop()
{
delete m_tuner;
m_mediaObject->blockSignals( true ); //prevent the error state due to setting current source to null
m_mediaObject->stop();
m_mediaObject->clearQueue();
m_mediaObject->setCurrentSource( QUrl() );
m_mediaObject->blockSignals( false );
clear();
changeState( Stopped );
}
void
Radio::clear()
{
m_track = lastfm::Track();
m_station = lastfm::RadioStation();
delete m_tuner;
}
void
Radio::onPhononStateChanged( Phonon::State newstate, Phonon::State oldstate )
{
qDebug() << oldstate << " -> " << newstate;
if (m_mediaObject == 0) {
qDebug() << "m_mediaObject is null!";
return;
}
switch (newstate)
{
case Phonon::ErrorState:
if (m_mediaObject->errorType() == Phonon::FatalError) {
qWarning() << "Phonon fatal error:" << m_mediaObject->errorString();
emit error( lastfm::ws::UnknownError, QVariant( m_mediaObject->errorString() ));
deInitRadio();
changeState( Radio::Stopped );
} else {
// seems we need to clear the error state before trying to play again.
m_bErrorRecover = true;
m_mediaObject->stop();
}
break;
case Phonon::PausedState:
// if the play queue runs out we get this for some reason
// this means we are fetching new tracks still, we should show a
// tuning in state;
if (m_mediaObject->queue().size() == 0) {
qDebug() << "queue empty, going to TuningIn";
changeState( TuningIn );
}
break;
case Phonon::StoppedState:
if (m_bErrorRecover) {
m_bErrorRecover = false;
skip();
}
break;
case Phonon::BufferingState:
changeState( Buffering );
break;
case Phonon::PlayingState:
changeState( Playing );
break;
case Phonon::LoadingState:
break;
}
}
void
Radio::phononEnqueue()
{
if (m_mediaObject->queue().size() || !m_tuner)
return;
// keep only one track in the phononQueue
// Loop until we get a null url or a valid url.
for (;;)
{
// consume next track from the track source. a null track
// response means wait until the trackAvailable signal
lastfm::Track t = m_tuner->takeNextTrack();
if (t.isNull()) break;
// Invalid urls won't trigger the correct phonon
// state changes, so we must filter them.
if (!t.url().isValid()) continue;
m_track = t;
Phonon::MediaSource ms( t.url() );
// if we are playing a track now, enqueue, otherwise start now!
if (m_mediaObject->currentSource().url().isValid()) {
qDebug() << "enqueuing " << t;
m_mediaObject->enqueue( ms );
} else {
qDebug() << "starting " << t;
m_mediaObject->setCurrentSource( ms );
m_mediaObject->play();
}
break;
}
}
// onPhononCurrentSourceChanged happens always (even if the source is
// unplayable), so we use it to update our now playing track.
void
Radio::onPhononCurrentSourceChanged( const Phonon::MediaSource& )
{
lastfm::MutableTrack( m_track ).stamp();
if (m_mediaObject->state() == Phonon::PlayingState) {
emit trackSpooled( m_track );
} else {
changeState( Buffering );
emit trackSpooled( m_track );
}
}
void
Radio::changeState( Radio::State const newstate )
{
State const oldstate = m_state;
if (oldstate == newstate)
return;
qDebug().nospace() << newstate << " (was " << oldstate << ')';
m_state = newstate; // always assign state properties before you tell other
// objects about it
switch (newstate)
{
case TuningIn:
qDebug() << "Tuning to:" << m_station;
emit tuningIn( m_station );
break;
case Buffering:
break;
case Playing:
break;
case Stopped:
emit stopped();
break;
}
}
void
Radio::setStationName( const QString& s )
{
m_station.setTitle( s );
emit tuningIn( m_station );
}
void
Radio::onBuffering( int percent_filled )
{
Q_UNUSED(percent_filled);
}
void
Radio::onMutedChanged(bool muted)
{
//unicorn::GlobalSettings().setValue("Muted", muted);
}
void
Radio::onOutputDeviceChanged(const Phonon::AudioOutputDevice& newDevice)
{
qDebug() << "name: " << newDevice.name() << " description: " << newDevice.description();
}
void
Radio::onVolumeChanged(qreal vol)
{
//unicorn::GlobalSettings().setValue("Volume", vol);
}
void
Radio::onFinished()
{
// the play queue has come to a natural end
qDebug() << ".";
}
// returns true on successful initialisation
bool
Radio::initRadio()
{
Phonon::AudioOutput* audioOutput = new Phonon::AudioOutput( Phonon::MusicCategory, this );
qDebug() << audioOutput->name();
qDebug() << audioOutput->outputDevice().description();
qDebug() << audioOutput->outputDevice().name();
qDebug() << (audioOutput->isMuted() ? "muted,": "not-muted,") <<
(audioOutput->isValid() ? "valid,": "not-valid,") <<
audioOutput->volumeDecibel() << "db " <<
audioOutput->volume();
foreach (QByteArray a, audioOutput->outputDevice().propertyNames()) {
qDebug() << a << ":" << audioOutput->outputDevice().property(a);
}
QString audioOutputDeviceName = "";//TODO moose::Settings().audioOutputDeviceName();
if (audioOutputDeviceName.size())
{
foreach (Phonon::AudioOutputDevice d, Phonon::BackendCapabilities::availableAudioOutputDevices())
if (d.name() == audioOutputDeviceName) {
audioOutput->setOutputDevice( d );
break;
}
}
Phonon::MediaObject* mediaObject = new Phonon::MediaObject;
m_path = Phonon::createPath( mediaObject, audioOutput );
if (!m_path.isValid()) {
qDebug() << "Phonon::createPath failed";
// can't delete the mediaObject after a failed Phonon::createPath without a crash in phonon... (Qt 4.4.3)
// so, we leak it:
// mediaObject->deleteLater();
audioOutput->deleteLater();
return false;
}
mediaObject->setTickInterval( 1000 );
connect( mediaObject, SIGNAL(stateChanged( Phonon::State, Phonon::State )), SLOT(onPhononStateChanged( Phonon::State, Phonon::State )) );
connect( mediaObject, SIGNAL(bufferStatus(int)), SLOT(onBuffering(int)));
connect( mediaObject, SIGNAL(currentSourceChanged( Phonon::MediaSource )), SLOT(onPhononCurrentSourceChanged( Phonon::MediaSource )) );
connect( mediaObject, SIGNAL(aboutToFinish()), SLOT(phononEnqueue()) ); // this fires when the whole queue is about to finish
connect( mediaObject, SIGNAL(finished()), SLOT(onFinished()));
connect( mediaObject, SIGNAL(tick(qint64)), SIGNAL(tick(qint64)));
connect( audioOutput, SIGNAL(mutedChanged(bool)), SLOT(onMutedChanged(bool)));
connect( audioOutput, SIGNAL(outputDeviceChanged(Phonon::AudioOutputDevice)), SLOT(onOutputDeviceChanged(Phonon::AudioOutputDevice)));
connect( audioOutput, SIGNAL(volumeChanged(qreal)), SLOT(onVolumeChanged(qreal)));
m_audioOutput = audioOutput;
m_mediaObject = mediaObject;
return true;
}
void
Radio::deInitRadio()
{
// try to deleteLater and phonon crashes. poo.
// leak em... :(
m_audioOutput = 0;
m_mediaObject = 0;
}