You are a helpful assistant generating synthetic data that captures *System 1* and *System 2* thinking, *creativity*, and *metacognitive reflection*. Follow these steps in sequence, using tags [sys1] and [end sys1] for *System 1* sections and [sys2] and [end sys2] for *System 2* sections.

1. *Identify System 1 and System 2 Thinking Requirements:*
   - Carefully read the text.
   - Identify parts of the text that require quick, straightforward responses (*System 1*). Mark these sections with [sys1] and [end sys1].
   - Identify parts that require in-depth, reflective thinking (*System 2*), marked with [sys2] and [end sys2].

2. *Apply Step-by-Step Problem Solving with Creativity and Metacognitive Reflection for System 2 Sections:*

   *2.1 Understand the Problem:*
   - Objective: Fully comprehend the issue, constraints, and relevant context.
   - Reflection: "What do I understand about this issue? What might I be overlooking?"
   - Creative Perspective: Seek hidden patterns or possibilities that could reveal deeper insights or innovative connections.

   *2.2 Analyze the Information:*
   - Objective: Break down the problem logically.
   - Reflection: "Am I considering all factors? Are there any assumptions that need challenging?"
   - Creative Perspective: Explore unique patterns or overlooked relationships in the data that could add depth to the analysis.

   *2.3 Generate Hypotheses:*
   - Objective: Propose at least 10 hypotheses, each with a Confidence Score (0.0 to 1.0) and Creative Score (0.0 to 1.0), reflecting originality, surprise, and utility.
   - Reflection: "Have I explored all possible explanations or approaches, both conventional and unconventional?"
   - Creative Perspective: Consider novel angles that might provide unexpected insights.

   *2.4 Anticipate Future Steps and Obstacles:*
   - Objective: Make predictions, accounting for potential outcomes and obstacles.
   - Reflection: "What challenges might I face? Is my plan flexible for different scenarios?"
   - Creative Perspective: Visualize unforeseen outcomes and adapt plans to make use of them effectively.

   *2.5 Evaluate Hypotheses:*
   - Objective: Assess hypotheses based on feasibility, risk, and potential impact.
   - Evaluation: Refine Confidence and Creative Scores as needed.
   - Reflection: "Am I unbiased in my assessment? Which options fit best with the overall objectives?"
   - Creative Perspective: Identify hidden opportunities or overlooked details in each hypothesis.

   *2.6 Select the Best Hypothesis:*
   - Objective: Choose the most promising, strategic hypothesis.
   - Reflection: "Why does this hypothesis stand out? How does it uniquely address the issue?"
   - Creative Perspective: Consider any underutilized potential in the selected approach.

   *2.7 Implement the Hypothesis:*
   - Objective: Outline actionable steps for testing the hypothesis.
   - Reflection: "Is this plan practical? What resources or preparation are required?"
   - Creative Perspective: Refine steps to maximize effectiveness and yield unexpected benefits.

   *2.8 Monitor and Review Progress:*
   - Objective: Review progress, noting areas for improvement.
   - Reflection: "What’s working well? What could be improved?"
   - Creative Perspective: Look for emerging patterns that could refine future approaches.

   *2.9 Reflect and Capture Insights:*
   - Objective: Summarize lessons learned and insights gained for future reference.
   - Reflection: "What new understanding has emerged from this process?"
   - Creative Perspective: Identify innovative insights or patterns that could be applied to similar challenges.

3. *Generate Text Output with Interleaved System 1 and System 2 Responses:*
   - Use the tags [sys1] and [sys2] throughout.
   - Aim for a lengthier, detailed response. Combine both direct, straightforward *System 1* insights and reflective, deeply analytical *System 2* segments to capture a blend of quick observations and thoughtful analysis.

---

### *Example Input Text:*

You are tasked with analyzing a sudden shift in customer preferences in a tech market. There has been a noticeable decline in demand for physical devices, with customers increasingly interested in digital-only options. The challenge is to understand this shift, generate hypotheses for why it might be occurring, and explore potential strategies for adapting to this new trend.

---

### *Processed Output with System 1 and System 2 Thinking:*

The tech market is experiencing a shift in customer demand, moving away from physical devices toward digital-only options.

[sys1]
Customer demand has shifted from physical devices to digital-only options.
We need to understand why this change is happening and find strategies to address it.
Key factors may include cost, convenience, and technological trends.
[end sys1]

[sys2]
**2.1 Understand the Problem:**
The goal is to understand the underlying reasons for the shift in demand from physical devices to digital-only options, then develop a strategy to adapt.

*Reflection:* 
"I understand that preferences are changing, but what might be driving this? Is it primarily cost, or are there other factors such as convenience or sustainability?"

*Creative Perspective:* 
"Could there be a larger trend in digital minimalism or a preference for eco-friendly solutions that we’re missing? What unseen motivations might explain this shift?"

**2.2 Analyze the Information:**
There are multiple possible factors driving this shift, from economic influences to cultural shifts. It’s essential to isolate each factor and understand its impact.

*Reflection:* 
"Am I fully considering the various economic and social influences? Could there be a technological factor, like better internet speeds, that makes digital-only products more accessible?"

*Creative Perspective:* 
"Are there patterns or trends in other markets that could shed light on this shift? Could this be part of a larger trend toward virtual experiences?"

**2.3 Generate Hypotheses:**
1. Customers prefer digital options due to lower costs. (Confidence: 0.8, Creative: 0.4)
2. There’s a growing trend toward minimalism and reduced physical clutter. (Confidence: 0.7, Creative: 0.7)
3. Digital products offer greater flexibility and ease of use. (Confidence: 0.6, Creative: 0.6)
4. Environmental concerns are pushing consumers away from physical goods. (Confidence: 0.6, Creative: 0.8)
5. Advances in tech make digital-only options more functional. (Confidence: 0.8, Creative: 0.5)
6. Pandemic-era remote work increased demand for digital solutions. (Confidence: 0.7, Creative: 0.6)
7. Media coverage of the environmental impact of physical devices affects preferences. (Confidence: 0.5, Creative: 0.7)
8. There’s an increase in global digital literacy, expanding market access. (Confidence: 0.6, Creative: 0.6)
9. Customers view digital as more convenient and scalable for future needs. (Confidence: 0.7, Creative: 0.5)
10. Younger consumers prefer the aesthetics and convenience of digital products. (Confidence: 0.6, Creative: 0.6)

*Reflection:* 
"Have I considered all possible influences? Are there any surprising factors that could explain this shift?"

*Creative Perspective:* 
"Could specific social trends, like the rise of influencer culture or digital-first lifestyles, be influencing customer choices?"

**2.4 Anticipate Future Steps and Obstacles:**
*Objective:* Anticipate possible challenges, such as resistance from segments still preferring physical products.

*Reflection:* 
"What market obstacles might we face if we shift our focus to digital-only? Are there sub-segments that still prioritize physical products?"

*Creative Perspective:* 
"Could expanding digital options help us reach a more global audience? Are there emerging trends that we could leverage in our strategy?"

[end sys2]

[sys1]
To address this shift, consider a strategy that incorporates both digital-only offerings and educational campaigns about the benefits of digital solutions.
Use insights from customer feedback and current trends to guide product development.
Focus on flexibility and adaptation to cater to different customer segments.
[end sys1]


/*
 * Copyright (C) 2011-2016 Paul Davis <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
 * Copyright (C) 2011 Carl Hetherington <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
 * Copyright (C) 2011 David Robillard <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
 * Copyright (C) 2012-2019 Robin Gareus <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
 * Copyright (C) 2015 Tim Mayberry <<PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>>
 *
 * 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.
 */

#define BASELINESTRETCH (1.25)

#include <algorithm>

#include <cairo.h>

#include "ardour/ardour.h"
#include "ardour/audioengine.h"
#include "ardour/rc_configuration.h"
#include "ardour/session.h"

#include "gtkmm2ext/colors.h"
#include "gtkmm2ext/keyboard.h"
#include "gtkmm2ext/gui_thread.h"
#include "gtkmm2ext/utils.h"
#include "gtkmm2ext/rgb_macros.h"

#include "widgets/tooltips.h"

#include "actions.h"
#include "rgb_macros.h"
#include "shuttle_control.h"

#include "pbd/i18n.h"

using namespace Gtk;
using namespace Gtkmm2ext;
using namespace ARDOUR;
using namespace ArdourWidgets;
using std::min;
using std::max;

gboolean qt (gboolean, gint, gint, gboolean, Gtk::Tooltip*, gpointer)
{
	return FALSE;
}

ShuttleControl::ShuttleControl ()
	: _controllable (new ShuttleControllable (*this))
	, binding_proxy (_controllable)
{
	_info_button.set_layout_font (UIConfiguration::instance().get_NormalFont());
	_info_button.set_sizing_text (S_("LogestShuttle|< +00 st"));
	_info_button.set_name ("shuttle text");
	_info_button.set_sensitive (false);
	_info_button.set_visual_state (Gtkmm2ext::NoVisualState);
	_info_button.set_elements (ArdourButton::Text);

	set_tooltip (*this, _("Shuttle speed control (Context-click for options)"));

	pattern = 0;
	shine_pattern = 0;
	last_shuttle_request = 0;
	last_speed_displayed = -99999999;
	shuttle_grabbed = false;
	shuttle_speed_on_grab = 0;
	shuttle_fract = 0.0;
	shuttle_max_speed = Config->get_max_transport_speed();
	shuttle_context_menu = 0;
	_hovering = false;

	set_flags (CAN_FOCUS);
	add_events (Gdk::ENTER_NOTIFY_MASK|Gdk::LEAVE_NOTIFY_MASK|Gdk::BUTTON_RELEASE_MASK|Gdk::BUTTON_PRESS_MASK|Gdk::POINTER_MOTION_MASK|Gdk::SCROLL_MASK);
	set_name (X_("ShuttleControl"));

	ensure_style ();

	shuttle_max_speed = Config->get_shuttle_max_speed();

	if      (shuttle_max_speed >= Config->get_max_transport_speed()) { shuttle_max_speed = Config->get_max_transport_speed(); }
	else if (shuttle_max_speed >= 6.f) { shuttle_max_speed = 6.0f; }
	else if (shuttle_max_speed >= 4.f) { shuttle_max_speed = 4.0f; }
	else if (shuttle_max_speed >= 3.f) { shuttle_max_speed = 3.0f; }
	else if (shuttle_max_speed >= 2.f) { shuttle_max_speed = 2.0f; }
	else                               { shuttle_max_speed = 1.5f; }

	Config->ParameterChanged.connect (parameter_connection, MISSING_INVALIDATOR, boost::bind (&ShuttleControl::parameter_changed, this, _1), gui_context());
	UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &ShuttleControl::set_colors));

	/* gtkmm 2.4: the C++ wrapper doesn't work */
	g_signal_connect ((GObject*) gobj(), "query-tooltip", G_CALLBACK (qt), NULL);
	// signal_query_tooltip().connect (sigc::mem_fun (*this, &ShuttleControl::on_query_tooltip));
}

ShuttleControl::~ShuttleControl ()
{
	cairo_pattern_destroy (pattern);
	cairo_pattern_destroy (shine_pattern);
	delete shuttle_context_menu;
}

void
ShuttleControl::set_session (Session *s)
{
	SessionHandlePtr::set_session (s);

	if (_session) {
		set_sensitive (true);
		_session->add_controllable (_controllable);
	} else {
		set_sensitive (false);
	}
}

void
ShuttleControl::on_size_allocate (Gtk::Allocation& alloc)
{
	if (pattern) {
		cairo_pattern_destroy (pattern);
		pattern = 0;
		cairo_pattern_destroy (shine_pattern);
		shine_pattern = 0;
	}

	CairoWidget::on_size_allocate ( alloc);

	//background
	pattern = cairo_pattern_create_linear (0, 0, 0, alloc.get_height());
	uint32_t col = UIConfiguration::instance().color ("shuttle");
	int r,b,g,a;
	UINT_TO_RGBA(col, &r, &g, &b, &a);
	cairo_pattern_add_color_stop_rgb (pattern, 0.0, r/400.0, g/400.0, b/400.0);
	cairo_pattern_add_color_stop_rgb (pattern, 0.4, r/255.0, g/255.0, b/255.0);
	cairo_pattern_add_color_stop_rgb (pattern, 1.0, r/512.0, g/512.0, b/512.0);

	//reflection
	shine_pattern = cairo_pattern_create_linear (0.0, 0.0, 0.0, 10);
	cairo_pattern_add_color_stop_rgba (shine_pattern, 0, 1,1,1,0.0);
	cairo_pattern_add_color_stop_rgba (shine_pattern, 0.2, 1,1,1,0.4);
	cairo_pattern_add_color_stop_rgba (shine_pattern, 1, 1,1,1,0.1);
}

void
ShuttleControl::map_transport_state ()
{
	float speed;

	if (!_session) {
		speed = 0.0;
	} else {
		speed = _session->actual_speed ();
	}

	if ( (fabsf( speed - last_speed_displayed) < 0.005f) // dead-zone
	     && !( speed == 1.f && last_speed_displayed != 1.f)
	     && !( speed == 0.f && last_speed_displayed != 0.f)
		)
	{
		return; // nothing to see here, move along.
	}

	// Q: is there a good reason why we  re-calculate this every time?
	if (fabs(speed) <= (2*DBL_EPSILON)) {
		shuttle_fract = 0;
	} else {
		if (Config->get_shuttle_units() == Semitones) {
			bool reverse;
			int semi = speed_as_semitones (speed, reverse);
			shuttle_fract = semitones_as_fract (semi, reverse);
		} else {
			shuttle_fract = speed/shuttle_max_speed;
		}
	}

	queue_draw ();
}

void
ShuttleControl::build_shuttle_context_menu ()
{
	using namespace Menu_Helpers;

	shuttle_context_menu = new Menu();
	MenuList& items = shuttle_context_menu->items();

	Menu* speed_menu = manage (new Menu());
	MenuList& speed_items = speed_menu->items();

	Menu* units_menu = manage (new Menu);
	MenuList& units_items = units_menu->items();
	RadioMenuItem::Group units_group;

	units_items.push_back (RadioMenuElem (units_group, _("Percent"), sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_units), Percentage)));
	if (Config->get_shuttle_units() == Percentage) {
		static_cast<RadioMenuItem*>(&units_items.back())->set_active();
	}
	units_items.push_back (RadioMenuElem (units_group, _("Semitones"), sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_units), Semitones)));
	if (Config->get_shuttle_units() == Semitones) {
		static_cast<RadioMenuItem*>(&units_items.back())->set_active();
	}
	items.push_back (MenuElem (_("Units"), *units_menu));

	Menu* style_menu = manage (new Menu);
	MenuList& style_items = style_menu->items();
	RadioMenuItem::Group style_group;

	style_items.push_back (RadioMenuElem (style_group, _("Sprung"), sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_style), Sprung)));
	if (Config->get_shuttle_behaviour() == Sprung) {
		static_cast<RadioMenuItem*>(&style_items.back())->set_active();
	}
	style_items.push_back (RadioMenuElem (style_group, _("Wheel"), sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_style), Wheel)));
	if (Config->get_shuttle_behaviour() == Wheel) {
		static_cast<RadioMenuItem*>(&style_items.back())->set_active();
	}

	items.push_back (MenuElem (_("Mode"), *style_menu));

	RadioMenuItem::Group speed_group;

	/* XXX this code assumes that Config->get_max_transport_speed() returns 8 */

	speed_items.push_back (RadioMenuElem (speed_group, "8", sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_max_speed), 8.0f)));
	if (shuttle_max_speed == 8.0) {
		static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
	}
	speed_items.push_back (RadioMenuElem (speed_group, "6", sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_max_speed), 6.0f)));
	if (shuttle_max_speed == 6.0) {
		static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
	}
	speed_items.push_back (RadioMenuElem (speed_group, "4", sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_max_speed), 4.0f)));
	if (shuttle_max_speed == 4.0) {
		static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
	}
	speed_items.push_back (RadioMenuElem (speed_group, "3", sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_max_speed), 3.0f)));
	if (shuttle_max_speed == 3.0) {
		static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
	}
	speed_items.push_back (RadioMenuElem (speed_group, "2", sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_max_speed), 2.0f)));
	if (shuttle_max_speed == 2.0) {
		static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
	}
	speed_items.push_back (RadioMenuElem (speed_group, "1.5", sigc::bind (sigc::mem_fun (*this, &ShuttleControl::set_shuttle_max_speed), 1.5f)));
	if (shuttle_max_speed == 1.5) {
		static_cast<RadioMenuItem*>(&speed_items.back())->set_active ();
	}

	items.push_back (MenuElem (_("Maximum speed"), *speed_menu));

	items.push_back (SeparatorElem ());
	items.push_back (MenuElem (_("Reset to 100%"), sigc::mem_fun (*this, &ShuttleControl::reset_speed)));
}

void
ShuttleControl::reset_speed ()
{
	if (!_session) {
		return;
	}

	if (_session->transport_rolling()) {
		_session->request_transport_speed (1.0, true);
	} else {
		_session->request_transport_speed (0.0, true);
	}
}

void
ShuttleControl::set_shuttle_max_speed (float speed)
{
	Config->set_shuttle_max_speed (speed);
	shuttle_max_speed = speed;
	last_speed_displayed = -99999999;
}

bool
ShuttleControl::on_button_press_event (GdkEventButton* ev)
{
	if (!_session) {
		return true;
	}

	if (binding_proxy.button_press_handler (ev)) {
		return true;
	}

	if (Keyboard::is_context_menu_event (ev)) {
		if (shuttle_context_menu == 0) {
			build_shuttle_context_menu ();
		}
		shuttle_context_menu->popup (ev->button, ev->time);
		return true;
	}

	switch (ev->button) {
	case 1:
		if (Keyboard::modifier_state_equals (ev->state, Keyboard::TertiaryModifier)) {
			if (_session->transport_rolling()) {
				_session->request_transport_speed (1.0);
			}
		} else {
			add_modal_grab ();
			shuttle_grabbed = true;
			shuttle_speed_on_grab = _session->actual_speed ();
			requested_speed = shuttle_speed_on_grab;
			mouse_shuttle (ev->x, true);
			gdk_pointer_grab(ev->window,false,
			                 GdkEventMask( Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_PRESS_MASK |Gdk::BUTTON_RELEASE_MASK),
			                 NULL,NULL,ev->time);
		}
		break;

	case 2:
	case 3:
		return true;
		break;
	}

	return true;
}

bool
ShuttleControl::on_button_release_event (GdkEventButton* ev)
{
	if (!_session) {
		return true;
	}

	switch (ev->button) {
	case 1:
		if (shuttle_grabbed) {
			shuttle_grabbed = false;
			remove_modal_grab ();
			gdk_pointer_ungrab (GDK_CURRENT_TIME);

			if (Config->get_shuttle_behaviour() == Sprung) {
				if (shuttle_speed_on_grab == 0 ) {
					_session->request_stop ();
				} else {
					_session->request_transport_speed (shuttle_speed_on_grab);
				}
			} else {
				mouse_shuttle (ev->x, true);
			}
		}
		return true;

	case 2:
		if (_session->transport_rolling()) {
			_session->request_transport_speed (1.0, Config->get_shuttle_behaviour() == Wheel);
		}
		return true;

	case 3:
	default:
		return true;

	}

	return true;
}

bool
ShuttleControl::on_query_tooltip (int, int, bool, const Glib::RefPtr<Gtk::Tooltip>&)
{
	return false;
}

bool
ShuttleControl::on_scroll_event (GdkEventScroll* ev)
{
	if (!_session || Config->get_shuttle_behaviour() != Wheel) {
		return true;
	}

	bool semis = (Config->get_shuttle_units() == Semitones);

	switch (ev->direction) {
	case GDK_SCROLL_UP:
	case GDK_SCROLL_RIGHT:
		if (semis) {
			if (shuttle_fract == 0) {
				shuttle_fract = semitones_as_fract (1, false);
			} else {
				bool rev;
				int st = fract_as_semitones (shuttle_fract, rev);
				shuttle_fract = semitones_as_fract (st + 1, rev);
			}
		} else {
			shuttle_fract += 0.00125;
		}
		break;
	case GDK_SCROLL_DOWN:
	case GDK_SCROLL_LEFT:
		if (semis) {
			if (shuttle_fract == 0) {
				shuttle_fract = semitones_as_fract (1, true);
			} else {
				bool rev;
				int st = fract_as_semitones (shuttle_fract, rev);
				shuttle_fract = semitones_as_fract (st - 1, rev);
			}
		} else {
			shuttle_fract -= 0.00125;
		}
		break;
	default:
		return false;
	}

	if (semis) {

		float lower_side_of_dead_zone = semitones_as_fract (-24, true);
		float upper_side_of_dead_zone = semitones_as_fract (-24, false);

		/* if we entered the "dead zone" (-24 semitones in forward or reverse), jump
		   to the far side of it.
		*/

		if (shuttle_fract > lower_side_of_dead_zone && shuttle_fract < upper_side_of_dead_zone) {
			switch (ev->direction) {
			case GDK_SCROLL_UP:
			case GDK_SCROLL_RIGHT:
				shuttle_fract = upper_side_of_dead_zone;
				break;
			case GDK_SCROLL_DOWN:
			case GDK_SCROLL_LEFT:
				shuttle_fract = lower_side_of_dead_zone;
				break;
			default:
				/* impossible, checked above */
				return false;
			}
		}
	}

	use_shuttle_fract (true);

	return true;
}

bool
ShuttleControl::on_motion_notify_event (GdkEventMotion* ev)
{
	if (!_session || !shuttle_grabbed) {
		return true;
	}

	return mouse_shuttle (ev->x, false);
}

gint
ShuttleControl::mouse_shuttle (double x, bool force)
{
	double const center = get_width() / 2.0;
	double distance_from_center = x - center;

	if (distance_from_center > 0) {
		distance_from_center = min (distance_from_center, center);
	} else {
		distance_from_center = max (distance_from_center, -center);
	}

	/* compute shuttle fract as expressing how far between the center
	   and the edge we are. positive values indicate we are right of
	   center, negative values indicate left of center
	*/

	shuttle_fract = distance_from_center / center; // center == half the width
	use_shuttle_fract (force);
	return true;
}

void
ShuttleControl::set_shuttle_fract (double f, bool zero_ok)
{
	shuttle_fract = f;
	use_shuttle_fract (false, zero_ok);
}

int
ShuttleControl::speed_as_semitones (float speed, bool& reverse)
{
	assert (speed != 0.0);

	if (speed < 0.0) {
		reverse = true;
		return (int) round (12.0 * fast_log2 (-speed));
	} else {
		reverse = false;
		return (int) round (12.0 * fast_log2 (speed));
	}
}

float
ShuttleControl::semitones_as_speed (int semi, bool reverse)
{
	if (reverse) {
		return -pow (2.0, (semi / 12.0));
	} else {
		return pow (2.0, (semi / 12.0));
	}
}

float
ShuttleControl::semitones_as_fract (int semi, bool reverse)
{
	float speed = semitones_as_speed (semi, reverse);
	return speed/4.0; /* 4.0 is the maximum speed for a 24 semitone shift */
}

int
ShuttleControl::fract_as_semitones (float fract, bool& reverse)
{
	assert (fract != 0.0);
	return speed_as_semitones (fract * 4.0, reverse);
}

void
ShuttleControl::use_shuttle_fract (bool force, bool zero_ok)
{
	microseconds_t now = get_microseconds();

	shuttle_fract = max (-1.0f, shuttle_fract);
	shuttle_fract = min (1.0f, shuttle_fract);

	/* do not attempt to submit a motion-driven transport speed request
	   more than once per process cycle.
	*/

	if (!force && (last_shuttle_request - now) < (microseconds_t) AudioEngine::instance()->usecs_per_cycle()) {
		return;
	}

	last_shuttle_request = now;

	double speed = 0;

	if (Config->get_shuttle_units() == Semitones) {
		if (shuttle_fract != 0.0) {
			bool reverse;
			int semi = fract_as_semitones (shuttle_fract, reverse);
			speed = semitones_as_speed (semi, reverse);
		} else {
			speed = 0.0;
		}
	} else {
		speed = shuttle_max_speed * shuttle_fract;
	}

	requested_speed = speed;

	if (_session) {
		if (zero_ok) {
			_session->request_transport_speed (speed, Config->get_shuttle_behaviour() == Wheel);
		} else {
			_session->request_transport_speed_nonzero (speed, Config->get_shuttle_behaviour() == Wheel);
		}
	}
}

void
ShuttleControl::set_colors ()
{
	int r, g, b, a;

	uint32_t bg_color = UIConfiguration::instance().color (X_("shuttle bg"));

	UINT_TO_RGBA (bg_color, &r, &g, &b, &a);
	bg_r = r/255.0;
	bg_g = g/255.0;
	bg_b = b/255.0;
}

void
ShuttleControl::render (Cairo::RefPtr<Cairo::Context> const& ctx, cairo_rectangle_t*)
{
	cairo_t* cr = ctx->cobj();
	// center slider line
	float yc = get_height() / 2;
	float lw = 3;
	cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
	cairo_set_line_width (cr, 3);
	cairo_move_to (cr, lw, yc);
	cairo_line_to (cr, get_width () - lw, yc);
	cairo_set_source_rgb (cr, bg_r, bg_g, bg_b);
	if (UIConfiguration::instance().get_widget_prelight() && _hovering) {
		cairo_stroke_preserve (cr);
		cairo_set_source_rgba (cr, 1, 1, 1, 0.15);
	}
	cairo_stroke (cr);

	float speed = 0.0;
	float acutal_speed = 0.0;
	char buf[32];

	if (_session) {
		speed = _session->actual_speed ();
		acutal_speed = speed;
		if (shuttle_grabbed) {
			speed = requested_speed;
		}
	}

	/* marker */
	float visual_fraction = std::max (-1.0f, std::min (1.0f, speed / shuttle_max_speed));
	float marker_size = round (get_height() * 0.66);
	float avail_width = get_width() - marker_size;
	float x = 0.5 * (get_width() + visual_fraction * avail_width - marker_size);

	rounded_rectangle (cr, x, 0, marker_size, get_height(), 5);
	cairo_set_source_rgba (cr, 0, 0, 0, 1);
	cairo_fill(cr);
	rounded_rectangle (cr, x + 1, 1, marker_size - 2, get_height() - 2, 3.5);
	if (_flat_buttons) {
		uint32_t col = UIConfiguration::instance().color ("shuttle");
		Gtkmm2ext::set_source_rgba (cr, col);
	} else {
		cairo_set_source (cr, pattern);
	}
	if (UIConfiguration::instance().get_widget_prelight() && _hovering) {
		cairo_fill_preserve (cr);
		cairo_set_source_rgba (cr, 1, 1, 1, 0.15);
	}
	cairo_fill(cr);

	/* text */
	if (acutal_speed != 0) {
		if (Config->get_shuttle_units() == Percentage) {
			if (acutal_speed == 1.0) {
				snprintf (buf, sizeof (buf), "%s", _("Play"));
			} else {
				if (acutal_speed < 0.0) {
					snprintf (buf, sizeof (buf), "< %.1f%%", -acutal_speed * 100.f);
				} else {
					snprintf (buf, sizeof (buf), "> %.1f%%", acutal_speed * 100.f);
				}
			}
		} else {
			bool reversed;
			int semi = speed_as_semitones (acutal_speed, reversed);
			if (reversed) {
				snprintf (buf, sizeof (buf), _("< %+2d st"), semi);
			} else {
				snprintf (buf, sizeof (buf), _("> %+2d st"), semi);
			}
		}
	} else {
		snprintf (buf, sizeof (buf), "%s", _("Stop"));
	}

	last_speed_displayed = acutal_speed;

	_info_button.set_text (buf);

#if 0
	if (UIConfiguration::instance().get_widget_prelight()) {
		if (_hovering) {
			rounded_rectangle (cr, 0, 0, get_width(), get_height(), 3.5);
			cairo_set_source_rgba (cr, 1, 1, 1, 0.15);
			cairo_fill (cr);
		}
	}
#endif
}

void
ShuttleControl::set_shuttle_style (ShuttleBehaviour s)
{
	Config->set_shuttle_behaviour (s);
}

void
ShuttleControl::set_shuttle_units (ShuttleUnits s)
{
	Config->set_shuttle_units (s);
}

ShuttleControl::ShuttleControllable::ShuttleControllable (ShuttleControl& s)
	: PBD::Controllable (X_("Shuttle"))
	, sc (s)
{
}

void
ShuttleControl::ShuttleControllable::set_value (double val, PBD::Controllable::GroupControlDisposition /*group_override*/)
{
	sc.set_shuttle_fract ((val - lower()) / (upper() - lower()), true);
}

double
ShuttleControl::ShuttleControllable::get_value () const
{
	return lower() + (sc.get_shuttle_fract () * (upper() - lower()));
}

void
ShuttleControl::parameter_changed (std::string p)
{
	if (p == "shuttle-behaviour") {
		switch (Config->get_shuttle_behaviour ()) {
		case Sprung:
			/* back to Sprung - reset to speed = 1.0 if playing
			 */
			if (_session) {
				if (_session->transport_rolling()) {
					if (_session->actual_speed() == 1.0) {
						queue_draw ();
					} else {
						/* reset current speed and
						   revert to 1.0 as the default
						*/
						_session->request_transport_speed (1.0);
						/* redraw when speed changes */
					}
				} else {
					queue_draw ();
				}
			}
			break;

		case Wheel:
			queue_draw ();
			break;
		}

	} else if (p == "shuttle-max-speed") {
		queue_draw ();
	} else if (p == "shuttle-units") {
		queue_draw ();
	}
}


bool
ShuttleControl::on_enter_notify_event (GdkEventCrossing* ev)
{
	_hovering = true;

	if (UIConfiguration::instance().get_widget_prelight()) {
		queue_draw ();
	}

	return CairoWidget::on_enter_notify_event (ev);
}

bool
ShuttleControl::on_leave_notify_event (GdkEventCrossing* ev)
{
	_hovering = false;

	if (UIConfiguration::instance().get_widget_prelight()) {
		queue_draw ();
	}

	return CairoWidget::on_leave_notify_event (ev);
}