| 1 | /* |
| 2 | Copyright (C) 2008, 2009 Andres Cabrera |
| 3 | (C) 2003 by John D. Ramsdell |
| 4 | [email protected] |
| 5 | |
| 6 | This file is part of CsoundQt. |
| 7 | |
| 8 | CsoundQt is free software; you can redistribute it |
| 9 | and/or modify it under the terms of the GNU Lesser General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2.1 of the License, or (at your option) any later version. |
| 12 | |
| 13 | CsoundQt is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with Csound; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 21 | 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include "curve.h" |
| 25 | |
| 26 | // Curve is a straightforward abstract data type for a curve |
| 27 | |
| 28 | void Curve::copy(size_t size, MYFLT *data) |
| 29 | { |
| 30 | // set_size must be called prior to this, as bounds are not checked. |
| 31 | for (size_t i = 0; i < size; i++) |
| 32 | m_data[i] = data[i]; |
| 33 | } |
| 34 | |
| 35 | void Curve::destroy() |
| 36 | { |
| 37 | if (m_size != 0) { |
| 38 | free(m_data); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Curve::Curve(MYFLT *data, size_t size, const QString& caption, |
| 43 | Polarity polarity, MYFLT max, MYFLT min, MYFLT absmax, |
| 44 | MYFLT y_scale, bool dotted_divider, WINDAT *original) |
| 45 | : m_caption(caption) |
| 46 | { |
| 47 | m_size = 0; |
| 48 | mutex.lock(); |
| 49 | set_size(size); |
| 50 | copy(size, data); |
| 51 | m_polarity = polarity; |
| 52 | m_max = max; |
| 53 | m_min = min; |
| 54 | m_absmax = absmax; |
| 55 | m_y_scale = y_scale; |
| 56 | m_dotted_divider = dotted_divider; |
| 57 | m_original = original; |
| 58 | if(m_caption.contains("fft")) { |
| 59 | m_curveType = CURVE_SPECTRUM; |
| 60 | } else if(m_caption.contains("ftable")) { |
| 61 | m_curveType = CURVE_FTABLE; |
| 62 | } else { |
| 63 | m_curveType = CURVE_AUDIOSIGNAL; |
| 64 | } |
| 65 | mutex.unlock(); |
| 66 | qDebug() << "Curve " << m_curveType; |
| 67 | |
| 68 | } |
| 69 | |
| 70 | Curve::Curve(const Curve& curve) |
| 71 | : m_caption(curve.m_caption) |
| 72 | { |
| 73 | mutex.lock(); |
| 74 | set_size(curve.m_size); |
| 75 | copy(curve.m_size, curve.m_data); |
| 76 | m_polarity = curve.m_polarity; |
| 77 | m_max = curve.m_max; |
| 78 | m_min = curve.m_min; |
| 79 | m_absmax = curve.m_absmax; |
| 80 | m_y_scale = curve.m_y_scale; |
| 81 | m_dotted_divider = curve.m_dotted_divider; |
| 82 | mutex.unlock(); |
| 83 | } |
| 84 | |
| 85 | Curve& Curve::operator=(const Curve& curve) |
| 86 | { |
| 87 | mutex.lock(); |
| 88 | if (this != &curve) { |
| 89 | destroy(); |
| 90 | set_size(curve.m_size); |
| 91 | copy(curve.m_size, curve.m_data); |
| 92 | m_caption = curve.m_caption; |
| 93 | m_polarity = curve.m_polarity; |
| 94 | m_max = curve.m_max; |
| 95 | m_min = curve.m_min; |
| 96 | m_absmax = curve.m_absmax; |
| 97 | m_y_scale = curve.m_y_scale; |
| 98 | m_dotted_divider = curve.m_dotted_divider; |
| 99 | } |
| 100 | mutex.unlock(); |
| 101 | return *this; |
| 102 | } |
| 103 | |
| 104 | Curve::~Curve() |
| 105 | { |
| 106 | mutex.lock(); |
| 107 | destroy(); |
| 108 | mutex.unlock(); |
| 109 | } |
| 110 | |
| 111 | size_t Curve::get_size() const |
| 112 | { |
| 113 | return m_size; |
| 114 | } |
| 115 | |
| 116 | //uintptr_t Curve::get_id() const |
| 117 | //{ |
| 118 | // return m_id; |
| 119 | //} |
| 120 | |
| 121 | // MYFLT *Curve::get_data() const |
| 122 | // { |
| 123 | // return m_data; |
| 124 | // } |
| 125 | |
| 126 | MYFLT Curve::get_data(int index) |
| 127 | { |
| 128 | // mutex.lock(); |
| 129 | MYFLT out = m_data[index]; |
| 130 | // mutex.unlock(); |
| 131 | return out; |
| 132 | } |
| 133 | |
| 134 | QString Curve::get_caption() const |
| 135 | { |
| 136 | return m_caption; |
| 137 | } |
| 138 | |
| 139 | Polarity Curve::get_polarity() const |
| 140 | { |
| 141 | return m_polarity; |
| 142 | } |
| 143 | |
| 144 | MYFLT Curve::get_max() const |
| 145 | { |
| 146 | return m_max; |
| 147 | } |
| 148 | |
| 149 | MYFLT Curve::get_min() const |
| 150 | { |
| 151 | return m_min; |
| 152 | } |
| 153 | |
| 154 | MYFLT Curve::get_absmax() const |
| 155 | { |
| 156 | return m_absmax; |
| 157 | } |
| 158 | |
| 159 | MYFLT Curve::get_y_scale() const |
| 160 | { |
| 161 | return m_y_scale; |
| 162 | } |
| 163 | |
| 164 | WINDAT * Curve::getOriginal() |
| 165 | { |
| 166 | return m_original; |
| 167 | } |
| 168 | |
| 169 | //void Curve::set_id(uintptr_t id) |
| 170 | //{ |
| 171 | // m_id = id; |
| 172 | //} |
| 173 | |
| 174 | void Curve::set_data(MYFLT * data) |
| 175 | { |
| 176 | copy(m_size, data); |
| 177 | } |
| 178 | |
| 179 | void Curve::set_size(size_t size) |
| 180 | { |
| 181 | if (m_size < size) { // This should happen only once on constructing the curve, as curves should change length |
| 182 | if (m_size != 0) { |
| 183 | free(m_data); |
| 184 | } |
| 185 | m_data = (MYFLT *) calloc(size, sizeof(MYFLT)); |
| 186 | } |
| 187 | m_size = size; |
| 188 | } |
| 189 | |
| 190 | void Curve::set_caption(QString caption) |
| 191 | { |
| 192 | m_caption = caption.trimmed(); |
| 193 | } |
| 194 | |
| 195 | void Curve::set_polarity(Polarity polarity) |
| 196 | { |
| 197 | m_polarity = polarity; |
| 198 | } |
| 199 | |
| 200 | void Curve::set_max(MYFLT max) |
| 201 | { |
| 202 | m_max = max; |
| 203 | } |
| 204 | void Curve::set_min(MYFLT min) |
| 205 | { |
| 206 | m_min = min; |
| 207 | } |
| 208 | |
| 209 | void Curve::set_absmax(MYFLT absmax) |
| 210 | { |
| 211 | m_absmax = absmax; |
| 212 | } |
| 213 | |
| 214 | void Curve::set_y_scale(MYFLT y_scale) |
| 215 | { |
| 216 | m_y_scale = y_scale; |
| 217 | } |
| 218 | |
| 219 | void Curve::setOriginal(WINDAT *windat) |
| 220 | { |
| 221 | m_original = windat; |
| 222 | } |
| 223 | |
| 224 | bool Curve::is_divider_dotted() const |
| 225 | { |
| 226 | return m_dotted_divider; |
| 227 | } |
| 228 | |
| 229 | bool Curve::has_same_caption(Curve *curve) const |
| 230 | { |
| 231 | return curve && m_caption == curve->m_caption; |
| 232 | } |
| 233 | |
| 234 | CurveType Curve::get_type() const |
| 235 | { |
| 236 | return m_curveType; |
| 237 | } |
| 238 | |
| 239 | QStringList parseCaption(QString caption) { |
| 240 | QStringList out; |
| 241 | if(caption.contains("fft")) { |
| 242 | auto parts = caption.splitRef(", ", QString::SkipEmptyParts); |
| 243 | auto p1 = parts[0].mid(6); // skip "instr" |
| 244 | auto signal = parts[1].mid(7); |
| 245 | auto ffttype = parts[2].mid(5, parts[2].trimmed().size() - 2); |
| 246 | out.append("fft"); |
| 247 | out.append(p1.toString()); |
| 248 | out.append(signal.toString()); |
| 249 | out.append(ffttype.toString()); |
| 250 | return out; |
| 251 | } |
| 252 | else { |
| 253 | out.append(caption); |
| 254 | return out; |
| 255 | } |
| 256 | } |