summaryrefslogtreecommitdiff
path: root/src/canvas.cc
blob: f2956fc0f78af8a1e32469b0560088e88e990a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            canvas.cc
 *
 *  Tue Nov 10 08:37:37 CET 2009
 *  Copyright 2009 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "canvas.h"

#include <sndfile.h>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QKeyEvent>

#include <math.h>

#define DEFYSCALE 200

Canvas::Canvas(QWidget *parent)
	: QWidget(parent)
{
	setAttribute(Qt::WA_StaticContents);
	setMouseTracking(true);
	setFocusPolicy(Qt::ClickFocus);

	mipmap = NULL;

	data = NULL;
	size = 0;

	xscale = 1.0;
	yscale = 1.0;
	xoffset = 0.0;
	yoffset = 0.5;

	colBg = QColor(180, 200, 180);
	colSec = QColor(160, 180, 160);
	colWavMax = QColor(100, 100, 100);
	colWavAvg = QColor(0, 0, 0);
	colMax = QColor(127, 127, 255);
	colHalf = QColor(180, 180, 255);

	setCursor(Qt::ArrowCursor);

	wav = QImage(width(), height(), QImage::Format_RGB32);
}

Canvas::~Canvas()
{
	if(data)
	{
		delete[] data;
	}
	if(mipmap)
	{
		delete mipmap;
	}
}

void Canvas::load(QString file)
{
	if(data)
	{
		delete[] data;
		data = NULL;
		size = 0;
	}

	if(mipmap)
	{
		delete mipmap;
		mipmap = NULL;
	}

	SF_INFO sf_info;
	SNDFILE *fh = sf_open(file.toStdString().c_str(), SFM_READ, &sf_info);
	if(!fh)
	{
		printf("Load error...\n");
		return;
	}

	size = sf_info.frames;

	data = new float[size];

	sf_read_float(fh, data, size);

	sf_close(fh);

	mipmap = new MipMap(data, size);

	updateWav();
	update();
}


#define SCALEX ((xscale * (float)size/(float)width()) + 0.1)
#define OFFSETX (xoffset * (float)size)
float Canvas::mapX(float x)
{
	float val = (x - OFFSETX) / SCALEX;
	return val;
}

float Canvas::unmapX(float x)
{
	float val = x * SCALEX + OFFSETX;
	return val;
}

#define SCALEY (yscale * height())
#define OFFSETY (((float)height() / 2.0) + ((yoffset * 2.0 - 1.0) * SCALEY))
float Canvas::mapY(float y)
{
	float val = OFFSETY + (y * SCALEY);
	return val;
}

float Canvas::unmapY(float y)
{
	float val = (y - OFFSETY) / SCALEY;
	return val;
}

void Canvas::mouseMoveEvent(QMouseEvent *event)
{
	for(int i = 0; i < tools.size(); i++)
	{
		if(tools[i]->mouseMoveEvent(event)) return;
	}

	setCursor(Qt::ArrowCursor);
}

void Canvas::mousePressEvent(QMouseEvent *event)
{
	for(int i = 0; i < tools.size(); i++)
	{
		if(tools[i]->mousePressEvent(event)) return;
	}
}

void Canvas::mouseReleaseEvent(QMouseEvent *event)
{
	for(int i = 0; i < tools.size(); i++)
	{
		if(tools[i]->mouseReleaseEvent(event)) return;
	}
}

void Canvas::resizeEvent(QResizeEvent *event)
{
	for(int i = 0; i < tools.size(); i++)
	{
		tools[i]->resizeEvent(event);
	}

	wav = QImage(width(), height(), QImage::Format_RGB32);
	updateWav();
	update();
}

void Canvas::getWavValues(int last, int lx, float *vu, float *vl,
                          float *avgu, float *avgl)
{
	if(mipmap == NULL) return;

	MipMapValue val = mipmap->lookup(last, lx);

	*vu = val.max;
	*vl = val.min;
	*avgu = val.uavg;
	*avgl = val.lavg;
}

void Canvas::updateWav()
{
	QPainter painter(&wav);

	painter.setPen(colBg);
	painter.setBrush(colBg);
	painter.drawRect(0, 0, wav.width(), wav.height());

	painter.setPen(colSec);
	int step = 44100;
	for(size_t i = 0; i < size; i += step)
	{
		painter.drawLine(mapX(i), mapY(1.0), mapX(i), mapY(-1.0));
	}

	painter.setPen(colMax);
	painter.drawLine(0, mapY(1.0), wav.width(), mapY(1.0));
	painter.drawLine(0, mapY(-1.0), wav.width(), mapY(-1.0));

	painter.setPen(colHalf);
	painter.drawLine(0, mapY(0.5), wav.width(), mapY(0.5));
	painter.drawLine(0, mapY(-0.5), wav.width(), mapY(-0.5));

	if(data)
	{
		int last = unmapX(0);
		for(int x = 0; x < wav.width(); x++)
		{
			int lx = unmapX(x);
			if(lx > (int)size || lx < 0) break;
			float vu = 0;
			float vl = 0;
			float avgu = 0;
			float avgl = 0;

			getWavValues(last, lx, &vu, &vl, &avgu, &avgl);

			int c = mapY(0.0);

			painter.setPen(colWavMax);
			painter.drawLine(x, c, x, mapY(vu));
			painter.drawLine(x, c, x, mapY(vl));

			painter.setPen(colWavAvg);
			painter.drawLine(x, c, x, mapY(avgu));
			painter.drawLine(x, c, x, mapY(avgl));

			last = lx;
		}
	}
}

void Canvas::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);

	painter.drawImage(event->rect(),wav,event->rect());

	for(int i = 0; i < tools.size(); i++)
	{
		tools[i]->paintEvent(event, painter);
	}
}

void Canvas::keyReleaseEvent(QKeyEvent *event)
{
	for(int i = 0; i < tools.size(); i++)
	{
		tools[i]->keyReleaseEvent(event);
	}
}

void Canvas::setXScale(float scale)
{
	scale = (pow(100.0,scale) / 100.0) - (pow(100.0, 0.0)/ 100.0);
	if(scale < 0.0) scale = 0.0;
	if(scale > 1.0) scale = 1.0;
	xscale = scale;
	updateWav();
	update();
}

void Canvas::setYScale(float scale)
{
	yscale = scale;
	updateWav();
	update();
}

void Canvas::setXOffset(float offset)
{
	if(offset < 0.0) offset = 0.0;
	if(offset > 1.0) offset = 1.0;
	xoffset = offset;
	updateWav();
	update();
}

void Canvas::setYOffset(float offset)
{
	if(offset < 0.0) offset = 0.0;
	if(offset > 1.0) offset = 1.0;
	yoffset = offset;
	updateWav();
	update();
}