Mozzi  version v2.0
sound synthesis library for Arduino
MozziGuts_impl_TEENSY.hpp
1 /*
2  * MozziGuts_impl_STM32duino.hpp
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2013-2024 Tim Barrass 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_TEENSY3() || IS_TEENSY4())
13 # error "Wrong implementation included for this platform"
14 #endif
15 
16 #if (IS_TEENSY3() && F_CPU != 48000000)
17 #warning \
18  "Mozzi has been tested with a cpu clock speed of 16MHz on Arduino and 48MHz on Teensy 3! Results may vary with other speeds."
19 #endif
20 
21 namespace MozziPrivate {
23 #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD)
24 // required from http://github.com/pedvide/ADC for Teensy 3.*
25 } //namespace MozziPrivate
26 #include <ADC.h>
27 namespace MozziPrivate {
28 
29 ADC *adc; // adc object
30 uint8_t teensy_pin; // TODO: this is actually a "channel" according to our terminology, but "channel" and "pin" are equal on this platform
31 int8_t teensy_adc=0;
32 #define getADCReading() adc->readSingle(teensy_adc)
33 #define channelNumToIndex(channel) teensyPinMap(channel)
34 uint8_t adcPinToChannelNum(uint8_t pin) {
35  return pin;
36 }
37 
38 void setupFastAnalogRead(int8_t speed) {
39  adc->adc0->setAveraging(0);
40  adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED); // could be HIGH_SPEED, noisier
41 #ifdef ADC_DUAL_ADCS
42  adc->adc1->setAveraging(0);
43  adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED);
44 #endif
45 }
46 
47 } // namespace MozziPrivate
48 void adc0_isr(void)
49 {
50  MozziPrivate::advanceADCStep();
51 }
52 namespace MozziPrivate {
53 
54 void setupMozziADC(int8_t speed) {
55  adc = new ADC();
56  adc->adc0->enableInterrupts(adc0_isr); // TODO: is this even correct? Does the function take a parameter? And is adc0_isr a predefined name, or are we free to move this to MozziPrivate?
57 #ifdef ADC_DUAL_ADCS
58  adc->adc1->enableInterrupts(adc0_isr);
59 #endif
60 }
61 
62 void adcStartConversion(uint8_t channel) {
63  teensy_pin = channel; // remember for startSecondADCReadOnCurrentChannel()
64 #ifdef ADC_DUAL_ADCS
65  if (adc->adc0->checkPin(teensy_pin)) teensy_adc = 0;
66  else teensy_adc=1;
67 #endif
68  adc->startSingleRead(teensy_pin,teensy_adc);
69 }
70 
71 static void startSecondADCReadOnCurrentChannel() {
72  adc->startSingleRead(teensy_pin,teensy_adc);
73 }
74 #endif // MOZZI_ANALOG_READ
75 
77 
78 
79 
81 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC, MOZZI_OUTPUT_EXTERNAL_TIMED)
82 } // namespace MozziPrivate
83 #include "IntervalTimer.h"
84 namespace MozziPrivate {
85 IntervalTimer timer1;
86 #endif
87 
88 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC)
89 inline void audioOutput(const AudioOutput f) {
90  analogWrite(MOZZI_AUDIO_PIN_1, f.l()+MOZZI_AUDIO_BIAS);
91 # if (MOZZI_AUDIO_CHANNELS > 1)
92  analogWrite(MOZZI_AUDIO_PIN_2, f.r()+MOZZI_AUDIO_BIAS);
93 # endif
94 }
95 #endif
96 
97 static void startAudio() {
98 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC)
99 # if IS_TEENSY3()
100  analogWriteResolution(12);
101 # elif IS_TEENSY4()
102  analogWriteResolution(10);
103  analogWriteFrequency(MOZZI_AUDIO_PIN_1, 146484.38f);
104 # if (MOZZI_AUDIO_CHANNELS > 1)
105  analogWriteFrequency(MOZZI_AUDIO_PIN_2, 146484.38f);
106 # endif // end #if (MOZZI_AUDIO_CHANNELS > 1)
107 # endif // TEENSY3/4
108 #endif
109 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC, MOZZI_OUTPUT_EXTERNAL_TIMED)
110  timer1.begin(defaultAudioOutput, 1000000. / MOZZI_AUDIO_RATE);
111 #endif
112 }
113 
114 void stopMozzi() {
115 #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC, MOZZI_OUTPUT_EXTERNAL_TIMED)
116  timer1.end();
117 #endif
118  interrupts();
119 }
121 
123 void MozziRandPrivate::autoSeed() {
124 #warning Automatic random seeding is not implemented on this platform
125 }
127 
128 } // namespace MozziPrivate
#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.
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
This struct encapsulates one frame of mono audio output.
Definition: AudioOutput.h:111