Deluge Firmware 1.3.0
Build date: 2026.11.22
Loading...
Searching...
No Matches
time_stretcher.h
1/*
2 * Copyright © 2016-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "definitions_cxx.hpp"
21#include "model/sample/sample_low_level_reader.h"
22#include <cstdint>
23
24#define BUFFER_FILLING_OFF 0
25#define BUFFER_FILLING_NEWER 1
26#define BUFFER_FILLING_OLDER 2
27#define BUFFER_FILLING_NEITHER 3
28
29class VoiceSample;
30class Voice;
33class Cluster;
34class Sample;
35class SampleCache;
36
37class [[gnu::hot]] TimeStretcher {
38public:
39 TimeStretcher() = default;
40 bool init(Sample* sample, VoiceSample* voiceSample, SamplePlaybackGuide* guide, int64_t newSamplePosBig,
41 int32_t numChannels, int32_t phaseIncrement, int32_t timeStretchRatio, int32_t playDirection,
42 int32_t priorityRating, int32_t fudgingNumSamplesTilLoop, LoopType loopingType);
43 void reInit(int64_t newSamplePosBig, SamplePlaybackGuide* guide, VoiceSample* voiceSample, Sample* sample,
44 int32_t numChannels, int32_t timeStretchRatio, int32_t phaseIncrement, uint64_t combinedIncrement,
45 int32_t playDirection, LoopType loopingType, int32_t priorityRating);
46 void beenUnassigned();
47 void unassignAllReasonsForPercLookahead();
48 void unassignAllReasonsForPercCacheClusters();
49 void unassignAllReasonsForHopBack();
50 bool hopEnd(SamplePlaybackGuide* guide, VoiceSample* voiceSample, Sample* sample, int32_t numChannels,
51 int32_t timeStretchRatio, int32_t phaseIncrement, uint64_t combinedIncrement, int32_t playDirection,
52 LoopType loopingType, int32_t priorityRating);
53
54 void rememberPercCacheCluster(Cluster* cluster);
55 void updateClustersForPercLookahead(Sample* sample, uint32_t sourceBytePos, int32_t playDirection,
56 int32_t priorityRating);
57
58 int32_t getSamplePos(int32_t playDirection);
59 bool allocateBuffer(int32_t numChannels);
60
61 void readFromBuffer(int32_t* oscBufferPos, int32_t numSamples, int32_t numChannels,
62 int32_t numChannelsAfterCondensing, int32_t sourceAmplitudeNow, int32_t amplitudeIncrementNow,
63 int32_t* bufferReadPos);
64
65 void setupCrossfadeFromCache(SampleCache* cache, int32_t cacheBytePos, int32_t numChannels);
66
67#if TIME_STRETCH_ENABLE_BUFFER
68 void reassessWhetherToBeFillingBuffer(int32_t phaseIncrement, int32_t timeStretchRatio,
69 int32_t newBufferFillingMode, int32_t numChannels);
70#endif
71
72 int64_t samplePosBig; // In whole samples including both channels. From audioDataStart. <<'d by 24
73
74 uint32_t crossfadeProgress; // Out of kMaxSampleValue
75 uint32_t crossfadeIncrement;
76
77 int32_t samplesTilHopEnd;
78
79 SampleLowLevelReader olderPartReader;
80
81 int32_t* buffer;
82 bool olderHeadReadingFromBuffer;
83 bool hasLoopedBackIntoPreMargin;
84 bool playHeadStillActive[2];
85 uint8_t numTimesMissedHop;
86
87 int32_t olderBufferReadPos; // In whole samples including both channels
88
89#if TIME_STRETCH_ENABLE_BUFFER
90 bool newerHeadReadingFromBuffer;
91 int32_t newerBufferReadPos; // In whole samples including both channels
92
93 uint8_t bufferFillingMode;
94 int32_t bufferWritePos; // In whole samples including both channels
95 uint64_t bufferSamplesWritten; // Hopefully we can do away with the need for this
96#endif
97
98 Cluster* clustersForPercLookahead[kNumClustersLoadedAhead];
99
100 Cluster* percCacheClustersNearby[2]; // Remembers and acts as a "reason" for the two most recently needed / accessed
101 // Clusters, basically
102
103 // Reasons for the earliest Clusters of audio data the next hop could start the new play-head in, so they're
104 // enqueued for loading well before the hop. Otherwise, nothing would load audio before the start marker, which
105 // a hop can reach back into when time is slowed down a lot, e.g. when pitched up with the length kept.
106 Cluster* clustersForHopBack[kNumClustersLoadedAhead];
107
108 // A reason for the Cluster a failed hop wanted to start in, so it stays in the loading queue until we try again
109 Cluster* clusterForHopRetry;
110
111 // Where the last call to Sample::fillPercCache() started, and where it left the perc cache zone ending, so we can
112 // skip calling it (and its walk through the Sample's metadata, which lives in SDRAM) until the lookahead has moved
113 // on a bit
114 int32_t percCacheFillStartPos;
115 int32_t percCacheFillEndPos;
116 int8_t percCacheFillDirection;
117
118private:
119 bool setupNewPlayHead(Sample* sample, VoiceSample* voiceSample, SamplePlaybackGuide* guide, int32_t newHeadBytePos,
120 int32_t additionalOscPos, int32_t priorityRating, LoopType loopingType);
121 void updateClustersForHopBack(Sample* sample, int64_t samplePos, int32_t timeStretchRatio, int32_t phaseIncrement,
122 int32_t playDirection, int32_t priorityRating);
123 void keepClusterForHopRetry(Cluster* cluster);
124};
125
126inline int32_t getTotalDifferenceAbs(int32_t* totals1, int32_t* totals2) {
127 int32_t totalDifferenceAbs = 0;
128 for (int32_t i = 0; i < TimeStretch::Crossfade::kNumMovingAverages; i++) {
129 int32_t differenceAbsHere = totals2[i] - totals1[i];
130 if (differenceAbsHere < 0)
131 differenceAbsHere = -differenceAbsHere;
132 totalDifferenceAbs += differenceAbsHere;
133 }
134 return totalDifferenceAbs;
135}
136
137inline int32_t getTotalChange(int32_t* totals1, int32_t* totals2) {
138 int32_t totalChange = 0;
139 for (int32_t i = 0; i < TimeStretch::Crossfade::kNumMovingAverages; i++) {
140 totalChange += totals2[i];
141 }
142
143 for (int32_t i = 0; i < TimeStretch::Crossfade::kNumMovingAverages; i++) {
144 totalChange -= totals1[i];
145 }
146 return totalChange;
147}
Definition cluster.h:34
Definition sample_cache.h:25
Definition sample_low_level_reader.h:36
Definition sample_playback_guide.h:28
Definition sample.h:50
bool setupNewPlayHead(Sample *sample, VoiceSample *voiceSample, SamplePlaybackGuide *guide, int32_t newHeadBytePos, int32_t additionalOscPos, int32_t priorityRating, LoopType loopingType)
Definition time_stretcher.cpp:1163
Definition voice_sample_playback_guide.h:28
Definition voice_sample.h:36
Definition voice_unison_part_source.h:33
Definition voice.h:35