Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/waveform/waveformrendersignaltiles.cpp

Go to the documentation of this file.
00001 
00002 #include <QDebug>
00003 #include <QColor>
00004 #include <QDomNode>
00005 #include <QPaintEvent>
00006 #include <QPainter>
00007 #include <QObject>
00008 #include <QVector>
00009 #include <QLine>
00010 
00011 #include "waveformrendersignaltiles.h"
00012 #include "waveformrenderer.h"
00013 #include "controlobjectthreadmain.h"
00014 #include "controlobject.h"
00015 #include "widget/wskincolor.h"
00016 #include "widget/wwidget.h"
00017 #include "trackinfoobject.h"
00018 
00019 WaveformRenderSignalTiles::WaveformRenderSignalTiles(const char* group, WaveformRenderer *parent)
00020         : m_pParent(parent),
00021           m_iWidth(0),
00022           m_iHeight(0),
00023           m_pTrack(NULL),
00024           signalColor(255,255,255),
00025           kiTileWidth(300) {
00026 }
00027 
00028 void WaveformRenderSignalTiles::resize(int w, int h) {
00029     m_iWidth = w;
00030     m_iHeight = h;
00031     resetTiles();
00032 }
00033 
00034 void WaveformRenderSignalTiles::newTrack(TrackPointer pTrack) {
00035     m_pTrack = pTrack;
00036 }
00037 
00038 void WaveformRenderSignalTiles::setup(QDomNode node) {
00039     signalColor.setNamedColor(WWidget::selectNodeQString(node, "SignalColor"));
00040     signalColor = WSkinColor::getCorrectColor(signalColor);
00041 }
00042 
00043 void WaveformRenderSignalTiles::draw(QPainter *pPainter, QPaintEvent *event, QVector<float> *buffer, double dPlayPos, double rateAdjust) {
00044     if(buffer == NULL)
00045         return;
00046 
00047     int numBufferSamples = buffer->size();
00048     int iCurPos = 0;
00049     if(dPlayPos >= 0) {
00050         iCurPos = (int)(dPlayPos*numBufferSamples);
00051     }
00052 
00053     if((iCurPos % 2) != 0)
00054         iCurPos--;
00055 
00056     pPainter->save();
00057 
00058     pPainter->setPen(signalColor);
00059 
00060     double subpixelsPerPixel = m_pParent->getSubpixelsPerPixel();
00061 
00062     int subpixelWidth = int(m_iWidth * subpixelsPerPixel);
00063 
00064     // For now only deal with rate == 1.
00065     Q_ASSERT(1.0 + rateAdjust == 1.0);
00066     pPainter->scale((1.0+rateAdjust)/subpixelsPerPixel, 1.0);
00067 
00068     int halfw = subpixelWidth/2;
00069     for(int i=0;i<subpixelWidth;) {
00070         // Start at curPos minus half the waveform viewer
00071         int thisIndex = iCurPos+2*(i-halfw);
00072         Q_ASSERT(thisIndex % 2 == 0);
00073         int thisSubpixel = thisIndex / 2;
00074 
00075         if(thisIndex >= 0 && (thisIndex+1) < numBufferSamples) {
00076             Tile* tile = getTileForSubpixel(thisSubpixel, buffer);
00077 
00078             if (tile != NULL) {
00079                 Q_ASSERT(thisSubpixel >= tile->start_subpixel &&
00080                          thisSubpixel < tile->end_subpixel);
00081                 int tile_width = tile->end_subpixel - tile->start_subpixel;
00082                 int tile_offset = thisSubpixel - tile->start_subpixel;
00083                 int tile_remaining = tile->end_subpixel - thisSubpixel;
00084                 int screenStartSubpixel = i - tile_remaining;
00085                 int tileVisibleSubpixels = tile->end_subpixel - thisSubpixel;
00086                 //qDebug() << "Drawing tile for" << thisSubpixel << "screenStart" << screenStartSubpixel << "screenVisible" << tileVisibleSubpixels << "tile start" << tile->start_subpixel << "tile end" << tile->end_subpixel;
00087 
00088                 qDebug() << "Drawing" << thisSubpixel << i << tile->start_subpixel << screenStartSubpixel;
00089                 QRectF source(0, 0, tile_width, m_iHeight);
00090                 QRectF target(i - tile_offset, -m_iHeight/2,
00091                               tile_width, m_iHeight);
00092 
00093 
00094                 pPainter->drawPixmap(target, tile->pixmap, source);
00095                 pPainter->drawRect(target);
00096                 // pPainter->drawPixmap(screenStartSubpixel,
00097                 //                      -m_iHeight/2,
00098                 //                      tile->pixmap);
00099                 i += tileVisibleSubpixels;
00100             } else {
00101                 QPixmap pm = QPixmap(kiTileWidth, m_iHeight);
00102                 pm.fill(Qt::red);
00103                 pPainter->drawPixmap(i, -1.0, pm);
00104                 i += kiTileWidth;
00105             }
00106         } else {
00107             i+=1;
00108         }
00109     }
00110 
00111     pPainter->restore();
00112 }
00113 
00114 void WaveformRenderSignalTiles::resetTiles() {
00115 }
00116 
00117 void drawTile(Tile* tile, QVector<float>* buffer) {
00118     QPixmap& pixmap = tile->pixmap;
00119     float* rawBuffer = buffer->data();
00120     int bufferSize = buffer->size();
00121 
00122     QPainter painter(&pixmap);
00123 
00124     painter.fillRect(0, 0, pixmap.width(), pixmap.height(), Qt::black);
00125 
00126     painter.setPen(Qt::green);
00127     // Translate coordinates frame to the center of the pixmap
00128     painter.translate(0.0, pixmap.height()/2.0);
00129 
00130     // Now scale so that positive-y points up.
00131     painter.scale(1.0, -1.0);
00132 
00133     painter.scale(1.0, pixmap.height()*0.44);
00134 
00135     qDebug() << "Pre-rendering tile" << tile->start_subpixel;
00136     for (int i = 0; i < pixmap.width(); ++i) {
00137         int thisIndex = (tile->start_subpixel+i)*2;
00138         if (thisIndex+1 < bufferSize) {
00139             float sampl = rawBuffer[thisIndex];
00140             float sampr = rawBuffer[thisIndex+1];
00141             QLineF line = QLineF(i, sampl, i, -sampr);
00142             painter.drawLine(line);
00143         }
00144     }
00145     painter.end();
00146 
00147     QImage image = tile->pixmap.toImage();
00148     image.save(QString("/home/rryan/%1.jpg").arg(tile->start_subpixel));
00149 
00150 }
00151 
00152 Tile* WaveformRenderSignalTiles::getTileForSubpixel(int subpixel,
00153                                                     QVector<float>* buffer) {
00154     int tileForSubpixel = subpixel / kiTileWidth;
00155 
00156     if (m_qTileMap.contains(tileForSubpixel)) {
00157         return m_qTileMap[tileForSubpixel];
00158     }
00159 
00160     Tile* newTile = new Tile();
00161     newTile->start_subpixel = tileForSubpixel * kiTileWidth;
00162     newTile->end_subpixel = newTile->start_subpixel + kiTileWidth;
00163     newTile->pixmap = QPixmap(kiTileWidth, m_iHeight);
00164     drawTile(newTile, buffer);
00165     //newTile->pixmap.fill(Qt::green);
00166     m_qTiles.append(newTile);
00167     m_qTileMap[tileForSubpixel] = newTile;
00168     return newTile;
00169 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines