-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtonesynth.h
88 lines (76 loc) · 2.29 KB
/
tonesynth.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Minimal Synthesizer for Qt applications
Copyright (C) 2022-2023 Pedro Lopez-Cabanillas <plcl@users.sf.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TONESYNTH_H
#define TONESYNTH_H
#include <QIODevice>
#include <QObject>
#include <QString>
#include <QMap>
#include <QAudioFormat>
class ToneSynthesizer : public QIODevice
{
Q_OBJECT
public:
enum class EnvelopeState : int {
silentState,
attackState,
sustainState,
releaseState
};
Q_ENUM(EnvelopeState)
ToneSynthesizer(const QAudioFormat &format);
qint64 readData(char *data, qint64 maxlen) override;
qint64 writeData(const char *data, qint64 len) override;
qint64 size() const override;
qint64 bytesAvailable() const override;
void setOctave(int newOctave);
qint64 lastBufferSize() const;
void resetLastBufferSize();
public slots:
void start();
void stop();
void noteOn(const QString& note);
void noteOff();
private:
QAudioFormat m_format;
int m_octave; /* octave 3 */
/* Equal temperament scale */
const QMap<QString, qreal> m_freq{
{"C'", 261.626},
{"B", 246.942},
{"A#", 233.082},
{"A", 220.000},
{"G#", 207.652},
{"G", 195.998},
{"F#", 184.997},
{"F", 174.614},
{"E", 164.814},
{"D#", 155.563},
{"D", 146.832},
{"C#", 138.591},
{"C", 130.813}
};
qreal m_angleDelta;
qreal m_currentAngle;
qint64 m_lastBufferSize;
// Envelope generation
EnvelopeState m_envelState;
qreal m_envelVolume;
qreal m_envelDelta;
quint64 m_envelCount;
quint64 m_attackTime;
quint64 m_releaseTime;
};
#endif // TONESYNTH_H