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