Mozzi  version v1.1.0
sound synthesis library for Arduino
MonoOutput Struct Reference

This struct encapsulates one frame of mono audio output. More...

#include <AudioOutput.h>

Public Member Functions

 MonoOutput (AudioOutputStorage_t l=0)
 Construct an audio frame from raw values (zero-centered)
 
 operator AudioOutput_t () const
 Conversion to int operator. More...
 
AudioOutputStorage_t l () const
 
AudioOutputStorage_t r () const
 
MonoOutputclip ()
 Clip frame to supported range. More...
 

Static Public Member Functions

template<typename T >
static MonoOutput fromNBit (uint8_t bits, T l)
 Construct an audio frame a zero-centered value known to be in the N bit range. More...
 
static MonoOutput from8Bit (int16_t l)
 Construct an audio frame from a zero-centered value known to be in the 8 bit range. More...
 
static MonoOutput from16Bit (int16_t l)
 Construct an audio frame a zero-centered value known to be in the 16 bit range. More...
 
template<typename A , typename B >
static MonoOutput fromAlmostNBit (A bits, B l)
 

Detailed Description

This struct encapsulates one frame of mono audio output.

Internally, it really just boils down to a single int value, but the struct provides useful API an top of that, for the following:

a) To construct an output frame, you should use one of the from8Bit(), fromNBit(), etc. functions. Given a raw input value, at a known resolution (number of bits), this scales the output efficiently to whatever is needed on the target platform. Using this, your updateAudio() function will be portable across different CPU and different output methods, including external DACs. b) The struct provides some convenience API on top of this. Right now, this is the function clip(), replacing the more verbose, and non-portable constrain(x, -244, 243) found in some old sketches. c) The struct provides accessors l() and r() that are source-compatible with StereoOutput, making it easy to e.g. implement support for an external DAC in both mono and stereo. d) Finally, an automatic conversion operator to int aka AudioOutput_t provides backward compatibility with old Mozzi sketches. Internally, the compiler will actually do away with this wholw struct, leaving just the same basic fast integer operations as in older Mozzi sketches. However, now, you don't have to rewrite those for different configurations.

Definition at line 132 of file AudioOutput.h.

Member Function Documentation

◆ clip()

MonoOutput& MonoOutput::clip ( )
inline

Clip frame to supported range.

This is useful when at times, but only rarely, the signal may exceed the usual range. Using this function does not avoid artifacts, entirely, but gives much better results than an overflow.

Definition at line 148 of file AudioOutput.h.

◆ from16Bit()

static MonoOutput MonoOutput::from16Bit ( int16_t  l)
inlinestatic

Construct an audio frame a zero-centered value known to be in the 16 bit range.

This is jsut a shortcut for fromNBit(16, ...) provided for convenience.

Definition at line 158 of file AudioOutput.h.

◆ from8Bit()

static MonoOutput MonoOutput::from8Bit ( int16_t  l)
inlinestatic

Construct an audio frame from a zero-centered value known to be in the 8 bit range.

On AVR, STANDADR or STANDARD_PLUS mode, this is effectively the same as calling the constructor, directly (no scaling gets applied). On platforms/configs using more bits, an appropriate left-shift will be performed.

Definition at line 156 of file AudioOutput.h.

◆ fromNBit()

template<typename T >
static MonoOutput MonoOutput::fromNBit ( uint8_t  bits,
l 
)
inlinestatic

Construct an audio frame a zero-centered value known to be in the N bit range.

Appropriate left- or right-shifting will be performed, based on the number of output bits available. While this function takes care of the shifting, beware of potential overflow issues, if your intermediary results exceed the 16 bit range. Use proper casts to int32_t or larger in that case (and the compiler will automatically pick the 32 bit overload in this case)

Definition at line 153 of file AudioOutput.h.

◆ operator AudioOutput_t()

MonoOutput::operator AudioOutput_t ( ) const
inline

Conversion to int operator.

Definition at line 142 of file AudioOutput.h.