1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
45 #include <QtMultimedia/QAudioFormat>
47 #include <QScopedPointer>
49 QT_FORWARD_DECLARE_CLASS(QByteArray)
52 * Widget which displays a section of the audio waveform.
54 * The waveform is rendered on a set of QPixmaps which form a group of tiles
55 * whose extent covers the widget. As the audio position is updated, these
56 * tiles are scrolled from left to right; when the left-most tile scrolls
57 * outside the widget, it is moved to the right end of the tile array and
58 * painted with the next section of the waveform.
60 class Waveform : public QWidget {
63 Waveform(QWidget *parent = 0);
67 void paintEvent(QPaintEvent *event);
68 void resizeEvent(QResizeEvent *event);
70 void initialize(const QAudioFormat &format, qint64 audioBufferSize, qint64 windowDurationUs);
73 void setAutoUpdatePosition(bool enabled);
76 void bufferChanged(qint64 position, qint64 length, const QByteArray &buffer);
77 void audioPositionChanged(qint64 position);
80 static const int NullIndex = -1;
85 * (Re)create all pixmaps, repaint and update the display.
86 * Triggers an update();
88 void createPixmaps(const QSize &newSize);
91 * Update window position.
92 * Triggers an update().
94 void setWindowPosition(qint64 position);
97 * Base position of tile
99 qint64 tilePosition(int index) const;
102 * Structure which identifies a point within a given
107 TilePoint(int idx = 0, qint64 pos = 0, qint64 pix = 0)
108 : index(idx), positionOffset(pos), pixelOffset(pix)
114 // Number of bytes from start of tile
115 qint64 positionOffset;
117 // Number of pixels from left of corresponding pixmap
122 * Convert position in m_buffer into a tile index and an offset in pixels
123 * into the corresponding pixmap.
125 * \param position Offset into m_buffer, in bytes
127 * If position is outside the tile array, index is NullIndex and
130 TilePoint tilePoint(qint64 position) const;
133 * Convert offset in bytes into a tile into an offset in pixels
136 int tilePixelOffset(qint64 positionOffset) const;
139 * Convert offset in bytes into the window into an offset in pixels
140 * within the widget rect().
142 int windowPixelOffset(qint64 positionOffset) const;
145 * Paint all tiles which can be painted.
146 * \return true iff update() was called
151 * Paint the specified tile
153 * \pre Sufficient data is available to completely paint the tile, i.e.
154 * m_dataLength is greater than the upper bound of the tile.
156 void paintTile(int index);
159 * Move the first n tiles to the end of the array, and mark them as not
162 void shuffleTiles(int n);
167 void resetTiles(qint64 newStartPos);
170 qint64 m_bufferPosition;
171 qint64 m_bufferLength;
174 qint64 m_audioPosition;
175 QAudioFormat m_format;
180 QVector<QPixmap*> m_pixmaps;
183 // Pointer into parent m_pixmaps array
186 // Flag indicating whether this tile has been painted
190 QVector<Tile> m_tiles;
192 // Length of audio data in bytes depicted by each tile
195 // Position in bytes of the first tile, relative to m_buffer
196 qint64 m_tileArrayStart;
198 qint64 m_windowPosition;
199 qint64 m_windowLength;