Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

waveform.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002  ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003  **
00004  ** This file is part of the Qtopia Environment.
00005  **
00006  ** This file may be distributed and/or modified under the terms of the
00007  ** GNU General Public License version 2 as published by the Free Software
00008  ** Foundation and appearing in the file LICENSE.GPL included in the
00009  ** packaging of this file.
00010  **
00011  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013  **
00014  ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015  **
00016  ** Contact info@trolltech.com if any conditions of this licensing are
00017  ** not clear to you.
00018  **
00019  **********************************************************************/
00020 #include "waveform.h"
00021 
00022 /* OPIE */
00023 #include <opie2/odebug.h>
00024 using namespace Opie::Core;
00025 
00026 /* QT */
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 //   owarn << "change waveform " << frequency << ", " << channels << "" << oendl; 
00046    samplesPerPixel = frequency * channels / (5 * windowSize);
00047 //   owarn << "Waveform::changeSettings " << samplesPerPixel << "" << oendl; 
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    // Cache the object values in local variables.
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    // Average the incoming samples to scale them to the window.
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    // Copy the final state back to the object.
00103 //owarn << "" << currentValue << ", " << numSamples << ", " << windowPosn << "" << oendl; 
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 

Generated on Sat Nov 5 16:17:34 2005 for OPIE by  doxygen 1.4.2