Mozzi  version v1.1.0
sound synthesis library for Arduino
MozziGuts_impl_TEENSY.hpp
1 /*
2  * MozziGuts.cpp
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi by Tim Barrass is licensed under a Creative Commons
9  * Attribution-NonCommercial-ShareAlike 4.0 International License.
10  *
11  */
12 
13 #if !(IS_TEENSY3() || IS_TEENSY4())
14 # error "Wrong implementation included for this platform"
15 #endif
16 
17 // required from http://github.com/pedvide/ADC for Teensy 3.*
18 #include "IntervalTimer.h"
19 #include <ADC.h>
20 #include "teensyPinMap.h"
21 
22 #if (IS_TEENSY3() && F_CPU != 48000000)
23 #warning
24  "Mozzi has been tested with a cpu clock speed of 16MHz on Arduino and 48MHz on Teensy 3! Results may vary with other speeds."
25 #endif
26 
27 ////// BEGIN analog input code ////////
28 #define MOZZI_FAST_ANALOG_IMPLEMENTED
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 void adc0_isr(void)
48 {
49  advanceADCStep();
50 }
51 
52 void setupMozziADC(int8_t speed) {
53  adc = new ADC();
54  adc->adc0->enableInterrupts(adc0_isr);
55 #ifdef ADC_DUAL_ADCS
56  adc->adc1->enableInterrupts(adc0_isr);
57 #endif
58 }
59 
60 void adcStartConversion(uint8_t channel) {
61  teensy_pin = channel; // remember for startSecondADCReadOnCurrentChannel()
62 #ifdef ADC_DUAL_ADCS
63  if (adc->adc0->checkPin(teensy_pin)) teensy_adc = 0;
64  else teensy_adc=1;
65 #endif
66  adc->startSingleRead(teensy_pin,teensy_adc);
67 }
68 
69 static void startSecondADCReadOnCurrentChannel() {
70  adc->startSingleRead(teensy_pin,teensy_adc);
71 }
72 
73 ////// END analog input code ////////
74 
75 
76 
77 //// BEGIN AUDIO OUTPUT code ///////
78 IntervalTimer timer1;
79 
80 #if (EXTERNAL_AUDIO_OUTPUT != true) // otherwise, the last stage - audioOutput() - will be provided by the user
81 #if IS_TEENSY3()
82 #include "AudioConfigTeensy3_12bit.h"
83 #elif IS_TEENSY4()
84 #include "AudioConfigTeensy4.h"
85 #endif
86 inline void audioOutput(const AudioOutput f) {
87  analogWrite(AUDIO_CHANNEL_1_PIN, f.l()+AUDIO_BIAS);
88 #if (AUDIO_CHANNELS > 1)
89  analogWrite(AUDIO_CHANNEL_2_PIN, f.r()+AUDIO_BIAS);
90 #endif
91 }
92 #endif
93 
94 static void startAudio() {
95 #if IS_TEENSY3()
96  analogWriteResolution(12);
97 #elif IS_TEENSY4()
98  analogWriteResolution(10);
99 # if (!EXTERNAL_AUDIO_OUTPUT)
100  analogWriteFrequency(AUDIO_CHANNEL_1_PIN, 146484.38f);
101 # if (AUDIO_CHANNELS > 1)
102  analogWriteFrequency(AUDIO_CHANNEL_2_PIN, 146484.38f);
103 # endif // end #if (AUDIO_CHANNELS > 1)
104 # endif // end #if (!EXTERNAL_AUDIO_OUTPUT)
105 #endif
106  timer1.begin(defaultAudioOutput, 1000000. / AUDIO_RATE);
107 }
108 
109 void stopMozzi() {
110  timer1.end();
111  interrupts();
112 }
113 //// END AUDIO OUTPUT code ///////
void stopMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
#define AUDIO_CHANNELS
This sets allows to change from a single/mono audio output channel to stereo output.
Definition: mozzi_config.h:92
#define IS_TEENSY4()
#define AUDIO_RATE
Holds the audio rate setting.
Definition: mozzi_config.h:62
#define IS_TEENSY3()
void setupFastAnalogRead(int8_t speed)
NOTE: Code needed to set up faster than usual analog reads, e.g.
#define EXTERNAL_AUDIO_OUTPUT
Defining this option as true in mozzi_config.h allows to completely customize the audio output...
Definition: mozzi_config.h:99