Mozzi  version v2.0
sound synthesis library for Arduino
mozzi_config_documentation.h
Go to the documentation of this file.
1 #ifdef FOR_DOXYGEN_ONLY
2 /** @file */
3 
4 /*! @defgroup config Mozzi Configuration options */
5 
6 /** @ingroup config
7  * @page config_main Mozzi Configuration
8  *
9  * @section config_general Configuring Mozzi
10  *
11  * Mozzi configuration options include details such as audio rate, number of audio channels (mono or stereo), output generation method and many others,
12  * where details on the available options differ between the different platforms (see @ref hardware), and may include additional options beyond those listed, here.
13  *
14  * @note
15  * It is generally safe to leave the Mozzi Configuration unchanged, and that's very much recommended _until_ you have a very specific need to customize something.
16  * Contrary to past versions of Mozzi, all configuration options have a (usually sensible) default value, so you do not have to configure _anything_, unless you
17  * actually want to change something.
18  *
19  * Configuring Mozzi is mostly done using various preprocessor definitions. This approach is used to move as much of the processing involved to compile time, in order
20  * to save Flash, RAM, and CPU use at runtime. This section lists various global options, but in addition, most ports allow additional hardware dependent
21  * configuration options. See @ref hardware.
22  *
23  * Several configuration examples are provided in the "config" folder of the Mozzi sources. You may want to look at these, first. The general approach is as follows:
24  *
25  * @code
26  * #include <MozziConfigValues.h> // include this first, for named option values
27  * #define MOZZI_AUDIO_CHANNELS MOZZI_STEREO // set to stereo mode
28  *
29  * #include <Mozzi.h> // *after* all configuration options, include the main Mozzi headers
30  * @endcode
31  *
32  * Alternatively, if a suitable configuration example exists, use:
33  * @code
34  * #include <config/config_example_avr_stereo.h> // again, do this, before including the main headers
35  * @endcode
36  *
37  * @note
38  * Should you include Mozzi headers in more than one compilation unit (i.e. more than one .cpp file) inside the same sketch, you *must* use identical
39  * configuration options at the top of each file!
40  *
41  * TODO: Fix and complete Doxygen coverage
42 */
43 
44 /** @ingroup config
45  * @def MOZZI_COMPATIBILITY_LEVEL
46  *
47  * Mozzi generally tries to keep your old sketches working, but we continue to find new (and hopefully better) approaches to old problems.
48  * Sometimes, keeping API compatibilty with the pre-existing solution may come with a smaller or larger penalty in terms of performance or code size.
49  * Therefore - if your sketch supports it - you may be able to get some minor benefit from disabling compatibility code.
50  *
51  * Currently supported values are:
52  * - MOZZI_COMPATIBILITY_1_1 - try to support sketches written for Mozzi version 1.1 (or possibly lower); this is the default when including MozziGuts.h
53  * - MOZZI_COMPATIBILITY_2_0 - try to support sketches written for Mozzi version 2.0; this is - currently - the default when including Mozzi.h
54  * - MOZZI_COMPATIBILITY_LATEST - always live on the bleeding edge
55  *
56  * @note
57  * MOZZI_COMPATIBILITY_V1_1 does not guarantee, that *everything* from Mozzi 1.1 will continue to work, just that we're doing a reasonable effort.
58 */
59 #define MOZZI_COMPATIBILITY_LEVEL FOR_DOXYGEN_ONLY
60 
61 /** @ingroup config
62  * @def MOZZI_AUDIO_MODE
63  *
64  * @brief Configure how Mozzi outputs generated sounds.
65  *
66  * @note
67  * Not all options are available on all platforms, and several options require specific wiring or external components to function on top of this!
68  * When customizing this, it is highly recommended to start experimenting with a simple and known-to-work sketch (such as a basic sinewave) to verify that your
69  * hardware setup is correct. Similarly, if you observe problems running your "real" sketch, it is often a good idea ot test your sketch with the default audio mode,
70  * too (by leaving this option, and preferrably all others, unset).
71  *
72  * Refer to the @ref hardware specific documentation for which modes are supported on your hardware, and further details!
73  *
74  * Supported values:
75  * - MOZZI_OUTPUT_PWM Output using pulse width modulation (PWM) on a GPIO pin. This is the default on most platforms.
76  * On the Arduino Uno (more generally ATMEGA328P), this allows for a sample resolution of 488 (almost 9 bits) on pin 9.
77  * Usable pins and resolution will be different on other boards.
78  * - MOZZI_OUTPUT_2PIN_PWM Output using pulse width modulation on two GPIO pins, where one pin represents the lower bits, and the other the higer bits of the sample.
79  * On the Aduino Uno, this allows for 14 bits of resolution on pins 9 (low) and 10 (high). For further information (wiring etc.) see @ref hardware_avr_2pin.
80  * - MOZZI_OUTPUT_EXTERNAL_TIMED Output is not controlled by Mozzi itself, but left to the user sketch. This setting allows to completely customize the audio output, e.g.
81  * for connecting to external DACs. For more detail, see @ref external_audio
82  * - MOZZI_OUTPUT_EXTERNAL_CUSTOM As above, but additionally bypassing Mozzi's sample buffer. For more detail, see @ref external_audio
83  * - MOZZI_OUTPUT_PDM_VIA_I2S Output pulse density modulated (PDM) samples via a (hardware) I2S interface (without a DAC connected to it).
84  * - MOZZI_OUTPUT_PDM_VIA_SERIAL Output pulse density modulated (PDM) samples via a hardware serial interface.
85  * - MOZZI_OUTPUT_I2S_DAC Output samples to a PT8211 (or compatible) DAC connected to a hardware I2S interface.
86  * - MOZZI_OUTPUT_INTERNAL_DAC Output to the interal DAC on boards that support one.
87  *
88  * TODO: Adding an R2R-DAC option would be cool, http://blog.makezine.com/2008/05/29/makeit-protodac-shield-fo/ , some discussion on Mozzi-users.
89 */
90 #define MOZZI_AUDIO_MODE FOR_DOXYGEN_ONLY
91 
92 /** @ingroup config
93  * @def MOZZI_AUDIO_CHANNELS
94  *
95  * This sets allows to change from a single/mono audio output channel to
96  * stereo output. To actually generate two channels, your updateAudio()-function
97  * should return a StereoOutput(). Sketches returning a MonoOutput() in a stereo
98  * config, or vice versa will continue to work, but will generate a warning a
99  * compile time.
100  *
101  * @note This option superseeds the earlier STEREO_HACK in Mozzi < 1.1
102  *
103  * @note At the time of this writing, only MOZZI_MONO and MOZZI_STEREO are supported. The value of
104  * MOZZI_MONO is 1 and the value of MOZZI_STEREO is 2, so future extensions are also expected
105  * to set this to the number of available channels, and it's ok to use numerical comparison. */
106 #define MOZZI_AUDIO_CHANNELS FOR_DOXYGEN_ONLY
107 
108 
109 /** @ingroup config
110  * @def MOZZI_AUDIO_RATE
111  *
112  * @brief Defines the audio rate, i.e. rate of samples output per second.
113  *
114  * The default rate on the classis Arduino Uno is 16384 Hz, but can be increased to 32768 Hz, subject to the caveats, detailed below. For most other platforms 32678 Hz
115  * is the default, but even higher rates may be supported.
116  *.
117  * Increasing the rate allows for better frequency response, but generally all affects achievable sample bitdepth (especially from PWM output).
118  * Also, of course, doubling the sample rate also halves the amount of time available to calculate the each sample, so it
119  * may only be useful for relatively simple sketches. The increased frequency response can also make
120  * unwanted artefacts of low resolution synthesis calculations more apparent, so it's not always a bonus.
121  *
122  * It is highly recommended to keep the audio rate a power of two (16384, 32678, 64536, etc.), as some internal calculations can be highly be optimised for speed, this way.
123  *
124  * @note
125  * For compatibility reasons, the option MOZZI_AUDIO_RATE is automatically set to the same value as this option, and you will find some uses of that in old (pre Mozzi 2.0) code examples.
126  * It is advised to use only MOZZI_AUDIO_RATE in new code, however.
127  * TODO: Only do the above, for MOZZI_COMPATIBILITY_LEVEL < MOZZI_COMPATIBILITY_2_0?
128  */
129 #define MOZZI_AUDIO_RATE FOR_DOXYGEN_ONLY
130 
131 
132 /** @ingroup config
133  * @def MOZZI_CONTROL_RATE
134  *
135  * @brief Control rate setting.
136  *
137  * Mozzi's MOZZI_CONTROL_RATE sets how many times per second updateControl() is called.
138  * MOZZI_CONTROL_RATE has a default of 64 Hz. It is useful to have MOZZI_CONTROL_RATE set at a power of 2 (such as 64,128,256 etc),
139  * to have exact timing of audio and control operations. Non-power-of-2 MOZZI_CONTROL_RATE can cause glitches due to audio and control
140  * events not lining up precisely. If this happens a power of two MOZZI_CONTROL_RATE might solve it.
141  *
142  * Try to keep MOZZI_CONTROL_RATE low, for efficiency, though higher rates up to about 1000
143  * can sometimes give smoother results, avoiding the need to interpolate
144  * sensitive variables at audio rate in updateAudio().
145  *
146  * TODO: If a definition of MOZZI_CONTROL_RATE is detected, apply that with a warning.
147 */
148 #define MOZZI_CONTROL_RATE FOR_DOXYGEN_ONLY
149 
150 
151 /** @ingroup config
152  * @def MOZZI_ANALOG_READ
153  *
154  * Whether to compile in support for non-blocking analog reads. This is enabled by default on platforms that support it, but may be
155  * disabled, explicitly, to save resources, or in order to implement custom read schemes (e.g. with IO multiplexing).
156  *
157  * For simplicity, mozziAnalogRead() is always defined, but when MOZZI_ANALOG_READ s are disabled or unsupported, it simply relays
158  * to Arduino's regular analogRead(). It is thus quite recommended _not_ to depend on mozziAnalogRead() when disabling this.
159  *
160  * As a rough estimate (your numbers may differ a bit, depending on compiler version, etc.), on an ATMEGA328P (aka Arduino Uno),
161  * disabling analog reads saves 33 bytes of RAM and 340 bytes of FLASH. The performance savings are theorized to be neglegible, however.
162  *
163  * Currently allowed values are:
164  * - MOZZI_ANALOG_READ_NONE
165  * Disabled
166  * - MOZZI_ANALOG_READ_STANDARD
167  * Analog read implementation enabled (currently there is only the "standard" method, but future versions might allow additional choice, here).
168 */
169 #define MOZZI_ANALOG_READ FOR_DOXYGEN_ONLY
170 
171 
172 /** @ingroup config
173  * @def MOZZI_AUDIO_INPUT
174  *
175  * Whether to enable built in audio input feature. This is not supported on all platforms, and
176  * on platforms that do support it may come with a considerable performance overhead. Don't enable, unless you need this.
177  *
178  * Currently allowed values are:
179  * - MOZZI_AUDIO_INPUT_NONE
180  * No audio input
181  * - MOZZI_AUDIO_INPUT_STANDARD
182  * Audio input enabled (currently there is only the "standard" method, but future versions might allow additional choice, here).
183  * This mode implies that MOZZI_ANALOG_READ s are enabled and supported. You may have to call setupFastAnalogReads(FASTEST_ADC)
184  * after setupMozzi(), when using this.
185  *
186  * Further reading and config: @ref getAudioInput() @ref MOZZI_AUDIO_INPUT_PIN
187 */
188 #define MOZZI_AUDIO_INPUT FOR_DOXYGEN_ONLY
189 
190 
191 /** @ingroup config
192  * @def MOZZI_AUDIO_INPUT_PIN
193  *
194  * This sets which analog input channel to use for audio input, if you have enabled MOZZI_AUDIO_INPUT, above.
195  * Not all pins may be available for this, be sure to check the documentation for your platform.
196 */
197 #define MOZZI_AUDIO_INPUT_PIN FOR_DOXYGEN_ONLY
198 
199 
200 /** @ingroup config
201  * @def MOZZI_PWM_RATE
202  *
203  * <em>Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM</em>. On some platforms, the rate at which PWM signals are repeated may be higher
204  * than that at with audio signals are produced (i.e. MOZZI_AUDIO_RATE). E.g. for MOZZI_OUTPUT_PWM on the classic Arduino, the pwm defaults to 32768 while the
205  * audio rate defaults to 16384. The reasoning behind this is that 16384 Hz audio rate turned out to be te most useful compromise - in most casses - between
206  * output quality, and available computing power. However, output at that rate produced high-frequency whine, audible to some people, which could be mitigated
207  * by the higher PWM rate.
208  *
209  * In other words, increasing this improves the signal quality at less cost than doubling the audio rate itself. However, increasing this too far will limit the dynamic resolution of the samples that can be
210  * written to the output pin(s): 2 ^ (output bits) * MOZZI_PWM_RATE cannot be higher than the CPU frequency!
211 */
212 #define MOZZI_PWM_RATE FOR_DOXYGEN_ONLY
213 
214 
215 /** @ingroup config
216  * @def MOZZI_AUDIO_BITS_PER_CHANNEL
217  *
218  * <em>Only for MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM</em>. Sample resolution per channel to use in 2 pin output, given in bits (i.e. total resolution is twice as much).
219  * Defaults to 7 bits per channel. Note that increasing this requires very, very well matched output resistors.
220  *
221  * See @ref hardware_avr for a more detailed description.
222 */
223 #define MOZZI_AUDIO_BITS_PER_CHANNEL FOR_DOXYGEN_ONLY
224 
225 
226 /** @ingroup config
227  * @def MOZZI_AUDIO_PIN_1
228  *
229  * Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM: The IO pin to use as (first) audio output. This **must** be attached to Timer1.
230  * When settings this, you alsso need to specify the output compare register responsible for this pin (either OCR1A or OCR1B).
231  *
232  * Example:
233  * @code
234  * #define MOZZI_AUDIO_PIN_1 TIMER1_B_PIN
235  * #define MOZZI_AUDIO_PIN_1_REGISTER OCR1B // must also specify this, when customizing MOZZI_AUDIO_PIN_1
236  * @endcode
237  *
238  * Equivalent definitions can be used to control the pin for the right audio channel (in stereo mode), or the low byte channel (in 2 Pin PWM mode):
239  *
240  * @code
241  * #define MOZZI_AUDIO_PIN_2 [...]
242  * #define MOZZI_AUDIO_PIN_2_REGISTER [the matching OCR]
243  * // or
244  * #define MOZZI_AUDIO_PIN_1_LOW [...]
245  * #define MOZZI_AUDIO_PIN_1_LOW_REGISTER [the matching OCR]
246  * @endcode
247  *
248  * @see config/known_16bit_timers.h
249  * */
250 #define MOZZI_AUDIO_PIN_1 FOR_DOXYGEN_ONLY
251 
252 
253 /***************************************** ADVANCED SETTTINGS -- External audio output ******************************************
254  *
255  * The settings in the following section applies to MOZZI_OUTPUT_EXTERNAL_TIMED, and MOZZI_OUTPUT_EXTERNAL_CUSTOM, only.
256  *
257 ********************************************************************************************************************************/
258 
259 /** @ingroup audio_output
260  *
261  * @page external_audio External audio output
262  * @details
263  * <em>Only for @ref MOZZI_AUDIO_MODE set to MOZZI_OUTPUT_EXTERNAL_TIMED or MOZZI_OUTPUT_EXTERNAL_CUSTOM</em>. Most (all?) platforms support
264  * output using an "external" function. When using this option, you will need to provide a suitable definition for audioOutput() in
265  * your own sketch, yourself. Some understanding of the general Mozzi audio output architecture may be recommendable, when using this
266  * mode: See @ref AudioOutput .
267  *
268  * In the more simple case (MOZZI_OUTPUT_EXTERNAL_TIMED), Mozzi will still take care of buffering the samples, and calling this function
269  * at audio rate (hence "timed"). This generally involves use of a timer, which should be detailed in the @ref hardware details for
270  * your platform.
271  *
272  * Should you desire even more control - perhaps because your board, or your external DAC already comes with a rate controlled DMA buffer -
273  * using MOZZI_OUTPUT_EXTERNAL_CUSTOM also bypasses Mozzis sample buffer. In addition to audioOutput(), you will then need to provide
274  * a definition for canBufferAudioOutput(), which will control the rate at which samples are produced. In essence, whenever
275  * canBufferAudioOutput() returns true, Mozzi will call updateAudio(), and pass the produced sample to audioOutput(), unbuffered. It is
276  * entirely your job to make sure that this actually happens at MOZZI_AUDIO_RATE, and / or an appropriate buffer gets used.
277  *
278  * One additional configuration setting is MOZZI_AUDIO_BITS, which defaults to 16 bits for this mode, but might be set higher, if your
279  * hardware supports it.
280  *
281  * @see config
282 */
283 
284 
285 /** @ingroup config
286  * @def MOZZI_AUDIO_BITS
287  *
288  * Output resolution of audio samples. In most cases you should leave this value untouched (for the defaults that get applied, see @ref hardware .
289  * However, for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_EXTERNAL_TIMED and MOZZI_OUTPUT_EXTERNAL_CUSTOM you way wish to customize the default value
290  * of 16 bits.
291  *
292  * @note
293  * At the time of this writng single audio samples are stored as "int", unconditionally. This limits MOZZI_AUDIO_BITS to a maximum of 16 bits on
294  * some 8 bit boards!
295  */
296 #define MOZZI_AUDIO_BITS FOR_DOXYGEN_ONLY
297 
298 #endif