Audacity 3.2.0
AudioSegmentFactory.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 AudioSegmentFactory.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
11
12#include "AudioSegmentFactory.h"
13#include "ClipInterface.h"
14#include "ClipSegment.h"
15#include "SilenceSegment.h"
17
18#include <algorithm>
19
20using ClipConstHolder = std::shared_ptr<const ClipInterface>;
21
23 int sampleRate, int numChannels, ClipConstHolders clips)
24 : mClips { std::move(clips) }
25 , mSampleRate { sampleRate }
26 , mNumChannels { numChannels }
27{
28}
29
30std::vector<std::shared_ptr<AudioSegment>>
32 double playbackStartTime, PlaybackDirection direction)
33{
34 return direction == PlaybackDirection::forward ?
35 CreateAudioSegmentSequenceForward(playbackStartTime) :
36 CreateAudioSegmentSequenceBackward(playbackStartTime);
37}
38
39std::vector<std::shared_ptr<AudioSegment>>
41{
42 auto sortedClips = mClips;
43 std::sort(
44 sortedClips.begin(), sortedClips.end(),
45 [](const std::shared_ptr<const ClipInterface>& a,
46 const std::shared_ptr<const ClipInterface>& b) {
47 return a->GetPlayStartTime() < b->GetPlayStartTime();
48 });
49 std::vector<std::shared_ptr<AudioSegment>> segments;
50 for (const auto& clip : sortedClips)
51 {
52 if (clip->GetPlayStartTime() > t0)
53 {
54 const auto numSamples =
55 sampleCount { (clip->GetPlayStartTime() - t0) * mSampleRate + .5 };
56 segments.push_back(
57 std::make_shared<SilenceSegment>(mNumChannels, numSamples));
58 t0 = clip->GetPlayStartTime();
59 }
60 else if (clip->GetPlayEndTime() <= t0)
61 continue;
62 segments.push_back(std::make_shared<ClipSegment>(
63 *clip, t0 - clip->GetPlayStartTime(), PlaybackDirection::forward));
64 t0 = clip->GetPlayEndTime();
65 }
66 return segments;
67}
68
69std::vector<std::shared_ptr<AudioSegment>>
71{
72 auto sortedClips = mClips;
73 std::sort(
74 sortedClips.begin(), sortedClips.end(),
75 [&](
76 const std::shared_ptr<const ClipInterface>& a,
77 const std::shared_ptr<const ClipInterface>& b) {
78 return a->GetPlayEndTime() > b->GetPlayEndTime();
79 });
80 std::vector<std::shared_ptr<AudioSegment>> segments;
81 for (const auto& clip : sortedClips)
82 {
83 if (clip->GetPlayEndTime() < t0)
84 {
85 const auto numSamples =
86 sampleCount { (t0 - clip->GetPlayEndTime()) * mSampleRate + .5 };
87 segments.push_back(
88 std::make_shared<SilenceSegment>(mNumChannels, numSamples));
89 t0 = clip->GetPlayEndTime();
90 }
91 else if (clip->GetPlayStartTime() >= t0)
92 continue;
93 segments.push_back(std::make_shared<ClipSegment>(
94 *clip, clip->GetPlayEndTime() - t0, PlaybackDirection::backward));
95 t0 = clip->GetPlayStartTime();
96 }
97 return segments;
98}
std::shared_ptr< const ClipInterface > ClipConstHolder
std::vector< std::shared_ptr< const ClipInterface > > ClipConstHolders
PlaybackDirection
AudioSegmentFactory(int sampleRate, int numChannels, ClipConstHolders clips)
const ClipConstHolders mClips
std::vector< std::shared_ptr< AudioSegment > > CreateAudioSegmentSequenceBackward(double playbackStartTime)
std::vector< std::shared_ptr< AudioSegment > > CreateAudioSegmentSequenceForward(double playbackStartTime)
std::vector< std::shared_ptr< AudioSegment > > CreateAudioSegmentSequence(double playbackStartTime, PlaybackDirection) override
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
STL namespace.