| 1 |
/********************************************************************** |
| 2 |
|
| 3 |
Audacity: A Digital Audio Editor |
| 4 |
|
| 5 |
@file WaveClipUIUtilities.cpp |
| 6 |
|
| 7 |
Paul Licameli split from WaveClip.cpp |
| 8 |
|
| 9 |
**********************************************************************/ |
| 10 |
|
| 11 |
#include "WaveClipUIUtilities.h" |
| 12 |
|
| 13 |
#include "PitchAndSpeedDialog.h" |
| 14 |
#include "ProjectHistory.h" |
| 15 |
#include "SampleCount.h" |
| 16 |
#include "UndoManager.h" |
| 17 |
#include "ViewInfo.h" |
| 18 |
#include "WaveClip.h" |
| 19 |
#include <algorithm> |
| 20 |
#include <cassert> |
| 21 |
#include <cmath> |
| 22 |
|
| 23 |
void WaveClipUIUtilities::findCorrection( |
| 24 |
const std::vector<sampleCount>& oldWhere, size_t oldLen, size_t newLen, |
| 25 |
double t0, double sampleRate, double stretchRatio, double samplesPerPixel, |
| 26 |
int& oldX0, double& correction) |
| 27 |
{ |
| 28 |
// Mitigate the accumulation of location errors |
| 29 |
// in copies of copies of ... of caches. |
| 30 |
// Look at the loop that populates "where" below to understand this. |
| 31 |
|
| 32 |
// Find the sample position that is the origin in the old cache. |
| 33 |
const double oldWhere0 = oldWhere[1].as_double() - samplesPerPixel; |
| 34 |
const double oldWhereLast = oldWhere0 + oldLen * samplesPerPixel; |
| 35 |
// Find the length in samples of the old cache. |
| 36 |
const double denom = oldWhereLast - oldWhere0; |
| 37 |
|
| 38 |
// What sample would go in where[0] with no correction? |
| 39 |
const double guessWhere0 = t0 * sampleRate / stretchRatio; |
| 40 |
|
| 41 |
if ( // Skip if old and NEW are disjoint: |
| 42 |
oldWhereLast <= guessWhere0 || |
| 43 |
guessWhere0 + newLen * samplesPerPixel <= oldWhere0 || |
| 44 |
// Skip unless denom rounds off to at least 1. |
| 45 |
denom < 0.5) |
| 46 |
{ |
| 47 |
// The computation of oldX0 in the other branch |
| 48 |
// may underflow and the assertion would be violated. |
| 49 |
oldX0 = oldLen; |
| 50 |
correction = 0.0; |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
// What integer position in the old cache array does that map to? |
| 55 |
// (even if it is out of bounds) |
| 56 |
oldX0 = floor(0.5 + oldLen * (guessWhere0 - oldWhere0) / denom); |
| 57 |
// What sample count would the old cache have put there? |
| 58 |
const double where0 = oldWhere0 + double(oldX0) * samplesPerPixel; |
| 59 |
// What correction is needed to align the NEW cache with the old? |
| 60 |
const double correction0 = where0 - guessWhere0; |
| 61 |
correction = std::clamp(correction0, -samplesPerPixel, samplesPerPixel); |
| 62 |
assert(correction == correction0); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
void WaveClipUIUtilities::fillWhere( |
| 67 |
std::vector<sampleCount>& where, size_t len, bool addBias, double correction, |
| 68 |
double t0, double sampleRate, double stretchRatio, double samplesPerPixel) |
| 69 |
{ |
| 70 |
// Be careful to make the first value non-negative |
| 71 |
const auto bias = addBias ? .5 : 0.; |
| 72 |
const double w0 = 0.5 + correction + bias + t0 * sampleRate / stretchRatio; |
| 73 |
where[0] = sampleCount(std::max(0.0, floor(w0))); |
| 74 |
for (decltype(len) x = 1; x < len + 1; x++) |
| 75 |
where[x] = sampleCount(floor(w0 + double(x) * samplesPerPixel)); |
| 76 |
} |
| 77 |
|
| 78 |
std::vector<CommonTrackPanelCell::MenuItem> |
| 79 |
WaveClipUIUtilities::GetWaveClipMenuItems() |
| 80 |
{ |
| 81 |
return { |
| 82 |
{ L"Cut", XO("Cut") }, |
| 83 |
{ L"Copy", XO("Copy") }, |
| 84 |
{ L"Paste", XO("Paste") }, |
| 85 |
{}, |
| 86 |
{ L"Split", XO("Split Clip") }, |
| 87 |
{ L"Join", XO("Join Clips") }, |
| 88 |
{ L"TrackMute", XO("Mute/Unmute Track") }, |
| 89 |
{}, |
| 90 |
{ L"RenameClip", XO("Rename Clip...") }, |
| 91 |
{ L"ChangePitchAndSpeed", XO("Pitch and Speed...") }, |
| 92 |
{ L"RenderPitchAndSpeed", XO("Render Pitch and Speed") }, |
| 93 |
}; |
| 94 |
; |
| 95 |
} |
| 96 |
|
| 97 |
void WaveClipUIUtilities::PushClipSpeedChangedUndoState( |
| 98 |
AudacityProject& project, double speedInPercent) |
| 99 |
{ |
| 100 |
ProjectHistory::Get(project).PushState( |
| 101 |
/* i18n-hint: This is about changing the clip playback speed, speed is in |
| 102 |
percent */ |
| 103 |
XO("Changed Clip Speed to %.01f%%").Format(speedInPercent), |
| 104 |
/* i18n-hint: This is about changing the clip playback speed, speed is in |
| 105 |
percent */ |
| 106 |
XO("Changed Speed to %.01f%%").Format(speedInPercent), |
| 107 |
UndoPush::CONSOLIDATE); |
| 108 |
} |
| 109 |
|
| 110 |
void WaveClipUIUtilities::SelectClip( |
| 111 |
AudacityProject& project, const WaveTrack::Interval& clip) |
| 112 |
{ |
| 113 |
auto& viewInfo = ViewInfo::Get(project); |
| 114 |
viewInfo.selectedRegion.setTimes( |
| 115 |
clip.GetPlayStartTime(), clip.GetPlayEndTime()); |
| 116 |
} |