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 
10 namespace stk {
11 
12 /***************************************************/
27 /***************************************************/
28 
29 class Granulate: public Generator
30 {
31  public:
33  Granulate( void );
34 
36  Granulate( unsigned int nVoices, std::string fileName, bool typeRaw = false );
37 
39  ~Granulate( void );
40 
41  Granulate( unsigned int nVoices, MemoryFS &memoryFile);
42  void openMemory(MemoryFS &memoryFile);
43 
45 
49  void openFile( std::string fileName, bool typeRaw = false );
50 
52 
56  void reset( void );
57 
59 
64  void setVoices( unsigned int nVoices = 1 );
65 
67 
73  void setStretch( unsigned int stretchFactor = 1 );
74 
76 
91  void setGrainParameters( unsigned int duration = 30, unsigned int rampPercent = 50,
92  int offset = 0, unsigned int delay = 0 );
93 
95 
103  void setRandomFactor( StkFloat randomness = 0.1 );
104 
106 
114  StkFloat lastOut( unsigned int channel = 0 );
115 
117  StkFloat tick( unsigned int channel = 0 );
118 
120 
127  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
128 
129  enum GrainState {
130  GRAIN_STOPPED,
131  GRAIN_FADEIN,
132  GRAIN_SUSTAIN,
133  GRAIN_FADEOUT
134  };
135 
136  protected:
137 
138  struct Grain {
139  StkFloat eScaler;
140  StkFloat eRate;
141  unsigned long attackCount;
142  unsigned long sustainCount;
143  unsigned long decayCount;
144  unsigned long delayCount;
145  unsigned long counter;
146  //unsigned long pointer;
147  StkFloat pointer;
148  unsigned long startPointer;
149  unsigned int repeats;
150  GrainState state;
151 
152  // Default constructor.
153  Grain()
154  :eScaler(0.0), eRate(0.0), attackCount(0), sustainCount(0), decayCount(0),
155  delayCount(0), counter(0), pointer(0), startPointer(0), repeats(0), state(GRAIN_STOPPED) {}
156  };
157 
158  void calculateGrain( Granulate::Grain& grain );
159 
160  StkFrames data_;
161  std::vector<Grain> grains_;
162  Noise noise;
163  //long gPointer_;
164  StkFloat gPointer_;
165 
166  // Global grain parameters.
167  unsigned int gDuration_;
168  unsigned int gRampPercent_;
169  unsigned int gDelay_;
170  unsigned int gStretch_;
171  unsigned int stretchCounter_;
172  int gOffset_;
173  StkFloat gRandomFactor_;
174  StkFloat gain_;
175 
176 };
177 
178 inline StkFloat Granulate :: lastOut( unsigned int channel )
179 {
180 #if defined(_STK_DEBUG_)
181  if ( channel >= lastFrame_.channels() ) {
182  oStream_ << "Granulate::lastOut(): channel argument is invalid!";
183  handleError( StkError::FUNCTION_ARGUMENT );
184  }
185 #endif
186 
187  return lastFrame_[channel];
188 }
189 
190 inline StkFrames& Granulate :: tick( StkFrames& frames, unsigned int channel )
191 {
192  unsigned int nChannels = lastFrame_.channels();
193 #if defined(_STK_DEBUG_)
194  if ( channel > frames.channels() - nChannels ) {
195  oStream_ << "Granulate::tick(): channel and StkFrames arguments are incompatible!";
196  handleError( StkError::FUNCTION_ARGUMENT );
197  }
198 #endif
199 
200  StkFloat *samples = &frames[channel];
201  unsigned int j, hop = frames.channels() - nChannels;
202  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
203  *samples++ = tick();
204  for ( j=1; j<nChannels; j++ )
205  *samples++ = lastFrame_[j];
206  }
207 
208  return frames;
209 }
210 
211 } // stk namespace
212 
213 #endif
STK abstract unit generator parent class.
Definition: Generator.h:21
STK granular synthesis class.
Definition: Granulate.h:30
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:178
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:46
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