00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "waveform.h"
00021
00022
00023 #include <opie2/odebug.h>
00024 using namespace Opie::Core;
00025
00026
00027 #include <qpainter.h>
00028
00029 Waveform::Waveform( QWidget *parent, const char *name, WFlags fl )
00030 : QWidget( parent, name, fl )
00031 {
00032 pixmap = 0;
00033 windowSize = 100;
00034 samplesPerPixel = 8000 / (5 * windowSize);
00035 currentValue = 0;
00036 numSamples = 0;
00037 windowPosn = 0;
00038 window = 0;
00039 }
00040
00041
00042 void Waveform::changeSettings( int frequency, int channels )
00043 {
00044 makePixmap();
00045
00046 samplesPerPixel = frequency * channels / (5 * windowSize);
00047
00048 if ( !samplesPerPixel )
00049 samplesPerPixel = 1;
00050 currentValue = 0;
00051 numSamples = 0;
00052 windowPosn = 0;
00053 draw();
00054 }
00055
00056
00057 Waveform::~Waveform()
00058 {
00059 if ( window )
00060 delete[] window;
00061 if ( pixmap )
00062 delete pixmap;
00063 }
00064
00065
00066 void Waveform::reset()
00067 {
00068 makePixmap();
00069 currentValue = 0;
00070 numSamples = 0;
00071 windowPosn = 0;
00072 draw();
00073 }
00074
00075
00076 void Waveform::newSamples( const short *buf, int len )
00077 {
00078
00079 int samplesPerPixel = this->samplesPerPixel;
00080 int currentValue = this->currentValue;
00081 int numSamples = this->numSamples;
00082 short *window = this->window;
00083 int windowPosn = this->windowPosn;
00084 int windowSize = this->windowSize;
00085
00086
00087 while ( len > 0 ) {
00088 currentValue += *buf++;
00089 --len;
00090 if ( ++numSamples >= samplesPerPixel ) {
00091 window[windowPosn++] = (short)(currentValue / numSamples);
00092 if ( windowPosn >= windowSize ) {
00093 this->windowPosn = windowPosn;
00094 draw();
00095 windowPosn = 0;
00096 }
00097 numSamples = 0;
00098 currentValue = 0;
00099 }
00100 }
00101
00102
00103
00104 this->currentValue = currentValue;
00105 this->numSamples = numSamples;
00106 this->windowPosn = windowPosn;
00107 }
00108
00109
00110 void Waveform::makePixmap()
00111 {
00112 if ( !pixmap ) {
00113 pixmap = new QPixmap( size() );
00114 windowSize = pixmap->width();
00115 window = new short [windowSize];
00116 }
00117 }
00118
00119
00120 void Waveform::draw()
00121 {
00122 pixmap->fill( Qt::black );
00123 QPainter painter;
00124 painter.begin( pixmap );
00125 painter.setPen( Qt::green );
00126
00127 int middle = pixmap->height() / 2;
00128 int mag;
00129 short *window = this->window;
00130 int posn;
00131 int size = windowPosn;
00132 for( posn = 0; posn < size; ++posn )
00133 {
00134 mag = (window[posn] * middle / 32768);
00135 painter.drawLine(posn, middle - mag, posn, middle + mag);
00136 }
00137 if ( windowPosn < windowSize )
00138 {
00139 painter.drawLine(windowPosn, middle, windowSize, middle);
00140 }
00141
00142 painter.end();
00143
00144 paintEvent( 0 );
00145 }
00146
00147
00148 void Waveform::paintEvent( QPaintEvent * )
00149 {
00150 QPainter painter;
00151 painter.begin( this );
00152
00153 if ( pixmap ) {
00154 painter.drawPixmap( 0, 0, *pixmap );
00155 } else {
00156 painter.setPen( Qt::green );
00157 QSize sz = size();
00158 painter.drawLine(0, sz.height() / 2, sz.width(), sz.height() / 2);
00159 }
00160
00161 painter.end();
00162 }
00163