Skip to content

Commit 64aa183

Browse files
authored
Merge pull request #177 from iakov/master
Tiny cleanup
2 parents 63bc9fe + 597575c commit 64aa183

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

trikControl/src/audioSynthDevices.cpp

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ void AudioSynthDevice::start(int hzFreq)
3636
{
3737
reset();
3838
mPos = 0;
39-
newCall = true;
4039
mHzFreq = hzFreq;
40+
41+
mOmega = mHzFreq * M_PI / mSampleRate;
42+
mY1 = -std::sin(mOmega) * M;
43+
mY2 = -std::sin(2 * mOmega) * M;
44+
mB = 2.0 * cos(mOmega) * M;
45+
4146
if (mBuffered) {
4247
const qint64 length = (mSampleRate * (mSampleSize / 8));
4348
mBuffer.resize(length);
44-
generate(mBuffer.data(), length, hzFreq);
49+
generate(mBuffer.data(), length);
4550
}
4651
emit readyRead();
4752
}
@@ -52,39 +57,28 @@ void AudioSynthDevice::stop()
5257
mHzFreq = 0;
5358
}
5459

55-
// Modefied coupled first-order form algorithm with fixed point arithmetic
56-
int AudioSynthDevice::generate(char *data, int length, int hzFreq)
60+
// Modified coupled first-order form algorithm with fixed point arithmetic
61+
int AudioSynthDevice::generate(char *data, int length)
5762
{
58-
if(hzFreq == 0) return 0;
59-
63+
if(mHzFreq == 0)
64+
return 0;
65+
6066
const int channelBytes = mSampleSize / 8;
6167

62-
qint64 maxlen = length / channelBytes;
68+
const qint64 maxlen = length / channelBytes;
6369

64-
static const int M = 1 << 30;
65-
const auto w = hzFreq * M_PI / mSampleRate;
66-
const long long b1 = 2.0 * cos(w) * M;
67-
static const int AMPLITUDE = (1 << (mSampleSize - 1)) - 1;
70+
const int AMPLITUDE = (1 << (mSampleSize - 1)) - 1;
6871

6972
unsigned char *ptr = reinterpret_cast<unsigned char *>(data);
7073

71-
// Need to save values between readData(...) calls, so static
72-
static long long y0 = 0;
73-
static decltype(y0) y1 = 0;
74-
static decltype(y0) y2 = 0;
75-
76-
if (newCall) {
77-
y1 = M * std::sin(-w);
78-
y2 = M * std::sin(-2 * w);
79-
newCall = false;
80-
}
74+
long long y0;
8175

8276
int i = 0;
8377

8478
for (i = 0; i < maxlen; ++i) {
85-
y0 = b1 * y1 / M - y2;
86-
y2 = b1 * y0 / M - y1;
87-
y1 = b1 * y2 / M - y0;
79+
y0 = mB * mY1 / M - mY2;
80+
mY2 = mB * y0 / M - mY1;
81+
mY1 = mB * mY2 / M - y0;
8882

8983
if (mSampleSize == 8) {
9084
const qint8 val = static_cast<qint8>(y0 * AMPLITUDE / M);
@@ -113,7 +107,7 @@ qint64 AudioSynthDevice::readData(char *data, qint64 len)
113107

114108
return total;
115109
} else {
116-
return generate(data, len, mHzFreq);
110+
return generate(data, len);
117111
}
118112
}
119113

trikControl/src/audioSynthDevices.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2016 Artem Sharganov
1+
/* Copyright 2016 Artem Sharganov, Iakov Kirilenko
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ class AudioSynthDevice : public QIODevice
4646

4747
private:
4848
/// Sythesize sine wave values
49-
int generate(char *data, int length, int hzFreq);
49+
int generate(char *data, int lengthBytes);
5050

5151
private:
5252

@@ -62,9 +62,16 @@ class AudioSynthDevice : public QIODevice
6262
const int mSampleSize;
6363

6464
/// Mode of device
65-
bool mBuffered = false;
65+
const bool mBuffered = false;
6666

67-
/// New call of playTone(...), not readData(...) call
68-
bool newCall = true;
67+
long long mY1 = 0;
68+
69+
long long mY2 = 0;
70+
71+
long long mB = 0;
72+
73+
static const int M = 1 << 30;
74+
75+
double mOmega;
6976
};
7077

trikControl/src/brick.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,10 @@ void Brick::playTone(int hzFreq, int msDuration)
211211
{
212212
QLOG_INFO() << "Playing tone (" << hzFreq << "," << msDuration << ")";
213213

214-
if (msDuration < 10)
214+
if (hzFreq < 0 || msDuration < 0)
215215
return;
216-
if (hzFreq > 8000)
217-
return;
218-
if (hzFreq < 20)
219-
return;
220-
//mHardwareAbstraction->systemSound()->playTone(hzFreq, msDuration);
221-
//mTonePlayer->play(hzFreq, msDuration);
216+
// mHardwareAbstraction->systemSound()->playTone(hzFreq, msDuration);
217+
// mTonePlayer->play(hzFreq, msDuration);
222218
QMetaObject::invokeMethod(mTonePlayer.data(), "play", Q_ARG(int, hzFreq), Q_ARG(int, msDuration));
223219
}
224220

@@ -232,6 +228,8 @@ void Brick::stop()
232228
{
233229
QLOG_INFO() << "Stopping brick";
234230

231+
mTonePlayer->stop();
232+
235233
for (ServoMotor * const servoMotor : mServoMotors.values()) {
236234
servoMotor->powerOff();
237235
}

trikControl/src/tonePlayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void TonePlayer::play(int hzFreq, int msDuration)
5050
case QAudio::ActiveState:
5151
mOutput->suspend();
5252
mDevice->reset();
53-
mOutput->resume();
54-
break;
53+
mOutput->resume();
54+
break;
5555
case QAudio::SuspendedState: mOutput->resume(); break;
5656
case QAudio::StoppedState: mOutput->start(mDevice); break;
5757
case QAudio::IdleState: mOutput->start(mDevice); break;

trikControl/src/tonePlayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public slots:
5151
void initializeAudio();
5252

5353
public slots:
54+
/// Stop playing
5455
void stop();
5556
};
5657
}

0 commit comments

Comments
 (0)