Arduino STK 4.6.2
Granulate.h
1#ifndef STK_GRANULATE_H
2#define STK_GRANULATE_H
3
4#include <vector>
5#include "Generator.h"
6#include "Envelope.h"
7#include "Noise.h"
8#include "MemoryFS.h"
9
10namespace stk {
11
12/***************************************************/
27/***************************************************/
28
29class Granulate: public Generator
30{
31 public:
33 Granulate( void );
34
36 Granulate( unsigned int nVoices, std::string fileName, bool typeRaw = false );
37
38#ifdef __RAW_ARRAYS__
39 Granulate( unsigned int nVoices, MemoryFS &memoryFile);
40#endif
41
43 ~Granulate( void );
44
45#ifdef __RAW_ARRAYS__
46 void openMemory(MemoryFS &memoryFile);
47#endif
49
53 void openFile( std::string fileName, bool typeRaw = false );
54
56
60 void reset( void );
61
63
68 void setVoices( unsigned int nVoices = 1 );
69
71
77 void setStretch( unsigned int stretchFactor = 1 );
78
80
95 void setGrainParameters( unsigned int duration = 30, unsigned int rampPercent = 50,
96 int offset = 0, unsigned int delay = 0 );
97
99
107 void setRandomFactor( StkFloat randomness = 0.1 );
108
110
118 StkFloat lastOut( unsigned int channel = 0 );
119
121 StkFloat tick( unsigned int channel = 0 );
122
124
131 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
132
133 enum GrainState {
134 GRAIN_STOPPED,
135 GRAIN_FADEIN,
136 GRAIN_SUSTAIN,
137 GRAIN_FADEOUT
138 };
139
140 protected:
141
142 struct Grain {
143 StkFloat eScaler;
144 StkFloat eRate;
145 unsigned long attackCount;
146 unsigned long sustainCount;
147 unsigned long decayCount;
148 unsigned long delayCount;
149 unsigned long counter;
150 //unsigned long pointer;
151 StkFloat pointer;
152 unsigned long startPointer;
153 unsigned int repeats;
154 GrainState state;
155
156 // Default constructor.
157 Grain()
158 :eScaler(0.0), eRate(0.0), attackCount(0), sustainCount(0), decayCount(0),
159 delayCount(0), counter(0), pointer(0), startPointer(0), repeats(0), state(GRAIN_STOPPED) {}
160 };
161
162 void calculateGrain( Granulate::Grain& grain );
163
164 StkFrames data_;
165 std::vector<Grain> grains_;
166 Noise noise;
167 //long gPointer_;
168 StkFloat gPointer_;
169
170 // Global grain parameters.
171 unsigned int gDuration_;
172 unsigned int gRampPercent_;
173 unsigned int gDelay_;
174 unsigned int gStretch_;
175 unsigned int stretchCounter_;
176 int gOffset_;
177 StkFloat gRandomFactor_;
178 StkFloat gain_;
179
180};
181
182inline StkFloat Granulate :: lastOut( unsigned int channel )
183{
184#if defined(_STK_DEBUG_)
185 if ( channel >= lastFrame_.channels() ) {
186 oStream_ << "Granulate::lastOut(): channel argument is invalid!";
187 handleError( StkError::FUNCTION_ARGUMENT );
188 }
189#endif
190
191 return lastFrame_[channel];
192}
193
194inline StkFrames& Granulate :: tick( StkFrames& frames, unsigned int channel )
195{
196 unsigned int nChannels = lastFrame_.channels();
197#if defined(_STK_DEBUG_)
198 if ( channel > frames.channels() - nChannels ) {
199 oStream_ << "Granulate::tick(): channel and StkFrames arguments are incompatible!";
200 handleError( StkError::FUNCTION_ARGUMENT );
201 }
202#endif
203
204 StkFloat *samples = &frames[channel];
205 unsigned int j, hop = frames.channels() - nChannels;
206 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
207 *samples++ = tick();
208 for ( j=1; j<nChannels; j++ )
209 *samples++ = lastFrame_[j];
210 }
211
212 return frames;
213}
214
215} // stk namespace
216
217#endif
Generator(void)
Class constructor.
Definition Generator.h:25
void setRandomFactor(StkFloat randomness=0.1)
This factor is used when setting individual grain parameters (0.0 - 1.0).
StkFloat tick(unsigned int channel=0)
Compute one sample frame and return the specified channel value.
~Granulate(void)
Class destructor.
StkFloat lastOut(unsigned int channel=0)
Return the specified channel value of the last computed frame.
Definition Granulate.h:182
void openFile(std::string fileName, bool typeRaw=false)
Load a monophonic soundfile to be "granulated".
void setVoices(unsigned int nVoices=1)
Set the number of simultaneous grain "voices" to use.
Granulate(void)
Default constructor.
void reset(void)
Reset the file pointer and all existing grains to the file start.
void setGrainParameters(unsigned int duration=30, unsigned int rampPercent=50, int offset=0, unsigned int delay=0)
Set global grain parameters used to determine individual grain settings.
void setStretch(unsigned int stretchFactor=1)
Set the stretch factor used for grain playback (1 - 1000).
Granulate(unsigned int nVoices, std::string fileName, bool typeRaw=false)
Constructor taking input audio file and number of voices arguments.
STK in memory File An instance is representing an individual memory array which can be registered wit...
Definition MemoryFS.h:52
An STK class to handle vectorized audio data.
Definition Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
The STK namespace.
Definition ADSR.h:8