Skip to content

Commit bce5670

Browse files
author
Artem Sharganov
committed
implemented tone player
1 parent 5d32dcd commit bce5670

File tree

7 files changed

+351
-0
lines changed

7 files changed

+351
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/* Copyright 2016 Sharganov Artem and iakov
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#include "audioSynthDevices.h"
16+
17+
#include <QtMultimedia/QAudioDeviceInfo>
18+
#include <QsLog.h>
19+
20+
AudioSynthDeviceBuffered::AudioSynthDeviceBuffered(QObject *parent, int sampleRate, int sampleSize)
21+
: QIODevice(parent)
22+
, mBuffer(0)
23+
, mPos(0)
24+
, mSampleRate(sampleRate)
25+
, mSampleSize(sampleSize)
26+
{
27+
28+
}
29+
30+
AudioSynthDeviceBuffered::~AudioSynthDeviceBuffered()
31+
{
32+
33+
}
34+
35+
void AudioSynthDeviceBuffered::start(int hzFreq)
36+
{
37+
open(QIODevice::ReadOnly);
38+
if(mBuffered) {
39+
qint64 length = (mSampleRate * (mSampleSize / 8));
40+
mBuffer.resize(length);
41+
generate(mBuffer.data(), length, hzFreq);
42+
}
43+
else mHzFreq = hzFreq;
44+
}
45+
46+
void AudioSynthDeviceBuffered::stop()
47+
{
48+
newCall = true;
49+
mPos = 0;
50+
close();
51+
}
52+
53+
// Modefied coupled first-order form algorithm with fixed point arithmetic
54+
int AudioSynthDeviceBuffered::generate(char *data, int length, int hzFreq)
55+
{
56+
const int channelBytes = mSampleSize / 8;
57+
58+
qint64 maxlen = length/channelBytes;
59+
60+
static const int M = 1 << 16;
61+
const float w = hzFreq * M_PI / mSampleRate;
62+
const long b1 = 2.0 * cos(w)*M;
63+
static const int AMPLITUDE = (1 << (mSampleSize - 1))>>1;
64+
65+
unsigned char *ptr = reinterpret_cast<unsigned char *>(data);
66+
67+
static int y1;
68+
static int y2;
69+
static int y0;
70+
71+
if(newCall)
72+
{
73+
y1 = M * qSin(w);
74+
y2 = M * qSin(2*w);
75+
newCall = false;
76+
}
77+
78+
int i = 0;
79+
80+
for(i = 0; i < maxlen; ++i){
81+
82+
y0 = b1*y1 / M - y2;
83+
y2 = b1*y0 / M - y1;
84+
y1 = b1*y2 / M - y0;
85+
86+
if(mSampleSize == 8) {
87+
const qint8 val = static_cast<qint8>(y0*AMPLITUDE/M);
88+
*reinterpret_cast<quint8*>(ptr) = val;
89+
}
90+
if(mSampleSize == 16) {
91+
const qint16 val = static_cast<qint16>(y0*AMPLITUDE/M);
92+
*reinterpret_cast<quint16*>(ptr) = val;
93+
}
94+
95+
ptr+=channelBytes;
96+
}
97+
return i*channelBytes;
98+
}
99+
100+
qint64 AudioSynthDeviceBuffered::readData(char *data, qint64 len)
101+
{
102+
if(mBuffered) {
103+
qint64 total = 0;
104+
while (len - total > 0) {
105+
const qint64 chunk = qMin((mBuffer.size() - mPos), len - total);
106+
memcpy(data + total, mBuffer.constData() + mPos, chunk);
107+
mPos = (mPos + chunk) % mBuffer.size();
108+
total += chunk;
109+
}
110+
return total;
111+
} else
112+
return generate(data, len, mHzFreq);
113+
}
114+
115+
qint64 AudioSynthDeviceBuffered::writeData(const char *data, qint64 len)
116+
{
117+
Q_UNUSED(data);
118+
Q_UNUSED(len);
119+
120+
return 0;
121+
}
122+
123+
qint64 AudioSynthDeviceBuffered::bytesAvailable() const
124+
{
125+
return mBuffer.size() + QIODevice::bytesAvailable();
126+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Copyright 2016 Sharganov Artem
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#pragma once
16+
17+
#include <QtCore/QByteArray>
18+
#include <QtCore/QIODevice>
19+
#include <QtMultimedia/QAudioFormat>
20+
#include <qmath.h>
21+
22+
/// QIODevice that synthesize sine wave values
23+
class AudioSynthDeviceBuffered : public QIODevice
24+
{
25+
Q_OBJECT
26+
27+
public:
28+
/// Constructor
29+
AudioSynthDeviceBuffered(QObject *parent, int sampleRate, int sampleSize);
30+
31+
~AudioSynthDeviceBuffered();
32+
33+
/// Provides reading from device
34+
qint64 readData(char *data, qint64 maxlen);
35+
36+
/// Opens device, run generation in buffered mode
37+
void start(int hzFreq);
38+
39+
/// Close device and reset pose
40+
void stop();
41+
42+
/// Stub, because readonly device
43+
qint64 writeData(const char *data, qint64 len);
44+
45+
/// Returns amount of available bytes
46+
qint64 bytesAvailable() const;
47+
48+
private:
49+
/// Sythesize sine wave values
50+
int generate(char *data, int length, int hzFreq);
51+
52+
private:
53+
54+
/// Internal buffer, used in buffered mode
55+
QByteArray mBuffer;
56+
57+
qint64 mPos;
58+
59+
int mHzFreq;
60+
61+
const int mSampleRate;
62+
63+
const int mSampleSize;
64+
65+
/// Mode of device
66+
bool mBuffered = false;
67+
68+
bool newCall = true;
69+
};
70+

trikControl/src/brick.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "rangeSensor.h"
4545
#include "servoMotor.h"
4646
#include "soundSensor.h"
47+
#include "tonePlayer.h"
4748
#include "vectorSensor.h"
4849

4950
#include "mspBusAutoDetector.h"
@@ -71,6 +72,7 @@ Brick::Brick(const trikKernel::DifferentOwnerPointer<trikHal::HardwareAbstractio
7172
, const QString &modelConfig
7273
, const QString &mediaPath)
7374
: mHardwareAbstraction(hardwareAbstraction)
75+
, mTonePlayer(new TonePlayer())
7476
, mMediaPath(mediaPath)
7577
, mConfigurer(systemConfig, modelConfig)
7678
{
@@ -204,6 +206,22 @@ void Brick::playSound(const QString &soundFileName)
204206
}
205207
}
206208

209+
210+
void Brick::playTone(int hzFreq, int msDuration)
211+
{
212+
QLOG_INFO() << "Playing tone (" << hzFreq << "," << msDuration << ")";
213+
214+
if (msDuration < 10)
215+
return;
216+
if (hzFreq > 8000)
217+
return;
218+
if (hzFreq < 20)
219+
return;
220+
//mHardwareAbstraction->systemSound()->playTone(hzFreq, msDuration);
221+
//mTonePlayer->play(hzFreq, msDuration);
222+
QMetaObject::invokeMethod(mTonePlayer.data(), "play", Q_ARG(int, hzFreq), Q_ARG(int, msDuration));
223+
}
224+
207225
void Brick::say(const QString &text)
208226
{
209227
QStringList args{"-c", "espeak -v russian_test -s 100 \"" + text + "\""};

trikControl/src/brick.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PowerMotor;
4747
class PwmCapture;
4848
class RangeSensor;
4949
class ServoMotor;
50+
class TonePlayer;
5051
class VectorSensor;
5152

5253
/// Class representing TRIK controller board and devices installed on it, also provides access
@@ -83,6 +84,8 @@ public slots:
8384

8485
void playSound(const QString &soundFileName) override;
8586

87+
void playTone(int hzFreq, int msDuration);
88+
8689
void say(const QString &text) override;
8790

8891
void stop() override;
@@ -154,6 +157,7 @@ public slots:
154157
QScopedPointer<Keys> mKeys;
155158
QScopedPointer<Display> mDisplay;
156159
QScopedPointer<Led> mLed;
160+
QScopedPointer<TonePlayer> mTonePlayer;
157161

158162
QHash<QString, ServoMotor *> mServoMotors; // Has ownership.
159163
QHash<QString, PwmCapture *> mPwmCaptures; // Has ownership.

trikControl/src/tonePlayer.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Copyright 2016 Sharganov Artem
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
16+
#include "tonePlayer.h"
17+
18+
#include <QsLog.h>
19+
20+
namespace trikControl{
21+
22+
TonePlayer::TonePlayer()
23+
{
24+
25+
mTimer.setSingleShot(true);
26+
initializeAudio();
27+
mDevice = new AudioSynthDeviceBuffered(this, mFormat.sampleRate(), mFormat.sampleSize());
28+
mOutput = new QAudioOutput(mFormat, this);
29+
}
30+
31+
void TonePlayer::initializeAudio()
32+
{
33+
34+
mFormat.setChannelCount(1);
35+
mFormat.setSampleRate(44100);
36+
mFormat.setSampleSize(16);
37+
mFormat.setSampleType(QAudioFormat::SampleType::SignedInt);
38+
mFormat.setCodec("audio/pcm");
39+
40+
connect(&mTimer, SIGNAL(timeout()), this, SLOT(stop()));
41+
42+
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
43+
if (!info.isFormatSupported(mFormat)) {
44+
mFormat = info.nearestFormat(mFormat);
45+
}
46+
}
47+
48+
void TonePlayer::play(int hzFreq, int msDuration)
49+
{
50+
51+
mOutput->reset();
52+
switch (mOutput->state()) {
53+
case QAudio::IdleState: break;
54+
default:break;
55+
}
56+
57+
mTimer.setInterval(msDuration);
58+
mDevice->start(hzFreq);
59+
mTimer.start();
60+
mOutput->start(mDevice);
61+
}
62+
63+
void TonePlayer::stop()
64+
{
65+
mDevice->stop();
66+
mTimer.stop();
67+
mOutput->stop();
68+
69+
}
70+
}
71+
72+

trikControl/src/tonePlayer.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* Copyright 2016 Sharganov Artem
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#pragma once
16+
17+
#include "audioSynthDevices.h"
18+
19+
#include <QtCore/QObject>
20+
#include <QtCore/QTimer>
21+
#include <QtMultimedia/QAudioOutput>
22+
23+
24+
25+
namespace trikControl {
26+
27+
/// Tone player. Play tones
28+
class TonePlayer : public QObject
29+
{
30+
Q_OBJECT
31+
32+
public:
33+
34+
/// Constructor
35+
TonePlayer();
36+
37+
public slots:
38+
39+
/// Play sound
40+
void play(int freqHz,int durationMs);
41+
42+
private:
43+
QAudioFormat mFormat;
44+
45+
AudioSynthDeviceBuffered *mDevice; // Has ownership.
46+
47+
QAudioOutput *mOutput; // Has ownership.
48+
49+
QTimer mTimer;
50+
51+
void initializeAudio();
52+
53+
public slots:
54+
void stop();
55+
};
56+
}

0 commit comments

Comments
 (0)