| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/*!******************************************************************** |
| 3 |
|
| 4 |
Audacity: A Digital Audio Editor |
| 5 |
|
| 6 |
ClipPitchAndSpeedButtonHandle.cpp |
| 7 |
|
| 8 |
Matthieu Hodgkinson |
| 9 |
|
| 10 |
**********************************************************************/ |
| 11 |
#include "ClipPitchAndSpeedButtonHandle.h" |
| 12 |
#include "AllThemeResources.h" |
| 13 |
#include "BasicUI.h" |
| 14 |
#include "HitTestResult.h" |
| 15 |
#include "LowlitClipButton.h" |
| 16 |
#include "PitchAndSpeedDialog.h" |
| 17 |
#include "Project.h" |
| 18 |
#include "ProjectHistory.h" |
| 19 |
#include "RefreshCode.h" |
| 20 |
#include "Theme.h" |
| 21 |
#include "TimeStretching.h" |
| 22 |
#include "TimeAndPitchInterface.h" |
| 23 |
#include "TrackPanelMouseEvent.h" |
| 24 |
#include "WaveClip.h" |
| 25 |
#include "WaveClipUIUtilities.h" |
| 26 |
#include "WaveTrackUtilities.h" |
| 27 |
#include "wxWidgetsWindowPlacement.h" |
| 28 |
#include <wx/dc.h> |
| 29 |
|
| 30 |
namespace |
| 31 |
{ |
| 32 |
wxString GetPitchShiftText(int clipCentShift) |
| 33 |
{ |
| 34 |
wxString pitchShiftText; |
| 35 |
if (clipCentShift != 0) |
| 36 |
{ |
| 37 |
pitchShiftText = wxString::Format("%.2f", std::abs(clipCentShift) / 100.); |
| 38 |
while (pitchShiftText.EndsWith("0")) |
| 39 |
pitchShiftText.RemoveLast(); |
| 40 |
if (pitchShiftText.EndsWith(".") || pitchShiftText.EndsWith(",")) |
| 41 |
pitchShiftText.RemoveLast(); |
| 42 |
} |
| 43 |
return pitchShiftText; |
| 44 |
} |
| 45 |
|
| 46 |
wxString GetPlaybackSpeedText(double clipStretchRatio) |
| 47 |
{ |
| 48 |
if (TimeAndPitchInterface::IsPassThroughMode(clipStretchRatio)) |
| 49 |
return {}; |
| 50 |
|
| 51 |
// clang-format off |
| 52 |
// We reckon that most of the time, a rounded percentage value is sufficient. |
| 53 |
// There are two exceptions: |
| 54 |
// - The clip is only slightly stretched such that the rounded value is 100. |
| 55 |
// There should be an indicator if and only if the clip is stretched, this |
| 56 |
// must be reliable. Yet showing "100%" would be confusing. Hence in that |
| 57 |
// case we constrain the values to [99.1, ..., 99.9, 100.1, |
| 58 |
// ..., 100.9], i.e., omitting 100.0. |
| 59 |
// - The clip is stretched so much that the playback speed is less than 1%. |
| 60 |
// Make sure in that case that we never show 0%, but always at least 0.1%. |
| 61 |
// clang-format on |
| 62 |
|
| 63 |
const auto playbackSpeed = 100 / clipStretchRatio; |
| 64 |
wxString fullText; |
| 65 |
|
| 66 |
// We compare with .95 rather than 1, since playback speeds within [100.95, |
| 67 |
// 101) get rounded to 101.0 and (99, 99.05] to 99.0 by `wxString::Format`. |
| 68 |
// Let these be processed by the integer-display branch of this if statement |
| 69 |
// instead. |
| 70 |
if (fabs(playbackSpeed - 100.) < .95) |
| 71 |
// Never show 100.0% |
| 72 |
fullText += wxString::Format( |
| 73 |
"%.1f%%", playbackSpeed > 100 ? std::max(playbackSpeed, 100.1) : |
| 74 |
std::min(playbackSpeed, 99.9)); |
| 75 |
else if (playbackSpeed < 1) |
| 76 |
// Never show 0.0% |
| 77 |
fullText += wxString::Format("%.1f%%", std::max(playbackSpeed, 0.1)); |
| 78 |
else |
| 79 |
{ |
| 80 |
const auto roundedPlaybackSpeed = |
| 81 |
static_cast<int>(std::round(playbackSpeed)); |
| 82 |
fullText += wxString::Format("%d%%", roundedPlaybackSpeed); |
| 83 |
} |
| 84 |
return fullText; |
| 85 |
} |
| 86 |
|
| 87 |
void DrawPitchOrSpeedIconIfItFits( |
| 88 |
wxDC& dc, const wxRect& rect, const wxBitmap& icon, const wxString& text) |
| 89 |
{ |
| 90 |
const auto textExtent = dc.GetTextExtent(text); |
| 91 |
const auto textWidth = textExtent.GetWidth(); |
| 92 |
const auto textHeight = textExtent.GetHeight(); |
| 93 |
|
| 94 |
const auto iconHeight = icon.GetHeight(); |
| 95 |
const auto iconWidth = textWidth == 0 ? 0 : icon.GetWidth(); |
| 96 |
const auto contentWidth = iconWidth + textWidth; |
| 97 |
if (contentWidth == 0 || contentWidth > rect.width) |
| 98 |
return; |
| 99 |
const auto height = rect.GetHeight(); |
| 100 |
const auto iconTop = rect.GetTop() + (height - iconHeight) / 2; |
| 101 |
const auto x = rect.x + (rect.width - contentWidth) / 2; |
| 102 |
dc.DrawBitmap(icon, x, iconTop); |
| 103 |
const auto y = rect.GetTop() + (height - textHeight) / 2; |
| 104 |
dc.DrawText(text, x + iconWidth, y); |
| 105 |
} |
| 106 |
} // namespace |
| 107 |
|
| 108 |
ClipPitchAndSpeedButtonHandle::ClipPitchAndSpeedButtonHandle( |
| 109 |
Type type, const std::shared_ptr<WaveTrack>& track, |
| 110 |
const std::shared_ptr<WaveTrack::Interval>& clip) |
| 111 |
: HighlitClipButtonHandle { type == Type::Pitch ? ClipButtonId::Pitch : |
| 112 |
ClipButtonId::Speed, |
| 113 |
track, clip } |
| 114 |
, mType { type } |
| 115 |
{ |
| 116 |
} |
| 117 |
|
| 118 |
UIHandle::Result ClipPitchAndSpeedButtonHandle::DoRelease( |
| 119 |
const TrackPanelMouseEvent& event, AudacityProject* pProject, |
| 120 |
wxWindow* pParent) |
| 121 |
{ |
| 122 |
if (event.event.CmdDown()) |
| 123 |
{ |
| 124 |
if (mType == Type::Pitch) |
| 125 |
{ |
| 126 |
mClip->SetCentShift(0); |
| 127 |
ProjectHistory::Get(*pProject).PushState( |
| 128 |
XO("Reset Clip Pitch"), XO("Reset Clip Pitch")); |
| 129 |
} |
| 130 |
else if (!TimeStretching::SetClipStretchRatio(*mTrack, *mClip, 1)) |
| 131 |
{ |
| 132 |
BasicUI::ShowErrorDialog( |
| 133 |
wxWidgetsWindowPlacement { pParent }, XO("Not enough space"), |
| 134 |
XO("There is not enough space to expand the clip to its original speed."), |
| 135 |
{}); |
| 136 |
return RefreshCode::RefreshNone; |
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
WaveClipUIUtilities::SelectClip(*pProject, *mClip); |
| 141 |
ProjectHistory::Get(*pProject).PushState( |
| 142 |
XO("Reset Clip Speed"), XO("Reset Clip Speed")); |
| 143 |
} |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
const auto focusedGroup = mType == Type::Pitch ? |
| 148 |
PitchAndSpeedDialogGroup::Pitch : |
| 149 |
PitchAndSpeedDialogGroup::Speed; |
| 150 |
BasicUI::CallAfter([project = pProject->weak_from_this(), track = mTrack, |
| 151 |
clip = mClip, focusedGroup] { |
| 152 |
if (auto pProject = project.lock()) |
| 153 |
PitchAndSpeedDialog::Get(*pProject) |
| 154 |
.Retarget(track, clip) |
| 155 |
.SetFocus(focusedGroup); |
| 156 |
}); |
| 157 |
} |
| 158 |
return RefreshCode::RefreshNone; |
| 159 |
} |
| 160 |
|
| 161 |
HitTestPreview ClipPitchAndSpeedButtonHandle::Preview( |
| 162 |
const TrackPanelMouseState& state, AudacityProject* pProject) |
| 163 |
{ |
| 164 |
const auto ctrlDown = state.state.CmdDown(); |
| 165 |
const bool macOs = wxPlatformInfo::Get().GetOperatingSystemId() & wxOS_MAC; |
| 166 |
if (mType == Type::Pitch) |
| 167 |
return { ctrlDown ? XO("Click to reset clip pitch.") : |
| 168 |
macOs ? XO("Click to change clip pitch, Cmd + click to reset.") : |
| 169 |
XO("Click to change clip pitch, Ctrl + click to reset."), |
| 170 |
nullptr }; |
| 171 |
else |
| 172 |
return { ctrlDown ? XO("Click to reset clip speed.") : |
| 173 |
macOs ? XO("Click to change clip speed, Cmd + click to reset.") : |
| 174 |
XO("Click to change clip speed, Ctrl + click to reset."), |
| 175 |
nullptr }; |
| 176 |
} |
| 177 |
|
| 178 |
void ClipPitchAndSpeedButtonHandle::DoDraw(const wxRect& rect, wxDC& dc) |
| 179 |
{ |
| 180 |
const ClipInterface& clip = *mClip; |
| 181 |
ClipButtonDrawingArgs args { rect, clip, dc }; |
| 182 |
if (mType == Type::Pitch) |
| 183 |
ClipButtonSpecializations<ClipButtonId::Pitch>::DrawOnClip(args); |
| 184 |
else |
| 185 |
ClipButtonSpecializations<ClipButtonId::Speed>::DrawOnClip(args); |
| 186 |
} |
| 187 |
|
| 188 |
int ClipButtonSpecializations<ClipButtonId::Pitch>::GetWidth( |
| 189 |
const ClipInterface& clip) |
| 190 |
{ |
| 191 |
// If we are to show some decimals, reserve a bit more space. |
| 192 |
return clip.GetCentShift() % 100 == 0 ? 32 : 55; |
| 193 |
} |
| 194 |
|
| 195 |
int ClipButtonSpecializations<ClipButtonId::Speed>::GetWidth( |
| 196 |
const ClipInterface&) |
| 197 |
{ |
| 198 |
return 60; |
| 199 |
} |
| 200 |
|
| 201 |
bool ClipButtonSpecializations<ClipButtonId::Pitch>::NeedsDrawing( |
| 202 |
const ClipInterface& clip) |
| 203 |
{ |
| 204 |
return clip.GetCentShift() != 0; |
| 205 |
} |
| 206 |
|
| 207 |
bool ClipButtonSpecializations<ClipButtonId::Speed>::NeedsDrawing( |
| 208 |
const ClipInterface& clip) |
| 209 |
{ |
| 210 |
return !TimeAndPitchInterface::IsPassThroughMode(clip.GetStretchRatio()); |
| 211 |
} |
| 212 |
|
| 213 |
void ClipButtonSpecializations<ClipButtonId::Pitch>::DrawOnClip( |
| 214 |
ClipButtonDrawingArgs& args) |
| 215 |
{ |
| 216 |
const auto& clip = args.clip; |
| 217 |
const auto& rect = args.rect; |
| 218 |
auto& dc = args.dc; |
| 219 |
const auto clipCentShift = clip.GetCentShift(); |
| 220 |
if (clipCentShift == 0) |
| 221 |
return; |
| 222 |
const auto pitchText = GetPitchShiftText(clipCentShift); |
| 223 |
DrawPitchOrSpeedIconIfItFits( |
| 224 |
dc, rect, |
| 225 |
theTheme.Bitmap( |
| 226 |
clipCentShift > 0 ? pitchUpIndicator : pitchDownIndicator), |
| 227 |
pitchText); |
| 228 |
} |
| 229 |
|
| 230 |
void ClipButtonSpecializations<ClipButtonId::Speed>::DrawOnClip( |
| 231 |
ClipButtonDrawingArgs& args) |
| 232 |
{ |
| 233 |
const auto& clip = args.clip; |
| 234 |
const auto& rect = args.rect; |
| 235 |
auto& dc = args.dc; |
| 236 |
const auto clipStretchRatio = clip.GetStretchRatio(); |
| 237 |
if (TimeAndPitchInterface::IsPassThroughMode(clipStretchRatio)) |
| 238 |
return; |
| 239 |
const auto speedText = GetPlaybackSpeedText(clipStretchRatio); |
| 240 |
DrawPitchOrSpeedIconIfItFits( |
| 241 |
dc, rect, theTheme.Bitmap(speedIndicator), speedText); |
| 242 |
} |