Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


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 
9 namespace stk {
10 
11 /***************************************************/
26 /***************************************************/
27 
28 class Granulate: public Generator
29 {
30  public:
32  Granulate( void );
33 
35  Granulate( unsigned int nVoices, std::string fileName, bool typeRaw = false );
36 
38  ~Granulate( void );
39 
41 
45  void openFile( std::string fileName, bool typeRaw = false );
46 
48 
52  void reset( void );
53 
55 
60  void setVoices( unsigned int nVoices = 1 );
61 
63 
69  void setStretch( unsigned int stretchFactor = 1 );
70 
72 
87  void setGrainParameters( unsigned int duration = 30, unsigned int rampPercent = 50,
88  int offset = 0, unsigned int delay = 0 );
89 
91 
99  void setRandomFactor( StkFloat randomness = 0.1 );
100 
102 
110  StkFloat lastOut( unsigned int channel = 0 );
111 
113  StkFloat tick( unsigned int channel = 0 );
114 
116 
123  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
124 
125  enum GrainState {
126  GRAIN_STOPPED,
127  GRAIN_FADEIN,
128  GRAIN_SUSTAIN,
129  GRAIN_FADEOUT
130  };
131 
132  protected:
133 
134  struct Grain {
135  StkFloat eScaler;
136  StkFloat eRate;
137  unsigned long attackCount;
138  unsigned long sustainCount;
139  unsigned long decayCount;
140  unsigned long delayCount;
141  unsigned long counter;
142  //unsigned long pointer;
143  StkFloat pointer;
144  unsigned long startPointer;
145  unsigned int repeats;
146  GrainState state;
147 
148  // Default constructor.
149  Grain()
150  :eScaler(0.0), eRate(0.0), attackCount(0), sustainCount(0), decayCount(0),
151  delayCount(0), counter(0), pointer(0), startPointer(0), repeats(0), state(GRAIN_STOPPED) {}
152  };
153 
154  void calculateGrain( Granulate::Grain& grain );
155 
156  StkFrames data_;
157  std::vector<Grain> grains_;
158  Noise noise;
159  //long gPointer_;
160  StkFloat gPointer_;
161 
162  // Global grain parameters.
163  unsigned int gDuration_;
164  unsigned int gRampPercent_;
165  unsigned int gDelay_;
166  unsigned int gStretch_;
167  unsigned int stretchCounter_;
168  int gOffset_;
169  StkFloat gRandomFactor_;
170  StkFloat gain_;
171 
172 };
173 
174 inline StkFloat Granulate :: lastOut( unsigned int channel )
175 {
176 #if defined(_STK_DEBUG_)
177  if ( channel >= lastFrame_.channels() ) {
178  oStream_ << "Granulate::lastOut(): channel argument is invalid!";
179  handleError( StkError::FUNCTION_ARGUMENT );
180  }
181 #endif
182 
183  return lastFrame_[channel];
184 }
185 
186 inline StkFrames& Granulate :: tick( StkFrames& frames, unsigned int channel )
187 {
188  unsigned int nChannels = lastFrame_.channels();
189 #if defined(_STK_DEBUG_)
190  if ( channel > frames.channels() - nChannels ) {
191  oStream_ << "Granulate::tick(): channel and StkFrames arguments are incompatible!";
192  handleError( StkError::FUNCTION_ARGUMENT );
193  }
194 #endif
195 
196  StkFloat *samples = &frames[channel];
197  unsigned int j, hop = frames.channels() - nChannels;
198  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
199  *samples++ = tick();
200  for ( j=1; j<nChannels; j++ )
201  *samples++ = lastFrame_[j];
202  }
203 
204  return frames;
205 }
206 
207 } // stk namespace
208 
209 #endif

The Synthesis ToolKit in C++ (STK)
©1995--2014 Perry R. Cook and Gary P. Scavone. All Rights Reserved.