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 
23 #include <hydrogen/basics/sample.h>
24 #include <hydrogen/basics/song.h>
25 #include <hydrogen/basics/instrument.h>
26 #include "HydrogenApp.h"
27 #include "SampleEditor.h"
28 using namespace H2Core;
29 
30 #include "MainSampleWaveDisplay.h"
31 #include "../Skin.h"
32 
33 const char* MainSampleWaveDisplay::__class_name = "MainSampleWaveDisplay";
34 
MainSampleWaveDisplay(QWidget * pParent)35 MainSampleWaveDisplay::MainSampleWaveDisplay(QWidget* pParent)
36  : QWidget( pParent )
37  , Object( __class_name )
38 {
39 //	setAttribute(Qt::WA_NoBackground);
40 
41 	//INFOLOG( "INIT" );
42 	int w = 624;
43 	int h = 265;
44 	resize( w, h );
45 
46 	bool ok = m_background.load( Skin::getImagePath() + "/waveDisplay/mainsamplewavedisplay.png" );
47 	if( ok == false ){
48 		ERRORLOG( "Error loading pixmap" );
49 	}
50 
51 	m_pPeakDatal = new int[ w ];
52 	m_pPeakDatar = new int[ w ];
53 
54 	m_pStartFramePosition = 25;
55 	m_pLoopFramePosition = 25;
56 	m_pEndFramePosition = width() -25;
57 	m_pmove = false;
58 	m_plocator = -1;
59 	m_pupdateposi = false;
60 
61 	__startsliderismoved = false;
62 	__loopsliderismoved = false;
63 	__endsliderismoved = false;
64 }
65 
66 
67 
68 
~MainSampleWaveDisplay()69 MainSampleWaveDisplay::~MainSampleWaveDisplay()
70 {
71 	//INFOLOG( "DESTROY" );
72 
73 	delete[] m_pPeakDatal;
74 	delete[] m_pPeakDatar;
75 }
76 
paintLocatorEvent(int pos,bool updateposi)77 void MainSampleWaveDisplay::paintLocatorEvent( int pos, bool updateposi)
78 {
79 	m_pupdateposi = updateposi;
80 	if ( !updateposi ){
81 		m_plocator = -1;
82 	}else
83 	{
84 		m_plocator = pos;
85 	}
86 	update();
87 }
88 
paintEvent(QPaintEvent * ev)89 void MainSampleWaveDisplay::paintEvent(QPaintEvent *ev)
90 {
91 	QPainter painter( this );
92 	painter.setRenderHint( QPainter::HighQualityAntialiasing );
93 
94 	bool issmaller = false;
95 
96 	painter.drawPixmap( ev->rect(), m_background, ev->rect() );
97 	painter.setPen( QColor( 230, 230, 230 ) );
98 	int VCenterl = height() / 4;
99 	int VCenterr = height() / 4 + height() / 2;
100 
101 	if ( width() >= m_pSampleLength  ) issmaller = true;
102 
103 	for ( int x = 25; x < width() -25; x++ ) {
104 		if ( !issmaller || x <= m_pSampleLength){
105 			painter.drawLine( x, -m_pPeakDatal[x -25] +VCenterl, x, -m_pPeakDatal[x -24] +VCenterl  );
106 			painter.drawLine( x, -m_pPeakDatar[x -25] +VCenterr, x, -m_pPeakDatar[x -24] +VCenterr  );
107 		}else
108 		{
109 			painter.drawLine( x, 0 +VCenterl, x, 0 +VCenterl  );
110 			painter.drawLine( x, 0 +VCenterr, x, 0 +VCenterr  );
111 		}
112 
113 	}
114 
115 
116 	painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
117 
118 	painter.setPen( QPen( QColor( 255, 255, 255 ), 1, Qt::DotLine ) );
119 	painter.drawLine( 23, 4, 23, height() -4 );
120 	painter.drawLine( width() -23, 4,width() -23, height() -4 );
121 	painter.setPen( QPen( QColor( 255, 255, 255 ), 1, Qt::SolidLine ) );
122 	painter.drawLine( m_plocator, 4, m_plocator, height() -4);
123 	painter.drawLine( 0, VCenterl, width(),VCenterl );
124 	painter.drawLine( 0, VCenterr, width(),VCenterr );
125 
126 	QFont font;
127 	font.setWeight( 63 );
128 	painter.setFont( font );
129 //start frame pointer
130 	painter.setPen( QColor( 32, 173, 0, 200 ) );
131 	painter.drawLine( m_pStartFramePosition, 4, m_pStartFramePosition, height() -4 );
132 	painter.drawText( m_pStartFramePosition -10, 250, 10,20, Qt::AlignRight, "S" );
133 //endframe pointer
134 	painter.setPen( QColor( 217, 68, 0, 200 ) );
135 	painter.drawLine( m_pEndFramePosition, 4, m_pEndFramePosition, height() -4 );
136 	painter.drawText( m_pEndFramePosition -10, 123, 10, 20, Qt::AlignRight, "E" );
137 //loopframe pointer
138 	painter.setPen( QColor( 93, 170, 254, 200 ) );
139 	painter.drawLine( m_pLoopFramePosition, 4, m_pLoopFramePosition, height() -4 );
140 	painter.drawText( m_pLoopFramePosition , 0, 10, 20, Qt::AlignLeft, "L" );
141 
142 
143 }
144 
145 
146 
updateDisplayPointer()147 void MainSampleWaveDisplay::updateDisplayPointer()
148 {
149 	update();
150 }
151 
152 
153 
updateDisplay(const QString & filename)154 void MainSampleWaveDisplay::updateDisplay( const QString& filename )
155 {
156 
157 	Sample *pNewSample = Sample::load( filename );
158 
159 	if ( pNewSample ) {
160 
161 		int nSampleLength = pNewSample->get_frames();
162 		m_pSampleLength = nSampleLength;
163 		float nScaleFactor = nSampleLength / (width() -50);
164 		if ( nScaleFactor < 1 ){
165 			nScaleFactor = 1;
166 		}
167 
168 		float fGain = height() / 4.0 * 1.0;
169 
170 		float *pSampleDatal = pNewSample->get_data_l();
171 		float *pSampleDatar = pNewSample->get_data_r();
172 
173 		unsigned nSamplePos = 0;
174 		int nVall = 0;
175 		int nValr = 0;
176 		int newVall = 0;
177 		int newValr = 0;
178 		for ( int i = 0; i < width(); ++i ){
179 			for ( int j = 0; j < nScaleFactor; ++j ) {
180 				if ( j < nSampleLength && nSamplePos < nSampleLength) {
181 					if ( pSampleDatal[ nSamplePos ] && pSampleDatar[ nSamplePos ] ){
182 						newVall = static_cast<int>( pSampleDatal[ nSamplePos ] * fGain );
183 						newValr = static_cast<int>( pSampleDatar[ nSamplePos ] * fGain );
184 						nVall = newVall;
185 						nValr = newValr;
186 					}else
187 					{
188 						nVall = 0;
189 						nValr = 0;
190 					}
191 				}
192 				++nSamplePos;
193 			}
194 			m_pPeakDatal[ i ] = nVall;
195 			m_pPeakDatar[ i ] = nValr;
196 		}
197 	}
198 	delete pNewSample;
199 	pNewSample = nullptr;
200 	update();
201 
202 }
203 
mouseMoveEvent(QMouseEvent * ev)204 void MainSampleWaveDisplay::mouseMoveEvent(QMouseEvent *ev)
205 {
206 	testPosition( ev );
207 	update();
208 }
209 
mousePressEvent(QMouseEvent * ev)210 void MainSampleWaveDisplay::mousePressEvent(QMouseEvent *ev)
211 {
212 	testPosition( ev );
213 	update();
214 }
215 
testPosition(QMouseEvent * ev)216 void MainSampleWaveDisplay::testPosition( QMouseEvent *ev )
217 {
218 	assert(ev);
219 
220 //startframepointer
221 	if  (ev->y()>=200 ) {
222 		m_pStartFramePosition = ev->x() ;
223 		__startsliderismoved = true;
224 		if ( m_pStartFramePosition > m_pLoopFramePosition ){
225 			m_pLoopFramePosition = m_pStartFramePosition;
226 			__loopsliderismoved = true;
227 		}
228 		if ( m_pStartFramePosition > m_pEndFramePosition ){
229 			m_pEndFramePosition = m_pStartFramePosition;
230 			__endsliderismoved = true;
231 		}
232 //		update();
233 	}
234 
235 //loopframeposition
236 	else if  (ev->y()<=65 ) {
237 		m_pLoopFramePosition = ev->x() ;
238 		__loopsliderismoved = true;
239 		if ( m_pLoopFramePosition < m_pStartFramePosition ){
240 			m_pStartFramePosition = m_pLoopFramePosition;
241 			__startsliderismoved = true;
242 		}
243 		if ( m_pLoopFramePosition > m_pEndFramePosition ){
244 			m_pEndFramePosition = m_pLoopFramePosition;
245 			__endsliderismoved = true;
246 		}
247 //		update();
248 	}
249 //endframeposition
250 	else if  ( ev->y() >= 86 && ev->y() <= 179  ) {
251 		m_pEndFramePosition = ev->x() ;
252 		__endsliderismoved = true;
253 		if ( m_pEndFramePosition <  m_pLoopFramePosition ){
254 			m_pLoopFramePosition = m_pEndFramePosition;
255 			__loopsliderismoved = true;
256 		}
257 		if ( m_pEndFramePosition <  m_pStartFramePosition ){
258 			m_pStartFramePosition = m_pEndFramePosition;
259 			__startsliderismoved = true;
260 		}
261 //		update();
262 	}
263 
264 	if ( ( m_pStartFramePosition ) >= width() -25 ) m_pStartFramePosition =width() -25;
265 	if ( ( m_pLoopFramePosition ) >= width() -25 ) m_pLoopFramePosition =width() -25;
266 	if ( ( m_pEndFramePosition ) >= width() -25 ) m_pEndFramePosition =width() -25;
267 	if ( ( m_pStartFramePosition ) <= 25 ) m_pStartFramePosition = 25;
268 	if ( ( m_pLoopFramePosition ) <= 25 ) m_pLoopFramePosition = 25;
269 	if ( ( m_pEndFramePosition ) <= 25 ) m_pEndFramePosition = 25;
270 }
271 
272 
mouseReleaseEvent(QMouseEvent * ev)273 void MainSampleWaveDisplay::mouseReleaseEvent(QMouseEvent *ev)
274 {
275 	update();
276 	bool test = HydrogenApp::get_instance()->getSampleEditor()->returnAllMainWaveDisplayValues();
277 
278 	if (test){
279 		__startsliderismoved = false;
280 		__loopsliderismoved = false;
281 		__endsliderismoved = false;
282 	}
283 }
284 
285 
286 
287