/
audio.cpp
148 lines (122 loc) · 2.87 KB
/
audio.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "audio.h"
Audio::Audio(int sr, int cs)
{
SAMPLE_RATE = sr;
CHUNK_SIZE = cs;
qDebug() << "Sample Rate: " << SAMPLE_RATE;
qDebug() << "Chunk Size: " << CHUNK_SIZE;
}
Audio::~Audio()
{
snd_pcm_drain(handle);
snd_pcm_close(handle);
delete handle;
delete buffer;
delete translator;
}
void Audio::init()
{
device = "default";
buffer = new float[CHUNK_SIZE]();
synth = new Synth(&SAMPLE_RATE, &CHUNK_SIZE);
// baseHz, octave, range, quantize, divisions, interpolate, steps
translator = new Translator(110, 2, 3, false, 12, false, 3);
synth->setTone(Wavetable::SINE);
prepareDevice();
qDebug() << "Audio thread: " << QThread::currentThreadId();
for (int i = 0; i < CHUNK_SIZE; i++)
buffertest.push_back(&buffer[i]);
connect(translator, SIGNAL(sendDataToSynth(int,float,float)), synth, SLOT(sendData(int, float, float)));
emit initDone();
}
void Audio::prepareDevice()
{
snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0);
snd_pcm_set_params(handle, SND_PCM_FORMAT_FLOAT, SND_PCM_ACCESS_RW_INTERLEAVED,
1, SAMPLE_RATE, 1, 100000);
}
void Audio::run()
{
if (!initialized)
{
init();
initialized = true;
qDebug() << "Audio components initialized.";
}
if (synth->masterPlaying && synth->isPlaying())
{
replaceBuffer(synth->genChunk());
playBuffer();
// resetBuffer();
}
if (state != snd_pcm_state(handle))
{
state = snd_pcm_state(handle);
qDebug() << "State: " << state;
}
// Buffer underrun (not being written to fast enough)
if (state == 4)
{
snd_pcm_drain(handle);
prepareDevice();
}
}
void Audio::addToBuffer(float* newBuffer)
{
for (int i = 0; i < CHUNK_SIZE; i++)
buffer[i] += newBuffer[i];
}
void Audio::addToBuffer(std::vector<float>* newBuffer)
{
for (int i = 0; i < CHUNK_SIZE; i++)
buffer[i] += newBuffer[0][i];
}
void Audio::replaceBuffer(std::vector<float>* newBuffer)
{
for (int i = 0; i < CHUNK_SIZE; i++)
buffer[i] = newBuffer[0][i];
}
void Audio::resetBuffer()
{
for (int i = 0; i < CHUNK_SIZE; i++)
buffer[i] = 0;
}
void Audio::playBuffer()
{
frames = snd_pcm_writei(handle, buffer, CHUNK_SIZE);
}
void Audio::pause()
{
synth->playing = false;
snd_pcm_pause(handle, 1);
}
void Audio::resume()
{
synth->playing = true;
snd_pcm_pause(handle, 0);
}
void Audio::setTone(int waveCode)
{
synth->setTone(waveCode);
}
void Audio::sendFreq(float freq)
{
synth->sendFreq(freq);
}
void Audio::sendFreq(int oscNum, float freq)
{
synth->sendFreq(oscNum, freq);
}
void Audio::checkPlaying()
{
if (finalPoints->size())
{
synth->playing = true;
translator->translate();
}
else
{
resetBuffer();
synth->playing = false;
}
}