1 /* === S Y N F I G ========================================================= */
2 /*! \file widget_sound.cpp
3 ** \brief Widget Sound Implementation File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/adjustment.h>
34 #include <synfig/general.h>
37 #include "widget_sound.h"
38 #include "audiocontainer.h"
42 /* === U S I N G =========================================================== */
46 //using namespace synfig;
48 using studio::AudioProfile;
50 /* === M A C R O S ========================================================= */
52 /* === G L O B A L S ======================================================= */
54 /* === P R O C E D U R E S ================================================= */
56 /* === M E T H O D S ======================================================= */
58 /* === E N T R Y P O I N T ================================================= */
60 studio::Widget_Sound::Widget_Sound()
64 studio::Widget_Sound::~Widget_Sound()
68 void studio::Widget_Sound::set_position(double t)
70 //synfig::info("Setting position to %.2lf s", t);
71 if(adj_timescale && t != adj_timescale->get_value())
73 float upper = adj_timescale->get_upper();
74 float lower = adj_timescale->get_lower();
75 float framesize = upper - lower;
79 lower -= ceil((lower-t)/framesize)*framesize;
80 upper = lower + framesize;
81 adj_timescale->set_lower(lower); adj_timescale->set_upper(upper);
82 adj_timescale->set_value(t);
83 adj_timescale->changed(); adj_timescale->value_changed();
87 lower += ceil((t-upper)/framesize)*framesize;
88 upper = lower + framesize;
89 adj_timescale->set_lower(lower); adj_timescale->set_upper(upper);
90 adj_timescale->set_value(t);
91 adj_timescale->changed(); adj_timescale->value_changed();
94 adj_timescale->set_value(t);
95 adj_timescale->value_changed();
100 double studio::Widget_Sound::get_position() const
104 return adj_timescale->get_value();
109 bool studio::Widget_Sound::set_profile(etl::handle<AudioProfile> p)
125 etl::handle<AudioProfile> studio::Widget_Sound::get_profile() const
130 void studio::Widget_Sound::clear()
135 void studio::Widget_Sound::draw()
140 bool studio::Widget_Sound::on_expose_event(GdkEventExpose *heh)
142 if(!get_window()) return false;
144 //clear the background to dark grey
145 Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(get_window());
147 if(!gc) return false;
150 Gdk::Rectangle r(0,0,get_width(),get_height());
151 get_window()->begin_paint_rect(r);
153 Gdk::Color c("#3f3f3f");
154 gc->set_rgb_fg_color(c);
155 gc->set_background(c);
158 int baseline = get_height()/2;
159 get_window()->draw_rectangle(gc,true,0,0,w,get_height());
161 //set up the color to be blue
162 c.set_rgb_p(0,0.5,1);
163 gc->set_rgb_fg_color(c);
166 get_window()->draw_line(gc,0,baseline,w,baseline);
168 //redraw all the samples from begin to end, but only if we have samples to draw (or there is no space to draw)
170 //synfig::warning("Ok rendered everything, now must render actual sound wave");
171 if(!audioprof || !adj_timescale || !w)
173 get_window()->end_paint();
178 float framesize = adj_timescale->get_upper() - adj_timescale->get_lower();
183 //position in sample space
185 int cur=0,maxs=0,mins=0;
187 int i=0; //pixel counter
189 //etl::clock check; check.reset();
191 float position = adj_timescale->get_value();
192 float samplerate = audioprof->get_samplerate();
194 //enforce position inside of frame size
196 float offset = audioprof->get_offset();
198 //clamp begin and end to framesize
199 float beginf = adj_timescale->get_lower();
200 float endf = adj_timescale->get_upper();
202 posi = round_to_int((position-beginf)*w/framesize);
203 //posi = (int)((position-beginf)*w/framesize);
205 //calculate in sample space from seconds
206 begin = round_to_int((beginf - offset)*samplerate);
207 end = round_to_int((endf - offset)*samplerate);
208 //begin = (int)((beginf - offset)*samplerate);
209 //end = (int)((endf - offset)*samplerate);
212 delta = (end - begin)/(float)w; //samples per pixel
214 /*synfig::warning("Rendering a framesize of %f secs from [%d,%d) samples to %d samples, took %f sec",
215 framesize, begin, end, w, check());*/
221 //get the maximum of the collected samples
224 for(;cum < delta; ++cum, ++cur)
226 maxs = std::max(maxs,(int)(*audioprof)[cur]);
227 mins = std::min(mins,(int)(*audioprof)[cur]);
231 //draw spike if not needed be
234 int top = maxs * baseline / 64;
235 int bot = mins * baseline / 64;
237 get_window()->draw_line(gc,i,baseline+bot,i,baseline+top);
241 //synfig::warning("Drawing audio line");
243 gc->set_rgb_fg_color(c);
244 get_window()->draw_line(gc,posi,0,posi,get_height());
246 get_window()->end_paint();
251 //--- Handle the single clicking and dragging for scrubbing
253 bool studio::Widget_Sound::on_motion_notify_event(GdkEventMotion* event)
255 Gdk::ModifierType mod = Gdk::ModifierType(event->state);
257 //if we are scrubbing
258 if(mod & Gdk::BUTTON1_MASK)
260 //Can't do this if we don't have a time frame (heheh...)
261 if(!adj_timescale) return false;
263 double beg = adj_timescale->get_lower(), end = adj_timescale->get_upper();
265 //find event position in time
266 double t = beg + event->x * (end-beg) / get_width();
268 //signal that we are scrubbing to this new value...
272 // We should be able to just call
273 // Widget_Timeslider::on_motion_notify_event(),
274 // but that seems to cause the program to halt
275 // for some reason. So for now, let's do the job ourselves
276 //adj_timescale->set_value(t);
277 //adj_timescale->changed();
281 return Widget_Timeslider::on_motion_notify_event(event);
284 bool studio::Widget_Sound::on_button_press_event(GdkEventButton *event)
286 //Assume button PRESS
288 //if we are starting... using left click
289 if(event->button == 1)
291 if(!adj_timescale) return false;
293 double beg = adj_timescale->get_lower(), end = adj_timescale->get_upper();
295 //find event position in time
296 double t = beg + event->x * (end-beg) / get_width();
298 //signal the attached scrubbing devices...
299 signal_start_scrubbing()(t);
304 return Widget_Timeslider::on_button_press_event(event);
307 bool studio::Widget_Sound::on_button_release_event(GdkEventButton *event)
309 //Assume button RELEASE
311 //if we are ending... using left click
312 if(event->button == 1)
314 //signal the scrubbing device... to stop
315 signal_stop_scrubbing()();
320 return Widget_Timeslider::on_button_release_event(event);