Audacity 3.2.0
AudioSegmentSampleView.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 AudioSegmentSampleView.cpp
7
8 Matthieu Hodgkinson
9
10**********************************************************************/
12
13#include <algorithm>
14#include <cassert>
15#include <numeric>
16
18 std::vector<BlockSampleView> blockViews, size_t start, size_t length)
19 : mBlockViews { std::move(blockViews) }
20 , mStart { start }
21 , mLength { length }
22 , mIsSilent { false }
23{
24 assert(
25 start + length <=
26 std::accumulate(
27 mBlockViews.begin(), mBlockViews.end(), 0u,
28 [](size_t acc, const auto& block) { return acc + block->size(); }));
29}
30
32 : mLength { length }
33 , mIsSilent { true }
34{
35}
36
37void AudioSegmentSampleView::Copy(float* buffer, size_t bufferSize) const
38{
39 mIsSilent ? std::fill(buffer, buffer + bufferSize, 0.f) :
40 DoCopy(buffer, bufferSize);
41}
42
43void AudioSegmentSampleView::AddTo(float* buffer, size_t bufferSize) const
44{
45 if (mIsSilent)
46 return;
47 DoAdd(buffer, bufferSize);
48}
49
51{
52 return mLength;
53}
54
55void AudioSegmentSampleView::DoCopy(float* buffer, size_t bufferSize) const
56{
57 std::fill(buffer, buffer + bufferSize, 0.f);
58 DoAdd(buffer, bufferSize);
59}
60
61void AudioSegmentSampleView::DoAdd(float* buffer, size_t bufferSize) const
62{
63 size_t toWrite { limitSampleBufferSize(bufferSize, mLength) };
64 size_t written = 0u;
65 size_t offset = mStart;
66 for (const auto& block : mBlockViews)
67 {
68 const auto toWriteFromBlock = std::min(block->size() - offset, toWrite);
69 const auto src = block->data() + offset;
70 const auto dst = buffer + written;
71 std::transform(src, src + toWriteFromBlock, dst, dst, std::plus {});
72 toWrite -= toWriteFromBlock;
73 written += toWriteFromBlock;
74 offset = 0;
75 }
76}
An audio segment is either a whole clip or the silence between clips. Views allow shared references t...
int min(int a, int b)
size_t limitSampleBufferSize(size_t bufferSize, sampleCount limit)
Definition: SampleCount.cpp:22
const std::vector< BlockSampleView > mBlockViews
size_t GetSampleCount() const
The number of samples in this view.
void Copy(float *buffer, size_t bufferSize) const
Copies up to GetSampleCount() or bufferSize samples, whichever is less, into buffer....
AudioSegmentSampleView(std::vector< BlockSampleView > blockViews, size_t start, size_t length)
Constructs an AudioSegmentSampleView from BlockSampleViews.
void DoCopy(float *buffer, size_t bufferSize) const
void AddTo(float *buffer, size_t bufferSize) const
Adds up to GetSampleCount() or bufferSize samples, whichever is less, into buffer....
void DoAdd(float *buffer, size_t bufferSize) const
STL namespace.