Arduino STK  4.6.2
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
stk::Granulate Class Reference

STK granular synthesis class. More...

#include <Granulate.h>

Inheritance diagram for stk::Granulate:
stk::Generator stk::Stk

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.
 
StkFramestick (StkFrames &frames, unsigned int channel=0)
 Fill the StkFrames object with computed sample frames, starting at the specified channel. More...
 
unsigned int channelsOut (void) const
 Return the number of output channels for the class.
 
const StkFrameslastFrame (void) const
 Return an StkFrames reference to the last output sample frame.
 
void ignoreSampleRateChange (bool ignore=true)
 A function to enable/disable the automatic updating of class data when the STK sample rate changes. More...
 

Static Public Member Functions

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

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

void handleError (StkError::Type type) const
 Internal function for error reporting that assumes message in oStream_ variable.
 
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.
 

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–2019.

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()
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 }
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
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.

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

◆ setSampleRate()

static void stk::Stk::setSampleRate ( StkFloat  rate)
staticinherited

Static method that sets the STK sample rate.

The sample rate set using this method is queried by all STK classes that depend on its value. It is initialized to the default SRATE set in Stk.h. Many STK classes use the sample rate during instantiation. Therefore, if you wish to use a rate that is different from the default rate, it is imperative that it be set BEFORE STK objects are instantiated. A few classes that make use of the global STK sample rate are automatically notified when the rate changes so that internal class data can be appropriately updated. However, this has not been fully implemented. Specifically, classes that appropriately update their own data when either a setFrequency() or noteOn() function is called do not currently receive the automatic notification of rate change. If the user wants a specific class instance to ignore such notifications, perhaps in a multi-rate context, the function Stk::ignoreSampleRateChange() should be called.

◆ ignoreSampleRateChange()

void stk::Stk::ignoreSampleRateChange ( bool  ignore = true)
inlineinherited

A function to enable/disable the automatic updating of class data when the STK sample rate changes.

This function allows the user to enable or disable class data updates in response to global sample rate changes on a class by class basis.

184 { ignoreSampleRateChange_ = ignore; };

Member Data Documentation

◆ STK_SINT8

const StkFormat stk::Stk::STK_SINT8
staticinherited

-128 to +127

◆ STK_SINT16

const StkFormat stk::Stk::STK_SINT16
staticinherited

-32768 to +32767

◆ STK_SINT24

const StkFormat stk::Stk::STK_SINT24
staticinherited

Lower 3 bytes of 32-bit signed integer.

◆ STK_SINT32

const StkFormat stk::Stk::STK_SINT32
staticinherited

-2147483648 to +2147483647.

◆ STK_FLOAT32

const StkFormat stk::Stk::STK_FLOAT32
staticinherited

Normalized between plus/minus 1.0.

◆ STK_FLOAT64

const StkFormat stk::Stk::STK_FLOAT64
staticinherited

Normalized between plus/minus 1.0.


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