CSL  6.0
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
float * mSamples
sample buffer pointer
Definition: Granulator.h:70
The PThreads CSL Thread class.
float mWidthBase
stereo width
Definition: Granulator.h:63
GrainPlayer(GrainCloud *cloud)
Definition: Granulator.cpp:14
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
float pan
stereo pan 0 - 1
Definition: Granulator.h:26
float mDurationBase
grain duration base
Definition: Granulator.h:61
free state
Definition: Granulator.h:38
Grain * mSilentGrains
shared grain lists - ptr to the free pool (silent)
Definition: Granulator.h:74
float sampsPerTick
resolution of hi-res clock(s-rate / 1 billion)
Definition: Granulator.h:78
float mVolumeBase
amplitude scale
Definition: Granulator.h:65
float mOffsetRange
offset range
Definition: Granulator.h:58
float * samples
sample buffer pointer
Definition: Granulator.h:30
void startThreads()
method to start-up the create/reap threads
Definition: Granulator.cpp:221
GrainPlayer – low-level granular synthesis generator, uses a list of current grains.
Definition: Granulator.h:88
float time
current time (index) in samples
Definition: Granulator.h:25
float rate
playback rate (1.0 for normal pitch, < 0 reads backwards)
Definition: Granulator.h:23
GrainCloud * mCloud
the cloud I play
Definition: Granulator.h:95
float mOffsetBase
starting index offset base
Definition: Granulator.h:57
calculating audio samples
Definition: Granulator.h:39
void reset()
reset all grains to silent
Definition: Granulator.cpp:237
Grain * nextGrain
A pointer to the next grain in the linked list.
Definition: Granulator.h:32
float mEnvelopeRange
envelope range
Definition: Granulator.h:68
Grain * mPlayingGrains
ptr to the list of active grains
Definition: Granulator.h:75
float mWidthRange
stereo width
Definition: Granulator.h:64
float position
running sample index
Definition: Granulator.h:28
CThread * spawnerThread
thread to create grains
Definition: Granulator.h:81
scheduling grains
Definition: Granulator.h:40
float mRateBase
grain rate base
Definition: Granulator.h:55
float mDensityBase
grain density base
Definition: Granulator.h:59
unsigned delay
initial delay in samples
Definition: Granulator.h:29
float mEnvelopeBase
envelope base: 0 = perc, 0.5 = triangle, 1 = reverse perc
Definition: Granulator.h:67
unsigned numSamples
of samples in buffer
Definition: Granulator.h:71
Grain data structure This implementation uses a linked list data structure. You might want to add a f...
Definition: Granulator.h:21
float mVolumeRange
amplitude range
Definition: Granulator.h:66
GrainulatorState gState
granulator state flag
Definition: Granulator.h:76
Buffer – the multi-channel sample buffer class (passed around between generators and IO guys)...
Definition: CSL_Core.h:106
float mDensityRange
grain density range
Definition: Granulator.h:60
struct csl::Grain Grain
Grain data structure This implementation uses a linked list data structure. You might want to add a f...
void nextBuffer(Buffer &outputBuffer)
this sums up the list of live grains – very simple
Definition: Granulator.cpp:24
float duration
duration in samples
Definition: Granulator.h:24
GrainCloud()
simple constructor
Definition: Granulator.cpp:86
long gNow
clock for accurate timing
Definition: Granulator.h:77
float mRateRange
rate random range
Definition: Granulator.h:56
GrainCloud – routine for playing clouds under GUI control. This could be called a cloud or a stream...
Definition: Granulator.h:47
float mDurationRange
grain duration range
Definition: Granulator.h:62
forward declaration
Definition: CSL_Core.h:241
unsigned numSamples
length of sample vector
Definition: Granulator.h:31
bool isPlaying
whether I'm on or off
Definition: Granulator.h:72
CThread * reaperThread
thread to kill finished grains
Definition: Granulator.h:82
GrainulatorState
This flag is for the app state, so that we don't change the grain lists while calculating samples...
Definition: Granulator.h:37
bool threadOn
if the thread's running
Definition: Granulator.h:83
float amplitude
amplitude scale (0 - 1)
Definition: Granulator.h:22
Base class of CSL exceptions (written upper-case). Has a string message.
float env
envelope: 0 = perc, 0.5 = triangle, 1 = reverse perc
Definition: Granulator.h:27