-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
portedbox.cpp
212 lines (169 loc) · 5.23 KB
/
portedbox.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
209
210
211
212
#include <QLocale>
#include <QDebug>
#include <cmath>
#include "portedbox.h"
PortedBox::PortedBox(double volume, double resfreq, unsigned int portnum, double portdiam, double portlen) :
box(volume)
{
setResFreq(resfreq);
portNum = portnum;
setPortDiam(portdiam);
setPortLen(portlen);
slotActivated = false;
}
void PortedBox::setBoxVolume(double vol)
{
box.setVolume(vol);
}
void PortedBox::setPortNum(unsigned int value)
{
portNum = value;
}
void PortedBox::setPortLen(double len)
{
portLen = double ((int)(len * 100) / 100.0);
}
void PortedBox::setPortDiam(double diam)
{
portDiam = double ((int)(diam * 100) / 100.0);
}
void PortedBox::setSlotWidth(double width)
{
slotWidth = double ((int)(width * 100) / 100.0);
}
void PortedBox::setResFreq(double value)
{
resFreq = double ((int)(value * 100) / 100.0);
}
void PortedBox::setSlotPortActivated(bool enable)
{
slotActivated = enable;
}
double PortedBox::getBoxVolume() const
{
return box.getVolume();
}
unsigned int PortedBox::getPortNum() const
{
return portNum;
}
double PortedBox::getPortLen() const
{
if (!slotActivated)
return portLen;
double x = getSlotWidth() / getSlotHeight();
if (x < 1) {
x = getSlotHeight() / getSlotWidth();
}
/* estimated from https://sites.google.com/site/francisaudio69/ */
return double ((int)((1.1 - log10(x + 1.1) * 0.35) * portLen * 100) / 100.0);
}
double PortedBox::getPortDiam() const
{
return portDiam;
}
bool PortedBox::getSlotPortActivated() const
{
return slotActivated;
}
double PortedBox::getSlotWidth() const
{
return slotWidth;
}
double PortedBox::getSlotHeight() const
{
/* width is manual, height is computed from eq. diam. */
return double ((int)((M_PI * pow(portDiam, 2.0)) / (4.0 * slotWidth) * 100) / 100.0);
}
double PortedBox::getResFreq() const
{
return resFreq;
}
void PortedBox::updatePorts(double sd, double xmax)
{
/* compute minimum port diameter and optionaly set it */
double vol = sd * xmax * 0.001; /* m³ */
double dmin = 100 * (20.3 * pow((pow(vol, 2.0) / getResFreq()), 0.25)) / sqrt(getPortNum()); /* cm */
setPortDiam(dmin);
updateSlots();
updatePortsLength();
}
void PortedBox::updateSlots()
{
/* compute square slot */
double diam = getPortDiam();
double s = sqrt(M_PI * pow(diam, 2.0) / 4.0);
/* consider "width" over "height" and reset if too small */
if (slotWidth == 0.0)
slotWidth = double ((int)(s * 100) / 100.0);
}
void PortedBox::updatePortsLength()
{
/* compute resulting length */
double diam = getPortDiam();
double res = getResFreq();
double vol = getBoxVolume();
double num = getPortNum();
double plen = ((23562.5 * pow(diam, 2.0)) * num / (vol * pow(res, 2.0))) - (K * diam);
setPortLen(plen);
}
QDomElement PortedBox::toDomElement(QDomDocument& doc) const
{
QLocale c(QLocale::C);
QDomElement e = Box::toDomElement(doc);
e.setAttribute("type", "ported");
e.setAttribute("res", c.toString(resFreq));
QDomElement b = box.toDomElement(doc);
e.appendChild(b);
QDomElement p = doc.createElement("port");
p.setAttribute("num", c.toString(portNum));
p.setAttribute("len", c.toString(portLen));
p.setAttribute("diam", c.toString(portDiam));
p.setAttribute("width", c.toString(slotWidth));
p.setAttribute("slot", c.toString(slotActivated ? 1 : 0));
e.appendChild(p);
return e;
}
void PortedBox::fromDomElement(const QDomElement &e)
{
QLocale c(QLocale::C);
Box::fromDomElement(e);
if (e.attribute("type") != "ported") {
qWarning() << __func__ << "wrong box type! (not ported, giving up)";
return;
}
resFreq = c.toDouble(e.attribute("res"));
QDomElement b = e.elementsByTagName("box").at(0).toElement();
box.fromDomElement(b);
QDomElement p = e.elementsByTagName("port").at(0).toElement();
portNum = c.toUInt(p.attribute("num"));
portLen = c.toDouble(p.attribute("len"));
portDiam = c.toDouble(p.attribute("diam"));
slotWidth = c.toDouble(p.attribute("width"));
slotActivated = c.toUInt(p.attribute("slot"));
}
void PortedBox::render(QPainter *painter, const QRectF &area) const
{
if (!painter)
return;
#define PORTED_PARAMS 5
QString params[PORTED_PARAMS];
qreal tab = area.left();
painter->drawRoundedRect(area.toRect(), 5, 5);
params[0] = QObject::tr("Vol. %1 L").arg(getBoxVolume());
if (!slotActivated) {
params[1] = QObject::tr("Port Diam. %1 cm").arg(getPortDiam());
} else {
params[1] = QObject::tr("Slot port %1 x %2 cm").arg(QString::number(getSlotWidth(), 'f', 2), QString::number(getSlotHeight(), 'f', 2));
}
params[2] = QObject::tr("Port Len. %1 cm").arg(QString::number(getPortLen(), 'f', 2));
params[3] = QObject::tr("Port Num. %1").arg(QString::number(getPortNum()));
params[4] = QObject::tr("Fb %1 Hz").arg(QString::number(getResFreq()));
for (int i = 0; i < PORTED_PARAMS; i++) {
QString text = params[i];
QRectF where(tab, area.top(), area.width() / PORTED_PARAMS, area.height());
QTextOption option(Qt::AlignVCenter|Qt::AlignLeft);
painter->drawText(where, text, option);
tab += area.width() / PORTED_PARAMS;
}
}