-
Notifications
You must be signed in to change notification settings - Fork 11
/
granular_processor.h
257 lines (204 loc) · 6.67 KB
/
granular_processor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2014 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
// -----------------------------------------------------------------------------
//
// Main processing class.
#ifndef CLOUDS_DSP_GRANULAR_PROCESSOR_H_
#define CLOUDS_DSP_GRANULAR_PROCESSOR_H_
#include "stmlib/stmlib.h"
#include "stmlib/dsp/filter.h"
#include "supercell/dsp/correlator.h"
#include "supercell/dsp/frame.h"
#include "supercell/dsp/fx/diffuser.h"
#include "supercell/dsp/fx/pitch_shifter.h"
#include "supercell/dsp/fx/reverb.h"
#include "supercell/dsp/resonestor.h"
#include "supercell/dsp/fx/oliverb.h"
#include "supercell/dsp/granular_processor.h"
#include "supercell/dsp/granular_sample_player.h"
#include "supercell/dsp/kammerl_player.h"
#include "supercell/dsp/looping_sample_player.h"
#include "supercell/dsp/pvoc/phase_vocoder.h"
#include "supercell/dsp/sample_rate_converter.h"
#include "supercell/dsp/wsola_sample_player.h"
namespace clouds {
const int32_t kDownsamplingFactor = 2;
enum PlaybackMode {
PLAYBACK_MODE_GRANULAR,
PLAYBACK_MODE_STRETCH,
PLAYBACK_MODE_LOOPING_DELAY,
PLAYBACK_MODE_SPECTRAL,
PLAYBACK_MODE_OLIVERB,
PLAYBACK_MODE_RESONESTOR,
PLAYBACK_MODE_KAMMERL,
PLAYBACK_MODE_SPECTRAL_CLOUD,
PLAYBACK_MODE_LAST
};
// State of the recording buffer as saved in one of the 4 sample memories.
struct PersistentState {
int32_t write_head[2];
uint8_t quality;
uint8_t spectral;
};
// Data block as saved in one of the 4 sample memories.
struct PersistentBlock {
uint32_t tag;
uint32_t size;
void* data;
};
class GranularProcessor {
public:
GranularProcessor() { }
~GranularProcessor() { }
void Init(
void* large_buffer,
size_t large_buffer_size,
void* small_buffer,
size_t small_buffer_size);
void Process(ShortFrame* input, ShortFrame* output, size_t size);
void Prepare();
inline Parameters* mutable_parameters() {
return ¶meters_;
}
inline const Parameters& parameters() const {
return parameters_;
}
inline void ToggleFreeze() {
parameters_.freeze = !parameters_.freeze;
}
inline void set_freeze(bool freeze) {
parameters_.freeze = freeze;
}
inline bool frozen() const {
return parameters_.freeze;
}
inline void ToggleReverse() {
parameters_.granular.reverse = !parameters_.granular.reverse;
}
inline bool reversed() const {
return parameters_.granular.reverse;
}
inline void set_silence(bool silence) {
silence_ = silence;
}
inline void set_bypass(bool bypass) {
bypass_ = bypass;
}
inline bool bypass() const {
return bypass_;
}
inline void set_mute_out(bool mute) {
mute_out_ = mute;
}
inline bool mute_out() const {
return mute_out_;
}
inline void set_mute_in(bool mute) {
mute_in_ = mute;
}
inline bool mute_in() const {
return mute_in_;
}
inline void set_playback_mode(PlaybackMode playback_mode) {
playback_mode_ = playback_mode;
}
inline PlaybackMode playback_mode() const { return playback_mode_; }
inline void set_quality(int32_t quality) {
set_num_channels(quality & 1 ? 1 : 2);
set_low_fidelity(quality >> 1 ? true : false);
}
inline void set_num_channels(int32_t num_channels) {
reset_buffers_ = reset_buffers_ || num_channels_ != num_channels;
num_channels_ = num_channels;
}
inline void set_low_fidelity(bool low_fidelity) {
reset_buffers_ = reset_buffers_ || low_fidelity != low_fidelity_;
low_fidelity_ = low_fidelity;
}
inline int32_t quality() const {
int32_t quality = 0;
if (num_channels_ == 1) quality |= 1;
if (low_fidelity_) quality |= 2;
return quality;
}
void GetPersistentData(PersistentBlock* block, size_t *num_blocks);
bool LoadPersistentData(const uint32_t* data);
void PreparePersistentData();
private:
void WarmDistortion(float* in, float parameter);
inline int32_t resolution() const {
return low_fidelity_ ? 8 : 16;
}
inline float sample_rate() const {
return 32000.0f / \
(low_fidelity_ ? kDownsamplingFactor : 1);
}
void ResetFilters();
void ProcessGranular(FloatFrame* input, FloatFrame* output, size_t size);
PlaybackMode playback_mode_;
PlaybackMode previous_playback_mode_;
int32_t num_channels_;
bool low_fidelity_;
bool silence_;
bool bypass_;
bool reset_buffers_;
bool mute_in_;
bool mute_out_;
float mute_in_fade_;
float mute_out_fade_;
float freeze_lp_;
float dry_wet_;
void* buffer_[2];
size_t buffer_size_[2];
Correlator correlator_;
GranularSamplePlayer player_;
WSOLASamplePlayer ws_player_;
LoopingSamplePlayer looper_;
PhaseVocoder phase_vocoder_;
KammerlPlayer kammerl_;
Diffuser diffuser_;
Reverb reverb_;
Oliverb oliverb_;
Resonestor resonestor_;
PitchShifter pitch_shifter_;
stmlib::Svf fb_filter_[2];
stmlib::Svf hp_filter_[2];
stmlib::Svf lp_filter_[2];
AudioBuffer<RESOLUTION_8_BIT_MU_LAW> buffer_8_[2];
AudioBuffer<RESOLUTION_16_BIT> buffer_16_[2];
FloatFrame in_[kMaxBlockSize];
FloatFrame in_downsampled_[kMaxBlockSize / kDownsamplingFactor];
FloatFrame out_downsampled_[kMaxBlockSize / kDownsamplingFactor];
FloatFrame out_[kMaxBlockSize];
FloatFrame fb_[kMaxBlockSize];
int16_t tail_buffer_[2][256];
Parameters parameters_;
SampleRateConverter<-kDownsamplingFactor, 45, src_filter_1x_2_45> src_down_;
SampleRateConverter<+kDownsamplingFactor, 45, src_filter_1x_2_45> src_up_;
PersistentState persistent_state_;
DISALLOW_COPY_AND_ASSIGN(GranularProcessor);
};
} // namespace clouds
#endif // CLOUDS_DSP_GRANULAR_PROCESSOR_H_