-
Notifications
You must be signed in to change notification settings - Fork 8
/
Generator.cpp
208 lines (179 loc) · 4.84 KB
/
Generator.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "Generator.h"
#include <qdebug.h>
#include <QtGlobal>
#include <QtCore/QTime>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define SECONDS 1
#define SYSTEM_FREQ 44100
Generator::Generator(float secs, int freq)
:QIODevice( ), isGenerating(false), play_silent(true), max_read(65536)
{
finished = false;
buffer = new char[int(secs * SYSTEM_FREQ * 4) + 3000];
t=buffer;
m_freq = freq;
len=fillData(t, m_freq, secs); /* mono FREQHz sine */
pos = 0;
bytes_left = len;
}
Generator::Generator(Generator *copyFrom)
: QIODevice(), isGenerating(false), play_silent(true), max_read(65536)
{
buffer = new char[copyFrom->len];
memcpy(buffer, copyFrom->buffer, copyFrom->len);
t = buffer;
m_freq = copyFrom->m_freq;
len = copyFrom->len;
pos = 0;
bytes_left = len;
}
Generator::~Generator()
{
delete [] buffer;
}
void Generator::clearBuffer() {
delete buffer;
buffer = new char[4];
buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
t = buffer;
len = bytes_left = 4;
pos = 0;
isGenerating = false;
}
void Generator::appendDataFrom(const Generator *copyFrom) {
char *newbuf = new char[len + copyFrom->len];
memcpy(newbuf, buffer, len);
memcpy(newbuf + len, copyFrom->buffer, copyFrom->len);
len += copyFrom->len;
bytes_left += copyFrom->len;
delete buffer;
buffer = t = newbuf;
// qDebug() << "new left: "<< bytes_left;
}
void Generator::start()
{
//qDebug() << "starting";
if (!isGenerating) {
open(QIODevice::ReadOnly);
isGenerating = true;
}
}
void Generator::stop()
{
//qDebug() << "stopping";
close();
isGenerating = false;
}
int Generator::putShort(char *t, unsigned int value)
{
*(unsigned char *)(t++)=value&255;
*(unsigned char *)(t)=(value/256)&255;
return 2;
}
int Generator::fillData(char *start, int frequency, float seconds)
{
int i, len=0;
int value;
int ramp_samples = (int)(8e-3 * SYSTEM_FREQ); // ramp for 8ms
int total_samples = int(seconds*SYSTEM_FREQ);
//qDebug() << "filling";
for(i=0; i<total_samples; i++) {
if (frequency == 0.0)
value = 0;
else
value=(int)(32767.0*sin(2.0*M_PI*((double)(i))*(double)(frequency)/SYSTEM_FREQ));
if (i < ramp_samples || total_samples - i <= ramp_samples) {
int filter_sample = i < ramp_samples ? i : (total_samples - i - 1);
double filter = (1 - cos(M_PI * (double)filter_sample / (double)ramp_samples)) / 2;
value *= filter;
}
putShort(start, value);
start += 2;
len+=2;
}
bytes_left = len;
pos = 0;
//qDebug() << " fill len" << len;
return len;
}
void Generator::restartData()
{
bytes_left = len;
pos = 0;
qDebug() << " restarting" << bytes_left;
}
qint64 Generator::readData(char *data, qint64 maxlen)
{
int len = maxlen;
if (len > max_read)
len = max_read;
//qDebug() << "left: " << bytes_left << " / wanted: " << len;
if (len <= 0) {
return 0;
}
/* this is how it *should* be done, if the Qt output buffers didn't truncate things */
if (bytes_left == 0 && !play_silent)
bytes_left = -1;
if (bytes_left <= 0) {
if (play_silent) {
// qDebug() << "playing silent";
emit generatorDone();
memset(data, 0, len);
return max_read;
}
if (isGenerating) {
isGenerating = false;
emit generatorDone();
qDebug() << "done playing";
}
return -1;
}
if (len < bytes_left) {
// they wanted less than we have
memcpy(data, t+pos, len);
pos += len;
bytes_left -= len;
//qDebug() << "playing a chunk " << len;
return len;
} else if (bytes_left > 0) {
// Whats left
memcpy(data, t+pos, bytes_left);
int to_return = bytes_left;
bytes_left = 0;
pos=0;
//qDebug() << "playing remaining bytes " << to_return;
if (play_silent) {
memset(data + to_return, 0, len - to_return);
return max_read;
}
return to_return;
}
bytes_left = -1;
isGenerating = false;
emit generatorDone();
return 0;
}
qint64 Generator::writeData(const char *data, qint64 len)
{
qDebug() << " write";
Q_UNUSED(data);
Q_UNUSED(len);
return 0;
}
QTime Generator::timeLeft()
{
int secs = bytes_left/2/SYSTEM_FREQ;
int msec = ((bytes_left - 2*SYSTEM_FREQ*secs)*1000)/2/SYSTEM_FREQ;
return QTime(0, 0, secs, msec);
}
qint64 Generator::bytesAvailable() const
{
//qDebug() << "returning max read: " << max_read;
//qDebug() << "returning silent: " << play_silent;
if (play_silent)
return max_read;
return bytes_left + QIODevice::bytesAvailable();
}