Audacity 3.2.0
SampleTrackCache.h
Go to the documentation of this file.
1/**********************************************************************
2
3Audacity: A Digital Audio Editor
4
5SampleTrackCache.h
6@brief Buffer results to avoid repeated calls to WideSampleSequence::Get()
7
8Paul Licameli split from WaveTrack.h
9
10**********************************************************************/
11
12#ifndef __AUDACITY_SAMPLE_TRACK_CACHE__
13#define __AUDACITY_SAMPLE_TRACK_CACHE__
14
15#include "SampleCount.h"
16#include "SampleFormat.h"
17#include <memory>
18
20
22
24class MIXER_API SampleTrackCache {
25public:
27 : mBufferSize(0)
28 , mNValidBuffers(0)
29 {
30 }
31
33 const std::shared_ptr<const WideSampleSequence> &pSequence
34 ) : mBufferSize(0)
35 , mNValidBuffers(0)
36 {
37 SetSequence(pSequence);
38 }
40
41 const std::shared_ptr<const WideSampleSequence>& GetSequence() const {
42 return mpSequence;
43 }
44 void SetSequence(
45 const std::shared_ptr<const WideSampleSequence> &pSequence);
46
48
55 std::pair<const float *, const float *>
56 GetFloatsWide(
57 size_t nChannels, sampleCount start, size_t len, bool mayThrow);
58
60 const float *GetFloats(sampleCount start, size_t len, bool mayThrow)
61 {
62 return GetFloatsWide(1, start, len, mayThrow).first;
63 }
64
65private:
66 void Free();
67
68 struct Buffer {
72
73 Buffer() : start(0), len(0) {}
74 void Free() { areas.reset(); start = 0; len = 0; }
75 sampleCount end() const { return start + len; }
76
77 void swap ( Buffer &other )
78 {
79 areas .swap ( other.areas );
80 std::swap( start, other.start );
81 std::swap( len, other.len );
82 }
83
84 auto GetBuffers() const {
85 return reinterpret_cast<float **>(areas.get()); }
86
87 std::pair<const float*, const float *>
88 GetResults(size_t nChannels, size_t offset) const {
89 const auto buffers = GetBuffers();
90 const auto buffer0 = buffers[0],
91 buffer1 = buffers[1];
92 return {
93 (buffer0 ? buffer0 + offset : nullptr),
94 (nChannels > 1 && buffer1 ? buffer1 + offset : nullptr)
95 };
96 }
97 };
98
99 std::shared_ptr<const WideSampleSequence> mpSequence;
101 Buffer mBuffers[2];
102 GrowableSampleBuffer mOverlapBuffers[2];
104};
105
106#endif
A short-lived object, during whose lifetime, the contents of the WaveTrack are assumed not to change.
SampleTrackCache(const std::shared_ptr< const WideSampleSequence > &pSequence)
const float * GetFloats(sampleCount start, size_t len, bool mayThrow)
fetch first channel only
const std::shared_ptr< const WideSampleSequence > & GetSequence() const
std::shared_ptr< const WideSampleSequence > mpSequence
Positions or offsets within audio files need a wide type.
Definition: SampleCount.h:19
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:752
void swap(Buffer &other)
sampleCount end() const
std::pair< const float *, const float * > GetResults(size_t nChannels, size_t offset) const