| 1 | /* |
|---|
| 2 | * Hydrogen |
|---|
| 3 | * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net] |
|---|
| 4 | * |
|---|
| 5 | * http://www.hydrogen-music.org |
|---|
| 6 | * |
|---|
| 7 | * This program is free software; you can redistribute it and/or modify |
|---|
| 8 | * it under the terms of the GNU General Public License as published by |
|---|
| 9 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | * (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * This program is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY, without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | * GNU General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License |
|---|
| 18 | * along with this program; if not, write to the Free Software |
|---|
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | #include <QtGui> |
|---|
| 23 | |
|---|
| 24 | #include <hydrogen/sample.h> |
|---|
| 25 | #include <hydrogen/Song.h> |
|---|
| 26 | #include <hydrogen/instrument.h> |
|---|
| 27 | using namespace H2Core; |
|---|
| 28 | |
|---|
| 29 | #include "WaveDisplay.h" |
|---|
| 30 | #include "../Skin.h" |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | WaveDisplay::WaveDisplay(QWidget* pParent) |
|---|
| 34 | : QWidget( pParent ) |
|---|
| 35 | , Object( "WaveDisplay" ) |
|---|
| 36 | , m_sSampleName( "" ) |
|---|
| 37 | { |
|---|
| 38 | setAttribute(Qt::WA_NoBackground); |
|---|
| 39 | |
|---|
| 40 | //INFOLOG( "INIT" ); |
|---|
| 41 | int w = 235; |
|---|
| 42 | int h = 58; |
|---|
| 43 | resize( w, h ); |
|---|
| 44 | |
|---|
| 45 | bool ok = m_background.load( Skin::getImagePath() + "/waveDisplay/background.png" ); |
|---|
| 46 | if( ok == false ){ |
|---|
| 47 | ERRORLOG( "Error loading pixmap" ); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | m_pPeakData = new int[ w ]; |
|---|
| 51 | |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | WaveDisplay::~WaveDisplay() |
|---|
| 58 | { |
|---|
| 59 | //INFOLOG( "DESTROY" ); |
|---|
| 60 | |
|---|
| 61 | delete[] m_pPeakData; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | void WaveDisplay::paintEvent(QPaintEvent *ev) |
|---|
| 67 | { |
|---|
| 68 | QPainter painter( this ); |
|---|
| 69 | painter.setRenderHint( QPainter::Antialiasing ); |
|---|
| 70 | painter.drawPixmap( ev->rect(), m_background, ev->rect() ); |
|---|
| 71 | |
|---|
| 72 | painter.setPen( QColor( 102, 150, 205 ) ); |
|---|
| 73 | int VCenter = height() / 2; |
|---|
| 74 | for ( int x = 0; x < width(); x++ ) { |
|---|
| 75 | painter.drawLine( x, VCenter, x, m_pPeakData[x] + VCenter ); |
|---|
| 76 | painter.drawLine( x, VCenter, x, -m_pPeakData[x] + VCenter ); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | QFont font; |
|---|
| 80 | font.setWeight( 63 ); |
|---|
| 81 | painter.setFont( font ); |
|---|
| 82 | painter.setPen( QColor( 255 , 255, 255, 200 ) ); |
|---|
| 83 | painter.drawText( 0, 0, width(), 20, Qt::AlignCenter, m_sSampleName ); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | void WaveDisplay::updateDisplay( H2Core::InstrumentLayer *pLayer ) |
|---|
| 89 | { |
|---|
| 90 | if ( pLayer && pLayer->get_sample() ) { |
|---|
| 91 | // Extract the filename from the complete path |
|---|
| 92 | QString sName = pLayer->get_sample()->get_filename(); |
|---|
| 93 | int nPos = sName.lastIndexOf( "/" ); |
|---|
| 94 | m_sSampleName = sName.mid( nPos + 1, sName.length() ); |
|---|
| 95 | |
|---|
| 96 | // INFOLOG( "[updateDisplay] sample: " + m_sSampleName ); |
|---|
| 97 | |
|---|
| 98 | int nSampleLenght = pLayer->get_sample()->get_n_frames(); |
|---|
| 99 | float nScaleFactor = nSampleLenght / width(); |
|---|
| 100 | |
|---|
| 101 | float fGain = height() / 2.0 * pLayer->get_gain(); |
|---|
| 102 | |
|---|
| 103 | float *pSampleData = pLayer->get_sample()->get_data_l(); |
|---|
| 104 | |
|---|
| 105 | int nSamplePos =0; |
|---|
| 106 | int nVal; |
|---|
| 107 | for ( int i = 0; i < width(); ++i ){ |
|---|
| 108 | nVal = 0; |
|---|
| 109 | for ( int j = 0; j < nScaleFactor; ++j ) { |
|---|
| 110 | if ( j < nSampleLenght ) { |
|---|
| 111 | int newVal = (int)( pSampleData[ nSamplePos ] * fGain ); |
|---|
| 112 | if ( newVal > nVal ) { |
|---|
| 113 | nVal = newVal; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | ++nSamplePos; |
|---|
| 117 | } |
|---|
| 118 | m_pPeakData[ i ] = nVal; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | else { |
|---|
| 122 | m_sSampleName = "-"; |
|---|
| 123 | for ( int i =0; i < width(); ++i ){ |
|---|
| 124 | m_pPeakData[ i ] = 0; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | update(); |
|---|
| 129 | } |
|---|
| 130 | |
|---|