Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
MozziGuts.h
1 /*
2  * MozziGuts.h
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 Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef MOZZIGUTS_H_
13 #define MOZZIGUTS_H_
14 
15 //#define F_CPU 8000000 // testing
16 
17 #if ARDUINO >= 100
18  #include "Arduino.h"
19 #else
20  #include "WProgram.h"
21 #endif
22 
23 
24 
25 #include "mozzi_analog.h"
26 
38 #define CONTROL_RATE 64
39 
40 
41 
139 //enum audio_modes {STANDARD,STANDARD_PLUS,HIFI};
140 #define STANDARD 0
141 #define STANDARD_PLUS 1
142 #define HIFI 2
143 
144 
145 #include "mozzi_config.h" // User can change the config file to set audio mode
146 
147 
148 // Print warning/reminder about the AUDIO_MODE setting to the arduino console while compiling
149 #if AUDIO_MODE == STANDARD
150 #warning "AUDIO_MODE is set to STANDARD in mozzi_config.h. If things sound wrong, check if STANDARD is the correct AUDIO_MODE for your sketch."
151 #elif AUDIO_MODE == STANDARD_PLUS
152 #warning "AUDIO_MODE is set to STANDARD_PLUS in mozzi_config.h. If things sound wrong, check if STANDARD_PLUS is the correct AUDIO_MODE for your sketch."
153 #elif AUDIO_MODE == HIFI
154 #warning "AUDIO_MODE is set to HIFI in mozzi_config.h. If things sound wrong, check if HIFI is the correct AUDIO_MODE for your sketch."
155 #endif
156 
157 #if (AUDIO_MODE == STANDARD) && (AUDIO_RATE == 32768)
158 #error AUDIO_RATE 32768 does not work when AUDIO_MODE is STANDARD, try setting the AUDIO_MODE to STANDARD_PLUS in Mozzi/mozzi_config.h
159 #endif
160 
161 
162 #define CLOCK_TICKS_PER_AUDIO_TICK (F_CPU / AUDIO_RATE)
163 
164 
165 #if AUDIO_RATE == 16384
166 #define AUDIO_RATE_AS_LSHIFT 14
167 #define MICROS_PER_AUDIO_TICK 61 // 1000000 / 16384 = 61.035, ...* 256 = 15625
168 #elif AUDIO_RATE == 32768
169 #define AUDIO_RATE_AS_LSHIFT 15
170 #define MICROS_PER_AUDIO_TICK 31 // = 1000000 / 32768 = 30.518, ...* 256 = 7812.6
171 #endif
172 
173 
174 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // Teensy 3
175 #include "AudioConfigTeensy3_12bit.h"
176 #else
177 #if (AUDIO_MODE == STANDARD)
178 #include "AudioConfigStandard9bitPwm.h"
179 #elif (AUDIO_MODE == STANDARD_PLUS)
180 #include "AudioConfigStandardPlus.h"
181 #elif (AUDIO_MODE == HIFI)
182 #include "AudioConfigHiSpeed14bitPwm.h"
183 #endif
184 
185 #endif
186 
187 // common numeric types
188 typedef unsigned char uchar;
189 typedef unsigned int uint;
190 typedef unsigned long ulong;
191 
192 #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(TEENSYDUINO) // teensy 3, 3.1
193 //typedef uint8_t byte;//unsigned char;
194 //typedef int8_t char;
195 //typedef (uint16_t) (short unsigned int);
196 //typedef int16_t int;
197 //typedef (uint32_t) (unsigned long);
198 //typedef int32_t long;
199 #else
200 typedef unsigned char uint8_t;
201 typedef signed char int8_t;
202 typedef unsigned int uint16_t;
203 typedef signed int int16_t;
204 typedef unsigned long uint32_t;
205 typedef signed long int32_t;
206 #endif
207 
208 
234 void startMozzi(int control_rate_hz = CONTROL_RATE);
235 
236 
237 
254 void pauseMozzi();
255 
256 
257 
264 void unPauseMozzi();
265 
273 #if (STEREO_HACK == true)
274 void updateAudio();
275 #else
276 int updateAudio();
277 #endif
278 
285 void updateControl();
286 
287 
301 void audioHook();
302 
303 
304 
320 #if (USE_AUDIO_INPUT == true)
321 int getAudioInput();
322 #endif
323 
324 
332 unsigned long audioTicks();
333 
334 
335 
344 unsigned long mozziMicros();
345 
346 
347 
348 
349 // internal use
350 #if (AUDIO_MODE == HIFI)
351 static void setupTimer2();
352 #endif
353 
354 #endif /* MOZZIGUTS_H_ */
#define CONTROL_RATE
Control rate setting.
Definition: MozziGuts.h:38
unsigned long mozziMicros()
A replacement for Arduino micros() which is disabled by Mozzi which takes over Timer 0 for control in...
Definition: MozziGuts.cpp:635
void updateAudio()
This is where you put your audio code.
void updateControl()
This is where you put your control code.
void audioHook()
This is required in Arduino's loop().
Definition: MozziGuts.cpp:229
void unPauseMozzi()
Restores Mozzi audio and control interrupts, if they have been temporarily disabled with pauseMozzi()...
Definition: MozziGuts.cpp:589
unsigned long audioTicks()
An alternative for Arduino time funcitions like micros() which are disabled by Mozzi when it takes ov...
Definition: MozziGuts.cpp:629
void pauseMozzi()
Stops audio and control interrupts and restores the timers to the values they had before Mozzi was st...
Definition: MozziGuts.cpp:550
int getAudioInput()
This returns audio input from the input buffer, if #define USE_AUDIO_INPUT true is in the Mozzi/mozzi...
Definition: MozziGuts.cpp:145
void startMozzi(int control_rate_hz=CONTROL_RATE)
Sets up the timers for audio and control rate processes, storing the timer registers so they can be r...
Definition: MozziGuts.cpp:537