source: trunk/demos/spectrum/app/waveform.h@ 858

Last change on this file since 858 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
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
49QT_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 */
60class Waveform : public QWidget {
61 Q_OBJECT
62public:
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
75public slots:
76 void bufferChanged(qint64 position, qint64 length, const QByteArray &buffer);
77 void audioPositionChanged(qint64 position);
78
79private:
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
169private:
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
Note: See TracBrowser for help on using the repository browser.