1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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.
23 **
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef WAVEFORM_H
42 #define WAVEFORM_H
43 
44 #include <QWidget>
45 #include <QtMultimedia/QAudioFormat>
46 #include <QPixmap>
47 #include <QScopedPointer>
48 
QT_FORWARD_DECLARE_CLASS(QByteArray)49 QT_FORWARD_DECLARE_CLASS(QByteArray)
50 
51 /**
52  * Widget which displays a section of the audio waveform.
53  *
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.
59  */
60 class Waveform : public QWidget {
61     Q_OBJECT
62 public:
63     Waveform(QWidget *parent = 0);
64     ~Waveform();
65 
66     // QWidget
67     void paintEvent(QPaintEvent *event);
68     void resizeEvent(QResizeEvent *event);
69 
70     void initialize(const QAudioFormat &format, qint64 audioBufferSize, qint64 windowDurationUs);
71     void reset();
72 
73     void setAutoUpdatePosition(bool enabled);
74 
75 public slots:
76     void bufferChanged(qint64 position, qint64 length, const QByteArray &buffer);
77     void audioPositionChanged(qint64 position);
78 
79 private:
80     static const int NullIndex = -1;
81 
82     void deletePixmaps();
83 
84     /*
85      * (Re)create all pixmaps, repaint and update the display.
86      * Triggers an update();
87      */
88     void createPixmaps(const QSize &newSize);
89 
90     /*
91      * Update window position.
92      * Triggers an update().
93      */
94     void setWindowPosition(qint64 position);
95 
96     /*
97      * Base position of tile
98      */
99     qint64 tilePosition(int index) const;
100 
101     /*
102      * Structure which identifies a point within a given
103      * tile.
104      */
105     struct TilePoint
106     {
107         TilePoint(int idx = 0, qint64 pos = 0, qint64 pix = 0)
108         :   index(idx), positionOffset(pos), pixelOffset(pix)
109         { }
110 
111         // Index of tile
112         int     index;
113 
114         // Number of bytes from start of tile
115         qint64  positionOffset;
116 
117         // Number of pixels from left of corresponding pixmap
118         int     pixelOffset;
119     };
120 
121     /*
122      * Convert position in m_buffer into a tile index and an offset in pixels
123      * into the corresponding pixmap.
124      *
125      * \param position  Offset into m_buffer, in bytes
126 
127      * If position is outside the tile array, index is NullIndex and
128      * offset is zero.
129      */
130     TilePoint tilePoint(qint64 position) const;
131 
132     /*
133      * Convert offset in bytes into a tile into an offset in pixels
134      * within that tile.
135      */
136     int tilePixelOffset(qint64 positionOffset) const;
137 
138     /*
139      * Convert offset in bytes into the window into an offset in pixels
140      * within the widget rect().
141      */
142     int windowPixelOffset(qint64 positionOffset) const;
143 
144     /*
145      * Paint all tiles which can be painted.
146      * \return true iff update() was called
147      */
148     bool paintTiles();
149 
150     /*
151      * Paint the specified tile
152      *
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.
155      */
156     void paintTile(int index);
157 
158     /*
159      * Move the first n tiles to the end of the array, and mark them as not
160      * painted.
161      */
162     void shuffleTiles(int n);
163 
164     /*
165      * Reset tile array
166      */
167     void resetTiles(qint64 newStartPos);
168 
169 private:
170     qint64                  m_bufferPosition;
171     qint64                  m_bufferLength;
172     QByteArray              m_buffer;
173 
174     qint64                  m_audioPosition;
175     QAudioFormat            m_format;
176 
177     bool                    m_active;
178 
179     QSize                   m_pixmapSize;
180     QVector<QPixmap*>       m_pixmaps;
181 
182     struct Tile {
183         // Pointer into parent m_pixmaps array
184         QPixmap*            pixmap;
185 
186         // Flag indicating whether this tile has been painted
187         bool                painted;
188     };
189 
190     QVector<Tile>           m_tiles;
191 
192     // Length of audio data in bytes depicted by each tile
193     qint64                  m_tileLength;
194 
195     // Position in bytes of the first tile, relative to m_buffer
196     qint64                  m_tileArrayStart;
197 
198     qint64                  m_windowPosition;
199     qint64                  m_windowLength;
200 
201 };
202 
203 #endif // WAVEFORM_H
204