hydrogen 1.2.6
WaveDisplay.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-2025 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>
27using namespace H2Core;
28
29#include "WaveDisplay.h"
30#include "../Skin.h"
31#include "../HydrogenApp.h"
32
33WaveDisplay::WaveDisplay(QWidget* pParent)
34 : QWidget( pParent )
35 , m_nCurrentWidth( 0 )
36 , m_sSampleName( "-" )
37 , m_pLayer( nullptr )
38 , m_SampleNameAlignment( Qt::AlignCenter )
39{
40 setAttribute(Qt::WA_OpaquePaintEvent);
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[ width() ];
48 memset( m_pPeakData, 0, width() * sizeof( m_pPeakData[0] ) );
49
51}
52
53
54
55
57{
58 //INFOLOG( "DESTROY" );
59
60 delete[] m_pPeakData;
61}
62
63void WaveDisplay::paintEvent( QPaintEvent *ev ) {
64 UNUSED(ev);
65 QPainter painter( this );
66
67 createBackground( &painter );
68}
69
70void WaveDisplay::createBackground( QPainter* painter ) {
72
73 painter->setRenderHint( QPainter::Antialiasing );
74
75 QBrush brush = QBrush(Qt::red, m_Background);
76 brush.setStyle(Qt::TexturePattern);
77 painter->setBrush(brush);
78 painter->drawRect(0, 0, width(), height());
79
80 if( m_pLayer ){
81 painter->setPen( QColor( 102, 150, 205 ) );
82 int VCenter = height() / 2;
83 for ( int x = 0; x < width(); x++ ) {
84 painter->drawLine( x, VCenter, x, m_pPeakData[x] + VCenter );
85 painter->drawLine( x, VCenter, x, -m_pPeakData[x] + VCenter );
86 }
87
88 }
89
90 QFont font( pPref->getApplicationFontFamily(), getPointSize( pPref->getFontSize() ) );
91 font.setWeight( QFont::Bold );
92 painter->setFont( font );
93 painter->setPen( QColor( 255 , 255, 255, 200 ) );
94
95 if( m_SampleNameAlignment == Qt::AlignCenter ){
96 painter->drawText( 0, 0, width(), 20, m_SampleNameAlignment, m_sSampleName );
97 }
98 else if( m_SampleNameAlignment == Qt::AlignLeft )
99 {
100 // Use a small offnset iso. starting directly at the left border
101 painter->drawText( 20, 0, width(), 20, m_SampleNameAlignment, m_sSampleName );
102 }
103
104}
105
106void WaveDisplay::resizeEvent( QResizeEvent * event )
107{
109}
110
111
112
113void WaveDisplay::updateDisplay( std::shared_ptr<H2Core::InstrumentLayer> pLayer )
114{
115 int currentWidth = width();
116
117 if(!pLayer || currentWidth <= 0){
118 m_pLayer = nullptr;
119 m_sSampleName = "-";
120
121 update();
122 return;
123 }
124
125 if(currentWidth != m_nCurrentWidth){
126 delete[] m_pPeakData;
127 m_pPeakData = new int[ currentWidth ];
128
129 m_nCurrentWidth = currentWidth;
130 }
131
132 if ( pLayer && pLayer->get_sample() ) {
133 m_pLayer = pLayer;
134 m_sSampleName = pLayer->get_sample()->get_filename();
135
136 //INFOLOG( "[updateDisplay] sample: " + m_sSampleName );
137
138 int nSampleLength = pLayer->get_sample()->get_frames();
139 int nScaleFactor = nSampleLength / m_nCurrentWidth;
140
141 float fGain = height() / 2.0 * pLayer->get_gain();
142
143 auto pSampleData = pLayer->get_sample()->get_data_l();
144
145 int nSamplePos =0;
146 int nVal;
147 for ( int i = 0; i < width(); ++i ){
148 nVal = 0;
149 for ( int j = 0; j < nScaleFactor; ++j ) {
150 if ( j < nSampleLength ) {
151 int newVal = (int)( pSampleData[ nSamplePos ] * fGain );
152 if ( newVal > nVal ) {
153 nVal = newVal;
154 }
155 }
156 ++nSamplePos;
157 }
158 m_pPeakData[ i ] = nVal;
159 }
160 }
161 else {
162 m_pLayer = nullptr;
163 m_sSampleName = "-";
164 for ( int i =0; i < m_nCurrentWidth; ++i ){
165 m_pPeakData[ i ] = 0;
166 }
167
168 }
169
170 update();
171}
172
174{
175 if (ev->button() == Qt::LeftButton) {
176 emit doubleClicked(this);
177 }
178}
179
181{
182 if ( changes & H2Core::Preferences::Changes::Font ) {
183 update();
184 }
185}
#define ERRORLOG(x)
Definition Object.h:242
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
Changes
Bitwise or-able options showing which part of the Preferences were altered using the PreferencesDialo...
@ Font
Either the font size or font family have changed.
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
void preferencesChanged(H2Core::Preferences::Changes changes)
Propagates a change in the Preferences through the GUI.
static QString getImagePath()
Definition Skin.h:36
Qt::AlignmentFlag m_SampleNameAlignment
Definition WaveDisplay.h:67
WaveDisplay(QWidget *pParent)
virtual void mouseDoubleClickEvent(QMouseEvent *ev) override
int m_nCurrentWidth
Definition WaveDisplay.h:76
virtual void updateDisplay(std::shared_ptr< H2Core::InstrumentLayer > pLayer)
void createBackground(QPainter *painter)
QString m_sSampleName
Definition WaveDisplay.h:69
int * m_pPeakData
Definition WaveDisplay.h:70
void onPreferencesChanged(H2Core::Preferences::Changes changes)
void doubleClicked(QWidget *pWidget)
virtual void resizeEvent(QResizeEvent *event) override
QPixmap m_Background
Definition WaveDisplay.h:68
std::shared_ptr< H2Core::InstrumentLayer > m_pLayer
Definition WaveDisplay.h:78
virtual void paintEvent(QPaintEvent *ev) override
constexpr int getPointSize(H2Core::FontTheme::FontSize fontSize) const
#define UNUSED(v)
Definition Globals.h:42