2 Copyright (C) 2001-2007 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <gtkmm2ext/gtk_ui.h>
27 #include "ardour/midi_diskstream.h"
28 #include "ardour/midi_playlist.h"
29 #include "ardour/midi_region.h"
30 #include "ardour/midi_source.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/region_factory.h"
33 #include "ardour/smf_source.h"
34 #include "ardour/session.h"
36 #include "ardour_ui.h"
37 #include "canvas-simplerect.h"
38 #include "gui_thread.h"
40 #include "midi_region_view.h"
41 #include "midi_streamview.h"
42 #include "midi_time_axis.h"
43 #include "midi_util.h"
44 #include "public_editor.h"
45 #include "region_selection.h"
46 #include "region_view.h"
47 #include "rgb_macros.h"
48 #include "selection.h"
49 #include "simplerect.h"
53 using namespace ARDOUR
;
55 using namespace Editing
;
57 MidiStreamView::MidiStreamView (MidiTimeAxisView
& tv
)
59 , note_range_adjustment(0.0f
, 0.0f
, 0.0f
)
61 , _range_sum_cache(-1.0)
68 /* use a group dedicated to MIDI underlays. Audio underlays are not in this group. */
69 midi_underlay_group
= new ArdourCanvas::Group (*_canvas_group
);
70 midi_underlay_group
->lower_to_bottom();
72 /* put the note lines in the timeaxisview's group, so it
73 can be put below ghost regions from MIDI underlays*/
74 _note_lines
= new ArdourCanvas::LineSet(*_canvas_group
, ArdourCanvas::LineSet::Horizontal
);
76 _note_lines
->property_x1() = 0;
77 _note_lines
->property_y1() = 0;
78 _note_lines
->property_x2() = trackview().editor().frame_to_pixel (max_frames
);
79 _note_lines
->property_y2() = 0;
81 _note_lines
->signal_event().connect(sigc::bind(
82 sigc::mem_fun(_trackview
.editor(), &PublicEditor::canvas_stream_view_event
),
83 _note_lines
, &_trackview
));
85 _note_lines
->lower_to_bottom();
89 ColorsChanged
.connect(sigc::mem_fun(*this, &MidiStreamView::color_handler
));
91 note_range_adjustment
.set_page_size(_highest_note
- _lowest_note
);
92 note_range_adjustment
.set_value(_lowest_note
);
94 note_range_adjustment
.signal_value_changed().connect(
95 sigc::mem_fun(*this, &MidiStreamView::note_range_adjustment_changed
));
98 MidiStreamView::~MidiStreamView ()
103 veto_note_range(uint8_t& min
, uint8_t& max
)
105 /* Legal notes, thanks */
109 /* Always display at least one octave in [0, 127] */
111 if (min
> (127 - 11)) {
114 } else if (max
< min
+ 11) {
115 uint8_t d
= 11 - (max
- min
);
116 if (max
+ d
/2 > 127) {
123 assert(max
- min
>= 11);
129 MidiStreamView::create_region_view (boost::shared_ptr
<Region
> r
, bool /*wfd*/, bool)
131 boost::shared_ptr
<MidiRegion
> region
= boost::dynamic_pointer_cast
<MidiRegion
> (r
);
137 RegionView
* region_view
= new MidiRegionView (_canvas_group
, _trackview
, region
,
138 _samples_per_unit
, region_color
);
140 region_view
->init (region_color
, false);
146 MidiStreamView::add_region_view_internal (boost::shared_ptr
<Region
> r
, bool wfd
, bool recording
)
148 boost::shared_ptr
<MidiRegion
> region
= boost::dynamic_pointer_cast
<MidiRegion
> (r
);
154 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
155 if ((*i
)->region() == r
) {
157 /* great. we already have a MidiRegionView for this Region. use it again. */
159 (*i
)->set_valid (true);
161 display_region(dynamic_cast<MidiRegionView
*>(*i
), wfd
);
167 MidiRegionView
* region_view
= dynamic_cast<MidiRegionView
*> (create_region_view (r
, wfd
, recording
));
168 if (region_view
== 0) {
172 region_views
.push_front (region_view
);
174 /* display events and find note range */
175 display_region (region_view
, wfd
);
177 /* catch regionview going away */
178 region
->DropReferences
.connect (*this, invalidator (*this), boost::bind (&MidiStreamView::remove_region_view
, this, region
), gui_context());
180 RegionViewAdded (region_view
);
186 MidiStreamView::display_region(MidiRegionView
* region_view
, bool load_model
)
191 region_view
->enable_display(true);
193 boost::shared_ptr
<MidiSource
> source(region_view
->midi_region()->midi_source(0));
196 source
->load_model();
199 _range_dirty
= update_data_note_range(
200 source
->model()->lowest_note(),
201 source
->model()->highest_note());
203 // Display region contents
204 region_view
->set_height(height
);
205 region_view
->display_model(source
->model());
209 MidiStreamView::display_track (boost::shared_ptr
<Track
> tr
)
211 StreamView::display_track (tr
);
219 MidiStreamView::update_contents_metrics(boost::shared_ptr
<Region
> r
)
221 boost::shared_ptr
<MidiRegion
> mr
= boost::dynamic_pointer_cast
<MidiRegion
>(r
);
223 mr
->midi_source(0)->load_model();
224 _range_dirty
= update_data_note_range(
225 mr
->model()->lowest_note(),
226 mr
->model()->highest_note());
231 MidiStreamView::update_data_note_range(uint8_t min
, uint8_t max
)
234 if (min
< _data_note_min
) {
235 _data_note_min
= min
;
238 if (max
> _data_note_max
) {
239 _data_note_max
= max
;
246 MidiStreamView::redisplay_track ()
248 if (!_trackview
.is_midi_track()) {
252 list
<RegionView
*>::iterator i
;
254 // Load models if necessary, and find note range of all our contents
255 _range_dirty
= false;
256 _data_note_min
= 127;
258 _trackview
.track()->playlist()->foreach_region(
259 sigc::mem_fun (*this, &StreamView::update_contents_metrics
)
262 // No notes, use default range
268 // Extend visible range to show newly recorded data, if necessary
269 _lowest_note
= std::min(_lowest_note
, _data_note_min
);
270 _highest_note
= std::max(_highest_note
, _data_note_max
);
272 veto_note_range(_lowest_note
, _highest_note
);
274 // Flag region views as invalid and disable drawing
275 for (i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
276 (*i
)->set_valid(false);
277 (*i
)->enable_display(false);
280 // Add and display region views, and flag them as valid
281 _trackview
.track()->playlist()->foreach_region(
282 sigc::hide_return (sigc::mem_fun (*this, &StreamView::add_region_view
))
285 // Stack regions by layer, and remove invalid regions
288 // Update note range (not regions which are correct) and draw note lines
289 apply_note_range(_lowest_note
, _highest_note
, false);
294 MidiStreamView::update_contents_height ()
296 StreamView::update_contents_height();
297 _note_lines
->property_y2() = height
;
299 apply_note_range (lowest_note(), highest_note(), true);
303 MidiStreamView::draw_note_lines()
310 double prev_y
= contents_height();
313 _note_lines
->clear();
319 for (int i
= lowest_note(); i
<= highest_note(); ++i
) {
320 y
= floor(note_to_y(i
));
322 _note_lines
->add_line(prev_y
, 1.0, ARDOUR_UI::config()->canvasvar_PianoRollBlackOutline
.get());
330 color
= ARDOUR_UI::config()->canvasvar_PianoRollBlack
.get();
333 color
= ARDOUR_UI::config()->canvasvar_PianoRollWhite
.get();
337 if (i
== highest_note()) {
338 _note_lines
->add_line(y
, prev_y
- y
, color
);
340 _note_lines
->add_line(y
+ 1.0, prev_y
- y
- 1.0, color
);
348 MidiStreamView::set_note_range(VisibleNoteRange r
)
350 if (r
== FullRange
) {
354 _lowest_note
= _data_note_min
;
355 _highest_note
= _data_note_max
;
358 apply_note_range(_lowest_note
, _highest_note
, true);
362 MidiStreamView::apply_note_range(uint8_t lowest
, uint8_t highest
, bool to_region_views
)
364 _highest_note
= highest
;
365 _lowest_note
= lowest
;
367 int range
= _highest_note
- _lowest_note
;
368 int pixels_per_note
= floor (height
/range
);
370 /* do not grow note height beyond 10 pixels */
371 if (pixels_per_note
> 10) {
373 int available_note_range
= floor ((height
)/10);
374 int additional_notes
= available_note_range
- range
;
376 /* distribute additional notes to higher and lower ranges, clamp at 0 and 127 */
377 for (int i
= 0; i
< additional_notes
; i
++){
379 if (i
% 2 && _highest_note
< 127){
385 else if (_lowest_note
> 0){
394 note_range_adjustment
.set_page_size(_highest_note
- _lowest_note
);
395 note_range_adjustment
.set_value(_lowest_note
);
399 if (to_region_views
) {
400 for (list
<RegionView
*>::iterator i
= region_views
.begin(); i
!= region_views
.end(); ++i
) {
401 ((MidiRegionView
*)(*i
))->apply_note_range(_lowest_note
, _highest_note
);
409 MidiStreamView::update_note_range(uint8_t note_num
)
411 assert(note_num
<= 127);
412 _data_note_min
= min(_data_note_min
, note_num
);
413 _data_note_max
= max(_data_note_max
, note_num
);
417 MidiStreamView::setup_rec_box ()
419 // cerr << _trackview.name() << " streamview SRB\n";
421 if (_trackview
.session()->transport_rolling()) {
424 _trackview
.session()->record_status() == Session::Recording
&&
425 _trackview
.track()->record_enabled()) {
427 if (Config
->get_show_waveforms_while_recording() && rec_regions
.size() == rec_rects
.size()) {
429 /* add a new region, but don't bother if they set show-waveforms-while-recording mid-record */
431 MidiRegion::SourceList sources
;
433 rec_data_ready_connections
.drop_connections ();
435 sources
.push_back (_trackview
.midi_track()->write_source());
437 _trackview
.midi_track()->write_source()->ViewDataRangeReady
.connect
438 (rec_data_ready_connections
,
440 ui_bind (&MidiStreamView::rec_data_range_ready
, this, _1
, _2
, boost::weak_ptr
<Source
>(_trackview
.midi_track()->write_source())),
446 if (rec_regions
.size() > 0) {
447 start
= rec_regions
.back().first
->start()
448 + _trackview
.track()->get_captured_frames(rec_regions
.size()-1);
453 plist
.add (ARDOUR::Properties::start
, start
);
454 plist
.add (ARDOUR::Properties::length
, 1);
455 plist
.add (ARDOUR::Properties::name
, string());
456 plist
.add (ARDOUR::Properties::layer
, 0);
458 boost::shared_ptr
<MidiRegion
> region (boost::dynamic_pointer_cast
<MidiRegion
>
459 (RegionFactory::create (sources
, plist
, false)));
462 region
->block_property_changes ();
463 region
->set_position (_trackview
.session()->transport_frame(), this);
464 rec_regions
.push_back (make_pair(region
, (RegionView
*)0));
466 // rec regions are destroyed in setup_rec_box
468 /* we add the region later */
471 /* start a new rec box */
473 boost::shared_ptr
<MidiTrack
> mt
= _trackview
.midi_track(); /* we know what it is already */
474 nframes_t frame_pos
= mt
->current_capture_start ();
475 gdouble xstart
= _trackview
.editor().frame_to_pixel (frame_pos
);
479 assert(_trackview
.midi_track()->mode() == Normal
);
482 fill_color
= ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
484 ArdourCanvas::SimpleRect
* rec_rect
= new Gnome::Canvas::SimpleRect (*_canvas_group
);
485 rec_rect
->property_x1() = xstart
;
486 rec_rect
->property_y1() = 1.0;
487 rec_rect
->property_x2() = xend
;
488 rec_rect
->property_y2() = (double) _trackview
.current_height() - 1;
489 rec_rect
->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_RecordingRect
.get();
490 rec_rect
->property_fill_color_rgba() = fill_color
;
491 rec_rect
->lower_to_bottom();
494 recbox
.rectangle
= rec_rect
;
495 recbox
.start
= _trackview
.session()->transport_frame();
498 rec_rects
.push_back (recbox
);
500 screen_update_connection
.disconnect();
501 screen_update_connection
= ARDOUR_UI::instance()->SuperRapidScreenUpdate
.connect (
502 sigc::mem_fun (*this, &MidiStreamView::update_rec_box
));
506 } else if (rec_active
&&
507 (_trackview
.session()->record_status() != Session::Recording
||
508 !_trackview
.track()->record_enabled())) {
509 screen_update_connection
.disconnect();
511 rec_updating
= false;
516 // cerr << "\tNOT rolling, rec_rects = " << rec_rects.size() << " rec_regions = " << rec_regions.size() << endl;
518 if (!rec_rects
.empty() || !rec_regions
.empty()) {
520 /* disconnect rapid update */
521 screen_update_connection
.disconnect();
522 rec_data_ready_connections
.drop_connections ();
524 rec_updating
= false;
527 /* remove temp regions */
529 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin(); iter
!= rec_regions
.end();) {
530 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
;
535 (*iter
).first
->drop_references ();
542 // cerr << "\tclear " << rec_rects.size() << " rec rects\n";
544 /* transport stopped, clear boxes */
545 for (vector
<RecBoxInfo
>::iterator iter
=rec_rects
.begin(); iter
!= rec_rects
.end(); ++iter
) {
546 RecBoxInfo
&rect
= (*iter
);
547 delete rect
.rectangle
;
557 MidiStreamView::update_rec_regions (boost::shared_ptr
<MidiModel
> data
, nframes_t start
, nframes_t dur
)
559 ENSURE_GUI_THREAD (*this, &MidiStreamView::update_rec_regions
, data
, start
, dur
)
561 if (Config
->get_show_waveforms_while_recording ()) {
564 bool update_range
= false;
566 for (list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator iter
= rec_regions
.begin();
567 iter
!= rec_regions
.end(); n
++) {
569 list
<pair
<boost::shared_ptr
<Region
>,RegionView
*> >::iterator tmp
= iter
;
572 if (!canvas_item_visible (rec_rects
[n
].rectangle
)) {
573 /* rect already hidden, this region is done */
578 boost::shared_ptr
<MidiRegion
> region
= boost::dynamic_pointer_cast
<MidiRegion
>(iter
->first
);
584 nframes_t origlen
= region
->length();
586 if (region
== rec_regions
.back().first
&& rec_active
) {
588 if (start
>= region
->midi_source(0)->timeline_position()) {
590 nframes_t nlen
= start
+ dur
- region
->position();
592 if (nlen
!= region
->length()) {
594 region
->suspend_property_changes ();
595 region
->set_position (_trackview
.track()->get_capture_start_frame(n
), this);
596 region
->set_length (start
+ dur
- region
->position(), this);
597 region
->resume_property_changes ();
600 /* our special initial length */
601 iter
->second
= add_region_view_internal (region
, false);
602 ((MidiRegionView
*)iter
->second
)->begin_write();
605 /* also update rect */
606 ArdourCanvas::SimpleRect
* rect
= rec_rects
[n
].rectangle
;
607 gdouble xend
= _trackview
.editor().frame_to_pixel (region
->position() + region
->length());
608 rect
->property_x2() = xend
;
610 ARDOUR::BeatsFramesConverter
tconv(_trackview
.session()->tempo_map(), region
->position());
611 const MidiModel::TimeType start_beats
= tconv
.from(start
);
614 MidiRegionView
* mrv
= (MidiRegionView
*)iter
->second
;
616 for (MidiModel::Notes::const_iterator i
= data
->note_lower_bound(start_beats
);
617 i
!= data
->notes().end(); ++i
) {
619 const boost::shared_ptr
<MidiRegionView::NoteType
>& note
= *i
;
621 cerr
<< "New note arrived, length = " << note
->length()
622 << " num " << note
->note()
625 if (note
->length() == 0) {
626 /* we got NoteOn but not NoteOff (yet)
631 nframes_t note_start_frames
= tconv
.to(note
->time());
632 nframes_t note_end_frames
= tconv
.to(note
->end_time());
635 if (note
->length() > 0 && note_end_frames
+ region
->position() > start
) {
636 mrv
->resolve_note(note
->note(), note_end_frames
);
639 if (note_start_frames
+ region
->position() < start
) {
643 if (note_start_frames
+ region
->position() > start
+ dur
) {
647 if (note
->note() < _lowest_note
) {
648 _lowest_note
= note
->note();
650 } else if (note
->note() > _highest_note
) {
651 _highest_note
= note
->note();
655 mrv
->add_note (note
, !update_range
);
658 mrv
->extend_active_notes();
664 nframes_t nlen
= _trackview
.track()->get_captured_frames(n
);
666 if (nlen
!= region
->length()) {
668 if (region
->source_length(0) >= region
->position() + nlen
) {
670 region
->suspend_property_changes ();
671 region
->set_position (_trackview
.track()->get_capture_start_frame(n
), this);
672 region
->set_length (nlen
, this);
673 region
->resume_property_changes ();
676 /* our special initial length */
677 iter
->second
= add_region_view_internal (region
, false);
681 ArdourCanvas::Item
* rect
= rec_rects
[n
].rectangle
;
692 update_contents_height();
697 MidiStreamView::rec_data_range_ready (nframes_t start
, nframes_t cnt
, boost::weak_ptr
<Source
> weak_src
)
699 // this is called from the butler thread for now
701 ENSURE_GUI_THREAD (*this, &MidiStreamView::rec_data_range_ready
, start
, cnt
, weak_src
)
703 boost::shared_ptr
<SMFSource
> src (boost::dynamic_pointer_cast
<SMFSource
>(weak_src
.lock()));
709 if (start
+ cnt
> last_rec_data_frame
) {
710 last_rec_data_frame
= start
+ cnt
;
713 this->update_rec_regions (src
->model(), start
, cnt
);
717 MidiStreamView::color_handler ()
721 if (_trackview
.is_midi_track()) {
722 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiTrackBase
.get();
724 canvas_rect
->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_MidiBusBase
.get();;
729 MidiStreamView::note_range_adjustment_changed()
731 double sum
= note_range_adjustment
.get_value() + note_range_adjustment
.get_page_size();
732 int lowest
= (int) floor(note_range_adjustment
.get_value());
735 if (sum
== _range_sum_cache
) {
736 //cerr << "cached" << endl;
737 highest
= (int) floor(sum
);
739 //cerr << "recalc" << endl;
740 highest
= lowest
+ (int) floor(note_range_adjustment
.get_page_size());
741 _range_sum_cache
= sum
;
744 if (lowest
== _lowest_note
&& highest
== _highest_note
) {
748 //cerr << "note range adjustment changed: " << lowest << " " << highest << endl;
749 //cerr << " val=" << v_zoom_adjustment.get_value() << " page=" << v_zoom_adjustment.get_page_size() << " sum=" << v_zoom_adjustment.get_value() + v_zoom_adjustment.get_page_size() << endl;
751 _lowest_note
= lowest
;
752 _highest_note
= highest
;
753 apply_note_range(lowest
, highest
, true);