Skip to content

Commit f460ba5

Browse files
committed
Added outputs from SFix for MonoOutput and StereoOutput
1 parent cfe7973 commit f460ba5

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

AudioOutput.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#ifndef AUDIOOUTPUT_H
4848
#define AUDIOOUTPUT_H
49+
#include <FixMath.h>
4950

5051
/** The type used to store a single channel of a single frame, internally. For compatibility with earlier versions of Mozzi this is defined as int.
5152
* If you do not care about keeping old sketches working, you may be able to save some RAM by using int16_t, instead (on boards where int is larger
@@ -123,8 +124,11 @@ struct MonoOutput {
123124
/** Construct an audio frame from a zero-centered value known to be in the 8 bit range. On AVR, if MOZZI_OUTPUT_PWM mode, this is effectively the same as calling the
124125
* constructor, directly (no scaling gets applied). On platforms/configs using more bits, an appropriate left-shift will be performed. */
125126
static inline MonoOutput from8Bit(int16_t l) { return fromNBit(8, l); }
126-
/** 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. */
127+
/** Construct an audio frame from a zero-centered value known to be in the 16 bit range. This is jsut a shortcut for fromNBit(16, ...) provided for convenience. */
127128
static inline MonoOutput from16Bit(int16_t l) { return fromNBit(16, l); }
129+
/** Construct an audio frame from a SFix type from FixMath. Mozzi will figure out how many bits are in there and performs appropriate shifting to match the output range. */
130+
template<int8_t NI, int8_t NF, uint64_t RANGE>
131+
static inline MonoOutput fromSFix(SFix<NI,NF,RANGE> l) { return MonoOutput(SCALE_AUDIO(l.asRaw(), (NI+NF))) ;}
128132
/** Construct an audio frame a zero-centered value known to be above at almost but not quite the N bit range, e.g. at N=8 bits and a litte. On most platforms, this is
129133
* exactly the same as fromNBit(), shifting up or down to the platforms' available resolution.
130134
*
@@ -168,6 +172,9 @@ template<typename T> static inline StereoOutput fromNBit(uint8_t bits, T l, T r)
168172
static inline StereoOutput from8Bit(int16_t l, int16_t r) { return fromNBit(8, l, r); }
169173
/** See @ref MonoOutput::from16Bit(), stereo variant */
170174
static inline StereoOutput from16Bit(int16_t l, int16_t r) { return fromNBit(16, l, r); }
175+
/** See @ref MonoOutput::fromSFix(), stereo variant. Note that the two channels do not need to have the same number of bits. */
176+
template<int8_t NI, int8_t NF, uint64_t RANGE, int8_t _NI, int8_t _NF, uint64_t _RANGE>
177+
static inline StereoOutput from SFix(SFix<NI,NF,RANGE> l, SFix<_NI,_NF,_RANGE> r) { return MonoOutput(SCALE_AUDIO(l.asRaw(), (NI+NF)), SCALE_AUDIO(r.asRaw(), (_NI+_NF))); }
171178
/** See @ref MonoOutput::fromAlmostNBit(), stereo variant */
172179
template<typename A, typename B> static inline StereoOutput fromAlmostNBit(A bits, B l, B r) { return StereoOutput(SCALE_AUDIO_NEAR(l, bits), SCALE_AUDIO_NEAR(r, bits)); }
173180
private:

0 commit comments

Comments
 (0)