The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

Public Member Functions | List of all members
stk::Granulate Class Reference

STK granular synthesis class. More...

`#include <Granulate.h>`

Inheritance diagram for stk::Granulate:

## Public Member Functions
| Granulate (void)
| Default constructor.
| Granulate (unsigned int nVoices, std::string fileName, bool typeRaw=false)
| Constructor taking input audio file and number of voices arguments.
| ~Granulate (void)
| Class destructor.
void | openFile (std::string fileName, bool typeRaw=false)
| Load a monophonic soundfile to be "granulated". More...
void | reset (void)
| Reset the file pointer and all existing grains to the file start. More...
void | setVoices (unsigned int nVoices=1)
| Set the number of simultaneous grain "voices" to use. More...
void | setStretch (unsigned int stretchFactor=1)
| Set the stretch factor used for grain playback (1 - 1000). More...
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. More...
void | setRandomFactor (StkFloat randomness=0.1)
| This factor is used when setting individual grain parameters (0.0 - 1.0). More...
StkFloat | lastOut (unsigned int channel=0)
| Return the specified channel value of the last computed frame. More...
StkFloat | tick (unsigned int channel=0)
| Compute one sample frame and return the specified `channel` value.
StkFrames & | tick (StkFrames &frames, unsigned int channel=0)
| Fill the StkFrames object with computed sample frames, starting at the specified channel. More...
Public Member Functions inherited from stk::Generator
| Generator (void)
| Class constructor.
unsigned int | channelsOut (void) const
| Return the number of output channels for the class.
const StkFrames & | lastFrame (void) const
| Return an StkFrames reference to the last output sample frame.
Public Member Functions inherited from stk::Stk
void | ignoreSampleRateChange (bool ignore=true)
| A function to enable/disable the automatic updating of class data when the STK sample rate changes. More...

## Additional Inherited Members
Static Public Member Functions inherited from stk::Stk
static StkFloat | sampleRate (void)
| Static method that returns the current STK sample rate.
static void | setSampleRate (StkFloat rate)
| Static method that sets the STK sample rate. More...
static void | clear_alertList ()
| Static method that frees memory from alertList_.
static std::string | rawwavePath (void)
| Static method that returns the current rawwave path.
static void | setRawwavePath (std::string path)
| Static method that sets the STK rawwave path.
static void | swap16 (unsigned char *ptr)
| Static method that byte-swaps a 16-bit data type.
static void | swap32 (unsigned char *ptr)
| Static method that byte-swaps a 32-bit data type.
static void | swap64 (unsigned char *ptr)
| Static method that byte-swaps a 64-bit data type.
static void | sleep (unsigned long milliseconds)
| Static cross-platform method to sleep for a number of milliseconds.
static bool | inRange (StkFloat value, StkFloat min, StkFloat max)
| Static method to check whether a value is within a specified range.
static void | handleError (const char *message, StkError::Type type)
| Static function for error reporting and handling using c-strings.
static void | handleError (std::string message, StkError::Type type)
| Static function for error reporting and handling using c++ strings.
static void | showWarnings (bool status)
| Toggle display of WARNING and STATUS messages.
static void | printErrors (bool status)
| Toggle display of error messages before throwing exceptions.
Static Public Attributes inherited from stk::Stk
static const StkFormat | STK_SINT8
static const StkFormat | STK_SINT16
static const StkFormat | STK_SINT24
static const StkFormat | STK_SINT32
static const StkFormat | STK_FLOAT32
static const StkFormat | STK_FLOAT64
Protected Member Functions inherited from stk::Stk
| Stk (void)
| Default constructor.
virtual | ~Stk (void)
| Class destructor.
virtual void | sampleRateChanged (StkFloat newRate, StkFloat oldRate)
| This function should be implemented in subclasses that depend on the sample rate.
void | addSampleRateAlert (Stk *ptr)
| Add class pointer to list for sample rate change notification.
void | removeSampleRateAlert (Stk *ptr)
| Remove class pointer from list for sample rate change notification.
void | handleError (StkError::Type type) const
| Internal function for error reporting that assumes message in `oStream_` variable.

## Detailed Description

STK granular synthesis class.

This class implements a real-time granular synthesis algorithm that operates on an input soundfile. Multi-channel files are supported. Various functions are provided to allow control over voice and grain parameters.

The functionality of this class is based on the program MacPod by Chris Rolfe and Damian Keller, though there are likely to be a number of differences in the actual implementation.

by Gary Scavone, 2005–2021.

## Member Function Documentation

## ◆ openFile()

void stk::Granulate::openFile | ( | std::string | fileName,
|  | bool | typeRaw = `false`
| ) |  |

Load a monophonic soundfile to be "granulated".

An StkError will be thrown if the file is not found, its format is unknown or unsupported, or the file has more than one channel.

## ◆ reset()

void stk::Granulate::reset | ( | void |  | ) |

Reset the file pointer and all existing grains to the file start.

Multiple grains are offset from one another in time by grain duration / nVoices.

## ◆ setVoices()

void stk::Granulate::setVoices | ( | unsigned int | nVoices = `1` | ) |

Set the number of simultaneous grain "voices" to use.

Multiple grains are offset from one another in time by grain duration / nVoices. For this reason, it is best to set the grain parameters before calling this function (during initialization).

## ◆ setStretch()

void stk::Granulate::setStretch | ( | unsigned int | stretchFactor = `1` | ) |

Set the stretch factor used for grain playback (1 - 1000).

Granular synthesis allows for time-stetching without affecting the original pitch of a sound. A stretch factor of 4 will produce a resulting sound of length 4 times the orignal sound. The default parameter of 1 produces no stretching.

## ◆ setGrainParameters()

void stk::Granulate::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.

Each grain is defined as having a length of duration milliseconds which must be greater than zero. For values of rampPercent (0 - 100) greater than zero, a linear envelope will be applied to each grain. If rampPercent = 100, the resultant grain "window" is triangular while rampPercent = 50 produces a trapezoidal window. In addition, each grain can have a time delay of length delay and a grain pointer increment of length offset, which can be negative, before the next ramp onset (in milliseconds). The offset parameter controls grain pointer jumps between enveloped grain segments, while the delay parameter causes grain calculations to pause between grains. The actual values calculated for each grain will be randomized by a factor set using the setRandomFactor() function.

## ◆ setRandomFactor()

void stk::Granulate::setRandomFactor | ( | StkFloat | randomness = `0.1` | ) |

This factor is used when setting individual grain parameters (0.0 - 1.0).

This random factor is applied when all grain state durations are calculated. If set to 0.0, no randomness occurs. When randomness = 1.0, a grain segment of length duration will be randomly augmented by up to +- duration seconds (i.e., a 30 millisecond length will be augmented by an extra length of up to +30 or -30 milliseconds).

## ◆ lastOut()

StkFloat stk::Granulate::lastOut | ( | unsigned int | channel = `0` | ) |
| inline

Return the specified channel value of the last computed frame.

The `channel` argument must be less than the number of output channels, which can be determined with the channelsOut() function (the first channel is specified by 0). However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

See also
  lastFrame()

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}
stk::StkFrames::channels
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:416
stk::Stk::handleError
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.

## ◆ tick()

StkFrames & stk::Granulate::tick | ( | StkFrames & | frames,
|  | unsigned int | channel = `0`
| ) |  |
| inlinevirtual

Fill the StkFrames object with computed sample frames, starting at the specified channel.

The `channel` argument plus the number of output channels must be less than the number of channels in the StkFrames argument (the first channel is specified by 0). However, range checking is only performed if STK_DEBUG is defined during compilation, in which case an out-of-range value will trigger an StkError exception.

Implements stk::Generator.

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}
stk::Granulate::tick
StkFloat tick(unsigned int channel=0)
Compute one sample frame and return the specified channel value.

The documentation for this class was generated from the following file:

- Granulate.h

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