summaryrefslogtreecommitdiffstats
path: root/rtlfmradiotunercontrol.cpp
blob: 937411aabee9e5dfe04091ef53df0bfdeaaecbb8 (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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (C) 2016, The Qt Company Ltd. All Rights Reserved.
 * Copyright (C) 2016, 2017 Konsulko Group
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "rtlfmradiotunercontrol.h"
#include "rtl_fm.h"

#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtCore/QSettings>
#include <QtMultimedia/QAudioOutput>
#include "OutputBuffer.h"

// Structure to describe FM band plans, all values in Hz.
struct fmBandPlan {
    unsigned int minFreq;
    unsigned int maxFreq;
    unsigned int freqStep;
};

// Structure to hold output context for RTL-SDR callback
struct OutputContext {
    OutputBuffer *buffer;
    bool muted;
};

class RtlFmRadioTunerControl::Private
{
public:
    Private(RtlFmRadioTunerControl *parent);
    ~Private();

    bool muted() { return mOutputContext.muted; }
    void setMuted(bool muted) { mOutputContext.muted = muted; }
    void outputStart(void);
    void outputStop(void);

private:
    RtlFmRadioTunerControl *q;

public:
    QRadioTuner::State state;
    QRadioTuner::Band band;
    int frequency;
    QRadioTuner::StereoMode stereoMode;
    int signalStrength;
    int volume;
    bool searching;
    QRadioTuner::Error error;
    QString errorString;
    QTimer timer;
    bool searchOne;
    int step;
    bool present;
    struct fmBandPlan fmBandPlan;

private:
    void outputInit(void);

    static void rtlOutputThreadFunc(int16_t *result, int result_len, void *ctx);

    QAudioOutput* mAudioOutput;
    OutputContext mOutputContext;

    QMap<QString, struct fmBandPlan> knownFmBandPlans = {
        { QString::fromUtf8("US"), { .minFreq = 87900000, .maxFreq = 107900000, .freqStep = 200000 } },
        { QString::fromUtf8("JP"), { .minFreq = 76100000, .maxFreq = 89900000, .freqStep = 100000 } },
        { QString::fromUtf8("EU"), { .minFreq = 87500000, .maxFreq = 108000000, .freqStep = 50000 } },
        { QString::fromUtf8("ITU-1"), { .minFreq = 87500000, .maxFreq = 108000000, .freqStep = 50000 } },
        { QString::fromUtf8("ITU-2"), { .minFreq = 87900000, .maxFreq = 107900000, .freqStep = 50000 } }
    };
};

RtlFmRadioTunerControl::Private::Private(RtlFmRadioTunerControl *parent)
  : q(parent),
    state(QRadioTuner::StoppedState),
    band(QRadioTuner::FM),
    stereoMode(QRadioTuner::Auto),
    signalStrength(100),
    volume(50),
    searching(false),
    error(QRadioTuner::NoError),
    searchOne(true),
    step(0)
{
    outputInit();

    // Initialize RTL-SDR dongle
    present = false;
    if(rtl_fm_init(frequency, 200000, 48000, rtlOutputThreadFunc, &mOutputContext) < 0) {
        qDebug("%s: no RTL USB adapter?", Q_FUNC_INFO);
    } else {
        present = true;
    }

    // Initialize FM band plan
    QSettings settings("AGL", "qtmultimedia-rtlfm-radio-plugin");
    if(!settings.contains("fmbandplan"))
        settings.setValue("fmbandplan", QString::fromUtf8("US"));
    if(!knownFmBandPlans.contains(settings.value("fmbandplan").toString()))
        settings.setValue("fmbandplan", QString::fromUtf8("US"));
    fmBandPlan = knownFmBandPlans.value(settings.value("fmbandplan").toString());

    // Initialize frequency to lower bound of band plan
    frequency = fmBandPlan.minFreq;

    connect(q, &RtlFmRadioTunerControl::stationFound, [this](int frequency, const QString &name) {
        qDebug() << frequency << name;
    });
    connect(q, &RtlFmRadioTunerControl::frequencyChanged, [this](int frequency) {
	// Note that the following effectively disables any attempt at seeking.
	// If seeking support is required some logic will need to be added back
	// here (and elsewhere).
	if (searching) {
            emit q->stationFound(frequency, QString("%1 MHz").arg((double) frequency / 1000000, 0, 'f', 1));
	    if (timer.isActive() && searchOne)
	        timer.stop();
	}
    });
    connect(&timer, &QTimer::timeout, [this]() {
        q->setFrequency(frequency + step);
    });
}

void RtlFmRadioTunerControl::Private::outputInit(void)
{
    // Set up the format
    QAudioFormat format;
    format.setSampleRate(24000);
    format.setChannelCount(2);
    format.setSampleSize(16);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::SignedInt);

    QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
    if (!info.isFormatSupported(format)) {
        qWarning() << "Raw audio format not supported by backend, cannot play audio.";
	return;
    }

    // Create output
    mAudioOutput = new QAudioOutput(format);
    mAudioOutput->setCategory(QString("radio"));

    // Initialize context to connect RTL-SDR library to QAudioOutput,
    // including output buffer and muted state
    mOutputContext.buffer = new OutputBuffer(format, q);
    mOutputContext.muted = false;
}

void RtlFmRadioTunerControl::Private::outputStart(void)
{
    mOutputContext.buffer->start();
    mAudioOutput->start(mOutputContext.buffer);
}

void RtlFmRadioTunerControl::Private::outputStop(void)
{
    mAudioOutput->stop();
    mOutputContext.buffer->stop();
}

void RtlFmRadioTunerControl::Private::rtlOutputThreadFunc(int16_t *result, int result_len, void *ctx)
{
    const char *p = (const char *) result;
    OutputContext *context = reinterpret_cast<OutputContext*>(ctx);

    if (!context->muted) {
	context->buffer->writeData(p, result_len * 2);
    }
}

RtlFmRadioTunerControl::Private::~Private()
{
    delete mAudioOutput;
    delete mOutputContext.buffer;
    rtl_fm_cleanup();
}

RtlFmRadioTunerControl::RtlFmRadioTunerControl(QObject *parent)
  : QRadioTunerControl(parent),
    d(new Private(this))
{
}

RtlFmRadioTunerControl::~RtlFmRadioTunerControl()
{
    delete d;
}

QRadioTuner::State RtlFmRadioTunerControl::state() const
{
    return d->state;
}

QRadioTuner::Band RtlFmRadioTunerControl::band() const
{
    return d->band;
}

void RtlFmRadioTunerControl::setBand(QRadioTuner::Band band)
{
    // We only support FM, so this is a no-op, but leaving the code below
    // #if'ed out for reference.
#if 0
    if (d->band == band)
        return;
    d->band = band;
    emit bandChanged(band);
#else
    Q_UNUSED(band);
#endif
}

bool RtlFmRadioTunerControl::isBandSupported(QRadioTuner::Band band) const
{
    // We only support FM.
    switch (band) {
    case QRadioTuner::FM:
        return true;
    default:
        break;
    }
    return false;
}

int RtlFmRadioTunerControl::frequency() const
{
    return d->frequency;
}

int RtlFmRadioTunerControl::frequencyStep(QRadioTuner::Band band) const
{
    int ret = 0;

    switch (band) {
    case QRadioTuner::AM:
        ret = 1000; // 1 kHz
        break;
    case QRadioTuner::FM:
        ret = d->fmBandPlan.freqStep;
        break;
    default:
        break;
    }
    return ret;
}

QPair<int,int> RtlFmRadioTunerControl::frequencyRange(QRadioTuner::Band band) const
{
    QPair<int,int> ret;

    switch (band) {
    case QRadioTuner::AM:
        ret = qMakePair<int,int>(531000, 1602000);
        break;
    case QRadioTuner::FM:
        ret = qMakePair<int,int>(d->fmBandPlan.minFreq, d->fmBandPlan.maxFreq);
        break;
    default:
        break;
    }
    return ret;
}

void RtlFmRadioTunerControl::setFrequency(int frequency)
{
    if(!d->present)
        return;

    QPair<int,int> range = frequencyRange(d->band);
    frequency = qBound(range.first, frequency, range.second);
    if (d->frequency == frequency) {
        if (d->timer.isActive())
            d->timer.stop();
        return;
    }
    d->frequency = frequency;
    rtl_fm_set_freq(frequency);
    emit frequencyChanged(frequency);
}

bool RtlFmRadioTunerControl::isStereo() const
{
    bool ret = false;

    switch (d->stereoMode) {
    case QRadioTuner::ForceStereo:
    case QRadioTuner::Auto:
        ret = true;
        break;
    default:
        break;
    }
    return ret;
}

QRadioTuner::StereoMode RtlFmRadioTunerControl::stereoMode() const
{
    return d->stereoMode;
}

void RtlFmRadioTunerControl::setStereoMode(QRadioTuner::StereoMode stereoMode)
{
    if (d->stereoMode == stereoMode)
        return;
    bool prevStereo = isStereo();
    d->stereoMode = stereoMode;
    bool currentStereo = isStereo();
    if (prevStereo != currentStereo)
        emit stereoStatusChanged(currentStereo);
}

int RtlFmRadioTunerControl::signalStrength() const
{
    return d->signalStrength;
}

int RtlFmRadioTunerControl::volume() const
{
    return d->volume;
}

void RtlFmRadioTunerControl::setVolume(int volume)
{
    volume = qBound(0, volume, 100);
    if (d->volume == volume)
        return;
    d->volume = volume;
    emit volumeChanged(volume);
}

bool RtlFmRadioTunerControl::isMuted() const
{
    return d->muted();
}

void RtlFmRadioTunerControl::setMuted(bool muted)
{
    if (d->muted() == muted)
        return;
    d->setMuted(muted);
    emit mutedChanged(muted);
}

bool RtlFmRadioTunerControl::isSearching() const
{
    return d->searching;
}

void RtlFmRadioTunerControl::searchForward()
{
    d->searchOne = true;
    d->step = frequencyStep(d->band);
    d->timer.start(100);
    setSearching(true);
}

void RtlFmRadioTunerControl::searchBackward()
{
    d->searchOne = true;
    d->step = -frequencyStep(d->band);
    d->timer.start(250);
    setSearching(true);
}

void RtlFmRadioTunerControl::searchAllStations(QRadioTuner::SearchMode searchMode)
{
    Q_UNUSED(searchMode)
    d->searchOne = false;
    d->frequency = frequencyRange(d->band).first;
    d->step = frequencyStep(d->band);
    d->timer.start(250);
    setSearching(true);
}

void RtlFmRadioTunerControl::cancelSearch()
{
    d->timer.stop();
    setSearching(false);
}

void RtlFmRadioTunerControl::setSearching(bool searching)
{
    if (d->searching == searching)
        return;
    d->searching = searching;
    emit searchingChanged(searching);
}

void RtlFmRadioTunerControl::start()
{
    if(!d->present)
        return;

    qDebug() << "RtlFmRadioTunerControl::start - enter\n";
    if(d->state == QRadioTuner::ActiveState) {
        qDebug() << "RtlFmRadioTunerControl::start - already running, exit\n";
        return;
    }

    //rtl_fm_stop();
    rtl_fm_start();
    d->outputStart();
    d->state = QRadioTuner::ActiveState;
    emit stateChanged(d->state);
    qDebug() << "RtlFmRadioTunerControl::start - exit\n";
}

void RtlFmRadioTunerControl::stop()
{
    if(!d->present)
        return;

    qDebug() << "RtlFmRadioTunerControl::stop - enter\n";
    if(d->state == QRadioTuner::StoppedState) {
        qDebug() << "RtlFmRadioTunerControl::stop - already stopped, exit\n";
        return;
    }

    rtl_fm_stop();
    d->outputStop();
    d->state = QRadioTuner::StoppedState;
    emit stateChanged(d->state);
    qDebug() << "RtlFmRadioTunerControl::stop - exit\n";
}

QRadioTuner::Error RtlFmRadioTunerControl::error() const
{
    return d->error;
}

QString RtlFmRadioTunerControl::errorString() const
{
    return d->errorString;
}

void RtlFmRadioTunerControl::setError(QRadioTuner::Error error, const QString &errorString)
{
    d->error = error;
    d->errorString = errorString;
    emit QRadioTunerControl::error(error);
}