/[qspeakers]/portedbox.cpp
ViewVC logotype

Contents of /portedbox.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (show annotations)
Thu May 14 06:53:46 2015 UTC (2 years, 1 month ago) by ben
File size: 4498 byte(s)
conserve manual port width if > 0
1 #include <QLocale>
2 #include <QDebug>
3
4 #include <math.h>
5
6 #include "portedbox.h"
7
8 PortedBox::PortedBox(double volume, double resfreq, unsigned int portnum, double portdiam, double portlen) :
9 box(volume)
10 {
11 resFreq = resfreq;
12 portNum = portnum;
13 portDiam = portdiam;
14 portLen = portlen;
15 }
16
17 void PortedBox::setBoxVolume(double vol)
18 {
19 box.setVolume(vol);
20 }
21
22 void PortedBox::setPortNum(unsigned int value)
23 {
24 portNum = value;
25 }
26
27 void PortedBox::setPortLen(double len)
28 {
29 portLen = len;
30 }
31
32 void PortedBox::setPortDiam(double diam)
33 {
34 portDiam = diam;
35 }
36
37 void PortedBox::setSlotWidth(double width)
38 {
39 slotWidth = width;
40 }
41
42 void PortedBox::setResFreq(double value)
43 {
44 resFreq = value;
45 }
46
47 double PortedBox::getBoxVolume() const
48 {
49 return box.getVolume();
50 }
51
52 unsigned int PortedBox::getPortNum() const
53 {
54 return portNum;
55 }
56
57 double PortedBox::getPortLen() const
58 {
59 return portLen;
60 }
61
62 double PortedBox::getPortDiam() const
63 {
64 return portDiam;
65 }
66
67 double PortedBox::getSlotWidth() const
68 {
69 return slotWidth;
70 }
71
72 double PortedBox::getSlotHeight() const
73 {
74 /* width is manual, height is computed from eq. diam. */
75 return (PI * pow(portDiam, 2.0)) / (4.0 * slotWidth);
76 }
77
78 double PortedBox::getResFreq() const
79 {
80 return resFreq;
81 }
82
83 void PortedBox::updatePorts(double sd, double xmax)
84 {
85 /* compute minimum port diameter and optionaly set it */
86 double vol = sd * xmax * 0.001; /* m³ */
87 double dmin = 100 * (20.3 * pow((pow(vol, 2.0) / getResFreq()), 0.25)) / sqrt(getPortNum()); /* cm */
88 setPortDiam(dmin);
89 updateSlots();
90 updatePortsLength();
91 }
92
93 void PortedBox::updateSlots()
94 {
95 /* compute square slot */
96 double diam = getPortDiam();
97 double s = sqrt(PI * pow(diam, 2.0) / 4.0);
98
99 /* consider "width" over "height" and reset if too small */
100 if (slotWidth == 0.0)
101 slotWidth = s;
102 }
103
104 void PortedBox::updatePortsLength()
105 {
106 /* compute resulting length */
107 double diam = getPortDiam();
108 double res = getResFreq();
109 double vol = getBoxVolume();
110 double num = getPortNum();
111 double plen = ((23562.5 * pow(diam, 2.0)) * num / (vol * pow(res, 2.0))) - (K * diam);
112 setPortLen(plen);
113 }
114
115 QDomElement PortedBox::toDomElement(QDomDocument& doc) const
116 {
117 QLocale c(QLocale::C);
118 QDomElement e = Box::toDomElement(doc);
119
120 e.setAttribute("type", "ported");
121 e.setAttribute("res", c.toString(resFreq));
122
123 QDomElement b = box.toDomElement(doc);
124 e.appendChild(b);
125
126 QDomElement p = doc.createElement("port");
127
128 p.setAttribute("num", c.toString(portNum));
129 p.setAttribute("len", c.toString(portLen));
130 p.setAttribute("diam", c.toString(portDiam));
131 p.setAttribute("width", c.toString(slotWidth));
132
133 e.appendChild(p);
134
135 return e;
136 }
137
138 void PortedBox::fromDomElement(const QDomElement &e)
139 {
140 QLocale c(QLocale::C);
141 Box::fromDomElement(e);
142
143 if (e.attribute("type") != "ported") {
144 qWarning() << __func__ << "wrong box type! (not ported, giving up)";
145 return;
146 }
147
148 resFreq = c.toDouble(e.attribute("res"));
149
150 QDomElement b = e.elementsByTagName("box").at(0).toElement();
151 box.fromDomElement(b);
152
153 QDomElement p = e.elementsByTagName("port").at(0).toElement();
154
155 portNum = c.toUInt(p.attribute("num"));
156 portLen = c.toDouble(p.attribute("len"));
157 portDiam = c.toDouble(p.attribute("diam"));
158 slotWidth = c.toDouble(p.attribute("width"));
159 }
160
161 void PortedBox::render(QPainter *painter, const QRectF &area)
162 {
163 if (!painter)
164 return;
165
166 #define PORTED_PARAMS 6
167
168 QString params[PORTED_PARAMS];
169 qreal tab = area.left();
170
171 painter->drawRoundRect(area.toRect(), 5, 5);
172
173 params[0] = QObject::tr("Vol. %1 L").arg(getBoxVolume());
174 params[1] = QObject::tr("Port Diam. %1 cm").arg(getPortDiam());
175 params[2] = QObject::tr("[Slot %1x%2 cm]").arg(QString::number(getSlotWidth(), 'f', 2), QString::number(getSlotHeight(), 'f', 2));
176 params[3] = QObject::tr("Port Len. %1 cm").arg(QString::number(getPortLen(), 'f', 2));
177 params[4] = QObject::tr("Port Num. %1").arg(QString::number(getPortNum()));
178 params[5] = QObject::tr("Fb %1 Hz").arg(QString::number(getResFreq()));
179
180 for (int i = 0; i < PORTED_PARAMS; i++) {
181 QString text = params[i];
182 QRectF where(tab, area.top(), area.width() / PORTED_PARAMS, area.height());
183 QTextOption option(Qt::AlignVCenter|Qt::AlignLeft);
184 painter->drawText(where, text, option);
185 tab += area.width() / PORTED_PARAMS;
186 }
187 }

  ViewVC Help
Powered by ViewVC 1.1.26