Mozzi  version v2.0
sound synthesis library for Arduino
MozziGuts_impl_MBED.hpp
1 /*
2  * MozziGuts_impl_MBED.hpp
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2023-2024 T. Combriat 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 
12 #if !(IS_MBED())
13 # error "Wrong implementation included for this platform"
14 #endif
15 
16 #define CHUNKSIZE 64
17 
18 namespace MozziPrivate {
19 
21 
22 #if MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_STANDARD)
23 #define MOZZI__LEGACY_AUDIO_INPUT_IMPL 0
24 
25 } // namespace MozziPrivate
26 #include <Arduino_AdvancedAnalog.h>
27 namespace MozziPrivate {
28 
29 AdvancedADC adc(MOZZI_AUDIO_INPUT_PIN);
30 Sample inbuf[CHUNKSIZE];
31 int inbufpos=0;
32 
33 bool audioInputAvailable() {
34  if (inbufpos >= CHUNKSIZE) {
35  if (!adc.available()) return false;
36  SampleBuffer buf = adc.read();
37  memcpy(inbuf,buf.data(), CHUNKSIZE*sizeof(Sample));
38  inbufpos = 0;
39  buf.release();
40  return true;
41  }
42  else return true;
43 }
44 AudioOutputStorage_t readAudioInput(){
45  return inbuf[inbufpos++];
46 }
47 
48 
49 static void startAudioInput() {
50  if (!adc.begin(AN_RESOLUTION_12, MOZZI_AUDIO_RATE, CHUNKSIZE, 256/CHUNKSIZE)) {
51  Serial.println("Failed to start analog acquisition!");
52  while (1);
53  }
54 }
55 #else
56 static void startAudioInput() {}; // dummy to ease coding
57 #endif
58 
59 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
60 #error not yet implemented
65 // Insert here code to read the result of the latest asynchronous conversion, when it is finished.
66 // You can also provide this as a function returning unsigned int, should it be more complex on your platform
67 #define getADCReading() 0
68 
81 // NOTE: Theoretically, adcPinToChannelNum is public API for historical reasons, thus cannot be replaced by a define
82 uint8_t adcPinToChannelNum(uint8_t pin) {
83  return pin;
84 }
85 
87 void adcStartConversion(uint8_t channel) {
88 }
89 
92 void startSecondADCReadOnCurrentChannel() {
93 }
94 
97 void setupFastAnalogRead(int8_t speed) {
98 }
99 
102 void setupMozziADC(int8_t speed) {
103 }
104 
105 #endif
106 
108 
110 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED)
111 
112 #define US_PER_AUDIO_TICK (1000000L / MOZZI_AUDIO_RATE)
113 } // namespace MozziPrivate
114 #include <mbed.h>
115 namespace MozziPrivate {
116 mbed::Ticker audio_output_timer;
117 
118 volatile bool audio_output_requested = false;
119 inline void defaultAudioOutputCallback() {
120  audio_output_requested = true;
121 }
122 
123 #define AUDIO_HOOK_HOOK { if (audio_output_requested) { audio_output_requested = false; defaultAudioOutput(); } }
124 
125 static void startAudio() {
126  audio_output_timer.attach_us(&defaultAudioOutputCallback, US_PER_AUDIO_TICK);
127  startAudioInput();
128 }
129 
130 void stopMozzi() {
131  audio_output_timer.detach();
132 }
133 
134 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM)
135 // Demand-driven: no timer is claimed and no interrupt is installed. The sketch
136 // drives audioHook() itself and provides audioOutput() / canBufferAudioOutput(),
137 // so startAudio() has nothing to attach and stopMozzi() nothing to detach.
138 // startAudioInput() is still called so a sketch using
139 // MOZZI_AUDIO_INPUT_STANDARD gets its acquisition running.
140 static void startAudio() {
141  startAudioInput();
142 }
143 
144 void stopMozzi() {
145 }
146 
147 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC)
148 
149 } // namespace MozziPrivate
150 #include <Arduino_AdvancedAnalog.h>
151 namespace MozziPrivate {
152 
153 AdvancedDAC dac1(MOZZI_AUDIO_PIN_1);
154 Sample buf1[CHUNKSIZE];
155 #if (MOZZI_AUDIO_CHANNELS > 1)
156 AdvancedDAC dac2(MOZZI_AUDIO_PIN_2);
157 Sample buf2[CHUNKSIZE];
158 #endif
159 int bufpos = 0;
160 
161 inline void commitBuffer(Sample buffer[], AdvancedDAC &dac) {
162  SampleBuffer dmabuf = dac.dequeue();
163  // NOTE: Yes, this is silly code, and originated as an accident. Somehow it appears to help _a little_ against current problem wrt DAC stability
164  for (unsigned int i=0;i<CHUNKSIZE;i++) memcpy(dmabuf.data(), buffer, CHUNKSIZE*sizeof(Sample));
165  dac.write(dmabuf);
166 }
167 
168 inline void audioOutput(const AudioOutput f) {
169  if (bufpos >= CHUNKSIZE) {
170  commitBuffer(buf1, dac1);
171 #if (MOZZI_AUDIO_CHANNELS > 1)
172  commitBuffer(buf2, dac2);
173 #endif
174  bufpos = 0;
175  }
176  buf1[bufpos] = f.l()+MOZZI_AUDIO_BIAS;
177 #if (MOZZI_AUDIO_CHANNELS > 1)
178  buf2[bufpos] = f.r()+MOZZI_AUDIO_BIAS;
179 #endif
180  ++bufpos;
181 }
182 
183 bool canBufferAudioOutput() {
184  return (bufpos < CHUNKSIZE || (dac1.available()
185 #if (MOZZI_AUDIO_CHANNELS > 1)
186  && dac2.available()
187 #endif
188  ));
189 }
190 
191 static void startAudio() {
192  //NOTE: DAC setup currently affected by https://github.com/arduino-libraries/Arduino_AdvancedAnalog/issues/35 . Don't expect this to work, until using a fixed version fo Arduino_AdvancedAnalog!
193  if (!dac1.begin(AN_RESOLUTION_12, MOZZI_AUDIO_RATE, CHUNKSIZE, 256/CHUNKSIZE)) {
194  Serial.println("Failed to start DAC1 !");
195  while (1);
196  }
197 #if (MOZZI_AUDIO_CHANNELS > 1)
198  if (!dac2.begin(AN_RESOLUTION_12, MOZZI_AUDIO_RATE, CHUNKSIZE, 256/CHUNKSIZE)) {
199  Serial.println("Failed to start DAC2 !");
200  while (1);
201  }
202 #endif
203  startAudioInput();
204 }
205 
206 void stopMozzi() {
207  dac1.stop();
208 #if (MOZZI_AUDIO_CHANNELS > 1)
209  dac2.stop();
210 #endif
211 }
212 
213 #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PDM_VIA_SERIAL)
214 
215 } // namespace MozziPrivate
216 #include <mbed.h>
217 namespace MozziPrivate {
218 
219 mbed::BufferedSerial serial_out1(digitalPinToPinName(MOZZI_SERIAL_PIN_TX), digitalPinToPinName(MOZZI_SERIAL_PIN_RX));
220 uint8_t buf[MOZZI_PDM_RESOLUTION*4];
221 
222 bool canBufferAudioOutput() {
223  return serial_out1.writable();
224 }
225 
226 inline void audioOutput(const AudioOutput f) {
227  for (uint8_t i = 0; i < MOZZI_PDM_RESOLUTION*4; ++i) {
228  buf[i] = pdmCode8(f.l()+MOZZI_AUDIO_BIAS);
229  }
230  serial_out1.write(&buf, MOZZI_PDM_RESOLUTION*4);
231 }
232 
233 static void startAudio() {
234  serial_out1.set_baud(MOZZI_AUDIO_RATE*MOZZI_PDM_RESOLUTION*40); // NOTE: 40 == 4 * (8 bits + stop-bits)
235  serial_out1.set_format(8, mbed::BufferedSerial::None, 1);
236 }
237 
238 void stopMozzi() {
239 #warning implement me
240 }
241 
242 
243 #endif
245 
247 void MozziRandPrivate::autoSeed() {
248 #warning Automatic random seeding is not implemented on this platform
249 }
251 
252 } // namespace MozziPrivate
253 
254 #undef CHUNKSIZE
255 #undef US_PER_AUDIO_TICK
Sample is like Oscil, it plays a wavetable.
Definition: Sample.h:49
#define MOZZI_AUDIO_CHANNELS
This sets allows to change from a single/mono audio output channel to stereo output.
#define MOZZI_AUDIO_PIN_1
Only for MOZZI_AUDIO_MODE s MOZZI_OUTPUT_PWM and MOZZI_OUTPUT_2PIN_PWM: The IO pin to use as (first) ...
#define MOZZI_AUDIO_RATE
Defines the audio rate, i.e.
#define MOZZI_AUDIO_INPUT_PIN
This sets which analog input channel to use for audio input, if you have enabled MOZZI_AUDIO_INPUT,...
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
Definition: MozziGuts.hpp:305
Internal.
Definition: mozzi_rand_p.h:15
This struct encapsulates one frame of mono audio output.
Definition: AudioOutput.h:111