Audacity 3.2.0
WaveClipUtilities.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file WaveClipUtilities.cpp
6
7 Paul Licameli split from WaveClip.cpp
8
9**********************************************************************/
10
11#include "WaveClipUtilities.h"
12
13#include "SampleCount.h"
14#include <algorithm>
15#include <cassert>
16#include <cmath>
17
18#include "PitchAndSpeedDialog.h"
19#include "ProjectAudioIO.h"
20#include "ProjectHistory.h"
21#include "ProjectWindows.h"
22#include "UndoManager.h"
23#include "ViewInfo.h"
24#include "WaveTrack.h"
25
27 const std::vector<sampleCount>& oldWhere, size_t oldLen, size_t newLen,
28 double t0, double sampleRate, double stretchRatio, double samplesPerPixel,
29 int& oldX0, double& correction)
30{
31 // Mitigate the accumulation of location errors
32 // in copies of copies of ... of caches.
33 // Look at the loop that populates "where" below to understand this.
34
35 // Find the sample position that is the origin in the old cache.
36 const double oldWhere0 = oldWhere[1].as_double() - samplesPerPixel;
37 const double oldWhereLast = oldWhere0 + oldLen * samplesPerPixel;
38 // Find the length in samples of the old cache.
39 const double denom = oldWhereLast - oldWhere0;
40
41 // What sample would go in where[0] with no correction?
42 const double guessWhere0 = t0 * sampleRate / stretchRatio;
43
44 if ( // Skip if old and NEW are disjoint:
45 oldWhereLast <= guessWhere0 ||
46 guessWhere0 + newLen * samplesPerPixel <= oldWhere0 ||
47 // Skip unless denom rounds off to at least 1.
48 denom < 0.5)
49 {
50 // The computation of oldX0 in the other branch
51 // may underflow and the assertion would be violated.
52 oldX0 = oldLen;
53 correction = 0.0;
54 }
55 else
56 {
57 // What integer position in the old cache array does that map to?
58 // (even if it is out of bounds)
59 oldX0 = floor(0.5 + oldLen * (guessWhere0 - oldWhere0) / denom);
60 // What sample count would the old cache have put there?
61 const double where0 = oldWhere0 + double(oldX0) * samplesPerPixel;
62 // What correction is needed to align the NEW cache with the old?
63 const double correction0 = where0 - guessWhere0;
64 correction = std::clamp(correction0, -samplesPerPixel, samplesPerPixel);
65 assert(correction == correction0);
66 }
67}
68
70 std::vector<sampleCount>& where, size_t len, bool addBias, double correction,
71 double t0, double sampleRate, double stretchRatio, double samplesPerPixel)
72{
73 // Be careful to make the first value non-negative
74 const auto bias = addBias ? .5 : 0.;
75 const double w0 = 0.5 + correction + bias + t0 * sampleRate / stretchRatio;
76 where[0] = sampleCount(std::max(0.0, floor(w0)));
77 for (decltype(len) x = 1; x < len + 1; x++)
78 where[x] = sampleCount(floor(w0 + double(x) * samplesPerPixel));
79}
80
81std::vector<CommonTrackPanelCell::MenuItem> GetWaveClipMenuItems()
82{
83 return {
84 { L"Cut", XO("Cut") },
85 { L"Copy", XO("Copy") },
86 { L"Paste", XO("Paste") },
87 {},
88 { L"Split", XO("Split Clip") },
89 { L"Join", XO("Join Clips") },
90 { L"TrackMute", XO("Mute/Unmute Track") },
91 {},
92 { L"RenameClip", XO("Rename Clip...") },
93 { L"ChangePitchAndSpeed", XO("Pitch and Speed...") },
94 { L"RenderPitchAndSpeed", XO("Render Pitch and Speed") },
95 };
96 ;
97}
98
100 AudacityProject& project, double speedInPercent)
101{
103 /* i18n-hint: This is about changing the clip playback speed, speed is in
104 percent */
105 XO("Changed Clip Speed to %.01f%%").Format(speedInPercent),
106 /* i18n-hint: This is about changing the clip playback speed, speed is in
107 percent */
108 XO("Changed Speed to %.01f%%").Format(speedInPercent),
110}
111
114 std::optional<PitchAndSpeedDialogFocus> focus)
115{
117 ProjectAudioIO::Get(project).IsAudioActive(), track, interval,
118 &GetProjectFrame(project), focus);
119 if (wxID_OK == dlg.ShowModal())
121 XO("Changed Pitch and Speed"), XO("Changed Pitch and Speed"),
123}
XO("Cut/Copy/Paste")
AUDACITY_DLL_API wxFrame & GetProjectFrame(AudacityProject &project)
Get the top-level window associated with the project (as a wxFrame only, when you do not need to use ...
accessors for certain important windows associated with each project
const auto project
void findCorrection(const std::vector< sampleCount > &oldWhere, size_t oldLen, size_t newLen, double t0, double sampleRate, double stretchRatio, double samplesPerPixel, int &oldX0, double &correction)
void fillWhere(std::vector< sampleCount > &where, size_t len, bool addBias, double correction, double t0, double sampleRate, double stretchRatio, double samplesPerPixel)
void ShowClipPitchAndSpeedDialog(AudacityProject &project, WaveTrack &track, WaveTrack::Interval &interval, std::optional< PitchAndSpeedDialogFocus > focus)
std::vector< CommonTrackPanelCell::MenuItem > GetWaveClipMenuItems()
void PushClipSpeedChangedUndoState(AudacityProject &project, double speedInPercent)
The top-level handle to an Audacity project. It serves as a source of events that other objects can b...
Definition: Project.h:90
Abstract base class used in importing a file.
static ProjectAudioIO & Get(AudacityProject &project)
void PushState(const TranslatableString &desc, const TranslatableString &shortDesc)
static ProjectHistory & Get(AudacityProject &project)
A Track that contains audio waveform data.
Definition: WaveTrack.h:227
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19