Skip to content

Commit 67913dc

Browse files
author
Artem Sharganov
committed
solve problems with fixed point math'
1 parent bce5670 commit 67913dc

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

trikControl/src/audioSynthDevices.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2016 Sharganov Artem and iakov
1+
/* Copyright 2016 Artem Sharganov and 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.
@@ -15,9 +15,8 @@
1515
#include "audioSynthDevices.h"
1616

1717
#include <QtMultimedia/QAudioDeviceInfo>
18-
#include <QsLog.h>
1918

20-
AudioSynthDeviceBuffered::AudioSynthDeviceBuffered(QObject *parent, int sampleRate, int sampleSize)
19+
AudioSynthDevice::AudioSynthDevice(QObject *parent, int sampleRate, int sampleSize)
2120
: QIODevice(parent)
2221
, mBuffer(0)
2322
, mPos(0)
@@ -27,12 +26,12 @@ AudioSynthDeviceBuffered::AudioSynthDeviceBuffered(QObject *parent, int sampleRa
2726

2827
}
2928

30-
AudioSynthDeviceBuffered::~AudioSynthDeviceBuffered()
29+
AudioSynthDevice::~AudioSynthDevice()
3130
{
3231

3332
}
3433

35-
void AudioSynthDeviceBuffered::start(int hzFreq)
34+
void AudioSynthDevice::start(int hzFreq)
3635
{
3736
open(QIODevice::ReadOnly);
3837
if(mBuffered) {
@@ -41,37 +40,40 @@ void AudioSynthDeviceBuffered::start(int hzFreq)
4140
generate(mBuffer.data(), length, hzFreq);
4241
}
4342
else mHzFreq = hzFreq;
43+
4444
}
4545

46-
void AudioSynthDeviceBuffered::stop()
46+
void AudioSynthDevice::stop()
4747
{
4848
newCall = true;
4949
mPos = 0;
5050
close();
5151
}
5252

5353
// Modefied coupled first-order form algorithm with fixed point arithmetic
54-
int AudioSynthDeviceBuffered::generate(char *data, int length, int hzFreq)
54+
int AudioSynthDevice::generate(char *data, int length, int hzFreq)
5555
{
5656
const int channelBytes = mSampleSize / 8;
5757

5858
qint64 maxlen = length/channelBytes;
5959

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;
60+
static const int M = 1 << 30;
61+
const auto w = hzFreq * M_PI / mSampleRate;
62+
const long long b1 = 2.0 * cos(w)*M;
63+
static const int AMPLITUDE = (1 << (mSampleSize - 1)) - 1;
6464

6565
unsigned char *ptr = reinterpret_cast<unsigned char *>(data);
6666

67-
static int y1;
68-
static int y2;
69-
static int y0;
67+
68+
// Need to save values between readData(...) calls, so static
69+
static long long y0 = 0;
70+
static decltype(y0) y1 = 0;
71+
static decltype(y0) y2 = 0;
7072

7173
if(newCall)
7274
{
73-
y1 = M * qSin(w);
74-
y2 = M * qSin(2*w);
75+
y1 = M * std::sin(-w);
76+
y2 = M * std::sin(-2*w);
7577
newCall = false;
7678
}
7779

@@ -94,10 +96,11 @@ int AudioSynthDeviceBuffered::generate(char *data, int length, int hzFreq)
9496

9597
ptr+=channelBytes;
9698
}
99+
97100
return i*channelBytes;
98101
}
99102

100-
qint64 AudioSynthDeviceBuffered::readData(char *data, qint64 len)
103+
qint64 AudioSynthDevice::readData(char *data, qint64 len)
101104
{
102105
if(mBuffered) {
103106
qint64 total = 0;
@@ -112,15 +115,15 @@ qint64 AudioSynthDeviceBuffered::readData(char *data, qint64 len)
112115
return generate(data, len, mHzFreq);
113116
}
114117

115-
qint64 AudioSynthDeviceBuffered::writeData(const char *data, qint64 len)
118+
qint64 AudioSynthDevice::writeData(const char *data, qint64 len)
116119
{
117120
Q_UNUSED(data);
118121
Q_UNUSED(len);
119122

120123
return 0;
121124
}
122125

123-
qint64 AudioSynthDeviceBuffered::bytesAvailable() const
126+
qint64 AudioSynthDevice::bytesAvailable() const
124127
{
125128
return mBuffer.size() + QIODevice::bytesAvailable();
126129
}

trikControl/src/audioSynthDevices.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2016 Sharganov Artem
1+
/* Copyright 2016 Artem Sharganov
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.
@@ -17,18 +17,17 @@
1717
#include <QtCore/QByteArray>
1818
#include <QtCore/QIODevice>
1919
#include <QtMultimedia/QAudioFormat>
20-
#include <qmath.h>
2120

2221
/// QIODevice that synthesize sine wave values
23-
class AudioSynthDeviceBuffered : public QIODevice
22+
class AudioSynthDevice : public QIODevice
2423
{
2524
Q_OBJECT
2625

2726
public:
2827
/// Constructor
29-
AudioSynthDeviceBuffered(QObject *parent, int sampleRate, int sampleSize);
28+
AudioSynthDevice(QObject *parent, int sampleRate, int sampleSize);
3029

31-
~AudioSynthDeviceBuffered();
30+
~AudioSynthDevice();
3231

3332
/// Provides reading from device
3433
qint64 readData(char *data, qint64 maxlen);
@@ -65,6 +64,7 @@ class AudioSynthDeviceBuffered : public QIODevice
6564
/// Mode of device
6665
bool mBuffered = false;
6766

67+
/// New call of playTone(...), not readData(...) call
6868
bool newCall = true;
6969
};
7070

trikControl/src/tonePlayer.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2016 Sharganov Artem
1+
/* Copyright 2016 Artem Sharganov
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.
@@ -15,24 +15,22 @@
1515

1616
#include "tonePlayer.h"
1717

18-
#include <QsLog.h>
1918

2019
namespace trikControl{
2120

2221
TonePlayer::TonePlayer()
2322
{
24-
2523
mTimer.setSingleShot(true);
2624
initializeAudio();
27-
mDevice = new AudioSynthDeviceBuffered(this, mFormat.sampleRate(), mFormat.sampleSize());
25+
mDevice = new AudioSynthDevice(this, mFormat.sampleRate(), mFormat.sampleSize());
2826
mOutput = new QAudioOutput(mFormat, this);
2927
}
3028

3129
void TonePlayer::initializeAudio()
3230
{
3331

3432
mFormat.setChannelCount(1);
35-
mFormat.setSampleRate(44100);
33+
mFormat.setSampleRate(16000);
3634
mFormat.setSampleSize(16);
3735
mFormat.setSampleType(QAudioFormat::SampleType::SignedInt);
3836
mFormat.setCodec("audio/pcm");
@@ -47,7 +45,6 @@ void TonePlayer::initializeAudio()
4745

4846
void TonePlayer::play(int hzFreq, int msDuration)
4947
{
50-
5148
mOutput->reset();
5249
switch (mOutput->state()) {
5350
case QAudio::IdleState: break;

trikControl/src/tonePlayer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2016 Sharganov Artem
1+
/* Copyright 2016 Artem Sharganov
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.
@@ -37,12 +37,12 @@ class TonePlayer : public QObject
3737
public slots:
3838

3939
/// Play sound
40-
void play(int freqHz,int durationMs);
40+
void play(int freqHz, int durationMs);
4141

4242
private:
4343
QAudioFormat mFormat;
4444

45-
AudioSynthDeviceBuffered *mDevice; // Has ownership.
45+
AudioSynthDevice *mDevice; // Has ownership.
4646

4747
QAudioOutput *mOutput; // Has ownership.
4848

0 commit comments

Comments
 (0)