Audacity 3.2.0
WaveClipUtilities.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 WaveClipUtilities.cpp
7
8 Paul Licameli split from WaveClip.cpp
9
10**********************************************************************/
11#include "WaveClipUtilities.h"
12#include "WaveClip.h"
13#include <cmath>
14
16 double t, size_t iChannel, float& value, bool mayThrow)
17{
18 if (!clip.WithinPlayRegion(t))
19 return false;
20 const auto start = clip.TimeToSamples(t);
21 return clip.GetSamples(
22 iChannel, reinterpret_cast<samplePtr>(&value), floatSample, start, 1u,
23 mayThrow);
24}
25
27 double t, size_t iChannel, const float* buffer, size_t numFloats,
28 sampleFormat effectiveFormat)
29{
30 const auto maybeNegativeStart = clip.TimeToSamples(t);
31 const auto maybeOutOfBoundEnd = maybeNegativeStart + numFloats;
32 const auto effectiveStart = std::max(sampleCount { 0 }, maybeNegativeStart);
33 const auto effectiveEnd =
34 std::min(clip.GetVisibleSampleCount(), maybeOutOfBoundEnd);
35 if (effectiveStart >= effectiveEnd)
36 return;
37 // Cannot be greater than `numFloats` -> safe cast
38 const auto effectiveLen = (effectiveEnd - effectiveStart).as_size_t();
39 // Cannot be greater than `numFloats` -> safe cast
40 const auto numLeadingZeros =
41 (effectiveStart - maybeNegativeStart).as_size_t();
42 const auto offsetBuffer =
43 reinterpret_cast<const char*>(buffer + numLeadingZeros);
44 clip.SetSamples(
45 iChannel, offsetBuffer, floatSample, effectiveStart, effectiveLen,
46 effectiveFormat);
47}
48
50 const WaveTrack::Interval &prev, const WaveTrack::Interval& next)
51{
52 double endThis = prev.GetRate() * prev.GetPlayStartTime() +
54 double startNext = next.GetRate() * next.GetPlayStartTime();
55
56 // given that a double has about 15 significant digits, using a criterion
57 // of half a sample should be safe in all normal usage.
58 return fabs(startNext - endThis) < 0.5;
59}
int min(int a, int b)
constexpr sampleFormat floatSample
Definition: SampleFormat.h:45
sampleFormat
The ordering of these values with operator < agrees with the order of increasing bit width.
Definition: SampleFormat.h:30
char * samplePtr
Definition: SampleFormat.h:57
This allows multiple clips to be a part of one WaveTrack.
Definition: WaveClip.h:238
bool WithinPlayRegion(double t) const
t ∈ [...)
Definition: WaveClip.cpp:1903
double GetStretchRatio() const override
Definition: WaveClip.cpp:625
sampleCount TimeToSamples(double time) const override
Definition: WaveClip.cpp:1725
sampleCount GetVisibleSampleCount() const override
Definition: WaveClip.cpp:1798
double GetPlayStartTime() const noexcept override
Definition: WaveClip.cpp:1756
int GetRate() const override
Definition: WaveClip.h:337
void SetSamples(size_t ii, constSamplePtr buffer, sampleFormat format, sampleCount start, size_t len, sampleFormat effectiveFormat)
Definition: WaveClip.cpp:397
bool GetSamples(size_t ii, samplePtr buffer, sampleFormat format, sampleCount start, size_t len, bool mayThrow=true) const
Get samples from one channel.
Definition: WaveClip.cpp:378
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
double as_double() const
Definition: SampleCount.h:46
WAVE_TRACK_API void SetFloatsFromTime(WaveClip &clip, double t, size_t iChannel, const float *buffer, size_t numSamples, sampleFormat effectiveFormat)
Considers buffer as audio starting at TimeToSamples(t) (relative to clip play start time) and with eq...
WAVE_TRACK_API bool SharesBoundaryWithNextClip(const WaveTrack::Interval &prev, const WaveTrack::Interval &next)
used by commands which interact with clips using the keyboard
WAVE_TRACK_API bool GetFloatAtTime(const WaveClip &clip, double t, size_t iChannel, float &value, bool mayThrow)