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_CH32()
112 #include "config_checks_CH32.h"
113 #elif (IS_TEENSY3() || IS_TEENSY4())
114 #include "config_checks_teensy.h"
115 #else
116 #error Problem detecting hardware
117 #endif
118 
119 
121 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM) && !defined(MOZZI_AUDIO_BITS)
122 # define MOZZI_AUDIO_BITS 16
123 #endif
124 
125 
126 
128 MOZZI_CHECK_POW2(MOZZI_AUDIO_RATE)
129 MOZZI_CHECK_POW2(MOZZI_CONTROL_RATE)
130 MOZZI_CHECK_POW2(MOZZI_OUTPUT_BUFFER_SIZE)
131 #if (MOZZI_OUTPUT_BUFFER_SIZE > 256)
132 #error "Mozzi does not support buffer sizes greated than 256 at the moment"
133 #endif
134 
135 #if MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_STANDARD) && MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE)
136 #error "MOZZI_AUDIO_INPUT depends on MOZZI_ANALOG_READ option"
137 #endif
138 
139 #if MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) && defined(MOZZI_AUDIO_INPUT_PIN)
140 #warning "MOZZI_AUDIO_INPUT_PIN defined without MOZZI_AUDIO_INPUT"
141 #endif
142 
143 #if (MOZZI_AUDIO_CHANNELS < MOZZI_MONO) || (MOZZI_AUDIO_CHANNELS > MOZZI_STEREO)
144 #error "MOZZI_AUDIO_CHANNELS outside of (currently) supported range"
145 #endif
146 
147 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED) || MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
148 #warning "Mozzi is configured to use an external void 'audioOutput(const AudioOutput f)' function. Please define one in your sketch"
149 #endif
150 
151 // 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:
152 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)
153 MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE, MOZZI_ANALOG_READ_STANDARD)
154 
155 #if defined(MOZZI__ANALOG_READ_NOT_CONFIGURED)
156 # if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE)
157 # warning Asynchronous analog reads not implemented on this platform
158 # endif
159 # undef MOZZI__ANALOG_READ_NOT_CONFIGURED
160 #endif
161 
162 #if defined(MOZZI_ANALOG_READ_RESOLUTION)
163 # if (MOZZI_ANALOG_READ_RESOLUTION < 1) || (MOZZI_ANALOG_READ_RESOLUTION > 16)
164 // NOTE: We could certainly allow more than 16 bits, but then the data type would need to be adjusted/adjustable, accrodingly.
165 # error MOZZI_ANALOG_READ_RESOLUTION must be between 1 and 16 bits
166 # endif
167 #endif
168 
170 #if !defined(MOZZI_AUDIO_BIAS)
171 #define MOZZI_AUDIO_BIAS ((uint16_t) 1<<(MOZZI_AUDIO_BITS-1))
172 #endif
173 
174 #if !defined(MOZZI_AUDIO_BITS_OPTIMISTIC)
175 #define MOZZI_AUDIO_BITS_OPTIMISTIC MOZZI_AUDIO_BITS
176 #endif
177 
178 // TODO: Rename these defines
179 #if MOZZI_AUDIO_RATE == 8192
180 #define AUDIO_RATE_AS_LSHIFT 13
181 #define MICROS_PER_AUDIO_TICK 122
182 #elif MOZZI_AUDIO_RATE == 16384
183 #define AUDIO_RATE_AS_LSHIFT 14
184 #define MICROS_PER_AUDIO_TICK 61 // 1000000 / 16384 = 61.035, ...* 256 = 15625
185 #elif MOZZI_AUDIO_RATE == 32768
186 #define AUDIO_RATE_AS_LSHIFT 15
187 #define MICROS_PER_AUDIO_TICK 31 // = 1000000 / 32768 = 30.518, ...* 256 = 7812.6
188 #elif MOZZI_AUDIO_RATE == 65536
189 #define AUDIO_RATE_AS_LSHIFT 16
190 #define MICROS_PER_AUDIO_TICK 15
191 #else
192 #error Whoopsie, not LSHIFT defined for this audio rate. Please report and/or fix
193 #endif
194 
195 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
196 #define BYPASS_MOZZI_OUTPUT_BUFFER true
197 #endif
198 
200 #if MOZZI_COMPATIBILITY_LEVEL < MOZZI_COMPATIBILITY_LATEST
201 # define AUDIO_RATE MOZZI_AUDIO_RATE
202 # if !defined(CONTROL_RATE)
203 # define CONTROL_RATE MOZZI_CONTROL_RATE
204 # endif
205 #endif
206 
208 // TODO: Rather move this up again, and make AudioOutputStorage_t a primary config option
209 #include "../AudioOutput.h"
210 static_assert(MOZZI_AUDIO_BITS <= (8*sizeof(AudioOutputStorage_t)), "Configured MOZZI_AUDIO_BITS is too large for the internal storage type");
211 
212 #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.