Diffstat (limited to 'noncore/multimedia/opierec/waveform.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/multimedia/opierec/waveform.cpp | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/noncore/multimedia/opierec/waveform.cpp b/noncore/multimedia/opierec/waveform.cpp new file mode 100644 index 0000000..05be373 --- a/dev/null +++ b/noncore/multimedia/opierec/waveform.cpp | |||
@@ -0,0 +1,160 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of the Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "waveform.h" | ||
21 | |||
22 | #include <qlabel.h> | ||
23 | #include <qpainter.h> | ||
24 | |||
25 | |||
26 | Waveform::Waveform( QWidget *parent, const char *name, WFlags fl ) | ||
27 | : QWidget( parent, name, fl ) | ||
28 | { | ||
29 | pixmap = 0; | ||
30 | windowSize = 100; | ||
31 | samplesPerPixel = 8000 / (5 * windowSize); | ||
32 | currentValue = 0; | ||
33 | numSamples = 0; | ||
34 | windowPosn = 0; | ||
35 | window = 0; | ||
36 | } | ||
37 | |||
38 | |||
39 | void Waveform::changeSettings( int frequency, int channels ) | ||
40 | { | ||
41 | makePixmap(); | ||
42 | // qWarning("change waveform %d, %d", frequency, channels); | ||
43 | samplesPerPixel = frequency * channels / (5 * windowSize); | ||
44 | qWarning("Waveform::changeSettings %d", samplesPerPixel); | ||
45 | if ( !samplesPerPixel ) | ||
46 | samplesPerPixel = 1; | ||
47 | currentValue = 0; | ||
48 | numSamples = 0; | ||
49 | windowPosn = 0; | ||
50 | draw(); | ||
51 | } | ||
52 | |||
53 | |||
54 | Waveform::~Waveform() | ||
55 | { | ||
56 | if ( window ) | ||
57 | delete[] window; | ||
58 | if ( pixmap ) | ||
59 | delete pixmap; | ||
60 | } | ||
61 | |||
62 | |||
63 | void Waveform::reset() | ||
64 | { | ||
65 | makePixmap(); | ||
66 | currentValue = 0; | ||
67 | numSamples = 0; | ||
68 | windowPosn = 0; | ||
69 | draw(); | ||
70 | } | ||
71 | |||
72 | |||
73 | void Waveform::newSamples( const short *buf, int len ) | ||
74 | { | ||
75 | // Cache the object values in local variables. | ||
76 | int samplesPerPixel = this->samplesPerPixel; | ||
77 | int currentValue = this->currentValue; | ||
78 | int numSamples = this->numSamples; | ||
79 | short *window = this->window; | ||
80 | int windowPosn = this->windowPosn; | ||
81 | int windowSize = this->windowSize; | ||
82 | |||
83 | // Average the incoming samples to scale them to the window. | ||
84 | while ( len > 0 ) { | ||
85 | currentValue += *buf++; | ||
86 | --len; | ||
87 | if ( ++numSamples >= samplesPerPixel ) { | ||
88 | window[windowPosn++] = (short)(currentValue / numSamples); | ||
89 | if ( windowPosn >= windowSize ) { | ||
90 | this->windowPosn = windowPosn; | ||
91 | draw(); | ||
92 | windowPosn = 0; | ||
93 | } | ||
94 | numSamples = 0; | ||
95 | currentValue = 0; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | // Copy the final state back to the object. | ||
100 | //qWarning("%d, %d, %d", currentValue, numSamples, windowPosn); | ||
101 | this->currentValue = currentValue; | ||
102 | this->numSamples = numSamples; | ||
103 | this->windowPosn = windowPosn; | ||
104 | } | ||
105 | |||
106 | |||
107 | void Waveform::makePixmap() | ||
108 | { | ||
109 | if ( !pixmap ) { | ||
110 | pixmap = new QPixmap( size() ); | ||
111 | windowSize = pixmap->width(); | ||
112 | window = new short [windowSize]; | ||
113 | } | ||
114 | } | ||
115 | |||
116 | |||
117 | void Waveform::draw() | ||
118 | { | ||
119 | pixmap->fill( Qt::black ); | ||
120 | QPainter painter; | ||
121 | painter.begin( pixmap ); | ||
122 | painter.setPen( Qt::green ); | ||
123 | |||
124 | int middle = pixmap->height() / 2; | ||
125 | int mag; | ||
126 | short *window = this->window; | ||
127 | int posn; | ||
128 | int size = windowPosn; | ||
129 | for( posn = 0; posn < size; ++posn ) | ||
130 | { | ||
131 | mag = (window[posn] * middle / 32768); | ||
132 | painter.drawLine(posn, middle - mag, posn, middle + mag); | ||
133 | } | ||
134 | if ( windowPosn < windowSize ) | ||
135 | { | ||
136 | painter.drawLine(windowPosn, middle, windowSize, middle); | ||
137 | } | ||
138 | |||
139 | painter.end(); | ||
140 | |||
141 | paintEvent( 0 ); | ||
142 | } | ||
143 | |||
144 | |||
145 | void Waveform::paintEvent( QPaintEvent * ) | ||
146 | { | ||
147 | QPainter painter; | ||
148 | painter.begin( this ); | ||
149 | |||
150 | if ( pixmap ) { | ||
151 | painter.drawPixmap( 0, 0, *pixmap ); | ||
152 | } else { | ||
153 | painter.setPen( Qt::green ); | ||
154 | QSize sz = size(); | ||
155 | painter.drawLine(0, sz.height() / 2, sz.width(), sz.height() / 2); | ||
156 | } | ||
157 | |||
158 | painter.end(); | ||
159 | } | ||
160 | |||