/***************************************************************/
/* Paint event */
/***************************************************************/
{
// I call updateWave only if necessary if not I draw what is in cache
if( m_waveCachePixmap.isNull() )
{
updateWave();
}
p.
setRenderHint( QPainter::Antialiasing );
p.drawPixmap( 0, 0, m_waveCachePixmap );
}
/***************************************************************/
/* Paint event */
/***************************************************************/
void WaveWidget::paintEvent( QPaintEvent* pe )
{
// I call updateWave only if necessary 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
/************************************************************************/
/* updateWave */
/* call this only when the wave is changed, */
/************************************************************************/
void WaveWidget::updateWave()
{
int h = height();
int w = width();
// I want another width and height here.
// Also, you might to clear the cache at resize events
p.fillRect( temp.rect(), Qt::white );
p.
setRenderHint( QPainter::Antialiasing );
QPen pen
( Qt
::blue,
1 );
// blue solid line, 1 pixels wide p.setPen( pen );
// Draw the waveform or no data thingie
if( m_wave )
{
unsigned int numSamples = m_wave->getNumSamples();
int channels = m_wave->getNumChannels();
FMOD_SOUND_TYPE soundType = m_wave->getSoundType();
FMOD_SOUND_FORMAT soundFormat = m_wave->getSoundFormat();
int sampleSize = channels * ( soundFormat == PCM8 ) ? 1 : 2;
//char* stream = sound->stream;
void* soundStream = m_wave->getSoundStream();
int maxHeight = (h / 2) / channels;
int increment = (h / 2);
switch( soundFormat )
{
case PCM8: //8 bits mono
{
qDebug("PCM8 sound file");
char* audio = reinterpret_cast<char*>(soundStream);
for( unsigned int i = 0; i < numSamples ; i++ )
{
char* current = (audio) + (i * sampleSize);
char audio = *current;
float reproc = audio / (float)(1 << 8);
//DrawLine( i, 0, i, height * reproc );
//QLine line( x, startY, x, startY + Y);
//p.drawLine(line);
//startY += increment;
}
break;
}
case PCM16: //16 bits stereo (LRLRLRLR...LRLR)
{
qDebug("PCM16 sound file");
short* audio = reinterpret_cast<short*>(soundStream);
for( unsigned int i = 0; i < numSamples; i++ )
{
int startY = maxHeight;
for( int k = 0; k < channels; k++ )
{
//short* value1 = reinterpret_cast<short*>(stream + k * 2);
//short* value1 = audio + k * 2;
short* value1 = audio + k;
int x = i / ( numSamples / w);//?
int y = *value1 * maxHeight / 0x0000FFFF * 2;
QLine line
( x, startY, x, startY
+ y
);
//std::cout << "Drawing at x: " << x << "startY: " << startY << "y: " << y << "\n";
p.drawLine(line);
startY += increment;
}
//std::cout << audio[i] << "\n";
audio += sampleSize;
}
break;
}
default:
{
qDebug("Undefined sound type");
break;
}
}
}
else
{
// Inform the user that no file has been loaded - perhaps using a
// no-data icon or something
qDebug("No data");
}
m_waveCachePixmap = temp;
}
/************************************************************************/
/* updateWave */
/* call this only when the wave is changed, */
/************************************************************************/
void WaveWidget::updateWave()
{
int h = height();
int w = width();
// I want another width and height here.
// Also, you might to clear the cache at resize events
QPixmap temp = QPixmap( w, h );
QPainter p( &temp );
p.fillRect( temp.rect(), Qt::white );
p.setRenderHint( QPainter::Antialiasing );
QPen pen( Qt::blue, 1 ); // blue solid line, 1 pixels wide
p.setPen( pen );
// Draw the waveform or no data thingie
if( m_wave )
{
unsigned int numSamples = m_wave->getNumSamples();
int channels = m_wave->getNumChannels();
FMOD_SOUND_TYPE soundType = m_wave->getSoundType();
FMOD_SOUND_FORMAT soundFormat = m_wave->getSoundFormat();
int sampleSize = channels * ( soundFormat == PCM8 ) ? 1 : 2;
//char* stream = sound->stream;
void* soundStream = m_wave->getSoundStream();
int maxHeight = (h / 2) / channels;
int increment = (h / 2);
switch( soundFormat )
{
case PCM8: //8 bits mono
{
qDebug("PCM8 sound file");
char* audio = reinterpret_cast<char*>(soundStream);
for( unsigned int i = 0; i < numSamples ; i++ )
{
char* current = (audio) + (i * sampleSize);
char audio = *current;
float reproc = audio / (float)(1 << 8);
//DrawLine( i, 0, i, height * reproc );
//QLine line( x, startY, x, startY + Y);
//p.drawLine(line);
//startY += increment;
}
break;
}
case PCM16: //16 bits stereo (LRLRLRLR...LRLR)
{
qDebug("PCM16 sound file");
short* audio = reinterpret_cast<short*>(soundStream);
for( unsigned int i = 0; i < numSamples; i++ )
{
int startY = maxHeight;
for( int k = 0; k < channels; k++ )
{
//short* value1 = reinterpret_cast<short*>(stream + k * 2);
//short* value1 = audio + k * 2;
short* value1 = audio + k;
int x = i / ( numSamples / w);//?
int y = *value1 * maxHeight / 0x0000FFFF * 2;
QLine line( x, startY, x, startY + y );
//std::cout << "Drawing at x: " << x << "startY: " << startY << "y: " << y << "\n";
p.drawLine(line);
startY += increment;
}
//std::cout << audio[i] << "\n";
audio += sampleSize;
}
break;
}
default:
{
qDebug("Undefined sound type");
break;
}
}
}
else
{
// Inform the user that no file has been loaded - perhaps using a
// no-data icon or something
qDebug("No data");
}
m_waveCachePixmap = temp;
}
To copy to clipboard, switch view to plain text mode
Bookmarks