view edwaveform.cpp @ 388:015f2da65841

Add edview to the build.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 18 Oct 2012 20:11:34 +0300
parents e5220ff48bc8
children 28a74940f2b6
line wrap: on
line source

#include <QtGui>
#include <SDL_audio.h>
#include "edwaveform.h"


WaveDisplay::WaveDisplay(QWidget *parent) : QWidget(parent)
{
    data      = NULL;
    len       = 0;
    format    = AUDIO_S16SYS;
    channels  = 1;
    freq      = 1;
    scale     = 1.0f;
    time      = offs = 0;

    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
}


void WaveDisplay::setWaveform(void *mdata, int mlen, int mformat, int mchannels, int mfreq)
{
    data     = mdata;
    len      = mlen;
    format   = mformat;
    channels = mchannels;
    freq     = mfreq;
    update();
}


void WaveDisplay::setTime(const int mtime)
{
    time = mtime;
    update();
}


void WaveDisplay::setOffset(const int moffs)
{
    offs = moffs;
    update();
}


void WaveDisplay::setScale(const float mscale)
{
    if (mscale > 0.05)
        scale = mscale;
    update();
}


int WaveDisplay::getTime()
{
    return time;
}


int WaveDisplay::getOffset()
{
    return offs;
}


void WaveDisplay::paintEvent(QPaintEvent *)
{
    QColor waveColor(0, 150, 0);
    QColor waveCenterLine(0, 0, 0);
    QColor markerColor(0,0,0);
    QColor bgColor(0, 0, 0);//255, 255, 255);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.fillRect(QRect(0, 0, width(), height()), bgColor);

    if (data != NULL)
    {
        int voffs = 0;

        painter.save();
        painter.translate(0, height() / 2);

        painter.setPen(waveCenterLine);
        painter.drawLine(0, 0, width(), 0);

        switch (format)
        {
            case AUDIO_S16SYS:
                painter.scale(1.0f, (float) height() / 32768.0f);
                break;
            case AUDIO_U16SYS:
                voffs = -32768;
                painter.scale(1.0f, height() / 32768.0f);
                break;

            case AUDIO_S8:
                painter.scale(1.0f, height() / 128.0f);
                break;
            case AUDIO_U8:
                voffs = -128;
                painter.scale(1.0f, height() / 128.0f);
                break;
        }

        painter.scale(1.0f, 0.7f);
        painter.setPen(waveColor);

        int prevY = 0, prevX = 0;
        if (format == AUDIO_S16SYS || format == AUDIO_U16SYS)
        {
            qint16 *buf = (qint16 *) data;
            for (int xc = 0; xc < width(); xc++)
            {
                int value = buf[(int) (((offs + xc) * scale)) * channels] + voffs;
                painter.drawLine(prevX, prevY, xc, value);
                prevY = value;
                prevX = xc;
            }
        }
        else
        if (format == AUDIO_S8 || format == AUDIO_U8)
        {
            qint8 *buf = (qint8 *) data;
            for (int xc = 0; xc < width(); xc++)
            {
                int value = buf[((int) ((offs + xc) * scale)) * channels] + voffs;
                painter.drawLine(prevX, prevY, xc, value);
                prevY = value;
                prevX = xc;
            }
        }
        
        painter.restore();
    }
    
    if (time >= offs * scale) // && time - offs <= width() * scale)
    {
        int xc = time - offs;
        painter.scale(1.0f / scale, 1.0f);
        painter.setPen(markerColor);
        painter.drawLine(xc, 0, xc, height());
    }
}


void WaveDisplay::mousePressEvent(QMouseEvent *ev)
{
/*
    if (ev->button() == Qt::LeftButton)
    {
        lastPoint = ev->pos();
        scribbling = true;
    }
*/
}


void WaveDisplay::mouseMoveEvent(QMouseEvent *ev)
{
/*
    if ((ev->buttons() & Qt::LeftButton) && scribbling)
        drawLineTo(ev->pos());
*/
}


void WaveDisplay::mouseReleaseEvent(QMouseEvent *ev)
{
/*
    if (ev->button() == Qt::LeftButton && scribbling)
    {
        drawLineTo(ev->pos());
        scribbling = false;
    }
*/
}


WaveformView::WaveformView(QWidget *parent) : QWidget(parent)
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setMargin(0);
    wave = new WaveDisplay(this);

    QFrame *infoLayoutContainer = new QFrame(this);
    infoLayoutContainer->setFrameStyle(QFrame::StyledPanel | QFrame::Plain);
    infoLayoutContainer->setLineWidth(2);
//    infoLayoutContainer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
//    infoLayoutContainer->resize(250, 60);

    QVBoxLayout *infoLayout = new QVBoxLayout(infoLayoutContainer);
    infoLayout->setMargin(0);
//    QVBoxLayout *infoLayout = new QVBoxLayout();
    infoName = new QLabel("Audio");
    infoName->setStyleSheet("QLabel { background-color: black; color: white; padding: 2px; }");
    infoLayout->addWidget(infoName);

    infoData = new QLabel();
    infoData->setStyleSheet("QLabel { padding: 2px; }");
    infoLayout->addWidget(infoData);

    mainLayout->addWidget(infoLayoutContainer);
//    mainLayout->addLayout(infoLayout);
    mainLayout->addWidget(wave);
}


void WaveformView::setWaveform(void *mdata, int mlen, int mformat, int mchannels, int mfreq)
{
    QString fmt;
    switch (mformat)
    {
        case AUDIO_S16SYS: fmt = "16bit (S)"; break;
        case AUDIO_U16SYS: fmt = "16bit (U)"; break;
        case AUDIO_S8:     fmt = "8bit (S)"; break;
        case AUDIO_U8:     fmt = "8bit (U)"; break;
        default:           fmt = "?"; break;
    }
    infoData->setText(QString("<b>%1</b>, <b>%2</b> ch, <b>%3</b> Hz").arg(fmt).arg(mchannels).arg(mfreq));
    wave->setWaveform(mdata, mlen, mformat, mchannels, mfreq);
    update();
}


void WaveformView::setName(QString name)
{
    infoName->setText(name);
    update();
}

void WaveformView::setTime(const int mtime)
{
    wave->setTime(mtime);
}

void WaveformView::setOffset(const int moffs)
{
    wave->setOffset(moffs);
}

void WaveformView::setScale(const float mscale)
{
    wave->setScale(mscale);
}

int WaveformView::getTime()
{
    return wave->getTime();
}

int WaveformView::getOffset()
{
    return wave->getOffset();
}