-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathavtk_waveform.h
291 lines (239 loc) · 7.99 KB
/
avtk_waveform.h
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
/*
* Author: Harry van Haaren 2013
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#ifndef AVTK_WAVEFORM_H
#define AVTK_WAVEFORM_H
#include <FL/Fl_Widget.H>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
namespace Avtk
{
class Waveform : public Fl_Widget
{
public:
Waveform(int _x, int _y, int _w, int _h, const char *_label=0 ):
Fl_Widget(_x, _y, _w, _h, _label)
{
x = _x;
y = _y;
w = _w;
h = _h;
label = _label;
highlight = false;
newWaveform = false;
waveformCr = 0;
waveformSurf = 0;
realLength = 0;
data = 0;
newWaveform = true;
}
bool highlight;
int x, y, w, h;
const char* label;
cairo_t* waveformCr;
cairo_surface_t* waveformSurf;
bool newWaveform;
string waveformName;
int data_size;
long realLength;
float* data;
void setData( int numDataPoints, long realLen, float* d, std::string name )
{
// title of the sample
waveformName = name;
// the number of samples in the original file
realLength = realLen;
// number of pixel points, and the data*
data_size = numDataPoints;
data = d;
newWaveform = true;
redraw();
}
void draw()
{
if (damage() & FL_DAMAGE_ALL)
{
cairo_t *cr = Fl::cairo_cc();
//Cairo::Context cr = Cairo::Context( c_cr );
//Cairo::Context cairoContext = Context( c_cr, false );
//Cairo::Context* cr = &cairoContext;
cairo_save(cr);
// clear the surface
cairo_rectangle(cr, x, y, w, h);
cairo_set_source_rgb (cr, 0.1,0.1,0.1);
cairo_fill( cr );
//cout << "waveform have data" << endl;
if ( newWaveform )
{
if ( !waveformCr )
{
// create the waveform surface and context
//cout << "waveform draw() creating new objects" << endl;
waveformSurf= cairo_image_surface_create ( CAIRO_FORMAT_ARGB32, w, h);
waveformCr = cairo_create ( waveformSurf );
//cout << "waveform draw() creating objects done" << endl;
}
// clear the surface
cairo_rectangle(waveformCr, 0, 0, w, h);
cairo_set_source_rgb (waveformCr, 0.1,0.1,0.1);
cairo_fill( waveformCr );
// set up dashed lines, 1 px off, 1 px on
double dashes[1];
dashes[0] = 2.0;
cairo_set_dash ( waveformCr, dashes, 1, 0.0);
cairo_set_line_width( waveformCr, 1.0);
// loop over each 2nd line, drawing dots
cairo_set_line_width(waveformCr, 1.0);
cairo_set_source_rgb(waveformCr, 0.4,0.4,0.4);
for ( int i = 1; i < 4; i++ )
{
cairo_move_to( waveformCr, ((w / 4.f)*i), 0);
cairo_line_to( waveformCr, ((w / 4.f)*i), h );
}
for ( int i = 1; i < 4; i++ )
{
cairo_move_to( waveformCr, 0, ((h / 4.f)*i) );
cairo_line_to( waveformCr, w, ((h / 4.f)*i) );
}
cairo_set_source_rgba( waveformCr, 66 / 255.f, 66 / 255.f , 66 / 255.f , 0.5 );
cairo_stroke(waveformCr);
cairo_set_dash ( waveformCr, dashes, 0, 0.0);
if ( !data )
{
// draw X
cairo_move_to( waveformCr, 0, 0 );
cairo_line_to( waveformCr, w, h );
cairo_move_to( waveformCr, 0, h );
cairo_line_to( waveformCr, w, 0 );
cairo_set_source_rgba( waveformCr, 66 / 255.f, 66 / 255.f , 66 / 255.f , 0.5 );
cairo_stroke(waveformCr);
// draw text
cairo_move_to( waveformCr, (w/2.f) - 65, (h/2.f) + 10 );
cairo_set_source_rgb ( waveformCr, 0.6,0.6,0.6);
cairo_set_font_size( waveformCr, 20 );
cairo_show_text( waveformCr, "Load A Sample" );
}
else
{
// find how many samples per pixel
int samplesPerPix = data_size / w;
//cout << "width = " << w << " sampsPerPx " << samplesPerPix << endl;
// loop over each pixel value we need
for( int p = 0; p < w; p++ )
{
float average = 0.f;
// calc value for this pixel
for( int i = 0; i < samplesPerPix; i++ )
{
float tmp = data[i + (p * samplesPerPix)];
if ( tmp < 0 )
{
tmp = -tmp;
}
average += tmp;
}
average =(average / samplesPerPix);
cairo_move_to( waveformCr, p, (h/2) - (average * (h/2.2f) ) );
cairo_line_to( waveformCr, p, (h/2) + (average * (h/2.2f) ) );
}
// stroke the waveform
cairo_set_source_rgb( waveformCr, 0.8,0.8,0.8);
cairo_stroke( waveformCr );
/*
// draw text
cairo_move_to( waveformCr, 8, h-10 );
cairo_set_source_rgb ( waveformCr, 0.6,0.6,0.6);
cairo_set_font_size( waveformCr, 10 );
std::stringstream s;
s << "Length: " << realLength;
cairo_show_text( waveformCr, s.str().c_str() );
*/
// draw sample name
cairo_move_to( waveformCr, 8, h-10 );
cairo_set_source_rgb ( waveformCr, 0.6,0.6,0.6);
cairo_set_font_size( waveformCr, 10 );
std::stringstream s;
s << "Sample: " << waveformName;
cairo_show_text( waveformCr, s.str().c_str() );
}
newWaveform = false;
}
cairo_set_source_surface(cr, waveformSurf, x, y);
cairo_rectangle( cr, x, y, w, h);
cairo_paint(cr);
// stroke rim
cairo_set_line_width(cr, 0.9);
cairo_rectangle(cr, x, y, w, h);
cairo_set_source_rgba( cr, 126 / 255.f, 126 / 255.f , 126 / 255.f , 0.8 );
cairo_stroke( cr );
//cout << "waveform draw() done" << endl;
cairo_restore(cr);
}
}
void resize(int X, int Y, int W, int H)
{
Fl_Widget::resize(X,Y,W,H);
x = X;
y = Y;
w = W;
h = H;
redraw();
}
int handle(int event)
{
return 0;
switch(event)
{
case FL_PUSH:
highlight = 0;
redraw();
return 1;
case FL_DRAG: {
int t = Fl::event_inside(this);
if (t != highlight) {
redraw();
}
}
return 1;
case FL_RELEASE:
if (highlight) {
highlight = 0;
redraw();
do_callback();
}
return 1;
case FL_SHORTCUT:
if ( test_shortcut() )
{
do_callback();
return 1;
}
return 0;
default:
return Fl_Widget::handle(event);
}
}
};
} // Avtk
#endif