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.
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}
0 commit comments