Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
voice_sample.h
1/*
2 * Copyright © 2015-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
23enum class LateStartAttemptStatus {
24 SUCCESS = 0,
25 FAILURE = 1,
26 WAIT = 2,
27};
28class TimeStretcher;
29class SampleCache;
30class Sample;
31class Voice;
33class Source;
34class SampleControls;
35
36class VoiceSample final : public SampleLowLevelReader {
37public:
38 VoiceSample() = default;
39 explicit VoiceSample(VoiceSample& other) = default;
40 // explicit: an implicit conversion here is a footgun. `*voiceSample = SampleLowLevelReader(...)` would silently
41 // build a temporary VoiceSample (with default-null timeStretcher/cache) and memberwise-move it over the live one,
42 // nulling timeStretcher mid-flight and orphaning it (leaking its reasons -> E078, see TimeStretcher::hopEnd).
43 // Assign through the SampleLowLevelReader base subobject instead when you only mean to replace the reader state.
44 explicit VoiceSample(SampleLowLevelReader&& other) : SampleLowLevelReader{std::move(other)} {}
45 ~VoiceSample() override {
46 endTimeStretching();
47 freeLoopIncoming();
48 }
49 VoiceSample& operator=(const VoiceSample& other) = delete;
50 // Deleted: the defaulted memberwise move-assign would overwrite timeStretcher/cache without endTimeStretching(),
51 // leaking whatever the destination currently owns. VoiceSamples are pool-managed and referenced by pointer, so no
52 // object-level move-assign is needed; if one ever is, give it a body that releases resources first.
53 VoiceSample& operator=(VoiceSample&& other) = delete;
54
55 void noteOn(SamplePlaybackGuide* guide, uint32_t samplesLate, int32_t priorityRating);
56 bool noteOffWhenLoopEndPointExists(Voice* voice, VoiceSamplePlaybackGuide* voiceSource);
57
58 void setupCacheLoopPoints(SamplePlaybackGuide* voiceSource, Sample* sample, LoopType loopingType);
59 LateStartAttemptStatus attemptLateSampleStart(SamplePlaybackGuide* voiceSource, Sample* sample,
60 int64_t rawSamplesLate, int32_t priorityRating,
61 int32_t numSamples = 0);
62 void endTimeStretching();
63 bool render(SamplePlaybackGuide* guide, int32_t* oscBuffer, int32_t numSamples, Sample* sample, int32_t numChannels,
64 LoopType loopingType, int32_t phaseIncrement, int32_t timeStretchRatio, int32_t amplitude,
65 int32_t amplitudeIncrement, int32_t bufferSize, InterpolationMode desiredInterpolationMode,
66 int32_t priorityRating, int32_t loopCrossfadeSamples = 0);
67 void beenUnassigned(bool wontBeUsedAgain);
68
69 // AudioClips don't obey markers because they "fudge" instead.
70 // Or if fudging can't happen cos no pre-margin, then
71 // AudioClip::doTickForward() manually forces restart.
72 [[nodiscard]] bool shouldObeyMarkers() const override {
73 return (cache == nullptr && timeStretcher == nullptr && !forAudioClip);
74 }
75
76 void readSamplesResampledPossiblyCaching(int32_t** oscBufferPos, int32_t** oscBufferRPos, int32_t numSamples,
77 Sample* sample, int32_t jumpAmount, int32_t numChannels,
78 int32_t numChannelsAfterCondensing, int32_t phaseIncrement,
79 int32_t* sourceAmplitudeNow, int32_t amplitudeIncrement,
80 int32_t bufferSize, int32_t reduceMagnitudeBy = 1);
81
82 bool sampleZoneChanged(SamplePlaybackGuide* voiceSource, Sample* sample, bool reversed, MarkerType markerType,
83 LoopType loopingType, int32_t priorityRating, bool forAudioClip = false);
84 int32_t getPlaySample(Sample* sample, SamplePlaybackGuide* guide);
85 int32_t getLoopCrossfadeIncomingPlaySample(Sample* sample, SamplePlaybackGuide* guide);
86 bool stopUsingCache(SamplePlaybackGuide* guide, Sample* sample, int32_t priorityRating, bool loopingAtLowLevel);
87 bool possiblySetUpCache(SampleControls* sampleControls, SamplePlaybackGuide* guide, int32_t phaseIncrement,
88 int32_t timeStretchRatio, int32_t priorityRating, LoopType loopingType);
89 bool fudgeTimeStretchingToAvoidClick(Sample* sample, SamplePlaybackGuide* guide, int32_t phaseIncrement,
90 int32_t numSamplesTilLoop, int32_t playDirection, int32_t priorityRating);
91
92 uint32_t pendingSamplesLate = 0; // This isn't used for AudioClips. And for samples in STRETCH mode, the exact
93 // number isn't relevant - it gets recalculated
94 TimeStretcher* timeStretcher = nullptr;
95
96 SampleCache* cache = nullptr;
97 bool doneFirstRenderYet = false;
98 bool fudging = false;
99 bool forAudioClip = false; // This is a wee bit of a hack - but we need to be able to know this
100 bool writingToCache = false; // Value is only valid if cache assigned
101
102private:
103 // Out of line so it doesn't bloat render() and push its hot path out of the instruction cache
104 [[gnu::noinline]] void considerStartingLoopCrossfade(SamplePlaybackGuide* guide, Sample* sample,
105 int32_t wantedLength, int32_t priorityRating);
106 [[nodiscard]] bool isLoopCrossfading() const { return loopCrossfadeLength != 0; }
107 bool finishLoopCrossfade(SamplePlaybackGuide* guide, Sample* sample, LoopType loopingType, int32_t priorityRating);
108 void cancelLoopCrossfade();
109 void freeLoopIncoming();
110 bool readLoopCrossfadeIncoming(SamplePlaybackGuide* guide, Sample* sample, int32_t* outputBuffer,
111 int32_t numSamples, int32_t numChannels, int32_t numChannelsInOutputBuffer,
112 int32_t phaseIncrement, int32_t amplitude, int32_t amplitudeIncrement,
113 int32_t interpolationBufferSize, int32_t whichKernel, int32_t priorityRating);
114
116 int32_t outgoingAmplitude;
117 int32_t outgoingAmplitudeIncrement;
118 int32_t incomingAmplitude;
119 int32_t incomingAmplitudeIncrement;
120 bool done; // The crossfade finishes within this window
121 };
122 // These are out of line so they don't bloat render() and push its hot path out of the instruction cache
123 [[gnu::noinline]] LoopCrossfadeGains getLoopCrossfadeGains(SamplePlaybackGuide* guide, Sample* sample,
124 int32_t numSamples, int32_t phaseIncrement,
125 int32_t amplitude, int32_t amplitudeIncrement);
126 [[gnu::noinline]] bool mixInLoopCrossfadeIncoming(SamplePlaybackGuide* guide, Sample* sample, int32_t* outputBuffer,
127 int32_t numSamples, int32_t numChannels,
128 int32_t numChannelsInOutputBuffer, int32_t phaseIncrement,
129 const LoopCrossfadeGains& gains, int32_t interpolationBufferSize,
130 int32_t whichKernel, LoopType loopingType,
131 int32_t priorityRating);
132
133 // Loop crossfade: the next pass through the loop, started early so it can fade in while we fade out. Taken from
134 // AudioEngine's LoopCrossfadeReaderPool when the note first crossfades, and given back when the voice ends
135 SampleLowLevelReader* loopIncoming = nullptr;
136 int32_t loopCrossfadeLength = 0; // In source samples, for the crossfade in progress. 0 when not crossfading
137
138 bool weShouldBeTimeStretchingNow(Sample* sample, SamplePlaybackGuide* guide, int32_t numSamples,
139 int32_t phaseIncrement, int32_t timeStretchRatio, int32_t playDirection,
140 int32_t priorityRating, LoopType loopingType);
141 void switchToReadingCacheFromWriting();
142 bool stopReadingFromCache();
143
144 int32_t cacheBytePos = 0;
145 uint32_t cacheLoopLengthBytes = 0;
146 int32_t cacheLoopEndPointBytes = 0; // 2147483647 means no looping. Will be set to sample end-point if looping
147 // there. Gets re-set to 2147483647 when note "released"
148 int32_t cacheEndPointBytes = 0; // Will sometimes be the whole length of the sample. Wherever the red marker is. Or
149 // a little further if it's the full length of the sample, to allow for timestretch
150 // / interpolation ring-out
151};
Definition sample_cache.h:25
Definition sample_controls.h:22
Definition sample_playback_guide.h:28
Definition sample.h:50
Definition source.h:31
Definition time_stretcher.h:37
Definition voice_sample_playback_guide.h:28
Definition voice.h:36
Definition voice_sample.h:115