-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audacity-9999-auto-punch.patch
383 lines (382 loc) · 10.7 KB
/
audacity-9999-auto-punch.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
diff --git a/src/Makefile.am b/src/Makefile.am
index 75c7691b7..e4db14be2 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -352,6 +352,8 @@ audacity_SOURCES = \
effects/Amplify.h \
effects/AutoDuck.cpp \
effects/AutoDuck.h \
+ effects/AutoPunch.cpp \
+ effects/AutoPunch.h \
effects/BassTreble.cpp \
effects/BassTreble.h \
effects/Biquad.cpp \
diff --git a/src/effects/AutoPunch.cpp b/src/effects/AutoPunch.cpp
new file mode 100644
index 000000000..7ca8c0cbb
--- /dev/null
+++ b/src/effects/AutoPunch.cpp
@@ -0,0 +1,280 @@
+// Copyright (C) 2019 Alexander Lopatin
+
+#include "../Audacity.h"
+#include "../LabelTrack.h"
+
+#include <wx/intl.h>
+
+#include "AutoPunch.h"
+
+static const double MAX_NOISE_DURATION = 10.0;
+FloatBuffers EffectAutoPunch::sNoise;
+sampleCount EffectAutoPunch::sNoiseSize = 0;
+
+static const double SOUND_FADE_OUT_DURATION = 0.0125;
+static const double SOUND_FADE_IN_DURATION = SOUND_FADE_OUT_DURATION;
+
+EffectAutoPunch::EffectAutoPunch(Mode mode)
+ : mMode(mode)
+ , mRate(0)
+ , mNoiseIndex(0)
+ , mSilentRegionIndex(0)
+ , mTrackPosition(0)
+{
+}
+
+EffectAutoPunch::~EffectAutoPunch()
+{
+}
+
+ComponentInterfaceSymbol EffectAutoPunch::GetSymbol()
+{
+ switch (mMode) {
+ case Copy:
+ return AUTOPUNCHCOPY_PLUGIN_SYMBOL;
+ case Paste:
+ return AUTOPUNCHPASTE_PLUGIN_SYMBOL;
+ case Apply:
+ return AUTOPUNCHAPPLY_PLUGIN_SYMBOL;
+ default:
+ wxASSERT(false);
+ return AUTOPUNCHAPPLY_PLUGIN_SYMBOL;
+ }
+}
+
+wxString EffectAutoPunch::GetDescription()
+{
+ switch (mMode) {
+ case Copy:
+ return _("Copy background noise");
+ case Paste:
+ return _("Paste noise to selection");
+ case Apply:
+ return _("Paste noise to silent places");
+ default:
+ wxASSERT(false);
+ return _("");
+ }
+}
+
+EffectType EffectAutoPunch::GetType()
+{
+ return EffectTypeProcess;
+}
+
+bool EffectAutoPunch::IsInteractive()
+{
+ return false;
+}
+
+unsigned EffectAutoPunch::GetAudioInCount()
+{
+ return 1;
+}
+
+unsigned EffectAutoPunch::GetAudioOutCount()
+{
+ return 1;
+}
+
+bool EffectAutoPunch::ProcessInitialize(sampleCount WXUNUSED(totalLen), ChannelNames WXUNUSED(chanMap))
+{
+ switch (mMode) {
+ case Copy:
+ return CopyInitialize();
+ case Paste:
+ return PasteInitialize();
+ case Apply:
+ return ApplyInitialize();
+ default:
+ wxASSERT(false);
+ return false;
+ }
+
+ return true;
+}
+
+bool EffectAutoPunch::CopyInitialize()
+{
+ const WaveTrack* noiseTrack = nullptr;
+ int waveTracks = 0;
+ for (auto t : inputTracks()->Any()) {
+ if (t->GetSelected()) {
+ auto wt = track_cast<const WaveTrack *>(t);
+ if (wt != nullptr) {
+ waveTracks++;
+ noiseTrack = wt;
+ }
+ }
+ }
+
+ if (waveTracks != 1) {
+ Effect::MessageBox(_("You need to select exactly one track with background noise."), wxOK | wxICON_ERROR);
+ return false;
+ }
+
+ wxASSERT(noiseTrack);
+
+ const sampleCount maxNoiseSize = noiseTrack->TimeToLongSamples(MAX_NOISE_DURATION);
+ const sampleCount noiseStart = noiseTrack->TimeToLongSamples(mpSelectedRegion->t0());
+ const sampleCount noiseEnd = noiseTrack->TimeToLongSamples(mpSelectedRegion->t1());
+ sNoiseSize = std::min(maxNoiseSize, noiseEnd - noiseStart);
+
+ sNoise.reinit(1U, sNoiseSize.as_size_t());
+ noiseTrack->Get(
+ (samplePtr) sNoise[0].get(),
+ floatSample,
+ noiseTrack->TimeToLongSamples(mpSelectedRegion->t0()),
+ sNoiseSize.as_size_t()
+ );
+
+ return false;
+}
+
+bool EffectAutoPunch::PasteInitialize()
+{
+ if (!sNoise) {
+ Effect::MessageBox(_("You need to copy background noise first."), wxOK | wxICON_ERROR);
+ return false;
+ }
+
+ mTrackPosition = 0;
+ mSilentRegionIndex = 0;
+
+ int waveTracks = 0;
+ for (auto t : inputTracks()->Any()) {
+ if (t->GetSelected()) {
+ auto wt = track_cast<const WaveTrack *>(t);
+ if (wt != nullptr) {
+ waveTracks++;
+ mRate = wt->GetRate();
+ }
+ }
+ }
+
+ if (waveTracks == 0) {
+ Effect::MessageBox(_("You need to select at least one wave track."), wxOK | wxICON_ERROR);
+ return false;
+ }
+
+ mSilentRegions.clear();
+ mSilentRegions.push_back(Region(mpSelectedRegion->t0(), mpSelectedRegion->t1()));
+
+ return true;
+}
+
+bool EffectAutoPunch::ApplyInitialize()
+{
+ if (!sNoise) {
+ Effect::MessageBox(_("You need to copy background noise first."), wxOK | wxICON_ERROR);
+ return false;
+ }
+
+ mTrackPosition = 0;
+ mSilentRegionIndex = 0;
+
+ int waveTracks = 0;
+ int labelTracks = 0;
+ const LabelTrack* labelTrack = nullptr;
+
+ for (auto t : inputTracks()->Any()) {
+ if (t->GetSelected()) {
+ auto wt = track_cast<const WaveTrack *>(t);
+ auto lt = track_cast<const LabelTrack *>(t);
+ if (wt != nullptr) {
+ waveTracks++;
+ mRate = wt->GetRate();
+ }
+ if (lt != nullptr) {
+ labelTracks++;
+ labelTrack = lt;
+ }
+ }
+ }
+
+ if (waveTracks == 0 || labelTracks != 1) {
+ Effect::MessageBox(_("You need to select:\n- at least one wave track\n- exactly one label track. Label track should be generated with \"Analysis - Sound Finder...\""), wxOK | wxICON_ERROR);
+ return false;
+ }
+
+ const int silentRegionsNum = labelTrack->GetNumLabels();
+ if (silentRegionsNum == 0) {
+ Effect::MessageBox(_("No labels found"), wxOK | wxICON_ERROR);
+ return false;
+ }
+
+ mSilentRegions.clear();
+ mSilentRegions.reserve(silentRegionsNum);
+ mSilentRegions.push_back(Region(0.0, labelTrack->GetLabel(0)->getT0()));
+ for (int i = 0; i < silentRegionsNum - 1; i++) {
+ mSilentRegions.push_back(Region(
+ std::max(0.0, labelTrack->GetLabel(i)->getT1()),
+ std::max(0.0, labelTrack->GetLabel(i + 1)->getT0())
+ ));
+ }
+
+ return true;
+}
+
+static double lerp(const float s0, const float s1, const double t, const double t0, const double t1)
+{
+ const double time = t - t0;
+ const double maxTime = t1 - t0;
+ const float maxSignal = s1 - s0;
+ return static_cast<float>(time * maxSignal / maxTime + s0);
+}
+
+float fadeIn(const float signal, const double trackTime, const double start, const double end)
+{
+ return lerp(0.0, signal, trackTime, start, end);
+}
+
+float fadeOut(const float signal, const double trackTime, const double start, const double end)
+{
+ return lerp(signal, 0.0, trackTime, start, end);
+}
+
+size_t EffectAutoPunch::ProcessBlock(float **inBlock, float **outBlock, size_t blockLen)
+{
+ wxASSERT(mMode != Copy);
+ wxASSERT(mRate > 0);
+
+ float *inBuf = inBlock[0];
+ float *outBuf = outBlock[0];
+
+ for (decltype(blockLen) i = 0; i < blockLen; i++)
+ {
+ const double trackTime = mpSelectedRegion->t0() + mTrackPosition.as_double() / mRate;
+ Region silentRegion = mSilentRegions[mSilentRegionIndex];
+ if (trackTime > silentRegion.end && mSilentRegionIndex < mSilentRegions.size() - 1) {
+ mSilentRegionIndex++;
+ silentRegion = mSilentRegions[mSilentRegionIndex];
+ }
+
+ const bool inSilentRegion = trackTime >= silentRegion.start && trackTime <= silentRegion.end;
+ if (inSilentRegion) {
+ float noise = static_cast<float>(sNoise[0][mNoiseIndex]);
+ const double soundRegionEnd = silentRegion.start + SOUND_FADE_OUT_DURATION;
+ const double soundRegionStart = silentRegion.end - SOUND_FADE_IN_DURATION;
+ const bool silenceStarts = trackTime <= soundRegionEnd;
+ const bool silenceEnds = trackTime >= soundRegionStart;
+ if (silenceStarts) {
+ outBuf[i] = fadeOut(inBuf[i], trackTime, silentRegion.start, soundRegionEnd);
+ outBuf[i] += fadeIn(noise, trackTime, silentRegion.start, soundRegionEnd);
+ } else if (silenceEnds) {
+ outBuf[i] = fadeIn(inBuf[i], trackTime, soundRegionStart, silentRegion.end);
+ outBuf[i] += fadeOut(noise, trackTime, soundRegionStart, silentRegion.end);
+ } else {
+ outBuf[i] = noise;
+ }
+
+ mNoiseIndex = (mNoiseIndex + 1) % sNoiseSize.as_size_t();
+ } else {
+ outBuf[i] = inBuf[i];
+ }
+
+ mTrackPosition++;
+ }
+
+ return blockLen;
+}
diff --git a/src/effects/AutoPunch.h b/src/effects/AutoPunch.h
new file mode 100644
index 000000000..5b55f0831
--- /dev/null
+++ b/src/effects/AutoPunch.h
@@ -0,0 +1,56 @@
+// Copyright (C) 2019 Alexander Lopatin
+
+#ifndef __AUDACITY_EFFECT_AUTOPUNCH__
+#define __AUDACITY_EFFECT_AUTOPUNCH__
+
+#include <wx/string.h>
+
+#include "Effect.h"
+#include "../WaveTrack.h"
+
+#define AUTOPUNCHCOPY_PLUGIN_SYMBOL ComponentInterfaceSymbol{ XO("AutoPunch Copy") }
+#define AUTOPUNCHPASTE_PLUGIN_SYMBOL ComponentInterfaceSymbol{ XO("AutoPunch Paste") }
+#define AUTOPUNCHAPPLY_PLUGIN_SYMBOL ComponentInterfaceSymbol{ XO("AutoPunch Apply") }
+
+class EffectAutoPunch final : public Effect
+{
+public:
+ enum Mode {
+ Copy,
+ Paste,
+ Apply
+ };
+
+ EffectAutoPunch(Mode mode);
+ virtual ~EffectAutoPunch();
+
+ ComponentInterfaceSymbol GetSymbol() override;
+ wxString GetDescription() override;
+
+ EffectType GetType() override;
+ bool IsInteractive() override;
+
+ unsigned GetAudioInCount() override;
+ unsigned GetAudioOutCount() override;
+ bool ProcessInitialize(sampleCount totalLen, ChannelNames chanMap = NULL) override;
+ size_t ProcessBlock(float **inBlock, float **outBlock, size_t blockLen) override;
+
+private:
+ bool CopyInitialize();
+ bool PasteInitialize();
+ bool ApplyInitialize();
+
+ static FloatBuffers sNoise;
+ static sampleCount sNoiseSize;
+
+ Mode mMode;
+ double mRate;
+ size_t mNoiseIndex;
+
+ std::vector<Region> mSilentRegions;
+ size_t mSilentRegionIndex;
+
+ sampleCount mTrackPosition;
+};
+
+#endif
diff --git a/src/effects/LoadEffects.cpp b/src/effects/LoadEffects.cpp
index 2a5ab3677..89420724d 100644
--- a/src/effects/LoadEffects.cpp
+++ b/src/effects/LoadEffects.cpp
@@ -21,6 +21,7 @@
#include "Amplify.h"
#include "AutoDuck.h"
+#include "AutoPunch.h"
#include "BassTreble.h"
#include "ChangeSpeed.h"
#include "ClickRemoval.h"
@@ -114,6 +115,9 @@
EFFECT( SILENCE, EffectSilence, () ) \
EFFECT( TONE, EffectToneGen, (false) ) \
EFFECT( AMPLIFY, EffectAmplify, () ) \
+ EFFECT( AUTOPUNCHCOPY, EffectAutoPunch, (EffectAutoPunch::Mode::Copy) ) \
+ EFFECT( AUTOPUNCHPASTE, EffectAutoPunch, (EffectAutoPunch::Mode::Paste) ) \
+ EFFECT( AUTOPUNCHAPPLY, EffectAutoPunch, (EffectAutoPunch::Mode::Apply) ) \
EFFECT( BASSTREBLE, EffectBassTreble, () ) \
EFFECT( CHANGESPEED, EffectChangeSpeed, () ) \
EFFECT( CLICKREMOVAL, EffectClickRemoval, () ) \