From ef8904210e9b7bccdf5fa59035552df653b59de5 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 5 Feb 2022 00:12:38 +0100 Subject: [PATCH] Add method to format set of MIDI channels eg. channels 1,2,3,5,6 will be formatted as "1-3, 5, 6" --- gtk2_ardour/utils.cc | 45 ++++++++++++++++++++++++++++++++++++++++++++ gtk2_ardour/utils.h | 2 ++ 2 files changed, 47 insertions(+) diff --git a/gtk2_ardour/utils.cc b/gtk2_ardour/utils.cc index 7dacebf109..58723d6059 100644 --- a/gtk2_ardour/utils.cc +++ b/gtk2_ardour/utils.cc @@ -801,6 +801,51 @@ ARDOUR_UI_UTILS::samples_as_time_string (samplecnt_t s, float rate, bool show_sa return buf; } +string +ARDOUR_UI_UTILS::midi_channels_as_string (std::set const& channels) +{ + if (channels.empty ()) { + return _("none"); + } + + string rv; + auto i = channels.begin (); + + uint8_t next = *i; + uint8_t prev = next; + rv += to_string (next + 1); + + do { + if (*i == next) { + ++i; + ++next; + if (i != channels.end ()) { + continue; + } + } + if (next - prev > 2) { + rv += "-"; + rv += to_string (next); + } else if (next - prev > 1) { + rv += ", "; + rv += to_string (next); + } + + if (i == channels.end ()) { + break; + } + + rv += ", "; + rv += to_string (*i + 1); + + prev = *i; + next = prev + 1; + ++i; + } while (i != channels.end ()); + + return rv; +} + bool ARDOUR_UI_UTILS::windows_overlap (Gtk::Window *a, Gtk::Window *b) { diff --git a/gtk2_ardour/utils.h b/gtk2_ardour/utils.h index 71d4981cf6..c742b681be 100644 --- a/gtk2_ardour/utils.h +++ b/gtk2_ardour/utils.h @@ -103,6 +103,8 @@ Gdk::Color unique_random_color (std::list &); std::string rate_as_string (float r); std::string samples_as_time_string (ARDOUR::samplecnt_t s, float r, bool show_samples = false); +std::string midi_channels_as_string (std::set const&); + bool windows_overlap (Gtk::Window *a, Gtk::Window *b); bool overwrite_file_dialog (Gtk::Window& parent, std::string title, std::string text);