Hi to all,
I'm trying to write an audio editor with waveform visualizer.
The number of audio samples I have to draw is ~10E6 or more. This is the code where I draw them:
void WaveWidget::updateWave()
{
int h = height();
int w = width();
// I need width and height here.
// Also, I clear the cache at resize events
p.
setRenderHint( QPainter::Antialiasing );
p.fillRect( temp.rect(), Qt::white );
QPen pen
( Qt
::blue,
1 );
// blue solid line, 1 pixels wide p.setPen( pen );
short* audio = reinterpret_cast<short*>(soundStream);
for( unsigned int i = 0; i < numSamples; i++ )// numSamples very big (~10e6 or more)
{
int startY = maxHeight;
for( int k = 0; k < channels; k++ )
{
short* value1 = audio + k;
int x = i / ( numSamples / w);
int y = *value1 * maxHeight / 0x0000FFFF * 2;
QLine line
( x, startY, x, startY
+ y
);
p.drawLine(line);
startY += increment;
}
audio += sampleSize;
}
m_waveCachePixmap = temp;
}
void WaveWidget::updateWave()
{
int h = height();
int w = width();
// I need width and height here.
// Also, I clear the cache at resize events
QPixmap temp = QPixmap( w, h );
QPainter p( &temp );
p.setRenderHint( QPainter::Antialiasing );
p.fillRect( temp.rect(), Qt::white );
QPen pen( Qt::blue, 1 ); // blue solid line, 1 pixels wide
p.setPen( pen );
short* audio = reinterpret_cast<short*>(soundStream);
for( unsigned int i = 0; i < numSamples; i++ )// numSamples very big (~10e6 or more)
{
int startY = maxHeight;
for( int k = 0; k < channels; k++ )
{
short* value1 = audio + k;
int x = i / ( numSamples / w);
int y = *value1 * maxHeight / 0x0000FFFF * 2;
QLine line( x, startY, x, startY + y );
p.drawLine(line);
startY += increment;
}
audio += sampleSize;
}
m_waveCachePixmap = temp;
}
To copy to clipboard, switch view to plain text mode
and the paintEvent
{
// call updateWave only if necesary, if not I draw what is in cache
if( m_waveCachePixmap.isNull() )
{
updateWave();
}
p.
setRenderHint( QPainter::Antialiasing );
p.drawPixmap( 0, 0, m_waveCachePixmap );
}
void WaveWidget::paintEvent( QPaintEvent* pe )
{
// call updateWave only if necesary, if not I draw what is in cache
if( m_waveCachePixmap.isNull() )
{
updateWave();
}
QPainter p( this );
p.setRenderHint( QPainter::Antialiasing );
p.drawPixmap( 0, 0, m_waveCachePixmap );
}
To copy to clipboard, switch view to plain text mode
where waveWidget inherits from QWidget.
The problem is that the draw process is very slow as I have to draw more that 10 millions of lines. Is not a fast way do draw something? For example the same audio file in audacity is displayed very fast.
Best Regards
Bookmarks