Mozzi  version v2.0
sound synthesis library for Arduino
MozziGuts_impl_template.hpp
Go to the documentation of this file.
1 /*
2  * MozziGuts_impl_template.hpp
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 
35 // The main point of this check is to document, what platform & variants this implementation file is for.
36 #if !(IS_MYPLATFORM())
37 # error "Wrong implementation included for this platform"
38 #endif
39 // Add platform specific includes and declarations, here
40 
41 //#include <my_hardware_header.h>
42 
43 // In order to allow simple yet efficient user configuration, the entire contents of this file are compiled in the same translation unit
44 // as (the main file of) the user sketch. To avoid name clashes, we encapsulate everyghing in a namespace.
45 // For the most part, this namescape can just extend from the start of the file to the end (as shown, here), and you will not have to
46 // worry about it. However, there may be a few situations, where you have to "leave" the MozziPrivate namespace. This includes:
47 // - When you include a further (hardware-dependent library). Consider gathering all includes at the top of this file, instead.
48 // - When you provide definitions for special names, importantly for ISR-functions. If these would be placed in the namespace, the linker would not
49 // recognize them as the definition of the intended ISR-vector. See MozziGuts_impl_AVR.hpp for examples.
50 
51 namespace MozziPrivate {
52 
54 
55 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
68 #error not yet implemented
69 
70 // Insert here code to read the result of the latest asynchronous conversion, when it is finished.
71 // You can also provide this as a function returning unsigned int, should it be more complex on your platform
72 #define getADCReading() GET_MY_PLATFORM_ADC_REGISTER
73 
86 // NOTE: Theoretically, adcPinToChannelNum is public API for historical reasons, thus cannot be replaced by a define
87 #define channelNumToIndex(channel) channel
88 uint8_t adcPinToChannelNum(uint8_t pin) {
89  return pin;
90 }
91 
93 void adcStartConversion(uint8_t channel) {
94 }
95 
98 void startSecondADCReadOnCurrentChannel() {
99 }
100 
103 void setupMozziADC(int8_t speed) {
104  setupFastAnalogRead(speed);
105  // insert further custom code
106 }
107 
108 /* NOTE: Most platforms call a specific function/ISR when conversion is complete. Provide this function, here.
109  * From inside its body, simply call advanceADCStep(). E.g.:
110 void stm32_adc_eoc_handler() {
111  advanceADCStep();
112 }
113 */
114 
117 void setupFastAnalogRead(int8_t speed) {
118 }
119 
120 #endif
121 
123 
125 /* NOTE: Some platforms rely on control returning from loop() every so often. However, updateAudio() may take too long (it tries to completely fill the output buffer,
126  * which of course is being drained at the same time, theoretically it may not return at all). If you set this define, it will be called once per audio frame to keep things
127  * running smoothly. */
128 //#define LOOP_YIELD yield();
129 
130 /* NOTE: On some platforms, what can be called in the ISR used to output the sound is limited.
131  * This define can be used, for instance, to output the sound in audioHook() instead to overcome
132  * this limitation (see MozziGuts_impl_MBED.hpp). It can also be used if something needs to be called in audioHook() regarding
133  * analog reads for instance. */
134 //#define AUDIO_HOOK_HOOK
135 
136 /* NOTE: Code sections that are needed for a certain audio mode, only, should be guarded as follows (in this example, code will compile for the
137  * two modes MOZZI_OUTPUT_PWM, and MOZZI_OUTPUT_INTERNAL_DAC (should your port happen to support these two).
138  *
139  * Keep in mind that you probably want to support MOZZI_OUTPUT_EXTERNAL_TIMED, and MOZZI_OUTPUT_EXTERNAL_CUSTOM, too, which is usually very
140  * easy: For both, do *not* provide an audioOutput() function, as this will be provided by the user. For MOZZI_OUTPUT_EXTERNAL_TIMED make sure some
141  * timer is set up to call defaultAudioOutput() at MOZZI_AUDIO_RATE. For MOZZI_OUTPUT_EXTERNAL_CUSTOM, nothing else will be needed. */
142 
143 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC) // just an example!
145 inline void audioOutput(const AudioOutput f) {
146  // e.g. analogWrite(MOZZI_AUDIO_CHANNEL_1_PIN, f.l()+MOZZI_AUDIO_BIAS);
147 # if (MOZZI_AUDIO_CHANNELS > 1)
148  // e.g. analogWrite(MOZZI_AUDIO_CHANNEL_2_PIN, f.r()+MOZZI_AUDIO_BIAS);
149 # endif
150 }
151 #endif
152 
153 static void startAudio() {
154  // Add here code to get audio output going. This usually involves:
155  // 1) setting up some DAC mechanism (e.g. setting up a PWM pin with appropriate resolution
156  // 2a) setting up a timer to call defaultAudioOutput() at MOZZI_AUDIO_RATE
157  // OR 2b) setting up a buffered output queue such as I2S (see ESP32 / ESP8266 for examples for this setup)
158 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM)
159  // [...]
160 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC)
161  // [...]
162 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
163  // remember that the user may configure MOZZI_OUTPUT_EXTERNAL_TIMED, in which case, you'll want to provide step 2a), and only that.
164 #endif
165 }
166 
167 void stopMozzi() {
168  // Add here code to pause whatever mechanism moves audio samples to the output
169 }
171 
173 void MozziRandPrivate::autoSeed() {
174  // Add here code to initialize the values of MozziRandPrivate::x, MozziRandPrivate::y, and MozziRandPrivate::z to some random values
175  // This doesn't need to be crypographically safe. If nothing better is available, e.g. try reading an internal temperature sensor
176  // in order to get some noise. It also doesn't have to be fast.
177  // It *should* however ensure that rand() sequences will differ across reboots, after randSeed() has been called.
178  // x, y, and z are already initialized to non-zero, when this function is called.
179  // It's ok to leave this unimplemented, initially.
180 #warning Automatic random seeding is not implemented on this platform
181 }
183 
184 } // namespace MozziPrivate
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
Definition: MozziGuts.hpp:303
Internal.
Definition: mozzi_rand_p.h:15