diff edwaveform.cpp @ 400:f44a89a25c97

Fix waveform display from going over buffer limit.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 19 Oct 2012 15:56:59 +0300
parents 8660c6005032
children d34922e6a244
line wrap: on
line diff
--- a/edwaveform.cpp	Fri Oct 19 10:04:57 2012 +0300
+++ b/edwaveform.cpp	Fri Oct 19 15:56:59 2012 +0300
@@ -13,6 +13,7 @@
     scale     = 1.0f;
     time      = offs = 0;
     duration  = 0;
+    sduration = 0;
 
     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
 }
@@ -28,9 +29,18 @@
 
     int bps = getBps();
     if (bps != 0)
-        duration = ((float) (size / bps) / (float) freq) * 1000.0f;
+    {
+        // Duration in milliseconds
+        duration  = ((float) (size / bps) / (float) freq) * 1000.0f;
+        
+        // Duration in samples
+        sduration = msize / bps;
+    }
     else
-        duration = 0;
+    {
+        duration  = 0;
+        sduration = 0;
+    }
 
     update();
 }
@@ -123,7 +133,7 @@
 void QWaveTrackDisplay::paintEvent(QPaintEvent *)
 {
     QColor waveColor(0, 150, 0);
-    QColor waveCenterLine(0, 0, 0);
+    QColor waveCenterLine(100, 100, 100);
     QColor markerColor(255,255,255);
     QColor bgColor(0, 0, 0);//255, 255, 255);
 
@@ -170,7 +180,9 @@
             qint16 *buf = (qint16 *) data;
             for (int xc = 0; xc < width(); xc++)
             {
-                int value = buf[(int) (((offs + xc) * mscale)) * channels] + voffs;
+                int moffs = (int) (((offs + xc) * mscale));
+                if (moffs >= sduration) break;
+                int value = buf[moffs * channels] + voffs;
                 painter.drawLine(prevX, prevY, xc, value);
                 prevY = value;
                 prevX = xc;
@@ -182,7 +194,9 @@
             qint8 *buf = (qint8 *) data;
             for (int xc = 0; xc < width(); xc++)
             {
-                int value = buf[((int) ((offs + xc) * mscale)) * channels] + voffs;
+                int moffs = (int) (((offs + xc) * mscale));
+                if (moffs >= sduration) break;
+                int value = buf[moffs * channels] + voffs;
                 painter.drawLine(prevX, prevY, xc, value);
                 prevY = value;
                 prevX = xc;