trikRuntime
audioSynthDevice.h
Go to the documentation of this file.
1 /* Copyright 2016 Artem Sharganov, Iakov Kirilenko
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 <cmath>
21 
23 class AudioSynthDevice : public QIODevice
24 {
25  Q_OBJECT
26 
27 public:
29  AudioSynthDevice(int sampleRate, int sampleSize, QObject *parent = nullptr);
30 
31  ~AudioSynthDevice() = default;
32 
34  qint64 readData(char *data, qint64 maxlen);
35 
37  void start(int hzFreq);
38 
40  void stop();
41 
43  qint64 writeData(const char *data, qint64 len);
44 
46  qint64 bytesAvailable() const;
47 
48 private:
50  int generate(char *data, int lengthBytes);
51 
52 private:
53 
55  QByteArray mBuffer;
56 
57  qint64 mPos;
58 
59  int mHzFreq;
60 
61  const int mSampleRate;
62 
63  const int mSampleSize;
64 
66  const bool mBuffered { false };
67 
68  long long mY1 { 0 };
69 
70  long long mY2 { 0 };
71 
72  long long mB { 0 };
73 
74  static constexpr int M = 1 << 30;
75 
76  double mOmega { std::nan("") };
77 };
78 
~AudioSynthDevice()=default
void start(int hzFreq)
Opens device, run generation in buffered mode.
Definition: audioSynthDevice.cpp:31
QIODevice that synthesize sine wave values.
Definition: audioSynthDevice.h:23
qint64 writeData(const char *data, qint64 len)
Stub, because readonly device.
Definition: audioSynthDevice.cpp:112
qint64 bytesAvailable() const
Returns amount of available bytes.
Definition: audioSynthDevice.cpp:120
qint64 readData(char *data, qint64 maxlen)
Provides reading from device.
Definition: audioSynthDevice.cpp:95
AudioSynthDevice(int sampleRate, int sampleSize, QObject *parent=nullptr)
Constructor.
Definition: audioSynthDevice.cpp:21
void stop()
Close device and reset pose.
Definition: audioSynthDevice.cpp:51