Mozzi  version v2.0
sound synthesis library for Arduino
config_checks_generic.h
1 /*
2  * config_checks_generic.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2023-2024 Thomas Friedrichsmeier and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10 */
11 
34 #ifndef MOZZI_CONFIG_CHECK_GENERIC_H
35 #define MOZZI_CONFIG_CHECK_GENERIC_H
36 
37 #include "../MozziConfigValues.h" // in case not user-included
38 #include "mozzi_macros.h"
39 
41 #if defined(BYPASS_MOZZI_OUTPUT_BUFFER)
42 #error "BYPASS_MOZZI_OUTPUT_BUFFER may not be customized via config"
43 #endif
44 
45 
47 #if not defined(MOZZI_COMPATIBILITY_LEVEL)
48 #define MOZZI_COMPATIBILITY_LEVEL MOZZI_COMPATIBILITY_2_0
49 #endif
50 
51 #if not defined(MOZZI_AUDIO_CHANNELS)
52 #define MOZZI_AUDIO_CHANNELS MOZZI_MONO
53 #endif
54 
55 //MOZZI_AUDIO_MODE -> hardware specific
56 //MOZZI_AUDIO_RATE -> hardware specific
57 
58 #if not defined(MOZZI_CONTROL_RATE)
59 # if defined(CONTROL_RATE) && (MOZZI_COMPATIBILITY_LEVEL < MOZZI_COMPATIBILITY_LATEST)
60 # warning Please change CONTROL_RATE to MOZZI_CONTROL_RATE
61 # define MOZZI_CONTROL_RATE CONTROL_RATE
62 # else
63 # define MOZZI_CONTROL_RATE 64
64 # endif
65 #endif
66 
67 //MOZZI_ANALOG_READ -> hardware specific, but we want to insert a warning, if not supported, and user has not explicitly configured anything
68 #if not defined(MOZZI_ANALOG_READ)
69 #define MOZZI__ANALOG_READ_NOT_CONFIGURED
70 #endif
71 
72 #if not defined(MOZZI_AUDIO_INPUT)
73 #define MOZZI_AUDIO_INPUT MOZZI_AUDIO_INPUT_NONE
74 #endif
75 
76 #if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) && !defined(MOZZI_AUDIO_INPUT_PIN)
77 #warning Using audio input, but no audio input pin defined, explicitly. Defaulting to pin 0.
78 #define MOZZI_AUDIO_INPUT_PIN 0
79 #endif
80 
81 #if not defined(MOZZI_OUTPUT_BUFFER_SIZE)
82 #define MOZZI_OUTPUT_BUFFER_SIZE 256
83 #endif
84 
85 //MOZZI_PWM_RATE -> hardware specific
86 //MOZZI_AUDIO_PIN_1 -> hardware specific
87 //MOZZI_AUDIO_PIN_1_LOW -> hardware specific
88 //MOZZI_AUDIO_PIN_2 -> hardware specific
89 //MOZZI_AUDIO_PIN_2_LOW -> hardware specific
90 
91 
93 #if IS_AVR()
94 #include "config_checks_avr.h"
95 #elif IS_ESP32()
96 #include "config_checks_esp32.h"
97 #elif IS_ESP8266()
98 #include "config_checks_esp8266.h"
99 #elif IS_MBED()
100 #include "config_checks_mbed.h"
101 #elif IS_RENESAS()
102 #include "config_checks_renesas.h"
103 #elif IS_RP2040()
104 #include "config_checks_rp2040.h"
105 #elif IS_SAMD21()
106 #include "config_checks_samd21.h"
107 #elif IS_STM32DUINO()
108 #include "config_checks_stm32duino.h"
109 #elif IS_STM32MAPLE()
110 #include "config_checks_stm32maple.h"
111 #elif (IS_TEENSY3() || IS_TEENSY4())
112 #include "config_checks_teensy.h"
113 #else
114 #error Problem detecting hardware
115 #endif
116 
117 
119 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM) && !defined(MOZZI_AUDIO_BITS)
120 # define MOZZI_AUDIO_BITS 16
121 #endif
122 
123 
124 
126 MOZZI_CHECK_POW2(MOZZI_AUDIO_RATE)
127 MOZZI_CHECK_POW2(MOZZI_CONTROL_RATE)
128 MOZZI_CHECK_POW2(MOZZI_OUTPUT_BUFFER_SIZE)
129 #if (MOZZI_OUTPUT_BUFFER_SIZE > 256)
130 #error "Mozzi does not support buffer sizes greated than 256 at the moment"
131 #endif
132 
133 #if MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_STANDARD) && MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE)
134 #error "MOZZI_AUDIO_INPUT depends on MOZZI_ANALOG_READ option"
135 #endif
136 
137 #if MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) && defined(MOZZI_AUDIO_INPUT_PIN)
138 #warning "MOZZI_AUDIO_INPUT_PIN defined without MOZZI_AUDIO_INPUT"
139 #endif
140 
141 #if (MOZZI_AUDIO_CHANNELS < MOZZI_MONO) || (MOZZI_AUDIO_CHANNELS > MOZZI_STEREO)
142 #error "MOZZI_AUDIO_CHANNELS outside of (currently) supported range"
143 #endif
144 
145 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED) || MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
146 #warning "Mozzi is configured to use an external void 'audioOutput(const AudioOutput f)' function. Please define one in your sketch"
147 #endif
148 
149 // Hardware-specific checks file should have more narrow checks for most options, below, but is not required to, so let's check for anything that is wildly out of scope:
150 MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM, MOZZI_OUTPUT_PDM_VIA_I2S, MOZZI_OUTPUT_PDM_VIA_SERIAL, MOZZI_OUTPUT_I2S_DAC, MOZZI_OUTPUT_INTERNAL_DAC)
151 MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE, MOZZI_ANALOG_READ_STANDARD)
152 
153 #if defined(MOZZI__ANALOG_READ_NOT_CONFIGURED)
154 # if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE)
155 # warning Asynchronous analog reads not implemented on this platform
156 # endif
157 # undef MOZZI__ANALOG_READ_NOT_CONFIGURED
158 #endif
159 
160 #if defined(MOZZI_ANALOG_READ_RESOLUTION)
161 # if (MOZZI_ANALOG_READ_RESOLUTION < 1) || (MOZZI_ANALOG_READ_RESOLUTION > 16)
162 // NOTE: We could certainly allow more than 16 bits, but then the data type would need to be adjusted/adjustable, accrodingly.
163 # error MOZZI_ANALOG_READ_RESOLUTION must be between 1 and 16 bits
164 # endif
165 #endif
166 
168 #if !defined(MOZZI_AUDIO_BIAS)
169 #define MOZZI_AUDIO_BIAS ((uint16_t) 1<<(MOZZI_AUDIO_BITS-1))
170 #endif
171 
172 #if !defined(MOZZI_AUDIO_BITS_OPTIMISTIC)
173 #define MOZZI_AUDIO_BITS_OPTIMISTIC MOZZI_AUDIO_BITS
174 #endif
175 
176 // TODO: Rename these defines
177 #if MOZZI_AUDIO_RATE == 8192
178 #define AUDIO_RATE_AS_LSHIFT 13
179 #define MICROS_PER_AUDIO_TICK 122
180 #elif MOZZI_AUDIO_RATE == 16384
181 #define AUDIO_RATE_AS_LSHIFT 14
182 #define MICROS_PER_AUDIO_TICK 61 // 1000000 / 16384 = 61.035, ...* 256 = 15625
183 #elif MOZZI_AUDIO_RATE == 32768
184 #define AUDIO_RATE_AS_LSHIFT 15
185 #define MICROS_PER_AUDIO_TICK 31 // = 1000000 / 32768 = 30.518, ...* 256 = 7812.6
186 #elif MOZZI_AUDIO_RATE == 65536
187 #define AUDIO_RATE_AS_LSHIFT 16
188 #define MICROS_PER_AUDIO_TICK 15
189 #else
190 #error Whoopsie, not LSHIFT defined for this audio rate. Please report and/or fix
191 #endif
192 
193 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
194 #define BYPASS_MOZZI_OUTPUT_BUFFER true
195 #endif
196 
198 #if MOZZI_COMPATIBILITY_LEVEL < MOZZI_COMPATIBILITY_LATEST
199 # define AUDIO_RATE MOZZI_AUDIO_RATE
200 # if !defined(CONTROL_RATE)
201 # define CONTROL_RATE MOZZI_CONTROL_RATE
202 # endif
203 #endif
204 
206 // TODO: Rather move this up again, and make AudioOutputStorage_t a primary config option
207 #include "../AudioOutput.h"
208 static_assert(MOZZI_AUDIO_BITS <= (8*sizeof(AudioOutputStorage_t)), "Configured MOZZI_AUDIO_BITS is too large for the internal storage type");
209 
210 #endif
#define MOZZI_AUDIO_BITS
Output resolution of audio samples.
#define MOZZI_OUTPUT_BUFFER_SIZE
Audio buffer setting.
#define MOZZI_CONTROL_RATE
Control rate setting.
#define MOZZI_AUDIO_MODE
Configure how Mozzi outputs generated sounds.
#define MOZZI_AUDIO_RATE
Defines the audio rate, i.e.
#define MOZZI_ANALOG_READ
Whether to compile in support for non-blocking analog reads.