summaryrefslogtreecommitdiffstats
path: root/rtlfmradiotunercontrol.cpp
blob: 83096b69ecc5e67add5d6f2e54c1808a76124843 (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
/* Copyright (C) 2016, The Qt Company Ltd. All Rights Reserved.
 *
 * 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 <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtCore/QProcess>

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

private:
    RtlFmRadioTunerControl *q;

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

    QProcess rtlFm;
    QProcess aplay;

private:
    QMap<int, QString> amFrequenciesTokyo = {
          {  594000, QString::fromUtf8("NHK Tokyo #1") }
        , {  693000, QString::fromUtf8("NHK Tokyo #2") }
        , {  810000, QString::fromUtf8("Eagle 810 (AFN TOKYO)") }
        , {  954000, QString::fromUtf8("TBS Radio Tokyo") }
        , { 1134000, QString::fromUtf8("Nippon Cultural Broadcasting") }
        , { 1242000, QString::fromUtf8("Nippon Broadcasting") }
    };

    QMap<int, QString> fmFrequenciesTokyo = {
          { 76100, QString::fromUtf8("Inter FM") }
        , { 77100, QString::fromUtf8("The Open University of Japan") }
        , { 80000, QString::fromUtf8("TOKYO FM") }
        , { 81300, QString::fromUtf8("J-WAVE") }
        , { 82500, QString::fromUtf8("NHK FM Tokyo") }
    };
};

RtlFmRadioTunerControl::Private::Private(RtlFmRadioTunerControl *parent)
    : q(parent)
    , state(QRadioTuner::StoppedState)
    , band(QRadioTuner::FM)
    , frequency(76100)
    , stereoMode(QRadioTuner::Auto)
    , signalStrength(100)
    , volume(50)
    , muted(false)
    , searching(false)
    , error(QRadioTuner::NoError)
    , searchOne(true)
    , step(0)
{
    connect(q, &RtlFmRadioTunerControl::stationFound, [this](int frequency, const QString &name) {
        qDebug() << frequency << name;
        if (rtlFm.state() != QProcess::NotRunning)
            q->start();
    });
    connect(q, &RtlFmRadioTunerControl::frequencyChanged, [this](int frequency) {
        QMap<int, QString> knownBands;
        switch (band) {
        case QRadioTuner::AM:
            knownBands = amFrequenciesTokyo;
            break;
        case QRadioTuner::FM:
            knownBands = fmFrequenciesTokyo;
            break;
        default:
            qFatal("%s:%d %d not supported", Q_FUNC_INFO, __LINE__, band);
            break;
        }
        if (knownBands.contains(frequency)) {
            emit q->stationFound(frequency, knownBands.value(frequency));
            if (timer.isActive() && searchOne)
                timer.stop();
        }
    });
    connect(&timer, &QTimer::timeout, [this]() {
        q->setFrequency(frequency + step);
    });

    connect(&rtlFm, &QProcess::readyReadStandardOutput, [this]() {
        aplay.write(rtlFm.readAllStandardOutput());
    });
    connect(&rtlFm, &QProcess::readyReadStandardError, [this]() {
        qDebug() << rtlFm.readAllStandardError();
    });
}

RtlFmRadioTunerControl::Private::~Private()
{
    aplay.kill();
    aplay.waitForFinished();
    rtlFm.kill();
    rtlFm.waitForFinished();
}

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)
{
    if (d->band == band) return;
    d->band = band;
    emit bandChanged(band);
}

bool RtlFmRadioTunerControl::isBandSupported(QRadioTuner::Band band) const
{
    switch (band) {
    case QRadioTuner::AM:
    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; // 1kHz
        break;
    case QRadioTuner::FM:
        ret = 100; // 100Hz
        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>(594000, 1242000);
        break;
    case QRadioTuner::FM:
        ret = qMakePair<int,int>(76100, 82500);
        break;
    default:
        break;
    }
    return ret;
}

void RtlFmRadioTunerControl::setFrequency(int frequency)
{
    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;
    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->muted = 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()
{
    stop();
    d->aplay.start(QStringLiteral("aplay"), QStringList() << QStringLiteral("-r") << QStringLiteral("48000") << QStringLiteral("-f") << QStringLiteral("S16_LE"));
    d->aplay.waitForStarted();
//    d->rtlFm.start(QStringLiteral("sudo"), QStringList() << QStringLiteral("rtl_fm -f %1 -M wbfm -s 200000 -r 48000").arg(d->frequency * 1000).split(" "));
    d->rtlFm.start(QStringLiteral("rtl_fm"), QStringList() << QStringLiteral("-f %1 -M wbfm -s 200000 -r 48000").arg(d->frequency * 1000).split(" "));
    d->rtlFm.waitForStarted();
    d->state = QRadioTuner::ActiveState;
}

void RtlFmRadioTunerControl::stop()
{
    d->aplay.kill();
    d->aplay.waitForFinished();
    d->rtlFm.kill();
    d->rtlFm.waitForFinished();
    d->state = QRadioTuner::StoppedState;
}

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);
}