waveform.cpp

来自「Qtopia下的多媒体录音源码」· C++ 代码 · 共 143 行

CPP
143
字号
/******************************************************************************** Copyright (C) 2000-2006 TROLLTECH ASA. All rights reserved.**** This file is part of the Phone Edition of the Qtopia Toolkit.**** Licensees holding a valid license agreement from Trolltech or any of its** authorized distributors may use this file in accordance with** the License Agreement provided with the Licensed Software.**** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for**   information about Trolltech's Commercial License Agreements.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.********** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "waveform.h"#include <qlabel.h>#include <qpainter.h>Waveform::Waveform(QWidget *parent, Qt::WFlags fl):    QWidget(parent, fl),    currentValue(0),    numSamples(0),    window(NULL),    windowPosn(0),    windowSize(100){    samplesPerPixel = 8000 / (5 * windowSize);    QPalette pal = palette();    pal.setColor(QPalette::Window, Qt::black);    setPalette(pal);    setAutoFillBackground(true);}void Waveform::changeSettings( int frequency, int channels ){    makePixmap();    samplesPerPixel = frequency * channels / (5 * windowSize);    if (samplesPerPixel == 0 )        samplesPerPixel = 1;    currentValue = 0;    numSamples = 0;    windowPosn = 0;    update();}Waveform::~Waveform(){	delete[] window;}void Waveform::reset(){    makePixmap();    currentValue = 0;    numSamples = 0;    windowPosn = 0;    update();}void Waveform::newSamples(const short *buf, int len){    // Average the incoming samples to scale them to the window.    while ( len > 0 ) {        currentValue += *buf++;        --len;        if (++numSamples >= samplesPerPixel) {            window[windowPosn++] = (short)(currentValue / numSamples);            if (windowPosn >= windowSize) {                repaint();                windowPosn = 0;            }            numSamples = 0;            currentValue = 0;        }    }}void Waveform::makePixmap(){    delete window;        windowSize = width();    window = new short[windowSize];}void Waveform::paintEvent(QPaintEvent*){    QPainter    painter(this);    painter.setPen(Qt::green);    if (windowPosn == 0) {        painter.drawLine(0, height() / 2, width(), height() / 2);    } else {        int     middle = height() / 2;        for (int posn = 0; posn < windowPosn; ++posn)        {            int mag = (window[posn] * middle) / 32768;  // SHRT_MAX + 1???            painter.drawLine(posn, middle - mag, posn, middle + mag);        }        if (windowPosn < windowSize)        {            painter.drawLine(windowPosn, middle, windowSize, middle);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?