Skip to content

Commit dbed60a

Browse files
committed
Added automatic scaling to DBW example
Typo in AudioOutput
1 parent 05ef0f1 commit dbed60a

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

AudioOutput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ template<typename T> static inline StereoOutput fromNBit(uint8_t bits, T l, T r)
174174
static inline StereoOutput from16Bit(int16_t l, int16_t r) { return fromNBit(16, l, r); }
175175
/** See @ref MonoOutput::fromSFix(), stereo variant. Note that the two channels do not need to have the same number of bits. */
176176
template<int8_t NI, int8_t NF, uint64_t RANGE, int8_t _NI, int8_t _NF, uint64_t _RANGE>
177-
static inline StereoOutput fromSFix(SFix<NI,NF,RANGE> l, SFix<_NI,_NF,_RANGE> r) { return MonoOutput(SCALE_AUDIO(l.asRaw(), (NI+NF)), SCALE_AUDIO(r.asRaw(), (_NI+_NF))); }
177+
static inline StereoOutput fromSFix(SFix<NI,NF,RANGE> l, SFix<_NI,_NF,_RANGE> r) { return StereoOutput(SCALE_AUDIO(l.asRaw(), (NI+NF)), SCALE_AUDIO(r.asRaw(), (_NI+_NF))); }
178178
/** See @ref MonoOutput::fromAlmostNBit(), stereo variant */
179179
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)); }
180180
private:

examples/06.Synthesis/Detuned_Beats_Wash/Detuned_Beats_Wash.ino

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@
3737
#include <FixMath.h>
3838

3939
// harmonics
40-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos1(COS8192_DATA);
41-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos2(COS8192_DATA);
42-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos3(COS8192_DATA);
43-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos4(COS8192_DATA);
44-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos5(COS8192_DATA);
45-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos6(COS8192_DATA);
46-
//Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos7(COS8192_DATA); // used to work smoothly in Arduino 1.05
40+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos1(COS8192_DATA);
41+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos2(COS8192_DATA);
42+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos3(COS8192_DATA);
43+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos4(COS8192_DATA);
44+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos5(COS8192_DATA);
45+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos6(COS8192_DATA);
46+
//Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos7(COS8192_DATA); // used to work smoothly in Arduino 1.05
4747

4848
// duplicates but slightly off frequency for adding to originals
49-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos1b(COS8192_DATA);
50-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos2b(COS8192_DATA);
51-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos3b(COS8192_DATA);
52-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos4b(COS8192_DATA);
53-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos5b(COS8192_DATA);
54-
Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos6b(COS8192_DATA);
55-
//Oscil<COS8192_NUM_CELLS, MOZZI_AUDIO_RATE> aCos7b(COS8192_DATA);
49+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos1b(COS8192_DATA);
50+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos2b(COS8192_DATA);
51+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos3b(COS8192_DATA);
52+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos4b(COS8192_DATA);
53+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos5b(COS8192_DATA);
54+
Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos6b(COS8192_DATA);
55+
//Oscil<COS8192_NUM_CELLS, AUDIO_RATE> aCos7b(COS8192_DATA);
5656

5757
// base pitch frequencies in Q16n16 fixed int format (for speed later)
5858
UFix<12,15> f1,f2,f3,f4,f5,f6;//,f7;
@@ -148,19 +148,40 @@ void updateControl(){
148148

149149

150150
AudioOutput updateAudio(){
151+
/*
152+
The following block is the "classical" way of outputting the sound, from a standard C/C++ type.
153+
You need to know how many bits you are dealing with and can use a reduced number to bring in some
154+
distorsion with .clip() if you want.
155+
*/
151156

152-
int asig =
157+
/* int asig =
153158
aCos1.next() + aCos1b.next() +
154159
aCos2.next() + aCos2b.next() +
155160
aCos3.next() + aCos3b.next() +
156161
aCos4.next() + aCos4b.next() +
157162
aCos5.next() + aCos5b.next() +
158163
aCos6.next() + aCos6b.next();// +
159164
// aCos7.next() + aCos7b.next();
160-
/*
161-
auto asig =
162-
toSFraction(aCos1.next()) + toSFraction(aCos1b.next());
163-
164-
return MonoOutput::fromNBit(asig.getNF()+asig.getNI(), asig.asRaw());*/
165165
return MonoOutput::fromAlmostNBit(12, asig);
166+
*/
167+
168+
169+
/*
170+
This is letting Mozzi compute the number of bits for you.
171+
The syntax is a bit more cumbersome but FixMath will be
172+
clever enough to figure out the exact number of bits needed
173+
to create asig without any overflow, but no more.
174+
This number of bits will be used by Mozzi for right/left shifting
175+
the number to match the capability of the system.
176+
*/
177+
auto asig =
178+
toSFraction(aCos1.next()) + toSFraction(aCos1b.next()) +
179+
toSFraction(aCos2.next()) + toSFraction(aCos2b.next()) +
180+
toSFraction(aCos3.next()) + toSFraction(aCos3b.next()) +
181+
toSFraction(aCos4.next()) + toSFraction(aCos4b.next()) +
182+
toSFraction(aCos5.next()) + toSFraction(aCos5b.next()) +
183+
toSFraction(aCos6.next()) + toSFraction(aCos6b.next()); /* +
184+
toSFraction(aCos7.next()) + toSFraction(aCos7b.next()) +*/
185+
return MonoOutput::fromSFix(asig);
186+
166187
}

0 commit comments

Comments
 (0)