|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "waveform.h" |
|
43 #include "utils.h" |
|
44 #include <QPainter> |
|
45 #include <QResizeEvent> |
|
46 #include <QDebug> |
|
47 |
|
48 |
|
49 Waveform::Waveform(const QByteArray &buffer, QWidget *parent) |
|
50 : QWidget(parent) |
|
51 , m_buffer(buffer) |
|
52 , m_dataLength(0) |
|
53 , m_position(0) |
|
54 , m_active(false) |
|
55 , m_tileLength(0) |
|
56 , m_tileArrayStart(0) |
|
57 , m_windowPosition(0) |
|
58 , m_windowLength(0) |
|
59 { |
|
60 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); |
|
61 setMinimumHeight(50); |
|
62 } |
|
63 |
|
64 Waveform::~Waveform() |
|
65 { |
|
66 deletePixmaps(); |
|
67 } |
|
68 |
|
69 void Waveform::paintEvent(QPaintEvent * /*event*/) |
|
70 { |
|
71 QPainter painter(this); |
|
72 |
|
73 painter.fillRect(rect(), Qt::black); |
|
74 |
|
75 if (m_active) { |
|
76 WAVEFORM_DEBUG << "Waveform::paintEvent" |
|
77 << "windowPosition" << m_windowPosition |
|
78 << "windowLength" << m_windowLength; |
|
79 qint64 pos = m_windowPosition; |
|
80 const qint64 windowEnd = m_windowPosition + m_windowLength; |
|
81 int destLeft = 0; |
|
82 int destRight = 0; |
|
83 while (pos < windowEnd) { |
|
84 const TilePoint point = tilePoint(pos); |
|
85 WAVEFORM_DEBUG << "Waveform::paintEvent" << "pos" << pos |
|
86 << "tileIndex" << point.index |
|
87 << "positionOffset" << point.positionOffset |
|
88 << "pixelOffset" << point.pixelOffset; |
|
89 |
|
90 if (point.index != NullIndex) { |
|
91 const Tile &tile = m_tiles[point.index]; |
|
92 if (tile.painted) { |
|
93 const qint64 sectionLength = qMin((m_tileLength - point.positionOffset), |
|
94 (windowEnd - pos)); |
|
95 Q_ASSERT(sectionLength > 0); |
|
96 |
|
97 const int sourceRight = tilePixelOffset(point.positionOffset + sectionLength); |
|
98 destRight = windowPixelOffset(pos - m_windowPosition + sectionLength); |
|
99 |
|
100 QRect destRect = rect(); |
|
101 destRect.setLeft(destLeft); |
|
102 destRect.setRight(destRight); |
|
103 |
|
104 QRect sourceRect(QPoint(), m_pixmapSize); |
|
105 sourceRect.setLeft(point.pixelOffset); |
|
106 sourceRect.setRight(sourceRight); |
|
107 |
|
108 WAVEFORM_DEBUG << "Waveform::paintEvent" << "tileIndex" << point.index |
|
109 << "source" << point.pixelOffset << sourceRight |
|
110 << "dest" << destLeft << destRight; |
|
111 |
|
112 painter.drawPixmap(destRect, *tile.pixmap, sourceRect); |
|
113 |
|
114 destLeft = destRight; |
|
115 |
|
116 if (point.index < m_tiles.count()) { |
|
117 pos = tilePosition(point.index + 1); |
|
118 WAVEFORM_DEBUG << "Waveform::paintEvent" << "pos ->" << pos; |
|
119 } else { |
|
120 // Reached end of tile array |
|
121 WAVEFORM_DEBUG << "Waveform::paintEvent" << "reached end of tile array"; |
|
122 break; |
|
123 } |
|
124 } else { |
|
125 // Passed last tile which is painted |
|
126 WAVEFORM_DEBUG << "Waveform::paintEvent" << "tile" << point.index << "not painted"; |
|
127 break; |
|
128 } |
|
129 } else { |
|
130 // pos is past end of tile array |
|
131 WAVEFORM_DEBUG << "Waveform::paintEvent" << "pos" << pos << "past end of tile array"; |
|
132 break; |
|
133 } |
|
134 } |
|
135 |
|
136 WAVEFORM_DEBUG << "Waveform::paintEvent" << "final pos" << pos << "final x" << destRight; |
|
137 } |
|
138 } |
|
139 |
|
140 void Waveform::resizeEvent(QResizeEvent *event) |
|
141 { |
|
142 if (event->size() != event->oldSize()) |
|
143 createPixmaps(event->size()); |
|
144 } |
|
145 |
|
146 void Waveform::initialize(const QAudioFormat &format, qint64 audioBufferSize, qint64 windowDurationUs) |
|
147 { |
|
148 WAVEFORM_DEBUG << "Waveform::initialize" |
|
149 << "audioBufferSize" << audioBufferSize |
|
150 << "m_buffer.size()" << m_buffer.size() |
|
151 << "windowDurationUs" << windowDurationUs; |
|
152 |
|
153 reset(); |
|
154 |
|
155 m_format = format; |
|
156 |
|
157 // Calculate tile size |
|
158 m_tileLength = audioBufferSize; |
|
159 |
|
160 // Calculate window size |
|
161 m_windowLength = audioLength(m_format, windowDurationUs); |
|
162 |
|
163 // Calculate number of tiles required |
|
164 int nTiles; |
|
165 if (m_tileLength > m_windowLength) { |
|
166 nTiles = 2; |
|
167 } else { |
|
168 nTiles = m_windowLength / m_tileLength + 1; |
|
169 if (m_windowLength % m_tileLength) |
|
170 ++nTiles; |
|
171 } |
|
172 |
|
173 WAVEFORM_DEBUG << "Waveform::initialize" |
|
174 << "tileLength" << m_tileLength |
|
175 << "windowLength" << m_windowLength |
|
176 << "nTiles" << nTiles; |
|
177 |
|
178 m_pixmaps.fill(0, nTiles); |
|
179 m_tiles.resize(nTiles); |
|
180 |
|
181 createPixmaps(rect().size()); |
|
182 |
|
183 m_active = true; |
|
184 } |
|
185 |
|
186 void Waveform::reset() |
|
187 { |
|
188 WAVEFORM_DEBUG << "Waveform::reset"; |
|
189 |
|
190 m_dataLength = 0; |
|
191 m_position = 0; |
|
192 m_format = QAudioFormat(); |
|
193 m_active = false; |
|
194 deletePixmaps(); |
|
195 m_tiles.clear(); |
|
196 m_tileLength = 0; |
|
197 m_tileArrayStart = 0; |
|
198 m_windowPosition = 0; |
|
199 m_windowLength = 0; |
|
200 } |
|
201 |
|
202 void Waveform::dataLengthChanged(qint64 length) |
|
203 { |
|
204 WAVEFORM_DEBUG << "Waveform::dataLengthChanged" << length; |
|
205 const qint64 oldLength = m_dataLength; |
|
206 m_dataLength = length; |
|
207 |
|
208 if (m_active) { |
|
209 if (m_dataLength < oldLength) |
|
210 positionChanged(m_dataLength); |
|
211 else |
|
212 paintTiles(); |
|
213 } |
|
214 } |
|
215 |
|
216 void Waveform::positionChanged(qint64 position) |
|
217 { |
|
218 WAVEFORM_DEBUG << "Waveform::positionChanged" << position; |
|
219 |
|
220 if (position + m_windowLength > m_dataLength) |
|
221 position = m_dataLength - m_windowLength; |
|
222 |
|
223 m_position = position; |
|
224 |
|
225 setWindowPosition(position); |
|
226 } |
|
227 |
|
228 void Waveform::deletePixmaps() |
|
229 { |
|
230 QPixmap *pixmap; |
|
231 foreach (pixmap, m_pixmaps) |
|
232 delete pixmap; |
|
233 m_pixmaps.clear(); |
|
234 } |
|
235 |
|
236 void Waveform::createPixmaps(const QSize &widgetSize) |
|
237 { |
|
238 m_pixmapSize = widgetSize; |
|
239 m_pixmapSize.setWidth(qreal(widgetSize.width()) * m_tileLength / m_windowLength); |
|
240 |
|
241 WAVEFORM_DEBUG << "Waveform::createPixmaps" |
|
242 << "widgetSize" << widgetSize |
|
243 << "pixmapSize" << m_pixmapSize; |
|
244 |
|
245 Q_ASSERT(m_tiles.count() == m_pixmaps.count()); |
|
246 |
|
247 // (Re)create pixmaps |
|
248 for (int i=0; i<m_pixmaps.size(); ++i) { |
|
249 delete m_pixmaps[i]; |
|
250 m_pixmaps[i] = 0; |
|
251 m_pixmaps[i] = new QPixmap(m_pixmapSize); |
|
252 } |
|
253 |
|
254 // Update tile pixmap pointers, and mark for repainting |
|
255 for (int i=0; i<m_tiles.count(); ++i) { |
|
256 m_tiles[i].pixmap = m_pixmaps[i]; |
|
257 m_tiles[i].painted = false; |
|
258 } |
|
259 |
|
260 paintTiles(); |
|
261 } |
|
262 |
|
263 void Waveform::setWindowPosition(qint64 position) |
|
264 { |
|
265 WAVEFORM_DEBUG << "Waveform::setWindowPosition" |
|
266 << "old" << m_windowPosition << "new" << position |
|
267 << "tileArrayStart" << m_tileArrayStart; |
|
268 |
|
269 const qint64 oldPosition = m_windowPosition; |
|
270 m_windowPosition = position; |
|
271 |
|
272 if((m_windowPosition >= oldPosition) && |
|
273 (m_windowPosition - m_tileArrayStart < (m_tiles.count() * m_tileLength))) { |
|
274 // Work out how many tiles need to be shuffled |
|
275 const qint64 offset = m_windowPosition - m_tileArrayStart; |
|
276 const int nTiles = offset / m_tileLength; |
|
277 shuffleTiles(nTiles); |
|
278 } else { |
|
279 resetTiles(m_windowPosition); |
|
280 } |
|
281 |
|
282 if(!paintTiles() && m_windowPosition != oldPosition) |
|
283 update(); |
|
284 } |
|
285 |
|
286 qint64 Waveform::tilePosition(int index) const |
|
287 { |
|
288 return m_tileArrayStart + index * m_tileLength; |
|
289 } |
|
290 |
|
291 Waveform::TilePoint Waveform::tilePoint(qint64 position) const |
|
292 { |
|
293 TilePoint result; |
|
294 if (position >= m_tileArrayStart) { |
|
295 const qint64 tileArrayEnd = m_tileArrayStart + m_tiles.count() * m_tileLength; |
|
296 if (position < tileArrayEnd) { |
|
297 const qint64 offsetIntoTileArray = position - m_tileArrayStart; |
|
298 result.index = offsetIntoTileArray / m_tileLength; |
|
299 Q_ASSERT(result.index >= 0 && result.index <= m_tiles.count()); |
|
300 result.positionOffset = offsetIntoTileArray % m_tileLength; |
|
301 result.pixelOffset = tilePixelOffset(result.positionOffset); |
|
302 Q_ASSERT(result.pixelOffset >= 0 && result.pixelOffset <= m_pixmapSize.width()); |
|
303 } |
|
304 } |
|
305 |
|
306 return result; |
|
307 } |
|
308 |
|
309 int Waveform::tilePixelOffset(qint64 positionOffset) const |
|
310 { |
|
311 Q_ASSERT(positionOffset >= 0 && positionOffset <= m_tileLength); |
|
312 const int result = (qreal(positionOffset) / m_tileLength) * m_pixmapSize.width(); |
|
313 return result; |
|
314 } |
|
315 |
|
316 int Waveform::windowPixelOffset(qint64 positionOffset) const |
|
317 { |
|
318 Q_ASSERT(positionOffset >= 0 && positionOffset <= m_windowLength); |
|
319 const int result = (qreal(positionOffset) / m_windowLength) * rect().width(); |
|
320 return result; |
|
321 } |
|
322 |
|
323 bool Waveform::paintTiles() |
|
324 { |
|
325 WAVEFORM_DEBUG << "Waveform::paintTiles"; |
|
326 bool updateRequired = false; |
|
327 |
|
328 for (int i=0; i<m_tiles.count(); ++i) { |
|
329 const Tile &tile = m_tiles[i]; |
|
330 if (!tile.painted) { |
|
331 const qint64 tileEnd = m_tileArrayStart + (i + 1) * m_tileLength; |
|
332 if (m_dataLength >= tileEnd) { |
|
333 paintTile(i); |
|
334 updateRequired = true; |
|
335 } |
|
336 } |
|
337 } |
|
338 |
|
339 if (updateRequired) |
|
340 update(); |
|
341 |
|
342 return updateRequired; |
|
343 } |
|
344 |
|
345 void Waveform::paintTile(int index) |
|
346 { |
|
347 WAVEFORM_DEBUG << "Waveform::paintTile" << "index" << index; |
|
348 |
|
349 const qint64 tileStart = m_tileArrayStart + index * m_tileLength; |
|
350 Q_ASSERT(m_dataLength >= tileStart + m_tileLength); |
|
351 |
|
352 Tile &tile = m_tiles[index]; |
|
353 Q_ASSERT(!tile.painted); |
|
354 |
|
355 const qint16* base = reinterpret_cast<const qint16*>(m_buffer.constData()); |
|
356 const qint16* buffer = base + (tileStart / 2); |
|
357 const int numSamples = m_tileLength / (2 * m_format.channels()); |
|
358 |
|
359 QPainter painter(tile.pixmap); |
|
360 |
|
361 painter.fillRect(tile.pixmap->rect(), Qt::black); |
|
362 |
|
363 QPen pen(Qt::white); |
|
364 painter.setPen(pen); |
|
365 |
|
366 // Calculate initial PCM value |
|
367 qint16 previousPcmValue = 0; |
|
368 if (buffer > base) |
|
369 previousPcmValue = *(buffer - m_format.channels()); |
|
370 |
|
371 // Calculate initial point |
|
372 const qreal previousRealValue = pcmToReal(previousPcmValue); |
|
373 const int originY = ((previousRealValue + 1.0) / 2) * m_pixmapSize.height(); |
|
374 const QPoint origin(0, originY); |
|
375 |
|
376 QLine line(origin, origin); |
|
377 |
|
378 for (int i=0; i<numSamples; ++i) { |
|
379 const qint16* ptr = buffer + i * m_format.channels(); |
|
380 const qint16 pcmValue = *ptr; |
|
381 const qreal realValue = pcmToReal(pcmValue); |
|
382 |
|
383 const int x = tilePixelOffset(i * 2 * m_format.channels()); |
|
384 const int y = ((realValue + 1.0) / 2) * m_pixmapSize.height(); |
|
385 |
|
386 line.setP2(QPoint(x, y)); |
|
387 painter.drawLine(line); |
|
388 line.setP1(line.p2()); |
|
389 } |
|
390 |
|
391 tile.painted = true; |
|
392 } |
|
393 |
|
394 void Waveform::shuffleTiles(int n) |
|
395 { |
|
396 WAVEFORM_DEBUG << "Waveform::shuffleTiles" << "n" << n; |
|
397 |
|
398 while (n--) { |
|
399 Tile tile = m_tiles.first(); |
|
400 tile.painted = false; |
|
401 m_tiles.erase(m_tiles.begin()); |
|
402 m_tiles += tile; |
|
403 m_tileArrayStart += m_tileLength; |
|
404 } |
|
405 |
|
406 WAVEFORM_DEBUG << "Waveform::shuffleTiles" << "tileArrayStart" << m_tileArrayStart; |
|
407 } |
|
408 |
|
409 void Waveform::resetTiles(qint64 newStartPos) |
|
410 { |
|
411 WAVEFORM_DEBUG << "Waveform::resetTiles" << "newStartPos" << newStartPos; |
|
412 |
|
413 QVector<Tile>::iterator i = m_tiles.begin(); |
|
414 for ( ; i != m_tiles.end(); ++i) |
|
415 i->painted = false; |
|
416 |
|
417 m_tileArrayStart = newStartPos; |
|
418 } |
|
419 |