Changeset 846 for trunk/demos/spectrum/app/waveform.cpp
- Timestamp:
- May 5, 2011, 5:36:53 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/vendor/nokia/qt/4.7.2 (added) merged: 845 /branches/vendor/nokia/qt/current merged: 844 /branches/vendor/nokia/qt/4.6.3 removed
- Property svn:mergeinfo changed
-
trunk/demos/spectrum/app/waveform.cpp
r769 r846 1 1 /**************************************************************************** 2 2 ** 3 ** Copyright (C) 201 0Nokia Corporation and/or its subsidiary(-ies).3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). 4 4 ** All rights reserved. 5 5 ** Contact: Nokia Corporation (qt-info@nokia.com) … … 45 45 #include <QDebug> 46 46 47 48 Waveform::Waveform(const QByteArray &buffer, QWidget *parent) 47 //#define PAINT_EVENT_TRACE 48 #ifdef PAINT_EVENT_TRACE 49 # define WAVEFORM_PAINT_DEBUG qDebug() 50 #else 51 # define WAVEFORM_PAINT_DEBUG nullDebug() 52 #endif 53 54 Waveform::Waveform(QWidget *parent) 49 55 : QWidget(parent) 50 , m_buffer (buffer)51 , m_ dataLength(0)52 , m_ position(0)56 , m_bufferPosition(0) 57 , m_bufferLength(0) 58 , m_audioPosition(0) 53 59 , m_active(false) 54 60 , m_tileLength(0) … … 73 79 74 80 if (m_active) { 75 WAVEFORM_ DEBUG << "Waveform::paintEvent"76 << "windowPosition" << m_windowPosition77 << "windowLength" << m_windowLength;81 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" 82 << "windowPosition" << m_windowPosition 83 << "windowLength" << m_windowLength; 78 84 qint64 pos = m_windowPosition; 79 85 const qint64 windowEnd = m_windowPosition + m_windowLength; … … 82 88 while (pos < windowEnd) { 83 89 const TilePoint point = tilePoint(pos); 84 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "pos" << pos85 << "tileIndex" << point.index86 << "positionOffset" << point.positionOffset87 << "pixelOffset" << point.pixelOffset;90 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "pos" << pos 91 << "tileIndex" << point.index 92 << "positionOffset" << point.positionOffset 93 << "pixelOffset" << point.pixelOffset; 88 94 89 95 if (point.index != NullIndex) { … … 105 111 sourceRect.setRight(sourceRight); 106 112 107 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "tileIndex" << point.index108 << "source" << point.pixelOffset << sourceRight109 << "dest" << destLeft << destRight;113 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "tileIndex" << point.index 114 << "source" << point.pixelOffset << sourceRight 115 << "dest" << destLeft << destRight; 110 116 111 117 painter.drawPixmap(destRect, *tile.pixmap, sourceRect); … … 115 121 if (point.index < m_tiles.count()) { 116 122 pos = tilePosition(point.index + 1); 117 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "pos ->" << pos;123 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "pos ->" << pos; 118 124 } else { 119 125 // Reached end of tile array 120 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "reached end of tile array";126 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "reached end of tile array"; 121 127 break; 122 128 } 123 129 } else { 124 130 // Passed last tile which is painted 125 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "tile" << point.index << "not painted";131 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "tile" << point.index << "not painted"; 126 132 break; 127 133 } 128 134 } else { 129 135 // pos is past end of tile array 130 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "pos" << pos << "past end of tile array";136 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "pos" << pos << "past end of tile array"; 131 137 break; 132 138 } 133 139 } 134 140 135 WAVEFORM_ DEBUG << "Waveform::paintEvent" << "final pos" << pos << "final x" << destRight;141 WAVEFORM_PAINT_DEBUG << "Waveform::paintEvent" << "final pos" << pos << "final x" << destRight; 136 142 } 137 143 } … … 147 153 WAVEFORM_DEBUG << "Waveform::initialize" 148 154 << "audioBufferSize" << audioBufferSize 149 << "m_buffer.size()" << m_buffer.size()150 155 << "windowDurationUs" << windowDurationUs; 151 156 … … 187 192 WAVEFORM_DEBUG << "Waveform::reset"; 188 193 189 m_dataLength = 0; 190 m_position = 0; 194 m_bufferPosition = 0; 195 m_buffer = QByteArray(); 196 m_audioPosition = 0; 191 197 m_format = QAudioFormat(); 192 198 m_active = false; … … 199 205 } 200 206 201 void Waveform::dataLengthChanged(qint64 length) 202 { 203 WAVEFORM_DEBUG << "Waveform::dataLengthChanged" << length; 204 const qint64 oldLength = m_dataLength; 205 m_dataLength = length; 206 207 if (m_active) { 208 if (m_dataLength < oldLength) 209 positionChanged(m_dataLength); 210 else 211 paintTiles(); 212 } 213 } 214 215 void Waveform::positionChanged(qint64 position) 216 { 217 WAVEFORM_DEBUG << "Waveform::positionChanged" << position; 218 219 if (position + m_windowLength > m_dataLength) 220 position = m_dataLength - m_windowLength; 221 222 m_position = position; 223 224 setWindowPosition(position); 207 void Waveform::bufferChanged(qint64 position, qint64 length, const QByteArray &buffer) 208 { 209 WAVEFORM_DEBUG << "Waveform::bufferChanged" 210 << "audioPosition" << m_audioPosition 211 << "bufferPosition" << position 212 << "bufferLength" << length; 213 m_bufferPosition = position; 214 m_bufferLength = length; 215 m_buffer = buffer; 216 paintTiles(); 217 } 218 219 void Waveform::audioPositionChanged(qint64 position) 220 { 221 WAVEFORM_DEBUG << "Waveform::audioPositionChanged" 222 << "audioPosition" << position 223 << "bufferPosition" << m_bufferPosition 224 << "bufferLength" << m_bufferLength; 225 226 if (position >= m_bufferPosition) { 227 if (position + m_windowLength > m_bufferPosition + m_bufferLength) 228 position = qMax(qint64(0), m_bufferPosition + m_bufferLength - m_windowLength); 229 m_audioPosition = position; 230 setWindowPosition(position); 231 } 225 232 } 226 233 … … 256 263 m_tiles[i].painted = false; 257 264 } 258 259 paintTiles();260 265 } 261 266 … … 328 333 const Tile &tile = m_tiles[i]; 329 334 if (!tile.painted) { 330 const qint64 tileEnd = m_tileArrayStart + (i + 1) * m_tileLength; 331 if (m_dataLength >= tileEnd) { 335 const qint64 tileStart = m_tileArrayStart + i * m_tileLength; 336 const qint64 tileEnd = tileStart + m_tileLength; 337 if (m_bufferPosition <= tileStart && m_bufferPosition + m_bufferLength >= tileEnd) { 332 338 paintTile(i); 333 339 updateRequired = true; … … 344 350 void Waveform::paintTile(int index) 345 351 { 346 WAVEFORM_DEBUG << "Waveform::paintTile" << "index" << index;347 348 352 const qint64 tileStart = m_tileArrayStart + index * m_tileLength; 349 Q_ASSERT(m_dataLength >= tileStart + m_tileLength); 353 354 WAVEFORM_DEBUG << "Waveform::paintTile" 355 << "index" << index 356 << "bufferPosition" << m_bufferPosition 357 << "bufferLength" << m_bufferLength 358 << "start" << tileStart 359 << "end" << tileStart + m_tileLength; 360 361 Q_ASSERT(m_bufferPosition <= tileStart); 362 Q_ASSERT(m_bufferPosition + m_bufferLength >= tileStart + m_tileLength); 350 363 351 364 Tile &tile = m_tiles[index]; … … 353 366 354 367 const qint16* base = reinterpret_cast<const qint16*>(m_buffer.constData()); 355 const qint16* buffer = base + ( tileStart/ 2);368 const qint16* buffer = base + ((tileStart - m_bufferPosition) / 2); 356 369 const int numSamples = m_tileLength / (2 * m_format.channels()); 357 370 … … 377 390 for (int i=0; i<numSamples; ++i) { 378 391 const qint16* ptr = buffer + i * m_format.channels(); 392 393 const int offset = reinterpret_cast<const char*>(ptr) - m_buffer.constData(); 394 Q_ASSERT(offset >= 0); 395 Q_ASSERT(offset < m_bufferLength); 396 379 397 const qint16 pcmValue = *ptr; 380 398 const qreal realValue = pcmToReal(pcmValue);
Note:
See TracChangeset
for help on using the changeset viewer.