|
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:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are met: |
|
14 ** - Redistributions of source code must retain the above copyright notice, |
|
15 ** this list of conditions and the following disclaimer. |
|
16 ** - Redistributions in binary form must reproduce the above copyright notice, |
|
17 ** this list of conditions and the following disclaimer in the documentation |
|
18 ** and/or other materials provided with the distribution. |
|
19 ** - Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the |
|
20 ** names of its contributors may be used to endorse or promote products |
|
21 ** derived from this software without specific prior written permission. |
|
22 ** |
|
23 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
24 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
25 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
26 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
|
27 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
28 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
29 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
30 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
31 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
32 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
33 ** POSSIBILITY OF SUCH DAMAGE. |
|
34 ** $QT_END_LICENSE$ |
|
35 ** |
|
36 *****************************************************************************/ |
|
37 |
|
38 #ifndef WAVEFORM_H |
|
39 #define WAVEFORM_H |
|
40 |
|
41 #include <QWidget> |
|
42 #include <QtMultimedia/QAudioFormat> |
|
43 #include <QPixmap> |
|
44 #include <QScopedPointer> |
|
45 |
|
46 class QByteArray; |
|
47 |
|
48 /** |
|
49 * Widget which displays a section of the audio waveform. |
|
50 * |
|
51 * The waveform is rendered on a set of QPixmaps which form a group of tiles |
|
52 * whose extent covers the widget. As the audio position is updated, these |
|
53 * tiles are scrolled from left to right; when the left-most tile scrolls |
|
54 * outside the widget, it is moved to the right end of the tile array and |
|
55 * painted with the next section of the waveform. |
|
56 */ |
|
57 class Waveform : public QWidget { |
|
58 Q_OBJECT |
|
59 public: |
|
60 Waveform(const QByteArray &buffer, QWidget *parent = 0); |
|
61 ~Waveform(); |
|
62 |
|
63 // QWidget |
|
64 void paintEvent(QPaintEvent *event); |
|
65 void resizeEvent(QResizeEvent *event); |
|
66 |
|
67 void initialize(const QAudioFormat &format, qint64 audioBufferSize, qint64 windowDurationUs); |
|
68 void reset(); |
|
69 |
|
70 void setAutoUpdatePosition(bool enabled); |
|
71 |
|
72 public slots: |
|
73 void dataLengthChanged(qint64 length); |
|
74 void positionChanged(qint64 position); |
|
75 |
|
76 private: |
|
77 static const int NullIndex = -1; |
|
78 |
|
79 void deletePixmaps(); |
|
80 |
|
81 /* |
|
82 * (Re)create all pixmaps, repaint and update the display. |
|
83 * Triggers an update(); |
|
84 */ |
|
85 void createPixmaps(const QSize &newSize); |
|
86 |
|
87 /* |
|
88 * Update window position. |
|
89 * Triggers an update(). |
|
90 */ |
|
91 void setWindowPosition(qint64 position); |
|
92 |
|
93 /* |
|
94 * Base position of tile |
|
95 */ |
|
96 qint64 tilePosition(int index) const; |
|
97 |
|
98 /* |
|
99 * Structure which identifies a point within a given |
|
100 * tile. |
|
101 */ |
|
102 struct TilePoint |
|
103 { |
|
104 TilePoint(int idx = 0, qint64 pos = 0, qint64 pix = 0) |
|
105 : index(idx), positionOffset(pos), pixelOffset(pix) |
|
106 { } |
|
107 |
|
108 // Index of tile |
|
109 int index; |
|
110 |
|
111 // Number of bytes from start of tile |
|
112 qint64 positionOffset; |
|
113 |
|
114 // Number of pixels from left of corresponding pixmap |
|
115 int pixelOffset; |
|
116 }; |
|
117 |
|
118 /* |
|
119 * Convert position in m_buffer into a tile index and an offset in pixels |
|
120 * into the corresponding pixmap. |
|
121 * |
|
122 * \param position Offset into m_buffer, in bytes |
|
123 |
|
124 * If position is outside the tile array, index is NullIndex and |
|
125 * offset is zero. |
|
126 */ |
|
127 TilePoint tilePoint(qint64 position) const; |
|
128 |
|
129 /* |
|
130 * Convert offset in bytes into a tile into an offset in pixels |
|
131 * within that tile. |
|
132 */ |
|
133 int tilePixelOffset(qint64 positionOffset) const; |
|
134 |
|
135 /* |
|
136 * Convert offset in bytes into the window into an offset in pixels |
|
137 * within the widget rect(). |
|
138 */ |
|
139 int windowPixelOffset(qint64 positionOffset) const; |
|
140 |
|
141 /* |
|
142 * Paint all tiles which can be painted. |
|
143 * \return true iff update() was called |
|
144 */ |
|
145 bool paintTiles(); |
|
146 |
|
147 /* |
|
148 * Paint the specified tile |
|
149 * |
|
150 * \pre Sufficient data is available to completely paint the tile, i.e. |
|
151 * m_dataLength is greater than the upper bound of the tile. |
|
152 */ |
|
153 void paintTile(int index); |
|
154 |
|
155 /* |
|
156 * Move the first n tiles to the end of the array, and mark them as not |
|
157 * painted. |
|
158 */ |
|
159 void shuffleTiles(int n); |
|
160 |
|
161 /* |
|
162 * Reset tile array |
|
163 */ |
|
164 void resetTiles(qint64 newStartPos); |
|
165 |
|
166 private: |
|
167 const QByteArray& m_buffer; |
|
168 qint64 m_dataLength; |
|
169 qint64 m_position; |
|
170 QAudioFormat m_format; |
|
171 |
|
172 bool m_active; |
|
173 |
|
174 QSize m_pixmapSize; |
|
175 QVector<QPixmap*> m_pixmaps; |
|
176 |
|
177 struct Tile { |
|
178 // Pointer into parent m_pixmaps array |
|
179 QPixmap* pixmap; |
|
180 |
|
181 // Flag indicating whether this tile has been painted |
|
182 bool painted; |
|
183 }; |
|
184 |
|
185 QVector<Tile> m_tiles; |
|
186 |
|
187 // Length of audio data in bytes depicted by each tile |
|
188 qint64 m_tileLength; |
|
189 |
|
190 // Position in bytes of the first tile, relative to m_buffer |
|
191 qint64 m_tileArrayStart; |
|
192 |
|
193 qint64 m_windowPosition; |
|
194 qint64 m_windowLength; |
|
195 |
|
196 }; |
|
197 |
|
198 #endif // WAVEFORM_H |