CSL  5.2
Granulator.h
Go to the documentation of this file.
1 //
2 // Granulator.h -- CSL class for doing granular synthesis
3 //
4 // See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 //
6 
7 #ifndef CSL_GRAN_H
8 #define CSL_GRAN_H
9 
10 #include "CSL_Core.h" // my superclass
11 #include "ThreadUtilities.h"
12 
13 namespace csl { // my namespace
14 
15 #define MAXGRAINS 200
16 
17 /// Grain data structure
18 /// This implementation uses a linked list data structure.
19 /// You might want to add a few more members to this for flexibility.
20 
21 typedef struct Grain {
22  float amplitude; ///< amplitude scale (0 - 1)
23  float rate; ///< playback rate (1.0 for normal pitch, < 0 reads backwards)
24  float duration; ///< duration in samples
25  float time; ///< current time (index) in samples
26  float pan; ///< stereo pan 0 - 1
27  float env; ///< envelope: 0 = perc, 0.5 = triangle, 1 = reverse perc
28  float position; ///< running sample index
29  unsigned delay; ///< initial delay in samples
30  float * samples; ///< sample buffer pointer
31  unsigned numSamples; ///< length of sample vector
32  Grain * nextGrain; ///< A pointer to the next grain in the linked list
33 } Grain;
34 
35 /// This flag is for the app state, so that we don't change the grain lists while calculating samples
36 
37 typedef enum {
38  kFree, ///< free state
39  kDSP, ///< calculating audio samples
40  kSched, ///< scheduling grains
42 
43 /// GrainCloud -- routine for playing clouds under GUI control.
44 /// This could be called a cloud or a stream.
45 /// You could also add a few more variables to make more flexible clouds.
46 
47 class GrainCloud {
48 public:
49  GrainCloud(); ///< simple constructor
50  ~GrainCloud();
51 
52  void startThreads(); ///< method to start-up the create/reap threads
53  void reset(); ///< reset all grains to silent
54  // public data members (set from GUI)
55  float mRateBase; ///< grain rate base
56  float mRateRange; ///< rate random range
57  float mOffsetBase; ///< starting index offset base
58  float mOffsetRange; ///< offset range
59  float mDensityBase; ///< grain density base
60  float mDensityRange; ///< grain density range
61  float mDurationBase; ///< grain duration base
62  float mDurationRange; ///< grain duration range
63  float mWidthBase; ///< stereo width
64  float mWidthRange; ///< stereo width
65  float mVolumeBase; ///< amplitude scale
66  float mVolumeRange; ///< amplitude range
67  float mEnvelopeBase; ///< envelope base: 0 = perc, 0.5 = triangle, 1 = reverse perc
68  float mEnvelopeRange; ///< envelope range
69 
70  float * mSamples; ///< sample buffer pointer
71  unsigned numSamples; ///< # of samples in buffer
72  bool isPlaying; ///< whether I'm on or off
73 
74  Grain * mSilentGrains; ///< shared grain lists - ptr to the free pool (silent)
75  Grain * mPlayingGrains; ///< ptr to the list of active grains
76  GrainulatorState gState; ///< granulator state flag
77  long gNow; ///< clock for accurate timing
78  float sampsPerTick; ///< resolution of hi-res clock(s-rate / 1 billion)
79 
80 protected:
81  CThread * spawnerThread; ///< thread to create grains
82  CThread * reaperThread; ///< thread to kill finished grains
83  bool threadOn; ///< if the thread's running
84 };
85 
86 /// GrainPlayer -- low-level granular synthesis generator, uses a list of current grains.
87 
88 class GrainPlayer : public UnitGenerator {
89 public:
90  GrainPlayer(GrainCloud * cloud);
91  ~GrainPlayer();
92  /// this sums up the list of live grains -- very simple
93  void nextBuffer(Buffer & outputBuffer) throw (CException);
94 
95  GrainCloud * mCloud; ///< the cloud I play
96 };
97 
98 } // end of namespace
99 
100 #endif