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

STK audio file input class. More...

#include <FileWvIn.h>

Inheritance diagram for stk::FileWvIn:
stk::WvIn stk::Stk stk::FileLoop stk::MemoryWvIn stk::MemoryLoop

Public Member Functions

 FileWvIn (unsigned long chunkThreshold=1000000, unsigned long chunkSize=1024)
 Default constructor.
 
 FileWvIn (std::string fileName, bool raw=false, bool doNormalize=true, unsigned long chunkThreshold=1000000, unsigned long chunkSize=1024, bool doInt2FloatScaling=true)
 Overloaded constructor for file input. More...
 
 ~FileWvIn (void)
 Class destructor.
 
virtual void openFile (std::string fileName, bool raw=false, bool doNormalize=true, bool doInt2FloatScaling=true)
 Open the specified file and load its data. More...
 
virtual void closeFile (void)
 Close a file if one is open.
 
virtual void reset (void)
 Clear outputs and reset time (file) pointer to zero.
 
virtual void normalize (void)
 Normalize data to a maximum of +-1.0. More...
 
virtual void normalize (StkFloat peak)
 Normalize data to a maximum of +-peak. More...
 
virtual unsigned long getSize (void) const
 Return the file size in sample frames.
 
virtual StkFloat getFileRate (void) const
 Return the input file sample rate in Hz (not the data read rate). More...
 
virtual bool isOpen (void)
 Query whether a file is open.
 
virtual bool isFinished (void) const
 Query whether reading is complete.
 
virtual void setRate (StkFloat rate)
 Set the data read rate in samples. The rate can be negative. More...
 
virtual void addTime (StkFloat time)
 Increment the read pointer by time samples. More...
 
void setInterpolate (bool doInterpolate)
 Turn linear interpolation on/off. More...
 
StkFloat lastOut (unsigned int channel=0)
 Return the specified channel value of the last computed frame. More...
 
virtual StkFloat tick (unsigned int channel=0)
 Compute a sample frame and return the specified channel value. More...
 
virtual StkFramestick (StkFrames &frames, unsigned int channel=0)
 Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference. More...
 
unsigned int channelsOut (void) const
 Return the number of audio channels in the data or stream.
 
const StkFrameslastFrame (void) const
 Return an StkFrames reference to the last computed sample frame. More...
 
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.
 
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 audio file input class.

This class inherits from WvIn. It provides a "tick-level" interface to the FileRead class. It also provides variable-rate playback functionality. Audio file support is provided by the FileRead class. Linear interpolation is used for fractional read rates.

FileWvIn supports multi-channel data. It is important to distinguish the tick() method that computes a single frame (and returns only the specified sample of a multi-channel frame) from the overloaded one that takes an StkFrames object for multi-channel and/or multi-frame data.

FileWvIn will either load the entire content of an audio file into local memory or incrementally read file data from disk in chunks. This behavior is controlled by the optional constructor arguments chunkThreshold and chunkSize. File sizes greater than chunkThreshold (in sample frames) will be read incrementally in chunks of chunkSize each (also in sample frames).

For file data read completely into local memory, the doNormalize flag can be used to normalize all values with respect to the maximum absolute value of the data.

If the file data format is fixed point, the flag doInt2FloatScaling can be used to control whether the values are scaled with respect to the corresponding fixed-point maximum. For example, if reading 16-bit signed integers, the input values will be scaled by 1 / 32768.0. This scaling will not happen for floating-point file data formats.

When the file end is reached, subsequent calls to the tick() functions return zeros and isFinished() returns true.

See the FileRead class for a description of the supported audio file formats.

by Perry R. Cook and Gary P. Scavone, 1995–2019.

Constructor & Destructor Documentation

◆ FileWvIn()

stk::FileWvIn::FileWvIn ( std::string  fileName,
bool  raw = false,
bool  doNormalize = true,
unsigned long  chunkThreshold = 1000000,
unsigned long  chunkSize = 1024,
bool  doInt2FloatScaling = true 
)

Overloaded constructor for file input.

An StkError will be thrown if the file is not found, its format is unknown, or a read error occurs.

Member Function Documentation

◆ openFile()

virtual void stk::FileWvIn::openFile ( std::string  fileName,
bool  raw = false,
bool  doNormalize = true,
bool  doInt2FloatScaling = true 
)
virtual

Open the specified file and load its data.

Data from a previously opened file will be overwritten by this function. An StkError will be thrown if the file is not found, its format is unknown, or a read error occurs. If the file length is less than the chunkThreshold limit and doNormalize is true, the file data will be normalized with respect to the maximum absolute value of the data. If the doInt2FloatScaling flag is true and the input data is fixed-point, a scaling will be applied with respect to the fixed-point limits.

Reimplemented in stk::MemoryWvIn, stk::MemoryLoop, and stk::FileLoop.

◆ normalize() [1/2]

virtual void stk::FileWvIn::normalize ( void  )
virtual

Normalize data to a maximum of +-1.0.

This function has no effect when data is incrementally loaded from disk.

Reimplemented in stk::FileLoop.

◆ normalize() [2/2]

virtual void stk::FileWvIn::normalize ( StkFloat  peak)
virtual

Normalize data to a maximum of +-peak.

This function has no effect when data is incrementally loaded from disk.

Reimplemented in stk::FileLoop.

◆ getFileRate()

virtual StkFloat stk::FileWvIn::getFileRate ( void  ) const
inlinevirtual

Return the input file sample rate in Hz (not the data read rate).

WAV, SND, and AIF formatted files specify a sample rate in their headers. STK RAW files have a sample rate of 22050 Hz by definition. MAT-files are assumed to have a rate of 44100 Hz.

Reimplemented in stk::FileLoop.

112 { return data_.dataRate(); };
StkFloat dataRate(void) const
Return the sample rate associated with the StkFrames data.
Definition: Stk.h:432

◆ setRate()

virtual void stk::FileWvIn::setRate ( StkFloat  rate)
virtual

Set the data read rate in samples. The rate can be negative.

If the rate value is negative, the data is read in reverse order.

Reimplemented in stk::FileLoop.

◆ addTime()

virtual void stk::FileWvIn::addTime ( StkFloat  time)
virtual

Increment the read pointer by time samples.

Note that this function will not modify the interpolation flag status.

Reimplemented in stk::FileLoop.

◆ setInterpolate()

void stk::FileWvIn::setInterpolate ( bool  doInterpolate)
inline

Turn linear interpolation on/off.

Interpolation is automatically off when the read rate is an integer value. If interpolation is turned off for a fractional rate, the time index is truncated to an integer value.

139 { interpolate_ = doInterpolate; };

◆ lastOut()

StkFloat stk::FileWvIn::lastOut ( unsigned int  channel = 0)
inline

Return the specified channel value of the last computed frame.

If no file is loaded, the returned value is 0.0. 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()
195 {
196 #if defined(_STK_DEBUG_)
197  if ( channel >= data_.channels() ) {
198  oStream_ << "FileWvIn::lastOut(): channel argument and soundfile data are incompatible!";
199  handleError( StkError::FUNCTION_ARGUMENT );
200  }
201 #endif
202 
203  if ( finished_ ) return 0.0;
204  return lastFrame_[channel];
205 }
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() [1/2]

virtual StkFloat stk::FileWvIn::tick ( unsigned int  channel = 0)
virtual

Compute a sample frame and return the specified channel value.

For multi-channel files, use the lastFrame() function to get all values from the computed frame. If no file data is loaded, the returned value is 0.0. The channel argument must be less than the number of channels in the file data (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::WvIn.

Reimplemented in stk::FileLoop.

◆ tick() [2/2]

virtual StkFrames& stk::FileWvIn::tick ( StkFrames frames,
unsigned int  channel = 0 
)
virtual

Fill the StkFrames object with computed sample frames, starting at the specified channel and return the same reference.

The channel argument plus the number of input 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::WvIn.

Reimplemented in stk::FileLoop.

◆ lastFrame()

const StkFrames& stk::WvIn::lastFrame ( void  ) const
inlineinherited

Return an StkFrames reference to the last computed sample frame.

If no file data is loaded, an empty container is returned.

29 { return lastFrame_; };

◆ 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: