diff --git a/.github/workflows/compile_examples.yml b/.github/workflows/compile_examples.yml index e43891ca6..cc8472533 100644 --- a/.github/workflows/compile_examples.yml +++ b/.github/workflows/compile_examples.yml @@ -62,6 +62,9 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 + - name: Add marker file for github run + run: echo "#define IN_GITHUB_RUNNER 1" > detect_github_runner.h + - name: Compile examples uses: arduino/compile-sketches@v1 with: diff --git a/AudioOutput.h b/AudioOutput.h index 668d2f69d..077a32955 100644 --- a/AudioOutput.h +++ b/AudioOutput.h @@ -44,10 +44,8 @@ * @see MonoOutput::fromNBit(), StereoOutput::fromNBit() */ -#ifndef AUDIOOUTPUT -#define AUDIOOUTPUT - -#include "mozzi_config.h" +#ifndef AUDIOOUTPUT_H +#define AUDIOOUTPUT_H /** The type used to store a single channel of a single frame, internally. For compatibility with earlier versions of Mozzi this is defined as int. * If you do not care about keeping old sketches working, you may be able to save some RAM by using int16_t, instead (on boards where int is larger @@ -79,7 +77,7 @@ */ #define AudioOutput_t AudioOutputStorage_t /** Representation of an single audio output sample/frame. This #define maps to either MonoOutput or StereoOutput, depending on what is configured - * in mozzi_config.h. Since the two are source compatible to a large degree, it often isn't even necessary to test, which it is, in your code. E.g. + * in MOZZI_AUDIO_CHANNELS. Since the two are source compatible to a large degree, it often isn't even necessary to test, which it is, in your code. E.g. * both have functions l() and r(), to return "two" audio channels (which will be the same in case of mono). * * You will not usually use or encounter this definition, unless using @ref external_audio output mode. @@ -98,7 +96,10 @@ struct StereoOutput { /** Conversion to int operator: If used in a mono config, returns only the left channel (and gives a compile time warning). This _could_ be turned into an operator for implicit conversion in this case. For now we chose to apply conversion on demand, only, as most of the time using StereoOutput in a mono config, is not intended. */ - inline AudioOutput_t portable() const __attribute__((deprecated("Sketch generates stereo output, but Mozzi is configured for mono. Check mozzi_config.h."))) { return _l; }; + inline AudioOutput_t portable() const __attribute__((deprecated("Sketch generates stereo output, but Mozzi is configured for mono. Check MOZZI_AUDIO_CHANNELS setting."))) { return _l; }; +# if GITHUB_RUNNER_ACCEPT_STEREO_IN_MONO + inline operator AudioOutput_t() const __attribute__((deprecated("Stereo converted to mono on github runner"))) { return _l; }; +# endif #endif AudioOutputStorage_t l() const { return _l; }; AudioOutputStorage_t r() const { return _r; }; @@ -139,7 +140,7 @@ struct MonoOutput { /** Conversion to stereo operator: If used in a stereo config, returns identical channels (and gives a compile time warning). This _could_ be turned into an operator for implicit conversion in this case. For now we chose to apply conversion on demand, only, as most of the time using StereoOutput in a mono config, is not intended. */ - inline StereoOutput portable() const __attribute__((deprecated("Sketch generates mono output, but Mozzi is configured for stereo. Check mozzi_config.h."))) { return StereoOutput(_l, _l); }; + inline StereoOutput portable() const __attribute__((deprecated("Sketch generates mono output, but Mozzi is configured for stereo. Check MOZZI_AUDIO_CHANNELS setting."))) { return StereoOutput(_l, _l); }; #else /** Conversion to int operator. */ inline operator AudioOutput_t() const { return _l; }; @@ -176,9 +177,11 @@ struct MonoOutput { }; +#if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM) /** When setting using one of the external output modes (@ref MOZZI_OUTPUT_EXTERNAL_TIMED or @ref MOZZI_OUTPUT_EXTERNAL_CUSTOM) implement this function to take care of writing samples to the hardware. * In all otther cases, it will be provided by the platform implementation. You should never call this function, directly, in your sketch. */ void audioOutput(const AudioOutput f); +#endif #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM) /** For @ref MOZZI_OUTPUT_EXTERNAL_CUSTOM implement this function to return true, if and only if your hardware (or custom buffer) is ready to accept the next sample. */ inline bool canBufferAudioOutput(); diff --git a/AutoMap.h b/AutoMap.h index ff9aef13f..9e2bec1c3 100644 --- a/AutoMap.h +++ b/AutoMap.h @@ -21,6 +21,9 @@ #include "AutoRange.h" +/** @defgroup sensortools Automatic range adjustment +*/ + /** @ingroup sensortools Automatically map an input value to an output range without knowing the precise range of inputs beforehand. */ diff --git a/IntegerType.h b/IntegerType.h index b8af4d341..47c944a0f 100644 --- a/IntegerType.h +++ b/IntegerType.h @@ -3,6 +3,9 @@ #include +/** @ingroup util +Provides appropriate integer types that can bit the given number of bytes on this platform (at most 64). +*/ template struct IntegerType { // at an odd value, such as 3 bytes? Add one more byte (up to at most 8 bytes).. typedef typename IntegerType<(BYTES < 8) ? (BYTES+1) : 8>::unsigned_type unsigned_type; diff --git a/Mozzi.h b/Mozzi.h new file mode 100644 index 000000000..38bfcf71e --- /dev/null +++ b/Mozzi.h @@ -0,0 +1,28 @@ +/* + * Mozzi.h + * + * Copyright 2024, Tim Barrass and the Mozzi team + * + * This file is part of Mozzi. + * + * Mozzi is licensed under a Creative Commons + * Attribution-NonCommercial-ShareAlike 4.0 International License. + * + */ + +/** @ingroup core + * @file Mozzi.h + * + * This is the main include file in Mozzi. Almost all sketches using Mozzi will want to include this file @em exactly once. + * + * Should your sketch require \ref core Mozzi functions in more than one translation unit (i.e. you have more than one .cpp-file + * in your sketch itself), only *one* of these shall include this file, while any others shall include \ref MozziHeadersOnly instead. + * (Failing to heed this advice will lead to "duplicate definition" errors.) + */ + +#ifndef MOZZI_H_ +#define MOZZI_H_ + +#include "MozziGuts.h" + +#endif diff --git a/MozziGuts.h b/MozziGuts.h index cc42c32fa..e6a79e76d 100644 --- a/MozziGuts.h +++ b/MozziGuts.h @@ -12,31 +12,25 @@ #ifndef MOZZIGUTS_H_ #define MOZZIGUTS_H_ -//#define F_CPU 8000000 // testing +#include "Arduino.h" -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" +#include "MozziConfigValues.h" + +#if !(defined(MOZZI_H_) || defined(MOZZI_HEADERS_ONLY_H_)) +#warning Direct inclusion of MozziGuts.h is deprecated. Use Mozzi.h, instead, and read about porting to Mozzi 2.0 +#define MOZZI_COMPATIBILITY_LEVEL MOZZI_COMPATIBILITY_1_1 #endif #include "hardware_defines.h" -#include "mozzi_config.h" #if IS_TEENSY3() || IS_TEENSY4() // required from http://github.com/pedvide/ADC for Teensy 3.* #include #endif -#include "mozzi_analog.h" - - #include "internal/config_checks_generic.h" -#if (STEREO_HACK == true) -extern int audio_out_1, audio_out_2; -#endif - +#include "mozzi_analog.h" #include "AudioOutput.h" // TODO Mozzi 2.0: These typedef probably obsolete? @@ -93,7 +87,7 @@ forked version of Mozzi on github, so sound production can continue while reading sensors. As it is, stopMozzi restores all the Timers used by Mozzi to their previous -settings. Another scenario which could be easily hacked in MozziGuts.cpp could +settings. Another scenario which could be easily hacked in MozziGuts.hpp could involve individually saving and restoring particular Timer registers depending on which one(s) are required for other tasks. @@ -184,4 +178,8 @@ is output, so the resolution is 1/AUDIO_RATE microseconds (61 microseconds when */ unsigned long mozziMicros(); +#ifndef _MOZZI_HEADER_ONLY +#include "internal/MozziGuts.hpp" +#endif + #endif /* MOZZIGUTS_H_ */ diff --git a/MozziHeadersOnly.h b/MozziHeadersOnly.h new file mode 100644 index 000000000..5d28041b3 --- /dev/null +++ b/MozziHeadersOnly.h @@ -0,0 +1,28 @@ +/* + * MozziHeadersOnly.h + * + * Copyright 2024, Tim Barrass and the Mozzi team + * + * This file is part of Mozzi. + * + * Mozzi is licensed under a Creative Commons + * Attribution-NonCommercial-ShareAlike 4.0 International License. + * + */ + +/** @ingroup core + * @file MozziHeadersOnly.h + * + * This file provides declarations of the \ref core Mozzi functions, but no implementation. Use this only, if you have more than one + * translation unit in your project (i.e. you have more than one .cpp-file in your sketch itself). Otherwise include \ref Mozzi.h, instead. + * + * (Failure to head this advice will lead to "symbol XY undefined" errors.). + */ + +#ifndef MOZZI_HEADERS_ONLY_H_ +#define MOZZI_HEADERS_ONLY_H_ + +#define _MOZZI_HEADER_ONLY +#include "MozziGuts.h" + +#endif diff --git a/Oscil.h b/Oscil.h index d5b302daf..945f4a0e1 100644 --- a/Oscil.h +++ b/Oscil.h @@ -14,12 +14,8 @@ #ifndef OSCIL_H_ #define OSCIL_H_ -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif -#include "MozziGuts.h" +#include "Arduino.h" +#include "MozziHeadersOnly.h" #include "mozzi_fixmath.h" #include "mozzi_pgmspace.h" diff --git a/Readme_Mozzi_2_0.md b/Readme_Mozzi_2_0.md index a4907f90a..c9b8c82af 100644 --- a/Readme_Mozzi_2_0.md +++ b/Readme_Mozzi_2_0.md @@ -1,6 +1,6 @@ Porting to Mozzi 2.0 -// TODO: properly type up +// TODO: These are just short notes taken while working. Needs to be typed up properly, in the end. changed config names and semantics TODO (incomplete) @@ -28,16 +28,17 @@ all new general: - Added many config sanity checks. Some may be too strict, if so please mention - Other removed stuff: +Other removed stuff: - pauseMozzi() - was still declared but not defined -> not usable, anyway - unpauseMozzi() - was still declared but not defined -> not usable, anyway - - - -Documentation bits that still need to find a new home (many other bits were moved around, many, many duplicates merged into a common place, and seom obsoleted bits discarded): - -Contrary to earlier versions of Mozzi, this version does not take over Timer 0, and thus Arduino -functions delay(), millis(), micros() and delayMicroseconds() remain usable in theory. That said, -you should avoid these functions, as they are slow (or even blocking). For measuring time, refer -to mozziMircos(). For delaying events, you can use Mozzi's EventDelay() unit instead -(not to be confused with AudioDelay()). + - Teensy3/4: channel2sc1a -> thought to be unused, removed + - Teensy2: adc_mapping -> hidden away; use adcPinToChannelNum(), as on all other platforms, instead + - removed several inclusions of "WProgram.h". If using Arduino versions < 1.0, you need to update, seriously ;-) (TODO many, many, instances of this are still around) + - Since Mozzi (AVR-port) no longer uses Timer 0 since a long time, the corresponding library (utility/TimerZero.h) has now been removed, too. + The Arduino functions delay(), millis(), micros() and delayMicroseconds() should now be usable in theory. That said, + you should avoid these functions, as they are slow (or even blocking). For measuring time, refer + to mozziMicros(). For delaying events, you can use Mozzi's EventDelay() unit instead (not to be confused with AudioDelay()). + +Moved headers: + - Header files not meant for user inclusion have been moved to "internal" + - New sketches should include "Mozzi.h", rather than "MozziGuts.h", thereby documenting, they have been written for Mozzi 2.0+ diff --git a/Sample.h b/Sample.h index 74a2f2eab..9c79c1084 100644 --- a/Sample.h +++ b/Sample.h @@ -12,7 +12,7 @@ #ifndef SAMPLE_H_ #define SAMPLE_H_ -#include "MozziGuts.h" +#include "MozziHeadersOnly.h" #include "mozzi_fixmath.h" #include "mozzi_pgmspace.h" diff --git a/WavePacket.h b/WavePacket.h index 4fed43da5..2b5f13033 100644 --- a/WavePacket.h +++ b/WavePacket.h @@ -13,13 +13,13 @@ #ifndef WAVEPACKET_H #define WAVEPACKET_H -#include -#include -#include -#include -#include -#include -#include +#include "MozziHeadersOnly.h" +#include "Oscil.h" +#include "tables/cos8192_int8.h" +#include "mozzi_fixmath.h" +#include "Phasor.h" +#include "Line.h" +#include "meta.h" enum algorithms {SINGLE,DOUBLE}; diff --git a/config/mozzi_config_documentation.h b/config/mozzi_config_documentation.h index 18407df75..523e1508b 100644 --- a/config/mozzi_config_documentation.h +++ b/config/mozzi_config_documentation.h @@ -1,8 +1,10 @@ #ifdef FOR_DOXYGEN_ONLY /** @file */ -/*! @defgroup config Mozzi Configuration options - * @brief Mozzi Configuration +/*! @defgroup config Mozzi Configuration options */ + +/** @ingroup config + * @page config_main Mozzi Configuration * * @section config_general Configuring Mozzi * @@ -123,7 +125,7 @@ * @note * For compatibility reasons, the option AUDIO_RATE is automatically set to the same value as this option, and you will find some uses of that in old (pre Mozzi 2.0) code examples. * It is advised to use only MOZZI_AUDIO_RATE in new code, however. - * TODO: Only do the above, if MozziGuts.h, rather than Mozzi.h was included? + * TODO: Only do the above, for MOZZI_COMPATIBILITY_LEVEL < MOZZI_COMPATIBILITY_2_0? */ #define MOZZI_AUDIO_RATE FOR_DOXYGEN_ONLY diff --git a/examples/01.Basics/Control_Gain/Control_Gain.ino b/examples/01.Basics/Control_Gain/Control_Gain.ino index 2316d2600..b7e32298b 100644 --- a/examples/01.Basics/Control_Gain/Control_Gain.ino +++ b/examples/01.Basics/Control_Gain/Control_Gain.ino @@ -17,7 +17,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator diff --git a/examples/01.Basics/Sinewave/Sinewave.ino b/examples/01.Basics/Sinewave/Sinewave.ino index 27b2d4a80..9314fa6c7 100644 --- a/examples/01.Basics/Sinewave/Sinewave.ino +++ b/examples/01.Basics/Sinewave/Sinewave.ino @@ -16,7 +16,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator diff --git a/examples/01.Basics/Sinewave_HIFI/Sinewave_HIFI.ino b/examples/01.Basics/Sinewave_HIFI/Sinewave_HIFI.ino index 2dd1b8301..fb1769c8c 100644 --- a/examples/01.Basics/Sinewave_HIFI/Sinewave_HIFI.ino +++ b/examples/01.Basics/Sinewave_HIFI/Sinewave_HIFI.ino @@ -3,16 +3,10 @@ Demonstrates the use of Oscil to play a wavetable. - This sketch using HIFI mode on AVR (i.e. the classic Arduino borads, not Teensy 3.x and friends). - - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - #define AUDIO_MODE STANDARD - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - #define AUDIO_MODE HIFI + Important: + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -41,7 +35,10 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM + +#include #include // oscillator template #include // sine table for oscillator @@ -49,7 +46,7 @@ Oscil aSin(SIN2048_DATA); void setup(){ - startMozzi(); // uses the default control rate of 64, defined in mozzi_config.h + startMozzi(); // uses the default control rate of 64 aSin.setFreq(440); // set the frequency } diff --git a/examples/01.Basics/Skeleton/Skeleton.ino b/examples/01.Basics/Skeleton/Skeleton.ino index 1bbc7fc23..cbc5a3d6e 100644 --- a/examples/01.Basics/Skeleton/Skeleton.ino +++ b/examples/01.Basics/Skeleton/Skeleton.ino @@ -1,4 +1,4 @@ -#include // at the top of your sketch +#include // at the top of your sketch #define CONTROL_RATE 64 void setup() { diff --git a/examples/01.Basics/Skeleton_Multi/Skeleton_Multi.ino b/examples/01.Basics/Skeleton_Multi/Skeleton_Multi.ino index 2c9cc85a2..3763d22b7 100644 --- a/examples/01.Basics/Skeleton_Multi/Skeleton_Multi.ino +++ b/examples/01.Basics/Skeleton_Multi/Skeleton_Multi.ino @@ -4,7 +4,7 @@ * Unless you have good reason to do this, it is recommended to base your sketch on the * single-file "Skeleton" example, instead. */ -#include // at the top of your sketch +#include // at the top of your sketch void setup() { startMozzi(64); diff --git a/examples/01.Basics/Skeleton_Multi/Skeleton_Multi_Unit2.cpp b/examples/01.Basics/Skeleton_Multi/Skeleton_Multi_Unit2.cpp index a661c8e32..4687b88b7 100644 --- a/examples/01.Basics/Skeleton_Multi/Skeleton_Multi_Unit2.cpp +++ b/examples/01.Basics/Skeleton_Multi/Skeleton_Multi_Unit2.cpp @@ -1,4 +1,6 @@ -#include // This file, too, will have to include the Mozzi headers. +#include // should be included only once in the whole program. Sketches needing + // core Mozzi functions in more than one .cpp file, shall include MozziHeadersOnly.h + // in all but one. AudioOutput_t updateAudio() { return MonoOutput::from8Bit(0); // just a dummy diff --git a/examples/01.Basics/Table_Resolution/Table_Resolution.ino b/examples/01.Basics/Table_Resolution/Table_Resolution.ino index 7a5852b61..c9f286b33 100644 --- a/examples/01.Basics/Table_Resolution/Table_Resolution.ino +++ b/examples/01.Basics/Table_Resolution/Table_Resolution.ino @@ -14,7 +14,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/01.Basics/Vibrato/Vibrato.ino b/examples/01.Basics/Vibrato/Vibrato.ino index 62a838be9..a232275f8 100644 --- a/examples/01.Basics/Vibrato/Vibrato.ino +++ b/examples/01.Basics/Vibrato/Vibrato.ino @@ -16,7 +16,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // table for Oscils to play #include // for mtof diff --git a/examples/02.Control/Control_Echo_Theremin/Control_Echo_Theremin.ino b/examples/02.Control/Control_Echo_Theremin/Control_Echo_Theremin.ino index cf1f1ee13..a0069f23d 100644 --- a/examples/02.Control/Control_Echo_Theremin/Control_Echo_Theremin.ino +++ b/examples/02.Control/Control_Echo_Theremin/Control_Echo_Theremin.ino @@ -25,7 +25,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/02.Control/Control_Oscil_Wash/Control_Oscil_Wash.ino b/examples/02.Control/Control_Oscil_Wash/Control_Oscil_Wash.ino index aef17e329..01482a2e2 100644 --- a/examples/02.Control/Control_Oscil_Wash/Control_Oscil_Wash.ino +++ b/examples/02.Control/Control_Oscil_Wash/Control_Oscil_Wash.ino @@ -21,7 +21,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/02.Control/Control_Tremelo/Control_Tremelo.ino b/examples/02.Control/Control_Tremelo/Control_Tremelo.ino index 73d47211c..40ea37af5 100644 --- a/examples/02.Control/Control_Tremelo/Control_Tremelo.ino +++ b/examples/02.Control/Control_Tremelo/Control_Tremelo.ino @@ -21,7 +21,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/02.Control/EventDelay/EventDelay.ino b/examples/02.Control/EventDelay/EventDelay.ino index 05a8a6a07..a81661ac3 100644 --- a/examples/02.Control/EventDelay/EventDelay.ino +++ b/examples/02.Control/EventDelay/EventDelay.ino @@ -20,7 +20,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/02.Control/Line_Gliss/Line_Gliss.ino b/examples/02.Control/Line_Gliss/Line_Gliss.ino index 33b4551c4..7c68ea7fb 100644 --- a/examples/02.Control/Line_Gliss/Line_Gliss.ino +++ b/examples/02.Control/Line_Gliss/Line_Gliss.ino @@ -25,7 +25,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // for smooth transitions #include // oscillator template #include // saw table for oscillator diff --git a/examples/02.Control/Line_Gliss_Double_32k_HIFI/Line_Gliss_Double_32k_HIFI.ino b/examples/02.Control/Line_Gliss_Double_32k_HIFI/Line_Gliss_Double_32k_HIFI.ino index 8ba18f9c7..1120af986 100644 --- a/examples/02.Control/Line_Gliss_Double_32k_HIFI/Line_Gliss_Double_32k_HIFI.ino +++ b/examples/02.Control/Line_Gliss_Double_32k_HIFI/Line_Gliss_Double_32k_HIFI.ino @@ -14,21 +14,14 @@ Also shows how to use random offsets between the oscillators' frequencies to produce a chorusing/doubling effect. - This sketch using HIFI mode is not for Teensy 3.1. - - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - #define AUDIO_MODE STANDARD - //#define AUDIO_MODE STANDARD_PLUS - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - //#define AUDIO_MODE STANDARD_PLUS - #define AUDIO_MODE HIFI - - Also, AUDIO_RATE can be changed from 16384 to 32768 in mozzi_config.h, - to try out the higher sample rate. + Important: + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). + + The MOZZI_AUDIO_RATE (sample rate) is additionally configured at 32768 Hz, + which is the default on most platforms, but twice the standard rate on + AVR-CPUs. Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -57,7 +50,11 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM +#define MOZZI_AUDIO_RATE 32768 + +#include #include // for smooth transitions #include // oscillator template #include // saw table for oscillator diff --git a/examples/02.Control/Metronome_SampleHuffman/Metronome_SampleHuffman.ino b/examples/02.Control/Metronome_SampleHuffman/Metronome_SampleHuffman.ino index a63cad2e5..8be25c49c 100644 --- a/examples/02.Control/Metronome_SampleHuffman/Metronome_SampleHuffman.ino +++ b/examples/02.Control/Metronome_SampleHuffman/Metronome_SampleHuffman.ino @@ -16,7 +16,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/02.Control/Stop_Start/Stop_Start.ino b/examples/02.Control/Stop_Start/Stop_Start.ino index 1c1937ce6..086338d2e 100644 --- a/examples/02.Control/Stop_Start/Stop_Start.ino +++ b/examples/02.Control/Stop_Start/Stop_Start.ino @@ -21,7 +21,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include // sine table for oscillator diff --git a/examples/03.Sensors/Knob_LightLevel_FMsynth/Knob_LightLevel_FMsynth.ino b/examples/03.Sensors/Knob_LightLevel_FMsynth/Knob_LightLevel_FMsynth.ino index 55a15d229..39b2ba042 100644 --- a/examples/03.Sensors/Knob_LightLevel_FMsynth/Knob_LightLevel_FMsynth.ino +++ b/examples/03.Sensors/Knob_LightLevel_FMsynth/Knob_LightLevel_FMsynth.ino @@ -33,7 +33,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator #include // table for Oscils to play #include // maps unpredictable inputs to a range diff --git a/examples/03.Sensors/Knob_LightLevel_x2_FMsynth/Knob_LightLevel_x2_FMsynth.ino b/examples/03.Sensors/Knob_LightLevel_x2_FMsynth/Knob_LightLevel_x2_FMsynth.ino index 294b9e70a..2686d8b13 100644 --- a/examples/03.Sensors/Knob_LightLevel_x2_FMsynth/Knob_LightLevel_x2_FMsynth.ino +++ b/examples/03.Sensors/Knob_LightLevel_x2_FMsynth/Knob_LightLevel_x2_FMsynth.ino @@ -36,7 +36,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator #include // table for Oscils to play #include diff --git a/examples/03.Sensors/Light_Temperature_Detuned/Light_Temperature_Detuned.ino b/examples/03.Sensors/Light_Temperature_Detuned/Light_Temperature_Detuned.ino index 0d321f871..0bb8d8677 100644 --- a/examples/03.Sensors/Light_Temperature_Detuned/Light_Temperature_Detuned.ino +++ b/examples/03.Sensors/Light_Temperature_Detuned/Light_Temperature_Detuned.ino @@ -30,7 +30,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/03.Sensors/Light_Temperature_Multi_Oscil/Light_Temperature_Multi_Oscil.ino b/examples/03.Sensors/Light_Temperature_Multi_Oscil/Light_Temperature_Multi_Oscil.ino index ab0543274..5f28a48a1 100644 --- a/examples/03.Sensors/Light_Temperature_Multi_Oscil/Light_Temperature_Multi_Oscil.ino +++ b/examples/03.Sensors/Light_Temperature_Multi_Oscil/Light_Temperature_Multi_Oscil.ino @@ -29,7 +29,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/03.Sensors/Piezo_Frequency/Piezo_Frequency.ino b/examples/03.Sensors/Piezo_Frequency/Piezo_Frequency.ino index 9ac767c52..47cdacbf5 100644 --- a/examples/03.Sensors/Piezo_Frequency/Piezo_Frequency.ino +++ b/examples/03.Sensors/Piezo_Frequency/Piezo_Frequency.ino @@ -26,7 +26,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator #include // table for Oscils to play #include diff --git a/examples/03.Sensors/Piezo_SampleScrubber/Piezo_SampleScrubber.ino b/examples/03.Sensors/Piezo_SampleScrubber/Piezo_SampleScrubber.ino index 4e3fba4ca..fcbdf69ed 100644 --- a/examples/03.Sensors/Piezo_SampleScrubber/Piezo_SampleScrubber.ino +++ b/examples/03.Sensors/Piezo_SampleScrubber/Piezo_SampleScrubber.ino @@ -28,7 +28,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // Sample template #include "blahblah4b_int8.h" #include diff --git a/examples/03.Sensors/Piezo_SampleTrigger/Piezo_SampleTrigger.ino b/examples/03.Sensors/Piezo_SampleTrigger/Piezo_SampleTrigger.ino index 96a26bdf6..d5e2b99cc 100644 --- a/examples/03.Sensors/Piezo_SampleTrigger/Piezo_SampleTrigger.ino +++ b/examples/03.Sensors/Piezo_SampleTrigger/Piezo_SampleTrigger.ino @@ -32,7 +32,7 @@ CC by-nc-sa */ -#include +#include #include // Sample template #include // a converted audio sample included in the Mozzi download diff --git a/examples/03.Sensors/Piezo_Switch_Pitch/Piezo_Switch_Pitch.ino b/examples/03.Sensors/Piezo_Switch_Pitch/Piezo_Switch_Pitch.ino index 8663bef1e..ed2e24288 100644 --- a/examples/03.Sensors/Piezo_Switch_Pitch/Piezo_Switch_Pitch.ino +++ b/examples/03.Sensors/Piezo_Switch_Pitch/Piezo_Switch_Pitch.ino @@ -32,7 +32,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // Sample template #include // a converted audio sample included in the Mozzi download diff --git a/examples/03.Sensors/RCpoll/RCpoll.ino b/examples/03.Sensors/RCpoll/RCpoll.ino index 0bb38942b..0cc0e54cd 100644 --- a/examples/03.Sensors/RCpoll/RCpoll.ino +++ b/examples/03.Sensors/RCpoll/RCpoll.ino @@ -38,7 +38,7 @@ sPin ---\/\/\/-----. */ -#include +#include #include #include // sine table for oscillator #include diff --git a/examples/03.Sensors/Sinewave_Pinchange_Interrupt/Sinewave_Pinchange_Interrupt.ino b/examples/03.Sensors/Sinewave_Pinchange_Interrupt/Sinewave_Pinchange_Interrupt.ino index 33c9a40b8..9bec01a16 100644 --- a/examples/03.Sensors/Sinewave_Pinchange_Interrupt/Sinewave_Pinchange_Interrupt.ino +++ b/examples/03.Sensors/Sinewave_Pinchange_Interrupt/Sinewave_Pinchange_Interrupt.ino @@ -27,7 +27,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/03.Sensors/Volume_Knob/Volume_Knob.ino b/examples/03.Sensors/Volume_Knob/Volume_Knob.ino index 4db03e90b..c87c01560 100644 --- a/examples/03.Sensors/Volume_Knob/Volume_Knob.ino +++ b/examples/03.Sensors/Volume_Knob/Volume_Knob.ino @@ -31,7 +31,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator diff --git a/examples/03.Sensors/Volume_Knob_LightLevel_Frequency/Volume_Knob_LightLevel_Frequency.ino b/examples/03.Sensors/Volume_Knob_LightLevel_Frequency/Volume_Knob_LightLevel_Frequency.ino index 47f4c9da6..6b1565b6f 100644 --- a/examples/03.Sensors/Volume_Knob_LightLevel_Frequency/Volume_Knob_LightLevel_Frequency.ino +++ b/examples/03.Sensors/Volume_Knob_LightLevel_Frequency/Volume_Knob_LightLevel_Frequency.ino @@ -36,7 +36,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator diff --git a/examples/04.Audio_Input/Audio_Input/Audio_Input.ino b/examples/04.Audio_Input/Audio_Input/Audio_Input.ino index 0abfdfbcd..5ca020913 100644 --- a/examples/04.Audio_Input/Audio_Input/Audio_Input.ino +++ b/examples/04.Audio_Input/Audio_Input/Audio_Input.ino @@ -1,11 +1,10 @@ /* Test of audio input using Mozzi sonification library. - An audio input using the range between 0 to 5V on analog pin A0 - is sampled and output on digital pin 9. + An audio input using the range between 0 to 5V on analog pin A0 (or as + set in MOZZI_AUDIO_INPUT_PIN) is sampled and output on digital pin 9. - Configuration: requires these lines in the Mozzi/mozzi_config.h file: - #define USE_AUDIO_INPUT true - #define AUDIO_INPUT_PIN 0 + NOTE: MOZZI_AUDIO_INPUT_STANDARD is not available as an option on all + platforms. Circuit: Audio cable centre wire on pin A0, outer shielding to Arduino Ground. @@ -18,7 +17,11 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_INPUT MOZZI_AUDIO_INPUT_STANDARD +#define MOZZI_AUDIO_INPUT_PIN 0 + +#include void setup(){ startMozzi(); diff --git a/examples/04.Audio_Input/Audio_Input_with_Knob_Filter/Audio_Input_with_Knob_Filter.ino b/examples/04.Audio_Input/Audio_Input_with_Knob_Filter/Audio_Input_with_Knob_Filter.ino index 585ac01b0..d8a991925 100644 --- a/examples/04.Audio_Input/Audio_Input_with_Knob_Filter/Audio_Input_with_Knob_Filter.ino +++ b/examples/04.Audio_Input/Audio_Input_with_Knob_Filter/Audio_Input_with_Knob_Filter.ino @@ -2,9 +2,11 @@ Test of audio input and processing with control input, using Mozzi sonification library. - Configuration: requires these lines in the Mozzi/mozzi_config.h file: - #define USE_AUDIO_INPUT true - #define AUDIO_INPUT_PIN 0 + An audio input using the range between 0 to 5V on analog pin A0 (or as + set in MOZZI_AUDIO_INPUT_PIN) is sampled and output on digital pin 9. + + NOTE: MOZZI_AUDIO_INPUT_STANDARD is not available as an option on all + platforms. Circuit: Audio input on pin analog 0 @@ -23,7 +25,11 @@ */ -#include +#include +#define MOZZI_AUDIO_INPUT MOZZI_AUDIO_INPUT_STANDARD +#define MOZZI_AUDIO_INPUT_PIN 0 + +#include #include #define KNOB_PIN 1 diff --git a/examples/04.Audio_Input/Audio_and_Control_Input/Audio_and_Control_Input.ino b/examples/04.Audio_Input/Audio_and_Control_Input/Audio_and_Control_Input.ino index 3f15f5f62..ed7493616 100644 --- a/examples/04.Audio_Input/Audio_and_Control_Input/Audio_and_Control_Input.ino +++ b/examples/04.Audio_Input/Audio_and_Control_Input/Audio_and_Control_Input.ino @@ -5,12 +5,10 @@ All the other analog channels are sampled at control rate and printed to Serial. Demonstrates mozziAnalogRead(pin), which reads analog inputs in the background, - and getAudioInput(), which samples audio on AUDIO_INPUT_PIN, configured in mozzi_config.h. - - Configuration: requires these lines in the Mozzi/mozzi_config.h file: - #define USE_AUDIO_INPUT true - #define AUDIO_INPUT_PIN 0 + and getAudioInput(), which samples audio on MOZZI_AUDIO_INPUT_PIN, configured below. + NOTE: MOZZI_AUDIO_INPUT_STANDARD is not available as an option on all + platforms. Circuit: Audio cable centre wire on pin A0, outer shielding to Arduino Ground. @@ -28,8 +26,11 @@ Tim Barrass 2013, CC by-nc-sa. */ +#include +#define MOZZI_AUDIO_INPUT MOZZI_AUDIO_INPUT_STANDARD +#define MOZZI_AUDIO_INPUT_PIN 0 -#include +#include void setup() { diff --git a/examples/05.Control_Filters/DCfilter/DCfilter.ino b/examples/05.Control_Filters/DCfilter/DCfilter.ino index 1b6337cc1..b1df39c9a 100644 --- a/examples/05.Control_Filters/DCfilter/DCfilter.ino +++ b/examples/05.Control_Filters/DCfilter/DCfilter.ino @@ -17,7 +17,7 @@ */ -#include +#include #include int sensorPin = A0; diff --git a/examples/05.Control_Filters/Line_vs_Smooth/Line_vs_Smooth.ino b/examples/05.Control_Filters/Line_vs_Smooth/Line_vs_Smooth.ino index 0d5229f8a..aa6e14e92 100644 --- a/examples/05.Control_Filters/Line_vs_Smooth/Line_vs_Smooth.ino +++ b/examples/05.Control_Filters/Line_vs_Smooth/Line_vs_Smooth.ino @@ -25,7 +25,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include // sine table for oscillator #include diff --git a/examples/05.Control_Filters/MIDI_portamento/MIDI_portamento.ino b/examples/05.Control_Filters/MIDI_portamento/MIDI_portamento.ino index 11a4d08a3..1cecc9fc5 100644 --- a/examples/05.Control_Filters/MIDI_portamento/MIDI_portamento.ino +++ b/examples/05.Control_Filters/MIDI_portamento/MIDI_portamento.ino @@ -22,7 +22,7 @@ */ #include -#include +#include #include // oscillator template #include // for envelope #include // sine table for oscillator diff --git a/examples/05.Control_Filters/RollingAverage/RollingAverage.ino b/examples/05.Control_Filters/RollingAverage/RollingAverage.ino index 060c35f07..73b107306 100644 --- a/examples/05.Control_Filters/RollingAverage/RollingAverage.ino +++ b/examples/05.Control_Filters/RollingAverage/RollingAverage.ino @@ -19,7 +19,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/05.Control_Filters/Smooth/Smooth.ino b/examples/05.Control_Filters/Smooth/Smooth.ino index 1c805fb98..9a8083754 100644 --- a/examples/05.Control_Filters/Smooth/Smooth.ino +++ b/examples/05.Control_Filters/Smooth/Smooth.ino @@ -18,7 +18,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/05.Control_Filters/Smooth_Frequency/Smooth_Frequency.ino b/examples/05.Control_Filters/Smooth_Frequency/Smooth_Frequency.ino index c4c98c725..9c964caec 100644 --- a/examples/05.Control_Filters/Smooth_Frequency/Smooth_Frequency.ino +++ b/examples/05.Control_Filters/Smooth_Frequency/Smooth_Frequency.ino @@ -16,7 +16,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/05.Control_Filters/Thermistor_OverSample/Thermistor_OverSample.ino b/examples/05.Control_Filters/Thermistor_OverSample/Thermistor_OverSample.ino index fabdf37b4..0459fb0f9 100644 --- a/examples/05.Control_Filters/Thermistor_OverSample/Thermistor_OverSample.ino +++ b/examples/05.Control_Filters/Thermistor_OverSample/Thermistor_OverSample.ino @@ -29,7 +29,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include // oscillator template #include #include // SINe table for oscillator diff --git a/examples/06.Synthesis/AMsynth/AMsynth.ino b/examples/06.Synthesis/AMsynth/AMsynth.ino index 66d08e053..8cd90761b 100644 --- a/examples/06.Synthesis/AMsynth/AMsynth.ino +++ b/examples/06.Synthesis/AMsynth/AMsynth.ino @@ -20,7 +20,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // table for Oscils to play #include diff --git a/examples/06.Synthesis/AMsynth_HIFI/AMsynth_HIFI.ino b/examples/06.Synthesis/AMsynth_HIFI/AMsynth_HIFI.ino index 2b70710a9..91ae70b9a 100644 --- a/examples/06.Synthesis/AMsynth_HIFI/AMsynth_HIFI.ino +++ b/examples/06.Synthesis/AMsynth_HIFI/AMsynth_HIFI.ino @@ -7,17 +7,10 @@ values, random numbers with rand(), and EventDelay() for scheduling. - This sketch using HIFI mode is not for Teensy 3.1. - - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - #define AUDIO_MODE STANDARD - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - #define AUDIO_MODE HIFI - + Important: + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -46,7 +39,10 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM + +#include #include #include // table for Oscils to play #include diff --git a/examples/06.Synthesis/Brown_Noise_Realtime/Brown_Noise_Realtime.ino b/examples/06.Synthesis/Brown_Noise_Realtime/Brown_Noise_Realtime.ino index 490b3716b..d1ad9e398 100644 --- a/examples/06.Synthesis/Brown_Noise_Realtime/Brown_Noise_Realtime.ino +++ b/examples/06.Synthesis/Brown_Noise_Realtime/Brown_Noise_Realtime.ino @@ -18,7 +18,7 @@ Tim Barrass 20118, CC by-nc-sa. */ -#include +#include #include #include // oscillator template #include // sine table for oscillator diff --git a/examples/06.Synthesis/Detuned_Beats_Wash/Detuned_Beats_Wash.ino b/examples/06.Synthesis/Detuned_Beats_Wash/Detuned_Beats_Wash.ino index 4c076a005..05eb161d1 100644 --- a/examples/06.Synthesis/Detuned_Beats_Wash/Detuned_Beats_Wash.ino +++ b/examples/06.Synthesis/Detuned_Beats_Wash/Detuned_Beats_Wash.ino @@ -29,7 +29,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/06.Synthesis/Difference_Tone/Difference_Tone.ino b/examples/06.Synthesis/Difference_Tone/Difference_Tone.ino index c7b8d41a6..46d2efbc4 100644 --- a/examples/06.Synthesis/Difference_Tone/Difference_Tone.ino +++ b/examples/06.Synthesis/Difference_Tone/Difference_Tone.ino @@ -17,7 +17,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/06.Synthesis/FMsynth/FMsynth.ino b/examples/06.Synthesis/FMsynth/FMsynth.ino index b450fb552..b21c40ebf 100644 --- a/examples/06.Synthesis/FMsynth/FMsynth.ino +++ b/examples/06.Synthesis/FMsynth/FMsynth.ino @@ -22,7 +22,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // table for Oscils to play #include diff --git a/examples/06.Synthesis/FMsynth_32k_HIFI/FMsynth_32k_HIFI.ino b/examples/06.Synthesis/FMsynth_32k_HIFI/FMsynth_32k_HIFI.ino index 7b6c4b65b..95bc0e8d3 100644 --- a/examples/06.Synthesis/FMsynth_32k_HIFI/FMsynth_32k_HIFI.ino +++ b/examples/06.Synthesis/FMsynth_32k_HIFI/FMsynth_32k_HIFI.ino @@ -7,20 +7,13 @@ This sketch using HIFI mode is not for Teensy 3.1. - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - //#define AUDIO_MODE STANDARD - #define AUDIO_MODE STANDARD_PLUS - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - //#define AUDIO_MODE STANDARD_PLUS - #define AUDIO_MODE HIFI - - The sketch also sounds better with a faster sample rate, for less aliasing - #define AUDIO_RATE 32768 - in mozzi_config. + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). + + The MOZZI_AUDIO_RATE (sample rate) is additionally configured at 32768 Hz, + which is the default on most platforms, but twice the standard rate on + AVR-CPUs. Try chaging it back to hear the difference. Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -49,7 +42,11 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM +#define MOZZI_AUDIO_RATE 32768 + +#include #include #include // table for Oscils to play #include diff --git a/examples/06.Synthesis/NonAlias_MetaOscil/NonAlias_MetaOscil.ino b/examples/06.Synthesis/NonAlias_MetaOscil/NonAlias_MetaOscil.ino index eeaf53d6e..6e4ff3fa2 100644 --- a/examples/06.Synthesis/NonAlias_MetaOscil/NonAlias_MetaOscil.ino +++ b/examples/06.Synthesis/NonAlias_MetaOscil/NonAlias_MetaOscil.ino @@ -35,7 +35,7 @@ Tim Barrass 2012, Combriat T. 2021, CC by-nc-sa. */ -#include +#include #include // oscillator template #include diff --git a/examples/06.Synthesis/PDresonant/PDresonant.ino b/examples/06.Synthesis/PDresonant/PDresonant.ino index 05452a34f..856dd7262 100644 --- a/examples/06.Synthesis/PDresonant/PDresonant.ino +++ b/examples/06.Synthesis/PDresonant/PDresonant.ino @@ -28,7 +28,7 @@ //#include may be needed on some systems/versions -#include +#include // for fake midi #include diff --git a/examples/06.Synthesis/PWM_Phasing/PWM_Phasing.ino b/examples/06.Synthesis/PWM_Phasing/PWM_Phasing.ino index dd9c07370..eafe06028 100644 --- a/examples/06.Synthesis/PWM_Phasing/PWM_Phasing.ino +++ b/examples/06.Synthesis/PWM_Phasing/PWM_Phasing.ino @@ -21,7 +21,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include Phasor aPhasor1; diff --git a/examples/06.Synthesis/WaveFolder/WaveFolder.ino b/examples/06.Synthesis/WaveFolder/WaveFolder.ino index c4ca20993..54bff7aa2 100644 --- a/examples/06.Synthesis/WaveFolder/WaveFolder.ino +++ b/examples/06.Synthesis/WaveFolder/WaveFolder.ino @@ -17,7 +17,7 @@ */ -#include +#include #include // oscillator template #include // sine table for oscillator #include // saw table for oscillator diff --git a/examples/06.Synthesis/WavePacket_Double/WavePacket_Double.ino b/examples/06.Synthesis/WavePacket_Double/WavePacket_Double.ino index 7558a4c1a..1c8d0be93 100644 --- a/examples/06.Synthesis/WavePacket_Double/WavePacket_Double.ino +++ b/examples/06.Synthesis/WavePacket_Double/WavePacket_Double.ino @@ -29,7 +29,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/06.Synthesis/WavePacket_Sample/WavePacket_Sample.ino b/examples/06.Synthesis/WavePacket_Sample/WavePacket_Sample.ino index 323c50d17..116e69593 100644 --- a/examples/06.Synthesis/WavePacket_Sample/WavePacket_Sample.ino +++ b/examples/06.Synthesis/WavePacket_Sample/WavePacket_Sample.ino @@ -32,6 +32,7 @@ Tim Barrass 2013, CC by-nc-sa. */ +#include #include #include #include diff --git a/examples/06.Synthesis/WavePacket_Single/WavePacket_Single.ino b/examples/06.Synthesis/WavePacket_Single/WavePacket_Single.ino index 60c0b80ca..ad8eed2d1 100644 --- a/examples/06.Synthesis/WavePacket_Single/WavePacket_Single.ino +++ b/examples/06.Synthesis/WavePacket_Single/WavePacket_Single.ino @@ -29,6 +29,7 @@ Tim Barrass 2013, CC by-nc-sa. */ +#include #include #include #include diff --git a/examples/06.Synthesis/Waveshaper/Waveshaper.ino b/examples/06.Synthesis/Waveshaper/Waveshaper.ino index f2fe31236..8a1d62e39 100644 --- a/examples/06.Synthesis/Waveshaper/Waveshaper.ino +++ b/examples/06.Synthesis/Waveshaper/Waveshaper.ino @@ -17,7 +17,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/07.Envelopes/ADSR_Audio_Rate_Envelope/ADSR_Audio_Rate_Envelope.ino b/examples/07.Envelopes/ADSR_Audio_Rate_Envelope/ADSR_Audio_Rate_Envelope.ino index 839a70b8a..3755951e7 100644 --- a/examples/07.Envelopes/ADSR_Audio_Rate_Envelope/ADSR_Audio_Rate_Envelope.ino +++ b/examples/07.Envelopes/ADSR_Audio_Rate_Envelope/ADSR_Audio_Rate_Envelope.ino @@ -19,7 +19,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_Long/ADSR_Audio_Rate_Envelope_Long.ino b/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_Long/ADSR_Audio_Rate_Envelope_Long.ino index 518f852ef..515a94ce7 100644 --- a/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_Long/ADSR_Audio_Rate_Envelope_Long.ino +++ b/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_Long/ADSR_Audio_Rate_Envelope_Long.ino @@ -22,7 +22,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_x2/ADSR_Audio_Rate_Envelope_x2.ino b/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_x2/ADSR_Audio_Rate_Envelope_x2.ino index 112fecfcf..299c1c7df 100644 --- a/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_x2/ADSR_Audio_Rate_Envelope_x2.ino +++ b/examples/07.Envelopes/ADSR_Audio_Rate_Envelope_x2/ADSR_Audio_Rate_Envelope_x2.ino @@ -17,7 +17,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/07.Envelopes/ADSR_Control_Rate_Envelope/ADSR_Control_Rate_Envelope.ino b/examples/07.Envelopes/ADSR_Control_Rate_Envelope/ADSR_Control_Rate_Envelope.ino index 30f27aab3..64679efb3 100644 --- a/examples/07.Envelopes/ADSR_Control_Rate_Envelope/ADSR_Control_Rate_Envelope.ino +++ b/examples/07.Envelopes/ADSR_Control_Rate_Envelope/ADSR_Control_Rate_Envelope.ino @@ -11,7 +11,7 @@ Tim Barrass 2013-14, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/07.Envelopes/Ead_Envelope/Ead_Envelope.ino b/examples/07.Envelopes/Ead_Envelope/Ead_Envelope.ino index 72a7b6c25..a75c2bf75 100644 --- a/examples/07.Envelopes/Ead_Envelope/Ead_Envelope.ino +++ b/examples/07.Envelopes/Ead_Envelope/Ead_Envelope.ino @@ -13,7 +13,7 @@ Tim Barrass 2012, CC by-nc-sa */ -#include +#include #include // oscillator template #include // recorded audio wavetable #include // exponential attack decay diff --git a/examples/07.Envelopes/Phasemod_Envelope/Phasemod_Envelope.ino b/examples/07.Envelopes/Phasemod_Envelope/Phasemod_Envelope.ino index 3dd6f9cf2..975e75980 100644 --- a/examples/07.Envelopes/Phasemod_Envelope/Phasemod_Envelope.ino +++ b/examples/07.Envelopes/Phasemod_Envelope/Phasemod_Envelope.ino @@ -14,7 +14,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/08.Samples/Sample/Sample.ino b/examples/08.Samples/Sample/Sample.ino index cc89bd066..b5723eaea 100644 --- a/examples/08.Samples/Sample/Sample.ino +++ b/examples/08.Samples/Sample/Sample.ino @@ -17,7 +17,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // Sample template #include #include diff --git a/examples/08.Samples/SampleHuffman_Umpah/SampleHuffman_Umpah.ino b/examples/08.Samples/SampleHuffman_Umpah/SampleHuffman_Umpah.ino index de8d4ab93..9958e7584 100644 --- a/examples/08.Samples/SampleHuffman_Umpah/SampleHuffman_Umpah.ino +++ b/examples/08.Samples/SampleHuffman_Umpah/SampleHuffman_Umpah.ino @@ -41,7 +41,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include "umpah_huff.h" diff --git a/examples/08.Samples/Sample_Loop_Points/Sample_Loop_Points.ino b/examples/08.Samples/Sample_Loop_Points/Sample_Loop_Points.ino index 4c674ea23..599b6b555 100644 --- a/examples/08.Samples/Sample_Loop_Points/Sample_Loop_Points.ino +++ b/examples/08.Samples/Sample_Loop_Points/Sample_Loop_Points.ino @@ -20,7 +20,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // Sample template #include // table for Sample #include diff --git a/examples/08.Samples/Sample_Scrub/Sample_Scrub.ino b/examples/08.Samples/Sample_Scrub/Sample_Scrub.ino index d8e02f575..caf07de1c 100644 --- a/examples/08.Samples/Sample_Scrub/Sample_Scrub.ino +++ b/examples/08.Samples/Sample_Scrub/Sample_Scrub.ino @@ -15,7 +15,7 @@ CC by-nc-sa */ -#include +#include #include // Sample template #include #include diff --git a/examples/08.Samples/Samples/Samples.ino b/examples/08.Samples/Samples/Samples.ino index 00854ccec..4a2cb4a76 100644 --- a/examples/08.Samples/Samples/Samples.ino +++ b/examples/08.Samples/Samples/Samples.ino @@ -18,7 +18,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include // Sample template #include // wavetable data #include // wavetable data diff --git a/examples/08.Samples/Samples_Tables_Arrays/Samples_Tables_Arrays.ino b/examples/08.Samples/Samples_Tables_Arrays/Samples_Tables_Arrays.ino index 2d2e4e4ab..4a0d84983 100644 --- a/examples/08.Samples/Samples_Tables_Arrays/Samples_Tables_Arrays.ino +++ b/examples/08.Samples/Samples_Tables_Arrays/Samples_Tables_Arrays.ino @@ -22,7 +22,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include // for rand() diff --git a/examples/08.Samples/Wavetable_Swap/Wavetable_Swap.ino b/examples/08.Samples/Wavetable_Swap/Wavetable_Swap.ino index bf44075b9..8711475ff 100644 --- a/examples/08.Samples/Wavetable_Swap/Wavetable_Swap.ino +++ b/examples/08.Samples/Wavetable_Swap/Wavetable_Swap.ino @@ -7,7 +7,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include diff --git a/examples/09.Delays/AudioDelay/AudioDelay.ino b/examples/09.Delays/AudioDelay/AudioDelay.ino index e651eb60a..99ef70189 100644 --- a/examples/09.Delays/AudioDelay/AudioDelay.ino +++ b/examples/09.Delays/AudioDelay/AudioDelay.ino @@ -16,7 +16,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // wavetable #include // wavetable diff --git a/examples/09.Delays/AudioDelayFeedback/AudioDelayFeedback.ino b/examples/09.Delays/AudioDelayFeedback/AudioDelayFeedback.ino index 250922569..fe91c592d 100644 --- a/examples/09.Delays/AudioDelayFeedback/AudioDelayFeedback.ino +++ b/examples/09.Delays/AudioDelayFeedback/AudioDelayFeedback.ino @@ -16,7 +16,7 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include #include #include // wavetable #include // wavetable diff --git a/examples/09.Delays/AudioDelayFeedbackAllpass/AudioDelayFeedbackAllpass.ino b/examples/09.Delays/AudioDelayFeedbackAllpass/AudioDelayFeedbackAllpass.ino index 431e4a3f1..9178af5c3 100644 --- a/examples/09.Delays/AudioDelayFeedbackAllpass/AudioDelayFeedbackAllpass.ino +++ b/examples/09.Delays/AudioDelayFeedbackAllpass/AudioDelayFeedbackAllpass.ino @@ -18,7 +18,7 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include #include #include #include // exponential attack decay diff --git a/examples/09.Delays/AudioDelayFeedbackX2/AudioDelayFeedbackX2.ino b/examples/09.Delays/AudioDelayFeedbackX2/AudioDelayFeedbackX2.ino index aed092e1b..09b68ffae 100644 --- a/examples/09.Delays/AudioDelayFeedbackX2/AudioDelayFeedbackX2.ino +++ b/examples/09.Delays/AudioDelayFeedbackX2/AudioDelayFeedbackX2.ino @@ -17,7 +17,7 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include #include #include // wavetable for audio #include // wavetable for delay sweep diff --git a/examples/09.Delays/AudioDelayFeedback_HIFI/AudioDelayFeedback_HIFI.ino b/examples/09.Delays/AudioDelayFeedback_HIFI/AudioDelayFeedback_HIFI.ino index d41276f72..d5f4c59b2 100644 --- a/examples/09.Delays/AudioDelayFeedback_HIFI/AudioDelayFeedback_HIFI.ino +++ b/examples/09.Delays/AudioDelayFeedback_HIFI/AudioDelayFeedback_HIFI.ino @@ -3,22 +3,14 @@ Demonstrates AudioDelayFeedback. - This sketch using HIFI mode is not for Teensy 3.1. - - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - //#define AUDIO_MODE STANDARD - #define AUDIO_MODE STANDARD_PLUS - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - //#define AUDIO_MODE STANDARD_PLUS - #define AUDIO_MODE HIFI - - The sketch also sounds better with a faster sample rate, for less aliasing - #define AUDIO_RATE 32768 - in mozzi_config. + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). + + Important: + The MOZZI_AUDIO_RATE (sample rate) is additionally configured at 32768 Hz, + which is the default on most platforms, but twice the standard rate on + AVR-CPUs. Try chaging it back to hear the difference. Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -47,7 +39,11 @@ Tim Barrass 2012-13, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM +#define MOZZI_AUDIO_RATE 32768 + +#include #include #include // wavetable #include // wavetable diff --git a/examples/09.Delays/ReverbTank_HIFI/ReverbTank_HIFI.ino b/examples/09.Delays/ReverbTank_HIFI/ReverbTank_HIFI.ino index e264c6671..c9fa316b1 100644 --- a/examples/09.Delays/ReverbTank_HIFI/ReverbTank_HIFI.ino +++ b/examples/09.Delays/ReverbTank_HIFI/ReverbTank_HIFI.ino @@ -22,7 +22,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/09.Delays/ReverbTank_STANDARD/ReverbTank_STANDARD.ino b/examples/09.Delays/ReverbTank_STANDARD/ReverbTank_STANDARD.ino index 20e73e694..e7eaf651e 100644 --- a/examples/09.Delays/ReverbTank_STANDARD/ReverbTank_STANDARD.ino +++ b/examples/09.Delays/ReverbTank_STANDARD/ReverbTank_STANDARD.ino @@ -22,7 +22,7 @@ Tim Barrass 2013, CC by-nc-sa. */ -#include +#include #include #include #include diff --git a/examples/10.Audio_Filters/LowPassFilterX2/LowPassFilterX2.ino b/examples/10.Audio_Filters/LowPassFilterX2/LowPassFilterX2.ino index cab4e77e3..a16d122a2 100644 --- a/examples/10.Audio_Filters/LowPassFilterX2/LowPassFilterX2.ino +++ b/examples/10.Audio_Filters/LowPassFilterX2/LowPassFilterX2.ino @@ -16,7 +16,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // recorded audio wavetable #include // for filter modulation diff --git a/examples/10.Audio_Filters/MultiResonantFilter/MultiResonantFilter.ino b/examples/10.Audio_Filters/MultiResonantFilter/MultiResonantFilter.ino index 06fb110df..a01e1afd1 100644 --- a/examples/10.Audio_Filters/MultiResonantFilter/MultiResonantFilter.ino +++ b/examples/10.Audio_Filters/MultiResonantFilter/MultiResonantFilter.ino @@ -23,7 +23,7 @@ Thomas Combriat 2023, CC by-nc-sa. */ -#include +#include #include #include // recorded audio wavetable #include // for filter modulation diff --git a/examples/10.Audio_Filters/ResonantFilter/ResonantFilter.ino b/examples/10.Audio_Filters/ResonantFilter/ResonantFilter.ino index 77b775c0e..5e4c63054 100644 --- a/examples/10.Audio_Filters/ResonantFilter/ResonantFilter.ino +++ b/examples/10.Audio_Filters/ResonantFilter/ResonantFilter.ino @@ -20,7 +20,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // recorded audio wavetable #include // for filter modulation diff --git a/examples/10.Audio_Filters/ResonantFilter16/ResonantFilter16.ino b/examples/10.Audio_Filters/ResonantFilter16/ResonantFilter16.ino index 19a7f048a..50980fe4c 100644 --- a/examples/10.Audio_Filters/ResonantFilter16/ResonantFilter16.ino +++ b/examples/10.Audio_Filters/ResonantFilter16/ResonantFilter16.ino @@ -22,7 +22,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include // recorded audio wavetable #include // for filter modulation diff --git a/examples/10.Audio_Filters/StateVariableFilter/StateVariableFilter.ino b/examples/10.Audio_Filters/StateVariableFilter/StateVariableFilter.ino index 8db1a6d57..3edd09133 100644 --- a/examples/10.Audio_Filters/StateVariableFilter/StateVariableFilter.ino +++ b/examples/10.Audio_Filters/StateVariableFilter/StateVariableFilter.ino @@ -16,7 +16,7 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include #include #include #include // for filter modulation diff --git a/examples/10.Audio_Filters/StateVariableFilter_HIFI/StateVariableFilter_HIFI.ino b/examples/10.Audio_Filters/StateVariableFilter_HIFI/StateVariableFilter_HIFI.ino index cb23712b4..5b0681aad 100644 --- a/examples/10.Audio_Filters/StateVariableFilter_HIFI/StateVariableFilter_HIFI.ino +++ b/examples/10.Audio_Filters/StateVariableFilter_HIFI/StateVariableFilter_HIFI.ino @@ -5,16 +5,10 @@ which in this case requires the input signal level to be reduced to avoid distortion which can occur with sharp resonance settings. - This sketch using HIFI mode is not for Teensy 3.1. - - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - #define AUDIO_MODE STANDARD - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - #define AUDIO_MODE HIFI + Important: + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -43,7 +37,10 @@ Tim Barrass 2012, CC by-nc-sa. */ -#include +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM + +#include #include #include #include diff --git a/examples/11.Communication/Mozzi_MIDI_Input/Mozzi_MIDI_Input.ino b/examples/11.Communication/Mozzi_MIDI_Input/Mozzi_MIDI_Input.ino index 24513f1a4..c563e00e5 100644 --- a/examples/11.Communication/Mozzi_MIDI_Input/Mozzi_MIDI_Input.ino +++ b/examples/11.Communication/Mozzi_MIDI_Input/Mozzi_MIDI_Input.ino @@ -20,7 +20,7 @@ */ #include -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/11.Communication/Mozzi_Processing_Serial/Mozzi_Processing_Serial.ino b/examples/11.Communication/Mozzi_Processing_Serial/Mozzi_Processing_Serial.ino index 27ecb18ac..904960bbf 100644 --- a/examples/11.Communication/Mozzi_Processing_Serial/Mozzi_Processing_Serial.ino +++ b/examples/11.Communication/Mozzi_Processing_Serial/Mozzi_Processing_Serial.ino @@ -6,7 +6,7 @@ in individual bytes, each of which ranges from 0 to 255. Arduino reads these bytes and uses them to set the frequency of the oscillator. - Circuit: Audio output on digital pin 9. (STANDARD_PLUS mode in mozzi_config.h) + Circuit: Audio output on digital pin 9. (on Arduino Uno/Nano, in default config) Serial connection to Processing, Max/MSP, or another serial application Dimmer example created 2006 @@ -19,8 +19,7 @@ */ - - +#include #include // oscillator template #include // sine table for oscillator diff --git a/examples/11.Communication/Sinewave_PWM_leds_HIFI/Sinewave_PWM_leds_HIFI.ino b/examples/11.Communication/Sinewave_PWM_leds_HIFI/Sinewave_PWM_leds_HIFI.ino index 208341260..0a5cd25bc 100644 --- a/examples/11.Communication/Sinewave_PWM_leds_HIFI/Sinewave_PWM_leds_HIFI.ino +++ b/examples/11.Communication/Sinewave_PWM_leds_HIFI/Sinewave_PWM_leds_HIFI.ino @@ -8,22 +8,16 @@ between 0-255. The technique is explained here: http://playground.arduino.cc/Main/PWMallPins - With AUDIO_RATE at 16384 Hz, this gives a 64 Hz pwm duty cycle for the LEDs. + + With MOZZI_AUDIO_RATE at 16384 Hz (default on AVR), this gives a 64 Hz pwm duty cycle for the LEDs. If there is visible flicker, the resolution of the pwm could be made lower, - or the AUDIO_RATE could be increased to 32768 Hz, if the + or the MOZZI_AUDIO_RATE could be increased to 32768 Hz, if the cpu isn't too busy. - HIFI mode is not for Teensy 3.x, but the PWM led part should work. - - IMPORTANT: this sketch requires Mozzi/mozzi_config.h to be - be changed from STANDARD mode to HIFI. - In Mozz/mozzi_config.h, change - #define AUDIO_MODE STANDARD - //#define AUDIO_MODE HIFI - to - //#define AUDIO_MODE STANDARD - #define AUDIO_MODE HIFI - + Important: + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -56,8 +50,11 @@ Tim Barrass 2012-13, CC by-nc-sa. */ +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM +//#define MOZZI_AUDIO_RATE 32768 -#include +#include #include // oscillator template #include // sine table for oscillator @@ -100,7 +97,7 @@ void setup(){ kBlue.setFreq(0.27f); // set audio oscil frequency aSin.setFreq(440); - startMozzi(); // uses the default control rate of 64, defined in mozzi_config.h + startMozzi(); // uses the default control rate of 64 } diff --git a/examples/11.Communication/Teensy_USB_MIDI_Input/Teensy_USB_MIDI_Input.ino b/examples/11.Communication/Teensy_USB_MIDI_Input/Teensy_USB_MIDI_Input.ino index 216a7ae5d..c5ce5cf5e 100644 --- a/examples/11.Communication/Teensy_USB_MIDI_Input/Teensy_USB_MIDI_Input.ino +++ b/examples/11.Communication/Teensy_USB_MIDI_Input/Teensy_USB_MIDI_Input.ino @@ -22,7 +22,7 @@ */ #include -#include +#include #include // oscillator template #include // sine table for oscillator #include diff --git a/examples/11.Communication/TwoWire_Read_ADXL345/TwoWire_Read_ADXL345.ino b/examples/11.Communication/TwoWire_Read_ADXL345/TwoWire_Read_ADXL345.ino index aedd5935a..1236c6c34 100644 --- a/examples/11.Communication/TwoWire_Read_ADXL345/TwoWire_Read_ADXL345.ino +++ b/examples/11.Communication/TwoWire_Read_ADXL345/TwoWire_Read_ADXL345.ino @@ -20,7 +20,7 @@ This example code is in the public domain. */ -#include +#include #include // oscillator template #include // exponential attack decay #include // sine table for oscillator diff --git a/examples/12.Misc/MozziByte_HIFI/MozziByte_HIFI.ino b/examples/12.Misc/MozziByte_HIFI/MozziByte_HIFI.ino index 3bc9df8e2..f8de1c267 100644 --- a/examples/12.Misc/MozziByte_HIFI/MozziByte_HIFI.ino +++ b/examples/12.Misc/MozziByte_HIFI/MozziByte_HIFI.ino @@ -3,8 +3,10 @@ The Arduino-compatible "Pro Micro" board sent with Mozzibytes needs "Arduino Leonardo" to be set under Arduino>Tools>Board. - Important: - #define AUDIO_MODE HIFI in mozzi_config.h + Important: + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -28,7 +30,10 @@ Tim Barrass 2018, CC by-nc-sa. */ +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM +#include #include #include #include diff --git a/examples/12.Misc/Risset_Beat_HIFI/Risset_Beat_HIFI.ino b/examples/12.Misc/Risset_Beat_HIFI/Risset_Beat_HIFI.ino index ed1b0c41e..f5116c239 100644 --- a/examples/12.Misc/Risset_Beat_HIFI/Risset_Beat_HIFI.ino +++ b/examples/12.Misc/Risset_Beat_HIFI/Risset_Beat_HIFI.ino @@ -5,7 +5,9 @@ Demonstrates Sample(), EventDelay(), Line(), fixed pint numbers and bit-shifting Important: - #define AUDIO_MODE HIFI in mozzi_config.h + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -32,8 +34,10 @@ Tim Barrass 2018, CC by-nc-sa. */ +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM -#include +#include #include // Sample template #include // table for Sample #include diff --git a/examples/12.Misc/Shepard_Tone_HIFI/Shepard_Tone_HIFI.ino b/examples/12.Misc/Shepard_Tone_HIFI/Shepard_Tone_HIFI.ino index eefecd0ff..64c828aae 100644 --- a/examples/12.Misc/Shepard_Tone_HIFI/Shepard_Tone_HIFI.ino +++ b/examples/12.Misc/Shepard_Tone_HIFI/Shepard_Tone_HIFI.ino @@ -7,7 +7,9 @@ More oscillators could be added for a more convincing effect. Important: - #define AUDIO_MODE HIFI in mozzi_config.h + This sketch uses MOZZI_OUTPUT_2PIN_PWM (aka HIFI) output mode, which + is not available on all boards (among others, it works on the + classic Arduino boards, but not Teensy 3.x and friends). Circuit: Audio output on digital pin 9 and 10 (on a Uno or similar), Check the Mozzi core module documentation for others and more detail @@ -29,8 +31,10 @@ Tim Barrass 2018, CC by-nc-sa. */ +#include +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_2PIN_PWM -#include +#include #include #include #include "triangle512_uint8.h" diff --git a/examples/12.Misc/Stereo_Hack/Stereo_Hack.ino b/examples/12.Misc/Stereo/Stereo.ino similarity index 70% rename from examples/12.Misc/Stereo_Hack/Stereo_Hack.ino rename to examples/12.Misc/Stereo/Stereo.ino index 1bcc71484..257b4c0ba 100644 --- a/examples/12.Misc/Stereo_Hack/Stereo_Hack.ino +++ b/examples/12.Misc/Stereo/Stereo.ino @@ -1,9 +1,11 @@ -/* Example crudely panning noise to test stereo output hack, +/* Example crudely panning noise to test stereo output, * using Mozzi sonification library. * - * Tests stereo output. This requires #define AUDIO_CHANNELS STEREO in mozzi_config.h + * Tests stereo output. * - * Circuit: Audio outputs on digital pins 9 and 10. + * NOTE: Stereo output is not supported on all platforms / configurations! + * + * Circuit: Audio outputs on digital pins 9 and 10 (on classic Arduino). * * Mozzi help/discussion/announcements: * https://groups.google.com/forum/#!forum/mozzi-users @@ -12,7 +14,12 @@ * This example code is in the public domain. */ -#include +// Configure Mozzi for Stereo output. This must be done before #include +// MozziConfigValues.h provides named defines for available config options. +#include +#define MOZZI_AUDIO_CHANNELS MOZZI_STEREO + +#include #include // for controlling panning position #include // oscil for audio sig #include // table for oscillator diff --git a/examples/12.Misc/Stereo_Hack_Pan/Stereo_Hack_Pan.ino b/examples/12.Misc/Stereo_Pan/Stereo_Pan.ino similarity index 78% rename from examples/12.Misc/Stereo_Hack_Pan/Stereo_Hack_Pan.ino rename to examples/12.Misc/Stereo_Pan/Stereo_Pan.ino index b6943face..4f1e14ee5 100644 --- a/examples/12.Misc/Stereo_Hack_Pan/Stereo_Hack_Pan.ino +++ b/examples/12.Misc/Stereo_Pan/Stereo_Pan.ino @@ -1,9 +1,11 @@ /* Example of constant power panning to test stereo output hack, using Mozzi sonification library. - Tests stereo output. This requires #define AUDIO_CHANNELS STEREO in mozzi_config.h + Tests stereo output. - Circuit: Audio outputs on digital pins 9 and 10. + NOTE: Stereo output is not supported on all platforms / configurations! + + Circuit: Audio outputs on digital pins 9 and 10 (on classic Arduino). Mozzi documentation/API https://sensorium.github.io/Mozzi/doc/html/index.html @@ -15,7 +17,12 @@ This example code is in the public domain. */ -#include +// Configure Mozzi for Stereo output. This must be done before #include +// MozziConfigValues.h provides named defines for available config options. +#include +#define MOZZI_AUDIO_CHANNELS MOZZI_STEREO + +#include #include // oscil for audio sig #include // table for audio oscillator #include // sine table for pan oscillator @@ -57,4 +64,4 @@ AudioOutput_t updateAudio() { void loop() { audioHook(); // required here -} \ No newline at end of file +} diff --git a/examples/13.External_Audio_Output/FMsynth_MCP4921_mono_12bits/FMsynth_MCP4921_mono_12bits.ino b/examples/13.External_Audio_Output/FMsynth_MCP4921_mono_12bits/FMsynth_MCP4921_mono_12bits.ino index 252d322ed..c9734bb37 100644 --- a/examples/13.External_Audio_Output/FMsynth_MCP4921_mono_12bits/FMsynth_MCP4921_mono_12bits.ino +++ b/examples/13.External_Audio_Output/FMsynth_MCP4921_mono_12bits/FMsynth_MCP4921_mono_12bits.ino @@ -3,8 +3,6 @@ using an user-defined audioOutput() function. Based on Mozzi's example: FMsynth. - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - Circuit: (see the DAC library README for details) MCP4921 // Connect to: @@ -29,7 +27,13 @@ T. Combriat 2020, CC by-nc-sa. */ -#include +// before including Mozzi.h, configure external audio output mode: +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +// Note: For demonstration purposes, this sketch does *not* set the following (although it would make sense): +//#define MOZZI_AUDIO_BITS 12 // the default value of 16 for external audio is thus used, instead + +#include #include #include // table for Oscils to play #include diff --git a/examples/13.External_Audio_Output/MCP4922_mono_24bits/MCP4922_mono_24bits.ino b/examples/13.External_Audio_Output/MCP4922_mono_24bits/MCP4922_mono_24bits.ino index 463515201..a590e21a2 100644 --- a/examples/13.External_Audio_Output/MCP4922_mono_24bits/MCP4922_mono_24bits.ino +++ b/examples/13.External_Audio_Output/MCP4922_mono_24bits/MCP4922_mono_24bits.ino @@ -4,9 +4,6 @@ WARNING: YOU CANNOT ACHEIVE MORE THAN 16BITS ON AN AVR ARDUINO, THIS EXAMPLE WON'T WORK AS IT IS. - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - #define EXTERNAL_AUDIO_BITS 24 should be set in mozzi_config.h - Circuit: (see the DAC library README for details) MCP4921 // Connect to: @@ -50,7 +47,12 @@ T. Combriat 2020, CC by-nc-sa. */ -#include +// before including Mozzi.h, configure external audio output mode: +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +#define MOZZI_AUDIO_BITS 24 + +#include #include #include // table for Oscils to play #include @@ -71,7 +73,6 @@ Oscil kEnv1(COS2048_DATA); // External audio output parameters and DAC declaration #define SS_PIN 38 // if you are on AVR and using PortWrite you need still need to put the pin you are actually using: 7 on Uno, 38 on Mega -//#define AUDIO_BIAS 8388608 // we are at 24 bits, so we have to bias the signal of 2^(24-1) = 8388608 (not needed since PR #98) #define BITS_PER_CHANNEL 12 // each channel of the DAC is outputting 12 bits DAC_MCP49xx dac(DAC_MCP49xx::MCP4922, SS_PIN); @@ -80,7 +81,7 @@ DAC_MCP49xx dac(DAC_MCP49xx::MCP4922, SS_PIN); void audioOutput(const AudioOutput f) // f is a structure containing both channels { - int out = AUDIO_BIAS + f.l(); + int out = MOZZI_AUDIO_BIAS + f.l(); unsigned short lowBits = (unsigned short) out; // unsigned short highBits = out >> BITS_PER_CHANNEL; diff --git a/examples/13.External_Audio_Output/PT8211_stereo_16bits/PT8211_stereo_16bits.ino b/examples/13.External_Audio_Output/PT8211_stereo_16bits/PT8211_stereo_16bits.ino index 996d94101..20664d4d1 100644 --- a/examples/13.External_Audio_Output/PT8211_stereo_16bits/PT8211_stereo_16bits.ino +++ b/examples/13.External_Audio_Output/PT8211_stereo_16bits/PT8211_stereo_16bits.ino @@ -5,10 +5,6 @@ using an user-defined audioOutput() function. I2S, the protocol used by this DAC, is here emulated in synced way using SPI. - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - #define AUDIO_CHANNELS STEREO should be set in mozzi_config.h - - Circuit: PT8211 // Connect to: @@ -30,13 +26,17 @@ T. Combriat 2020, CC by-nc-sa. */ -#include +// before including Mozzi.h, configure external audio output mode: +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +#define MOZZI_AUDIO_CHANNELS MOZZI_STEREO +#define CONTROL_RATE 256 // Hz, powers of 2 are most reliable + +#include #include #include // table for Oscils to play #include -#define CONTROL_RATE 256 // Hz, powers of 2 are most reliable - // Synthesis part Oscil aCos1(COS2048_DATA); @@ -50,11 +50,6 @@ Oscil kEnv2(COS2048_DATA); // External audio output parameters #define WS_pin 5 // channel select pin for the DAC -//#define AUDIO_BIAS 0 // this DAC works on 0-centered signals - - - - @@ -67,7 +62,7 @@ void audioOutput(const AudioOutput f) // f is a structure containing both channe */ digitalWrite(WS_pin, LOW); //select Right channel - SPI.transfer16(f.r()); + SPI.transfer16(f.r()); // Note: This DAC works on 0-centered samples, no need to add MOZZI_AUDIO_BIAS digitalWrite(WS_pin, HIGH); // select Left channel SPI.transfer16(f.l()); diff --git a/examples/13.External_Audio_Output/PT8211_stereo_16bits_STM32_SPI2/PT8211_stereo_16bits_STM32_SPI2.ino b/examples/13.External_Audio_Output/PT8211_stereo_16bits_STM32_SPI2/PT8211_stereo_16bits_STM32_SPI2.ino index 3658191a0..1904cc1c7 100644 --- a/examples/13.External_Audio_Output/PT8211_stereo_16bits_STM32_SPI2/PT8211_stereo_16bits_STM32_SPI2.ino +++ b/examples/13.External_Audio_Output/PT8211_stereo_16bits_STM32_SPI2/PT8211_stereo_16bits_STM32_SPI2.ino @@ -4,11 +4,6 @@ using Mozzi sonification library and an external dual DAC PT8211 (inspired by: https://sparklogic.ru/code-snipplets/i2s-example-code.html) using an user-defined audioOutput() function. I2S, the protocol used by this DAC, is here emulated in synced way using SPI. - - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - #define AUDIO_CHANNELS STEREO should be set in mozzi_config.h - - Circuit: PT8211 // Connect to: @@ -29,8 +24,11 @@ Tim Barrass 2012, CC by-nc-sa. T. Combriat 2020, CC by-nc-sa. */ +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +#define MOZZI_AUDIO_CHANNELS MOZZI_STEREO -#include +#include #include #include // table for Oscils to play #include @@ -50,7 +48,6 @@ Oscil kEnv2(COS2048_DATA); // External audio output parameters #define WS_pin PB12 // channel select pin for the DAC -//#define AUDIO_BIAS 0 // this DAC works on 0-centered signals SPIClass mySPI(2); // declaration of SPI for using SPI2 and thus freeing all ADC pins @@ -60,7 +57,7 @@ SPIClass mySPI(2); // declaration of SPI for using SPI2 and thus freeing all void audioOutput(const AudioOutput f) // f is a structure containing both channels { digitalWrite(WS_pin, LOW); //select Right channel - mySPI.transfer16(f.r()); + mySPI.transfer16(f.r()); // Note: This DAC works on 0-centered samples, no need to add MOZZI_AUDIO_BIAS digitalWrite(WS_pin, HIGH); // select Left channel mySPI.transfer16(f.l()); diff --git a/examples/13.External_Audio_Output/Sinewave_R2R_DAC/Sinewave_R2R_DAC.ino b/examples/13.External_Audio_Output/Sinewave_R2R_DAC/Sinewave_R2R_DAC.ino index 3979db190..716824085 100644 --- a/examples/13.External_Audio_Output/Sinewave_R2R_DAC/Sinewave_R2R_DAC.ino +++ b/examples/13.External_Audio_Output/Sinewave_R2R_DAC/Sinewave_R2R_DAC.ino @@ -2,9 +2,6 @@ using Mozzi sonification library and an user-defined audioOutput() function. - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - #define EXTERNAL_AUDIO_BITS 6 should be set in mozzi_config.h (you can increase that if you want, but will need to increase the number of step of the ladder accordingly). - Demonstrates the use of audioOutput() using a R/2R DAC connected on 6 digital pins of an Arduino. @@ -42,29 +39,29 @@ T. Combriat 2020, CC by-nc-sa. */ -#include +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +#define MOZZI_AUDIO_BITS 6 +#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable + +#include #include // oscillator template #include // sine table for oscillator // use: Oscil oscilName (wavetable), look in .h file of table #included above Oscil aSin(SIN2048_DATA); -// use #define for CONTROL_RATE, not a constant -#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable - - - // External output parameters for this example -#define R2R_N_PIN EXTERNAL_AUDIO_BITS // Number of stage of the resistance ladder = number of digits of the DAC, can be defined through EXTERNAL_AUDIO_BITS in mozzi_config.h +#define R2R_N_PIN MOZZI_AUDIO_BITS // Number of stage of the resistance ladder = number of digits of the DAC, can be defined through MOZZI_AUDIO_BITS const int r2r_pin[R2R_N_PIN] = {30, 31, 32, 33, 34, 35}; // pins to the resistance ladder, in order, // starting with LSB (pin closer to GND) // so D0, D1, etc. //#define AUDIO_BIAS 32 // we are at 6 bits so we have to bias the signal of 2^(6-1)=32, not needed since PR#98 -void audioOutput(const AudioOutput f) // f is a structure potentially containing both channels, scaled according to EXTERNAL_AUDIO_BITS +void audioOutput(const AudioOutput f) // f is a structure potentially containing both channels, scaled according to MOZZI_AUDIO_BITS { - int out = f.l() + AUDIO_BIAS; // get the audio and make it positive + int out = f.l() + MOZZI_AUDIO_BIAS; // get the audio and make it positive int mask = 0b00000001; // mask for outputting only 1 bit (one per pin) for (int i = 0; i < R2R_N_PIN; i++) { diff --git a/examples/13.External_Audio_Output/Sinewave_R2R_DAC_74HC595/Sinewave_R2R_DAC_74HC595.ino b/examples/13.External_Audio_Output/Sinewave_R2R_DAC_74HC595/Sinewave_R2R_DAC_74HC595.ino index b450d253d..9b06cb232 100644 --- a/examples/13.External_Audio_Output/Sinewave_R2R_DAC_74HC595/Sinewave_R2R_DAC_74HC595.ino +++ b/examples/13.External_Audio_Output/Sinewave_R2R_DAC_74HC595/Sinewave_R2R_DAC_74HC595.ino @@ -2,9 +2,6 @@ using Mozzi sonification library and an user-defined audioOutput() function. - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - #define EXTERNAL_AUDIO_BITS 8 should be set in mozzi_config.h, as the 74HC595 has 8 outputs. - Demonstrates the use of audioOutput() using a R/2R DAC connected on a shift register 74HC595. @@ -47,7 +44,12 @@ T. Combriat 2020, CC by-nc-sa. */ -#include +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +#define MOZZI_AUDIO_BITS 8 +#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable + +#include #include // oscillator template #include // sine table for oscillator #include // needed for the shift register @@ -55,19 +57,12 @@ // use: Oscil oscilName (wavetable), look in .h file of table #included above Oscil aSin(SIN2048_DATA); -// use #define for CONTROL_RATE, not a constant -#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable - - - // External output parameters for this example #define LATCH_PIN 31 // Number of stage of the resistance ladder = number of digits of the DAC -//#define AUDIO_BIAS 128 // not needed since PR#98 - -void audioOutput(const AudioOutput f) // f is a structure potentially containing both channels, scaled according to EXTERNAL_AUDIO_BITS +void audioOutput(const AudioOutput f) // f is a structure potentially containing both channels, scaled according to MOZZI_AUDIO_BITS { - int out = f.l() + AUDIO_BIAS; // make the signal positive + int out = f.l() + MOZZI_AUDIO_BIAS; // make the (zero centered) signal positive digitalWrite(LATCH_PIN, LOW); SPI.transfer(out); digitalWrite(LATCH_PIN, HIGH); diff --git a/examples/13.External_Audio_Output/Stereo_Pan_MCP4922_stereo_12bits/Stereo_Pan_MCP4922_stereo_12bits.ino b/examples/13.External_Audio_Output/Stereo_Pan_MCP4922_stereo_12bits/Stereo_Pan_MCP4922_stereo_12bits.ino index 2182c3bfd..61e4e3c9a 100644 --- a/examples/13.External_Audio_Output/Stereo_Pan_MCP4922_stereo_12bits/Stereo_Pan_MCP4922_stereo_12bits.ino +++ b/examples/13.External_Audio_Output/Stereo_Pan_MCP4922_stereo_12bits/Stereo_Pan_MCP4922_stereo_12bits.ino @@ -2,12 +2,6 @@ using Mozzi sonification library and an external dual DAC MCP4922 (original library by Thomas Backman - https://github.com/exscape/electronics/tree/master/Arduino/Libraries/DAC_MCP49xx) using an user-defined audioOutput() function. - - #define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h. - #define AUDIO_CHANNELS STEREO should be set in mozzi_config.h - - - Circuit: (see the DAC library README for details) MCP4921 // Connect to: @@ -32,15 +26,19 @@ T. Combriat 2020, CC by-nc-sa. */ -#include +#include "MozziConfigValues.h" // for named option values +#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED +#define MOZZI_AUDIO_CHANNELS MOZZI_STEREO +#define MOZZI_AUDIO_BITS 12 +#define CONTROL_RATE 256 // Hz, powers of 2 are most reliable + +#include #include #include // table for Oscils to play #include #include // https://github.com/tomcombriat/DAC_MCP49XX // which is an adapted fork from https://github.com/exscape/electronics/tree/master/Arduino/Libraries/DAC_MCP49xx (Thomas Backman) -#define CONTROL_RATE 256 // Hz, powers of 2 are most reliable - // Synthesis part Oscil aCos1(COS2048_DATA); @@ -60,8 +58,8 @@ DAC_MCP49xx dac(DAC_MCP49xx::MCP4922, SS_PIN); void audioOutput(const AudioOutput f) // f is a structure containing both channels { - int out_l = f.l() + AUDIO_BIAS; // the DAC wants positive signals only, so we need to add a bias. - int out_r = f.r() + AUDIO_BIAS; + int out_l = f.l() + MOZZI_AUDIO_BIAS; // the DAC wants positive signals only, so we need to add a bias. + int out_r = f.r() + MOZZI_AUDIO_BIAS; dac.output2(out_l, out_r); // outputs the two channels in one call. diff --git a/extras/NEWS.txt b/extras/NEWS.txt index 9aec0b01b..6cc2ec4b5 100644 --- a/extras/NEWS.txt +++ b/extras/NEWS.txt @@ -3,7 +3,11 @@ Mozzi sound synthesis library for Arduino NEWS get the latest version from https://sensorium.github.io/Mozzi/ -not yet released (latest github version) +NOT YET RELEASED (github version): Mozzi 2.0 +- explicitly specify bit depth for "int" or "long" in several places, for better cross-platform consistency +- restructured configuration options for more consistency across platforms, and easier customization + +release v1.1.2 - new partial port of the Arduino Uno R4 - new partial port of the Arduino Giga by Thomas Friedrichsmeier & Thomas Combriat - new (partial) port to the STM32duino core by Thomas Friedrichsmeier diff --git a/extras/devscripts/mozzi_release_step1.sh b/extras/devscripts/mozzi_release_step1.sh index adf063a09..1e0b6ca9a 100644 --- a/extras/devscripts/mozzi_release_step1.sh +++ b/extras/devscripts/mozzi_release_step1.sh @@ -16,31 +16,9 @@ cp -a ~/Mozzi/examples ~/Documents/Arduino/mozzi_compile/ #cd mozzi_compile/examples cd ~/Documents/Arduino/mozzi_compile/examples/ -# change mozzi config to STANDARD_PLUS, make sure STANDARD is commented -sed -i.bak 's/^#define AUDIO_MODE STANDARD/\/\/#define AUDIO_MODE STANDARD/' ~/Mozzi/mozzi_config.h -# make sure STANDARD_PLUS is uncommented, ^ matches start of line -sed -i.bak 's/\/\/#define AUDIO_MODE STANDARD_PLUS/#define AUDIO_MODE STANDARD_PLUS/' ~/Mozzi/mozzi_config.h - -# make sure STANDARD is commented, ^ matches start of line -#sed -i.bak 's/^#define AUDIO_MODE STANDARD/\/\/#define AUDIO_MODE STANDARD/' ~/Mozzi/mozzi_config.h -# change mozzi config to STANDARD_PLUS, make sure STANDARD_PLUS is uncommented -#sed -i.bak 's/\/\/#define AUDIO_MODE STANDARD_PLUS/#define AUDIO_MODE STANDARD_PLUS/' ~/Mozzi/mozzi_config.h - -# make sure HIFI is commented, ^ matches start of line -sed -i.bak 's/^#define AUDIO_MODE HIFI/\/\/#define AUDIO_MODE HIFI/' ~/Mozzi/mozzi_config.h - - #compile and record STANDARD examples ~/bin/mozzi_compile_examples.sh -#change mozzi config to HIFI -# make sure HIFI is uncommented -sed -i.bak 's/\/\/#define AUDIO_MODE HIFI/#define AUDIO_MODE HIFI/' ~/Mozzi/mozzi_config.h -# make sure STANDARD is commented, ^ matches start of line -sed -i.bak 's/^#define AUDIO_MODE STANDARD/\/\/#define AUDIO_MODE STANDARD/' ~/Mozzi/mozzi_config.h -# make sure STANDARD_PLUS is commented, ^ matches start of line -sed -i.bak 's/^#define AUDIO_MODE STANDARD_PLUS/\/\/#define AUDIO_MODE STANDARD_PLUS/' ~/Mozzi/mozzi_config.h - #compile and record HIFI examples ~/bin/mozzi_compile_examples_hifi.sh diff --git a/extras/devscripts/mozzi_release_step2.sh b/extras/devscripts/mozzi_release_step2.sh index b9196031e..d971587e7 100644 --- a/extras/devscripts/mozzi_release_step2.sh +++ b/extras/devscripts/mozzi_release_step2.sh @@ -4,24 +4,6 @@ NOW=$(date +"%Y-%m-%d-%H:%M") -#ensure STANDARD config again -# uncomment STANDARD -#sed -i.bak 's/\/\/#define AUDIO_MODE STANDARD/#define AUDIO_MODE STANDARD/' ~/Mozzi/mozzi_config.h -# comment HIFI, ^ matches start of line -#sed -i.bak 's/^#define AUDIO_MODE HIFI/\/\/#define AUDIO_MODE HIFI/' ~/Mozzi/mozzi_config.h - -# make sure STANDARD is commented, ^ matches start of line -sed -i.bak 's/^#define AUDIO_MODE STANDARD/\/\/#define AUDIO_MODE STANDARD/' ~/Mozzi/mozzi_config.h -# change mozzi config to STANDARD_PLUS, make sure STANDARD_PLUS is uncommented -sed -i.bak 's/\/\/#define AUDIO_MODE STANDARD_PLUS/#define AUDIO_MODE STANDARD_PLUS/' ~/Mozzi/mozzi_config.h -# make sure HIFI is commented, ^ matches start of line -sed -i.bak 's/^#define AUDIO_MODE HIFI/\/\/#define AUDIO_MODE HIFI/' ~/Mozzi/mozzi_config.h - - -# make sure audio input is off -sed -i.bak 's/#define USE_AUDIO_INPUT true/#define USE_AUDIO_INPUT false/' ~/Mozzi/mozzi_config.h - - #delete wav files find ~/Documents/Arduino/mozzi_compile/examples -name "*.wav" -print -delete diff --git a/hardware_defines.h b/hardware_defines.h index b93fc34e3..1cd708c0d 100644 --- a/hardware_defines.h +++ b/hardware_defines.h @@ -1,11 +1,7 @@ #ifndef HARDWARE_DEFINES_H_ #define HARDWARE_DEFINES_H_ -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif +#include "Arduino.h" /* Macros to tell apart the supported platforms. The advantages of using these are, rather than the underlying defines - Easier to read and write diff --git a/MozziGuts.cpp b/internal/MozziGuts.hpp similarity index 81% rename from MozziGuts.cpp rename to internal/MozziGuts.hpp index 5fe2676ee..7b00c7029 100644 --- a/MozziGuts.cpp +++ b/internal/MozziGuts.hpp @@ -12,11 +12,12 @@ #include #include "CircularBuffer.h" -#include "MozziGuts.h" #include "mozzi_analog.h" -//#include "mozzi_utils.h" +#include "internal/mozzi_rand_p.h" #include "AudioOutput.h" +namespace MozziPrivate { + // Forward declarations of functions to be provided by platform specific implementations #if (!BYPASS_MOZZI_OUTPUT_BUFFER) static void CACHED_FUNCTION_ATTR defaultAudioOutput(); @@ -26,6 +27,7 @@ static void advanceADCStep(); // to be provided by platfor static void startSecondADCReadOnCurrentChannel(); // to be provided by platform implementation static uint8_t adc_count = 0; // needed below #endif +} // Include the appropriate implementation #if IS_AVR() @@ -63,6 +65,7 @@ static uint8_t adc_count = 0; // needed below # endif #endif +namespace MozziPrivate { ////// BEGIN Output buffering ///// #if BYPASS_MOZZI_OUTPUT_BUFFER == true uint64_t samples_written_to_buffer = 0; @@ -266,7 +269,7 @@ unsigned long mozziMicros() { return audioTicks() * MICROS_PER_AUDIO_TICK; } ////// BEGIN initialization /////// void startMozzi(int control_rate_hz) { #if !MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE) - setupMozziADC(); // you can use setupFastAnalogRead() with FASTER_ADC or FASTEST_ADC + MozziPrivate::setupMozziADC(FAST_ADC); // you can use setupFastAnalogRead() with FASTER_ADC or FASTEST_ADC // in setup() if desired (not for Teensy 3.* ) #endif // delay(200); // so AutoRange doesn't read 0 to start with @@ -274,7 +277,12 @@ void startMozzi(int control_rate_hz) { startAudio(); } +uint32_t MozziRandPrivate::x=132456789; +uint32_t MozziRandPrivate::y=362436069; +uint32_t MozziRandPrivate::z=521288629; + ////// END initialization /////// +} // reduce Macro leakage #undef LOOP_YIELD @@ -282,3 +290,30 @@ void startMozzi(int control_rate_hz) { #undef AUDIO_HOOK_HOOK #undef AUDIOTICK_ADJUSTMENT #undef MOZZI__LEGACY_AUDIO_INPUT_IMPL + +// "export" publicly accessible functions defined in this file +// NOTE: unfortunately, we cannot just write using MozziPrivate::mozziMicros(), and that will conflict with, rather than define mozziMicros() +// we might want to rethink how this is done. What matters is that these functions are user accessible, though, while most of what we +// now keep in MozziPrivate is hidden away. +//unsigned long mozziMicros() { return MozziPrivate::mozziMicros(); }; +unsigned long audioTicks() { return MozziPrivate::audioTicks(); }; +void startMozzi(int control_rate_hz) { MozziPrivate::startMozzi(control_rate_hz); }; +void stopMozzi() { MozziPrivate::stopMozzi(); }; +int mozziAnalogRead(uint8_t pin) { return MozziPrivate::mozziAnalogRead(pin); }; +#if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) +AudioOutputStorage_t getAudioInput() { return MozziPrivate::getAudioInput(); }; +#endif +#if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) +void setupMozziADC(int8_t speed) { MozziPrivate::setupMozziADC(speed); }; +void setupFastAnalogRead(int8_t speed) { MozziPrivate::setupFastAnalogRead(speed); }; +uint8_t adcPinToChannelNum(uint8_t pin) { return MozziPrivate::adcPinToChannelNum(pin); }; +#endif +void audioHook() { MozziPrivate::audioHook(); }; + +// This is not strictly needed, but we want it to throw an error, if users have audioOutput() in their sketch without external output configured +#if !MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM) +MOZZI_DEPRECATED("n/a", "Sketch has audioOutput() function, although external output is not configured.") void audioOutput(const AudioOutput) {}; +#endif +#if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_CUSTOM) +MOZZI_DEPRECATED("n/a", "Sketch has canBufferAudioOutput() function, although custom external output is not configured.") bool canBufferAudioOutput() {}; +#endif diff --git a/MozziGuts_impl_AVR.hpp b/internal/MozziGuts_impl_AVR.hpp similarity index 84% rename from MozziGuts_impl_AVR.hpp rename to internal/MozziGuts_impl_AVR.hpp index e877c87b0..8b0c71ea2 100644 --- a/MozziGuts_impl_AVR.hpp +++ b/internal/MozziGuts_impl_AVR.hpp @@ -10,8 +10,8 @@ * */ -#include "FrequencyTimer2.h" -#include "TimerOne.h" +#include "utility/FrequencyTimer2.h" +#include "utility/TimerOne.h" #if (F_CPU != 16000000) #warning \ @@ -22,6 +22,12 @@ #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) extern uint8_t analog_reference; +ISR(ADC_vect, ISR_BLOCK) +{ + MozziPrivate::advanceADCStep(); +} + +namespace MozziPrivate { #define getADCReading() ADC /* officially (ADCL | (ADCH << 8)) but the compiler works it out */ #define channelNumToIndex(channel) channel uint8_t adcPinToChannelNum(uint8_t pin) { @@ -29,7 +35,15 @@ uint8_t adcPinToChannelNum(uint8_t pin) { if (pin >= 54) pin -= 54; // allow for channel or pin numbers #elif defined(__AVR_ATmega32U4__) if (pin >= 18) pin -= 18; // allow for channel or pin numbers - pin = analogPinToChannel(pin); // moved from extra #if which was below in Arduino code, and redefined in mozzi_analog.h, with notes +# if defined(CORE_TEENSY) // special handling for Teensy2, which does not (did not?) have an analogPinToChannel() define (see https://github.com/sensorium/Mozzi/issues/10) + static const uint8_t PROGMEM adc_mapping[] = { + // 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8 + 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8, 10, 11, 12, 13, 7, 6, 5, 4, 1, 0, 8 + }; + pin = pgm_read_byte(adc_mapping + (P)); +# else + pin = analogPinToChannel(pin); +# endif #elif defined(__AVR_ATmega1284__) if (pin >= 24) pin -= 24; // allow for channel or pin numbers #else @@ -74,11 +88,6 @@ void adcEnableInterrupt(){ } */ -ISR(ADC_vect, ISR_BLOCK) -{ - advanceADCStep(); -} - void setupMozziADC(int8_t speed) { ADCSRA |= (1 << ADIE); // adc Enable Interrupt adcDisconnectAllDigitalIns(); @@ -100,13 +109,14 @@ void setupFastAnalogRead(int8_t speed) { ADCSRA &= ~(1 << ADPS0); } } +} #endif ////// END analog input code //////// - +namespace MozziPrivate { //// BEGIN AUDIO OUTPUT code /////// /* ATmega328 technical manual, Section 12.7.4: @@ -181,9 +191,13 @@ static void startAudio() { // Timer1.attachInterrupt()) } +} // namespace MozziPrivate + ISR(TIMER1_OVF_vect, ISR_BLOCK) { - defaultAudioOutput(); + MozziPrivate::defaultAudioOutput(); } + +namespace MozziPrivate { #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM) inline void audioOutput(const AudioOutput f) { @@ -221,9 +235,10 @@ static void startAudio() { // Timer1.attachInterrupt()) } +} // namespace MozziPrivate + /* Interrupt service routine moves sound data from the output buffer to the Arduino output register, running at AUDIO_RATE. */ - ISR(TIMER1_OVF_vect, ISR_BLOCK) { # if (MOZZI_AUDIO_RATE < MOZZI_PWM_RATE) // only update every second ISR, if lower audio rate static_assert(2l*MOZZI_AUDIO_RATE == MOZZI_PWM_RATE, "audio rate must the same, or exactly half of the pwm rate!"); @@ -232,9 +247,10 @@ ISR(TIMER1_OVF_vect, ISR_BLOCK) { if (alternate) return; # endif - defaultAudioOutput(); + MozziPrivate::defaultAudioOutput(); } +namespace MozziPrivate { #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM) inline void audioOutput(const AudioOutput f) { // read about dual pwm at @@ -314,6 +330,8 @@ static void setupTimer2() { FrequencyTimer2::enable(); } +} // namespace MozziPrivate + #if defined(TIMER2_COMPA_vect) ISR(TIMER2_COMPA_vect) #elif defined(TIMER2_COMP_vect) @@ -326,11 +344,13 @@ ISR(TIMER4_COMPA_vect) void dummy_function(void) #endif { - defaultAudioOutput(); + MozziPrivate::defaultAudioOutput(); } // end of HIFI +namespace MozziPrivate { + #endif //----------------------------------------------------------------------------------------------------------------- @@ -377,3 +397,50 @@ void stopMozzi() { // Timer1.isrCallback(); // } //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +#if defined (__AVR_ATmega644P__) + +// a less fancy version for gizduino (__AVR_ATmega644P__) which doesn't know INTERNAL +static long longRandom() +{ + return ((long)analogRead(0)+63)*(analogRead(1)+97); // added offsets in case analogRead is 0 +} + +#else + +/* +longRandom(), used as a seed generator, comes from: +http://arduino.cc/forum/index.php/topic,38091.0.html +// AUTHOR: Rob Tillaart +// PURPOSE: Simple Random functions based upon unreliable internal temp sensor +// VERSION: 0.1 +// DATE: 2011-05-01 +// +// Released to the public domain, use at own risk +// +*/ +static long longRandom() +{ + //analogReference(INTERNAL); + unsigned long rv = 0; + for (uint8_t i=0; i< 32; i++) rv |= ((analogRead(8)+1171) & 1L) << i; // added 1171 in case analogRead is 0 + return rv; +} +#endif + +void MozziRandPrivate::autoSeed() { + ADCSRA &= ~ (1 << ADIE); // adc Disable Interrupt, re-enable at end + // this attempt at remembering analog_reference stops it working + // maybe needs a delay after changing analog reference in longRandom (Arduino reference suggests this) + // because the analog reads return 0 + //uint8_t analog_reference_orig = ADMUX&192; // analog_reference is high 2 bits of ADMUX, store this because longRandom sets it to internal + x = longRandom(); + y = longRandom(); + z = longRandom(); + //analogReference(analog_reference_orig); // change back to original + ADCSRA |= (1 << ADIE); // adc re-Enable Interrupt +} + +//// END Random seeding //////// +} diff --git a/MozziGuts_impl_ESP32.hpp b/internal/MozziGuts_impl_ESP32.hpp similarity index 95% rename from MozziGuts_impl_ESP32.hpp rename to internal/MozziGuts_impl_ESP32.hpp index b20c92772..32d8535c2 100644 --- a/MozziGuts_impl_ESP32.hpp +++ b/internal/MozziGuts_impl_ESP32.hpp @@ -14,6 +14,7 @@ # error "Wrong implementation included for this platform" #endif +namespace MozziPrivate { ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) #error not yet implemented @@ -36,11 +37,11 @@ void setupFastAnalogRead(int8_t speed) { ////// END analog input code //////// - - //// BEGIN AUDIO OUTPUT code /////// #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC) || MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC) || MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PDM_VIA_I2S) +} // namespace MozziPrivate # include // for I2S-based output modes, including - technically - internal DAC +namespace MozziPrivate { const i2s_port_t i2s_num = MOZZI_I2S_PORT; // On ESP32 we cannot test wether the DMA buffer has room. Instead, we have to use a one-sample mini buffer. In each iteration we @@ -93,7 +94,11 @@ inline void audioOutput(const AudioOutput f) { #endif #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED) + +} // namespace MozziPrivate # include +namespace MozziPrivate { + void CACHED_FUNCTION_ATTR timer0_audio_output_isr(void *) { TIMERG0.int_clr_timers.t0 = 1; TIMERG0.hw_timer[0].config.alarm_en = 1; @@ -161,4 +166,14 @@ void stopMozzi() { } //// END AUDIO OUTPUT code /////// +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { + x = esp_random(); + y = esp_random(); + z = esp_random(); +} +//// END Random seeding //////// + #undef ESP_SAMPLE_SIZE // only used inside this file + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_ESP8266.hpp b/internal/MozziGuts_impl_ESP8266.hpp similarity index 87% rename from MozziGuts_impl_ESP8266.hpp rename to internal/MozziGuts_impl_ESP8266.hpp index 3d4f3c11d..da95b723f 100644 --- a/MozziGuts_impl_ESP8266.hpp +++ b/internal/MozziGuts_impl_ESP8266.hpp @@ -14,6 +14,8 @@ # error "Wrong implementation included for this platform" #endif +namespace MozziPrivate { + ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) #error not yet implemented @@ -34,19 +36,21 @@ void setupFastAnalogRead(int8_t speed) { #endif ////// END analog input code //////// - - //// BEGIN AUDIO OUTPUT code /////// #define LOOP_YIELD yield(); +} // namespace MozziPrivate #include #include +namespace MozziPrivate { uint16_t output_buffer_size = 0; #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PDM_VIA_I2S, MOZZI_OUTPUT_PDM_VIA_SERIAL, MOZZI_OUTPUT_I2S_DAC) // i.e. not external # if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PDM_VIA_I2S) +} // namespace MozziPrivate # include +namespace MozziPrivate { inline bool canBufferAudioOutput() { return (i2s_available() >= MOZZI_PDM_RESOLUTION); } @@ -56,7 +60,9 @@ inline void audioOutput(const AudioOutput f) { } } # elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC) +} // namespace MozziPrivate # include +namespace MozziPrivate { inline bool canBufferAudioOutput() { return (i2s_available() >= MOZZI_PDM_RESOLUTION); } @@ -136,3 +142,17 @@ void stopMozzi() { #endif //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +} //namespace MozziPrivate +#include +namespace MozziPrivate { +void MozziRandPrivate::autoSeed() { + x = RANDOM_REG32; + // TODO: The XORs may not be needed, but for lack of documentation (that I could find), let's assume RANDOM_REG32 + // itself might not get updated on every read. NOTE: x, y, and z are initialized to non-zero, before this. + y = y ^ RANDOM_REG32; + z = z ^ RANDOM_REG32; +} +//// END Random seeding //////// +} // namespace MozziPrivate diff --git a/MozziGuts_impl_MBED.hpp b/internal/MozziGuts_impl_MBED.hpp similarity index 94% rename from MozziGuts_impl_MBED.hpp rename to internal/MozziGuts_impl_MBED.hpp index 5f612cc14..6f7d064e3 100644 --- a/MozziGuts_impl_MBED.hpp +++ b/internal/MozziGuts_impl_MBED.hpp @@ -16,12 +16,16 @@ #define CHUNKSIZE 64 +namespace MozziPrivate { + ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_STANDARD) #define MOZZI__LEGACY_AUDIO_INPUT_IMPL 0 +} // namespace MozziPrivate #include +namespace MozziPrivate { AdvancedADC adc(MOZZI_AUDIO_INPUT_PIN); Sample inbuf[CHUNKSIZE]; @@ -107,7 +111,9 @@ void setupMozziADC(int8_t speed) { #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED) #define US_PER_AUDIO_TICK (1000000L / MOZZI_AUDIO_RATE) +} // namespace MozziPrivate #include +namespace MozziPrivate { mbed::Ticker audio_output_timer; volatile bool audio_output_requested = false; @@ -128,7 +134,9 @@ void stopMozzi() { #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC) +} // namespace MozziPrivate #include +namespace MozziPrivate { AdvancedDAC dac1(MOZZI_AUDIO_PIN_1); Sample buf1[CHUNKSIZE]; @@ -192,7 +200,9 @@ void stopMozzi() { #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PDM_VIA_SERIAL) +} // namespace MozziPrivate #include +namespace MozziPrivate { mbed::BufferedSerial serial_out1(digitalPinToPinName(MOZZI_SERIAL_PIN_TX), digitalPinToPinName(MOZZI_SERIAL_PIN_RX)); uint8_t buf[MOZZI_PDM_RESOLUTION*4]; @@ -221,5 +231,13 @@ void stopMozzi() { #endif ////// END audio output code ////// +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate + #undef CHUNKSIZE #undef US_PER_AUDIO_TICK diff --git a/MozziGuts_impl_RENESAS.hpp b/internal/MozziGuts_impl_RENESAS.hpp similarity index 94% rename from MozziGuts_impl_RENESAS.hpp rename to internal/MozziGuts_impl_RENESAS.hpp index ddc9edeaf..4ad924c50 100644 --- a/MozziGuts_impl_RENESAS.hpp +++ b/internal/MozziGuts_impl_RENESAS.hpp @@ -16,6 +16,7 @@ #include +namespace MozziPrivate { ////// BEGIN analog input code //////// @@ -31,8 +32,12 @@ void adc_callback(adc_callback_args_t *p_args) { advanceADCStep(); } +} // namespace MozziPrivate + #include "MozziGuts_impl_RENESAS_ADC.hpp" +namespace MozziPrivate { + #define getADCReading() readADC(r4_pin) uint8_t adcPinToChannelNum(uint8_t pin) { @@ -83,7 +88,9 @@ FspTimer timer; #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC) CircularBuffer output_buffer; +} // namespace MozziPrivate #include "MozziGuts_impl_RENESAS_analog.hpp" +namespace MozziPrivate { #endif @@ -168,3 +175,11 @@ void stopMozzi() { #endif } //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_RENESAS_ADC.hpp b/internal/MozziGuts_impl_RENESAS_ADC.hpp similarity index 98% rename from MozziGuts_impl_RENESAS_ADC.hpp rename to internal/MozziGuts_impl_RENESAS_ADC.hpp index 079486e67..9b7afa97e 100644 --- a/MozziGuts_impl_RENESAS_ADC.hpp +++ b/internal/MozziGuts_impl_RENESAS_ADC.hpp @@ -8,6 +8,7 @@ It contains functions to interact with the ADC in order to implement async ADC r //#include #include +namespace MozziPrivate { /** VERBATIM from Arduino's analog.cpp */ @@ -119,3 +120,5 @@ uint16_t readADC(int pin) R_ADC_Read(&(_adc->ctrl), (adc_channel_t)GET_CHANNEL(cfg_adc), &result); return result; } + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_RENESAS_analog.hpp b/internal/MozziGuts_impl_RENESAS_analog.hpp similarity index 98% rename from MozziGuts_impl_RENESAS_analog.hpp rename to internal/MozziGuts_impl_RENESAS_analog.hpp index 2496cbfe8..80caffdd6 100644 --- a/MozziGuts_impl_RENESAS_analog.hpp +++ b/internal/MozziGuts_impl_RENESAS_analog.hpp @@ -4,11 +4,13 @@ for Renesas board from Arduino. It contains functions to create and start the on-board DAC (and associate timer). */ -FspTimer timer_dac; #include #include #include +namespace MozziPrivate { + +FspTimer timer_dac; volatile uint32_t pin; uint8_t dac_bits; @@ -86,3 +88,4 @@ void dac_init() { } } +} diff --git a/MozziGuts_impl_RP2040.hpp b/internal/MozziGuts_impl_RP2040.hpp similarity index 95% rename from MozziGuts_impl_RP2040.hpp rename to internal/MozziGuts_impl_RP2040.hpp index 1b2b87623..94a9d52e4 100644 --- a/MozziGuts_impl_RP2040.hpp +++ b/internal/MozziGuts_impl_RP2040.hpp @@ -17,6 +17,8 @@ #include +namespace MozziPrivate { + ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) @@ -28,8 +30,12 @@ * We'll abuse that to connect a callback to the DMA channel, instead. */ +} // namespace MozziPrivate + #include +namespace MozziPrivate { + #define getADCReading() rp2040_adc_result #define channelNumToIndex(channel) channel @@ -69,7 +75,7 @@ void rp2040_adc_queue_handler(); static uint16_t rp2040_adc_result = 0; int rp2040_adc_dma_chan; void setupMozziADC(int8_t speed) { - for (int i = 0; i < NUM_ANALOG_INPUTS; ++i) { + for (int i = 0; i < (int) NUM_ANALOG_INPUTS; ++i) { adc_gpio_init(i); } @@ -116,6 +122,7 @@ void rp2040_adc_queue_handler() { dma_channel_set_trans_count(rp2040_adc_dma_chan, 1, true); // set up for another read advanceADCStep(); } + #endif // MOZZI_ANALOG_READ ////// END analog input code //////// @@ -137,7 +144,9 @@ inline void audioOutput(const AudioOutput f) { } # endif // MOZZI_OUTPUT_PWM +} // namespace MozziPrivate #include +namespace MozziPrivate { /** Implementation notes: * - For the time being this port uses a very crude approach to audio output: PWM updated by a hardware timer running at AUDIO_RATE * - Hardware timer isn't fixed, but rather we claim the first unclaimed one @@ -159,7 +168,9 @@ void audioOutputCallback(uint) { } #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC) +} // namespace MozziPrivate #include +namespace MozziPrivate { I2S i2s(OUTPUT); inline bool canBufferAudioOutput() { @@ -266,5 +277,13 @@ void stopMozzi() { } ////// END audio output code ////// +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate + #undef MOZZI_RP2040_BUFFERS #undef MOZZI_RP2040_BUFFER_SIZE diff --git a/MozziGuts_impl_SAMD.hpp b/internal/MozziGuts_impl_SAMD.hpp similarity index 91% rename from MozziGuts_impl_SAMD.hpp rename to internal/MozziGuts_impl_SAMD.hpp index b885a28fe..37bfdb658 100644 --- a/MozziGuts_impl_SAMD.hpp +++ b/internal/MozziGuts_impl_SAMD.hpp @@ -14,6 +14,8 @@ # error "Wrong implementation included for this platform" #endif +namespace MozziPrivate { + ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, NOZZI_ANALOG_READ_STANDARD) #error not yet implemented @@ -93,18 +95,23 @@ static void tcConfigure(uint32_t sampleRate) { ; } +} // namespace MozziPrivate + void TC5_Handler(void) __attribute__((weak, alias("samd21AudioOutput"))); #ifdef __cplusplus extern "C" { #endif void samd21AudioOutput() { - defaultAudioOutput(); + MozziPrivate::defaultAudioOutput(); TC5->COUNT16.INTFLAG.bit.MC0 = 1; } #ifdef __cplusplus } #endif + +namespace MozziPrivate { + #endif // MOZZI_AUDIO_MODE #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_INTERNAL_DAC) @@ -138,3 +145,11 @@ void stopMozzi() { interrupts(); } //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_STM32.hpp b/internal/MozziGuts_impl_STM32.hpp similarity index 87% rename from MozziGuts_impl_STM32.hpp rename to internal/MozziGuts_impl_STM32.hpp index 3c5969ba0..2542016f8 100644 --- a/MozziGuts_impl_STM32.hpp +++ b/internal/MozziGuts_impl_STM32.hpp @@ -12,10 +12,15 @@ #include "HardwareTimer.h" +namespace MozziPrivate { + ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) +} // namespace MozziPrivate //#include // Disabled, here. See hardware_defines.h +namespace MozziPrivate { + STM32ADC adc(ADC1); uint8_t stm32_current_adc_pin; // TODO: this is actually a "channel" according to our terminology, but "channel" and "pin" are equal on this platform #define getADCReading() adc.getData() @@ -138,3 +143,26 @@ void stopMozzi() { } //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { + // Unfortunately the internal temp sensor on STM32s does _not_ appear to create a lot of noise. + // Ironically, the calls to calibrate help induce some random noise. You're still fairly likely to produce two equal + // random seeds in two subsequent runs, however. + adc.enableInternalReading(); + union { + float cf; + uint32_t ci; + } conv; + conv.cf = adc.readTemp(); + x=x^conv.ci; + adc.calibrate(); + conv.cf = adc.readTemp(); + y=y^conv.ci; + adc.calibrate(); + conv.cf = adc.readTemp(); + z=z^conv.ci; +} +//// END Random seeding //////// + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_STM32duino.hpp b/internal/MozziGuts_impl_STM32duino.hpp similarity index 96% rename from MozziGuts_impl_STM32duino.hpp rename to internal/MozziGuts_impl_STM32duino.hpp index b24f5a749..90452ae21 100644 --- a/MozziGuts_impl_STM32duino.hpp +++ b/internal/MozziGuts_impl_STM32duino.hpp @@ -12,6 +12,7 @@ #include "HardwareTimer.h" +namespace MozziPrivate { ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) @@ -36,7 +37,9 @@ int16_t previously_sampled_pin = -1; bool conversion_running = false; ADC_HandleTypeDef AdcHandle = {}; +} // namespace MozziPrivate #include "MozziGuts_impl_STM32duino_analog.hpp" +namespace MozziPrivate { void adcStartConversion(int8_t pin) { if (pin != previously_sampled_pin) { @@ -171,3 +174,11 @@ void stopMozzi() { } //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_STM32duino_analog.hpp b/internal/MozziGuts_impl_STM32duino_analog.hpp similarity index 99% rename from MozziGuts_impl_STM32duino_analog.hpp rename to internal/MozziGuts_impl_STM32duino_analog.hpp index c1a852b2a..f6c986582 100644 --- a/MozziGuts_impl_STM32duino_analog.hpp +++ b/internal/MozziGuts_impl_STM32duino_analog.hpp @@ -1,3 +1,4 @@ +namespace MozziPrivate { // NOTE: The contents of this file are copied mostly verbatim from Arduino_Core_STM32, (c) STMicroelectronics, BSD 3-clause license. static PinName g_current_pin = NC; @@ -310,3 +311,5 @@ bool adc_setup_read(PinName pin, uint32_t resolution) { return HAL_OK; } + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_TEENSY.hpp b/internal/MozziGuts_impl_TEENSY.hpp similarity index 84% rename from MozziGuts_impl_TEENSY.hpp rename to internal/MozziGuts_impl_TEENSY.hpp index 04c18ac73..ddb8daf25 100644 --- a/MozziGuts_impl_TEENSY.hpp +++ b/internal/MozziGuts_impl_TEENSY.hpp @@ -19,10 +19,13 @@ "Mozzi has been tested with a cpu clock speed of 16MHz on Arduino and 48MHz on Teensy 3! Results may vary with other speeds." #endif +namespace MozziPrivate { ////// BEGIN analog input code //////// #if MOZZI_IS(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_STANDARD) // required from http://github.com/pedvide/ADC for Teensy 3.* +} //namespace MozziPrivate #include +namespace MozziPrivate { ADC *adc; // adc object uint8_t teensy_pin; // TODO: this is actually a "channel" according to our terminology, but "channel" and "pin" are equal on this platform @@ -42,14 +45,16 @@ void setupFastAnalogRead(int8_t speed) { #endif } +} // namespace MozziPrivate void adc0_isr(void) { - advanceADCStep(); + MozziPrivate::advanceADCStep(); } +namespace MozziPrivate { void setupMozziADC(int8_t speed) { adc = new ADC(); - adc->adc0->enableInterrupts(adc0_isr); + 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? #ifdef ADC_DUAL_ADCS adc->adc1->enableInterrupts(adc0_isr); #endif @@ -75,7 +80,9 @@ static void startSecondADCReadOnCurrentChannel() { //// BEGIN AUDIO OUTPUT code /////// #if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_INTERNAL_DAC, MOZZI_OUTPUT_EXTERNAL_TIMED) +} // namespace MozziPrivate #include "IntervalTimer.h" +namespace MozziPrivate { IntervalTimer timer1; #endif @@ -112,3 +119,11 @@ void stopMozzi() { interrupts(); } //// END AUDIO OUTPUT code /////// + +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate diff --git a/MozziGuts_impl_template.hpp b/internal/MozziGuts_impl_template.hpp similarity index 82% rename from MozziGuts_impl_template.hpp rename to internal/MozziGuts_impl_template.hpp index 154c1ee29..b997e3cd3 100644 --- a/MozziGuts_impl_template.hpp +++ b/internal/MozziGuts_impl_template.hpp @@ -37,6 +37,17 @@ #endif // Add platform specific includes and declarations, here +//#include + +// In order to allow simple yet efficient user configuration, the entire contents of this file are compiled in the same translation unit +// as (the main file of) the user sketch. To avoid name clashes, we encapsulate everyghing in a namespace. +// 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 +// worry about it. However, there may be a few situations, where you have to "leave" the MozziPrivate namespace. This includes: +// - When you include a further (hardware-dependent library). Consider gathering all includes at the top of this file, instead. +// - When you provide definitions for special names, importantly for ISR-functions. If these would be placed in the namespace, the linker would not +// recognize them as the definition of the intended ISR-vector. See MozziGuts_impl_AVR.hpp for examples. + +namespace MozziPrivate { ////// BEGIN analog input code //////// @@ -156,3 +167,17 @@ void stopMozzi() { // Add here code to pause whatever mechanism moves audio samples to the output } ////// END audio output code ////// + +//// BEGIN Random seeding //////// +void MozziRandPrivate::autoSeed() { + // Add here code to initialize the values of MozziRandPrivate::x, MozziRandPrivate::y, and MozziRandPrivate::z to some random values + // This doesn't need to be crypographically safe. If nothing better is available, e.g. try reading an internal temperature sensor + // in order to get some noise. It also doesn't have to be fast. + // It *should* however ensure that rand() sequences will differ across reboots, after randSeed() has been called. + // x, y, and z are already initialized to non-zero, when this function is called. + // It's ok to leave this unimplemented, initially. +#warning Automatic random seeding is not implemented on this platform +} +//// END Random seeding //////// + +} // namespace MozziPrivate diff --git a/internal/config_checks_avr.h b/internal/config_checks_avr.h index 18454f219..424eb96fa 100644 --- a/internal/config_checks_avr.h +++ b/internal/config_checks_avr.h @@ -156,9 +156,11 @@ MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_RATE, 16384, 32768) MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE, MOZZI_ANALOG_READ_STANDARD) MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE, MOZZI_AUDIO_INPUT_STANDARD) #include "../config/known_16bit_timers.h" -#if defined(TIMER1_C_PIN) + +#if MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_2PIN_PWM) +# if defined(TIMER1_C_PIN) MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_PIN_1, TIMER1_A_PIN, TIMER1_B_PIN, TIMER1_C_PIN); -#else +# else MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_PIN_1, TIMER1_A_PIN, TIMER1_B_PIN); +# endif #endif - diff --git a/internal/config_checks_esp32.h b/internal/config_checks_esp32.h index 1d462385f..e7024ce1a 100644 --- a/internal/config_checks_esp32.h +++ b/internal/config_checks_esp32.h @@ -70,6 +70,7 @@ #error This header should be included for ESP32 architecture, only #endif +#include "disable_2pinmode_on_github_workflow.h" #if !defined(MOZZI_AUDIO_MODE) #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_INTERNAL_DAC #endif diff --git a/internal/config_checks_esp8266.h b/internal/config_checks_esp8266.h index b4ed95087..3e6ab227b 100644 --- a/internal/config_checks_esp8266.h +++ b/internal/config_checks_esp8266.h @@ -62,6 +62,7 @@ #error This header should be included for ESP architecture, only #endif +#include "disable_2pinmode_on_github_workflow.h" #if !defined(MOZZI_AUDIO_MODE) #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_PDM_VIA_SERIAL #endif @@ -86,6 +87,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) # if !defined(PDM_RESOLUTION) # define MOZZI_PDM_RESOLUTION 2 # endif +# include "disable_stereo_on_github_workflow.h" MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, 1) MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_BITS, 16) #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_I2S_DAC) diff --git a/internal/config_checks_generic.h b/internal/config_checks_generic.h index df29fe621..380b596ff 100644 --- a/internal/config_checks_generic.h +++ b/internal/config_checks_generic.h @@ -44,6 +44,7 @@ #endif #if !MOZZI_IS(MOZZI_AUDIO_INPUT, MOZZI_AUDIO_INPUT_NONE) && !defined(MOZZI_AUDIO_INPUT_PIN) +#warning Using audio input, but no audio input pin defined, explicitly. Defaulting to pin 0. #define MOZZI_AUDIO_INPUT_PIN 0 #endif diff --git a/internal/config_checks_mbed.h b/internal/config_checks_mbed.h index e92b33414..ea2e84dfc 100644 --- a/internal/config_checks_mbed.h +++ b/internal/config_checks_mbed.h @@ -56,6 +56,7 @@ #error This header should be included for MBED architecture, only #endif +#include "disable_2pinmode_on_github_workflow.h" #if !defined(MOZZI_AUDIO_MODE) #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_INTERNAL_DAC #endif diff --git a/internal/config_checks_renesas.h b/internal/config_checks_renesas.h index 53e33a40c..a006c5be5 100644 --- a/internal/config_checks_renesas.h +++ b/internal/config_checks_renesas.h @@ -35,6 +35,7 @@ #error This header should be included for RENESAS (Arduino Uno R4) architecture, only #endif +#include "disable_2pinmode_on_github_workflow.h" #if !defined(MOZZI_AUDIO_MODE) #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_INTERNAL_DAC #endif @@ -62,6 +63,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE, MOZZI_ANALOG_RE # define MOZZI_AUDIO_PIN_1 A0 # endif # define BYPASS_MOZZI_OUTPUT_BUFFER true +# include "disable_stereo_on_github_workflow.h" MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, 1) #endif diff --git a/internal/config_checks_rp2040.h b/internal/config_checks_rp2040.h index 467f9948b..270e1af49 100644 --- a/internal/config_checks_rp2040.h +++ b/internal/config_checks_rp2040.h @@ -62,6 +62,7 @@ #error This header should be included for RP2040 architecture (Raspberry Pi Pico and others), only #endif +#include "disable_2pinmode_on_github_workflow.h" #if !defined(MOZZI_AUDIO_MODE) # define MOZZI_AUDIO_MODE MOZZI_OUTPUT_PWM #endif diff --git a/internal/config_checks_samd21.h b/internal/config_checks_samd21.h index dc340fb2d..1176fb75a 100644 --- a/internal/config_checks_samd21.h +++ b/internal/config_checks_samd21.h @@ -35,6 +35,7 @@ #error This header should be included for SAMD21 architecture (Arduino Circuitplayground M0 and others), only #endif +#include "disable_2pinmode_on_github_workflow.h" #if !defined(MOZZI_AUDIO_MODE) #define MOZZI_AUDIO_MODE MOZZI_OUTPUT_INTERNAL_DAC #endif @@ -61,6 +62,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE) # if !defined(MOZZI_AUDIO_PIN_1) # define MOZZI_AUDIO_PIN_1 DAC0 # endif +# include "disable_stereo_on_github_workflow.h" MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, 1) #endif diff --git a/internal/config_checks_stm32duino.h b/internal/config_checks_stm32duino.h index 3c3f1d419..2efbbdd5d 100644 --- a/internal/config_checks_stm32duino.h +++ b/internal/config_checks_stm32duino.h @@ -117,11 +117,12 @@ MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPU # if !defined(MOZZI_AUDIO_PIN_1_LOW) # define MOZZI_AUDIO_PIN_1_LOW PA9 # endif +# include "disable_stereo_on_github_workflow.h" MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, 1) # if !defined(MOZZI_AUDIO_PER_CHANNEL) -# define MOZZI_AUDIO_PER_CHANNEL 7 +# define MOZZI_AUDIO_BITS_PER_CHANNEL 7 # endif -# define MOZZI_AUDIO_BITS MOZZI_AUDIO_BITS_PER_CHANNEL * 2 +# define MOZZI_AUDIO_BITS (MOZZI_AUDIO_BITS_PER_CHANNEL * 2) #endif #if !defined(MOZZI_ANALOG_READ) diff --git a/internal/config_checks_stm32maple.h b/internal/config_checks_stm32maple.h index 27b08736f..507f93b31 100644 --- a/internal/config_checks_stm32maple.h +++ b/internal/config_checks_stm32maple.h @@ -108,6 +108,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPU # if !defined(MOZZI_AUDIO_PIN_1_LOW) # define MOZZI_AUDIO_PIN_1_LOW PB9 # endif +# include "disable_stereo_on_github_workflow.h" MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, 1) # if !defined(MOZZI_AUDIO_PER_CHANNEL) # define MOZZI_AUDIO_PER_CHANNEL 7 diff --git a/internal/config_checks_teensy.h b/internal/config_checks_teensy.h index 6f2e642a9..875addc18 100644 --- a/internal/config_checks_teensy.h +++ b/internal/config_checks_teensy.h @@ -84,11 +84,13 @@ #if IS_TEENSY3() +# include "disable_2pinmode_on_github_workflow.h" # if !defined(MOZZI_AUDIO_MODE) # define MOZZI_AUDIO_MODE MOZZI_OUTPUT_INTERNAL_DAC # endif MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_EXTERNAL_TIMED, MOZZI_OUTPUT_EXTERNAL_CUSTOM, MOZZI_OUTPUT_INTERNAL_DAC) #elif IS_TEENSY4() +# include "disable_2pinmode_on_github_workflow.h" # if !defined(MOZZI_AUDIO_MODE) # define MOZZI_AUDIO_MODE MOZZI_OUTPUT_PWM # endif @@ -124,6 +126,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_ANALOG_READ, MOZZI_ANALOG_READ_NONE, MOZZI_ANALOG_RE # error DAC pin not know for this board. Please define MOZZI_AUDIO_PIN_1 as appropriate # endif # endif +# include "disable_stereo_on_github_workflow.h" MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, 1) #elif MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM) # define MOZZI_AUDIO_BITS 10 // not configurable diff --git a/internal/config_checks_template.h b/internal/config_checks_template.h index f957545f0..8227934a7 100644 --- a/internal/config_checks_template.h +++ b/internal/config_checks_template.h @@ -9,6 +9,8 @@ * 3) Document some details of your port */ +/** NOTE: If your port doesn't support MOZZI_OUTPUT_2PIN_PWM, add this include to make compilation of HIFI examples pass on the github runner */ +#include "disable_2pinmode_on_github_workflow.h" /** NOTE: All ports need to provide a default for this */ #if not defined(MOZZI_AUDIO_MODE) # define MOZZI_AUDIO_MODE MOZZI_OUTPUT_PWM @@ -39,6 +41,7 @@ MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_PWM, MOZZI_OUTPUT_EXTERNAL_ # define MOZZI_AUDIO_BITS 10 # endif /** NOTE: If only mono is supported in this output mode: */ +# include "disable_stereo_on_github_workflow.h" // This allows stereo sketches to compile (in mono) in automated testing builds. MOZZI_CHECK_SUPPORTED(MOZZI_AUDIO_CHANNELS, MOZZI_MONO) #endif diff --git a/internal/disable_2pinmode_on_github_workflow.h b/internal/disable_2pinmode_on_github_workflow.h new file mode 100644 index 000000000..68bd88f44 --- /dev/null +++ b/internal/disable_2pinmode_on_github_workflow.h @@ -0,0 +1,11 @@ +/* This purely internal header takes care of disabling stereo mode when on a github runner. Intended to be used + * on platforms that don't support stereo, allowing the compilation to proceed without error. */ + +#if __has_include("../detect_github_runner.h") // the marker file we added inside the workflow + // do nothing, if this isn't present +# if defined(MOZZI_AUDIO_MODE) && MOZZI_IS(MOZZI_AUDIO_MODE, MOZZI_OUTPUT_2PIN_PWM) +# undef MOZZI_AUDIO_MODE +# warning Disabled 2pin pwm output mode on github runner +# endif + +#endif diff --git a/internal/disable_stereo_on_github_workflow.h b/internal/disable_stereo_on_github_workflow.h new file mode 100644 index 000000000..1360a5a8d --- /dev/null +++ b/internal/disable_stereo_on_github_workflow.h @@ -0,0 +1,13 @@ +/* This purely internal header takes care of disabling stereo mode when on a github runner. Intended to be used + * on platforms that don't support stereo, allowing the compilation to proceed without error. */ + +#if __has_include("../detect_github_runner.h") // the marker file we added inside the workflow + // do nothing, if this isn't present +# if defined(MOZZI_AUDIO_CHANNELS) && (MOZZI_AUDIO_CHANNELS > 1) +# define GITHUB_RUNNER_ACCEPT_STEREO_IN_MONO +# undef MOZZI_AUDIO_CHANNELS +# define MOZZI_AUDIO_CHANNELS MOZZI_MONO +# warning Disabled stereo compilation while in Github runner. +# endif + +#endif diff --git a/internal/mozzi_macros.h b/internal/mozzi_macros.h index cbd021d97..51c2af075 100644 --- a/internal/mozzi_macros.h +++ b/internal/mozzi_macros.h @@ -27,12 +27,15 @@ MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, \ MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE)) -/** Compile time check to complain if the given argument is not a power of two */ +/** @file mozzi_internal_macros + * Compile time check to complain if the given argument is not a power of two */ #define MOZZI_CHECK_POW2(X) static_assert((X & (X - 1)) == 0, #X " must be a power of two"); #define MOZZI__IS(X, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, B0, B1, B2, B3, B4, B5, B6, B7, B8, B9, ...) \ ((X == A0) || (X == A1) || (X == A2) || (X == A3) || (X == A4) || (X == A5) || (X == A6) || (X == A7) || (X == A8) || (X == A9) || (X == B0) || (X == B1) || (X == B2) || (X == B3) || (X == B4) || (X == B5) || (X == B6) || (X == B7) || (X == B8) || (X == B9)) -/** Short-hand to check if given first value is any of the following values (up to 20). + +/** @file mozzi_internal_macros + * Short-hand to check if given first value is any of the following values (up to 20). * * (Orgignally, this macro was intended to also produce an error, should any of the values be non-defined (such as because it's a typo), but alas, the preprocessor would * let me have that). @@ -51,7 +54,8 @@ MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, \ MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE, MOZZI__INVALID_CONFIG_VALUE)) -/** Short-hand for a compile time complaint, if the given define does not have the expected value. +/** @file mozzi_internal_macros + * Short-hand for a compile time complaint, if the given define does not have the expected value. * * Use this to check - and clarify - complex nested logic inside #fidefs. * @@ -66,7 +70,27 @@ */ #define MOZZI_ASSERT_EQUAL(X, Y) static_assert(X == Y, "Internal error in preprocessor logic: " #X " != " #Y "."); -/** See MOZZI_ASSERT_EQUAL, but reversed */ +/** @file mozzi_internal_macros + * See MOZZI_ASSERT_EQUAL, but reversed */ #define MOZZI_ASSERT_NOTEQUAL(X, Y) static_assert(X != Y, "Internal error in preprocessor logic: " #X " == " #Y "."); + +#if __cplusplus >= 201402L +/** @file mozzi_internal_macros + * Document that a function has been deprecated, and when, if possible giving the user an explanatory message */ +#define MOZZI_DEPRECATED(WHEN, WHY) [[deprecated(WHY)]] +#elif defined(__GNUC__) && __has_cpp_attribute(deprecated) +#define MOZZI_DEPRECATED(WHEN, WHY) [[deprecated(WHY)]] +#elif defined(__GNUC__) || defined(__clang__) +#define MOZZI_DEPRECATED(WHEN, WHY) __attribute__((deprecated)) +#elif defined(_MSC_VER) +#define MOZZI_DEPRECATED(WHEN, WHY) __declspec(deprecated) +#else +#define MOZZI_DEPRECATED(WHEN, WHY) +#endif + +/** @file mozzi_internal_macros + * Document that a function is not implemented on this platform */ +#define MOZZI_UNIMPLEMENTED() MOZZI_DEPRECATED("n/a", "This feature is not implemented on this platform.") + #endif diff --git a/internal/mozzi_rand_p.h b/internal/mozzi_rand_p.h new file mode 100644 index 000000000..02674bd36 --- /dev/null +++ b/internal/mozzi_rand_p.h @@ -0,0 +1,36 @@ +#ifndef MOZZI_RAND_P_H +#define MOZZI_RAND_P_H + +namespace MozziPrivate { + +class MozziRandPrivate { +friend void randSeed(); +friend void randSeed(uint32_t); +friend uint32_t xorshift96(); + static uint32_t x; + static uint32_t y; + static uint32_t z; +public: + static uint32_t xorshift96() { + //period 2^96-1 + uint32_t t; + + x ^= x << 16; + x ^= x >> 5; + x ^= x << 1; + + t = x; + x = y; + y = z; + z = t ^ x ^ y; + + return z; + } + static void autoSeed(); // defined in hardware specific MozziGuts_impl-files +}; + +inline void randSeed(uint32_t seed) { MozziRandPrivate::x = seed; }; + +} + +#endif diff --git a/teensyPinMap.h b/internal/teensyPinMap.h similarity index 98% rename from teensyPinMap.h rename to internal/teensyPinMap.h index 6904e124a..1f028115e 100644 --- a/teensyPinMap.h +++ b/internal/teensyPinMap.h @@ -13,10 +13,6 @@ #define TEENSYPINMAP_H -#include "mozzi_config.h" - - - inline uint8_t teensyPinMap(uint8_t pin) { if (pin < 24) return pin-14; // common to all teensys diff --git a/library.properties b/library.properties index 053ce5357..4cbb8480c 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Mozzi -version=1.1.2 +version=1.9.9 author=Tim Barrass and contributors as documented in source, and at https://github.com/sensorium/Mozzi/graphs/contributors maintainer=Tim Barrass sentence=Sound synthesis library for Arduino @@ -8,4 +8,4 @@ category=Signal Input/Output url=https://sensorium.github.io/Mozzi/ architectures=* dot_a_linkage=false -includes=MozziGuts.h +includes=Mozzi.h diff --git a/meta.h b/meta.h index 85f99cc5b..0682bb6a6 100644 --- a/meta.h +++ b/meta.h @@ -6,6 +6,8 @@ Template meta-programming extras. #ifndef META_H_ #define META_H_ +/** @defgroup util Assorted meta-programming utils +*/ /** @ingroup util Enables you to instantiate a template based on an integer value. diff --git a/mozzi_analog.cpp b/mozzi_analog.cpp deleted file mode 100644 index 72fd2ab82..000000000 --- a/mozzi_analog.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * mozzi_analog.cpp - * - * Copyright 2012 Tim Barrass. - * - * This file is part of Mozzi. - * - * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. - * - */ - - -#include "mozzi_analog.h" - -#include "hardware_defines.h" -#include "mozzi_config.h" - -/** NOTE: Since analog input code is heavily hardware dependent, and also heavily interweaved with AUDIO_INPUT, - * it was moved to MozziGuts.cpp / MozziGuts_impl_XYZ.hpp for better maintainability. - * - * TODO: The (dis|re)connect functions below remain for now, as I'm not sure what to do about them. They were only ever - * implemented for AVR. - */ - -void disconnectDigitalIn(uint8_t channel_num){ -#if IS_AVR() - DIDR0 |= 1<= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif +#include "Arduino.h" #include "hardware_defines.h" -#if (USE_AUDIO_INPUT==true) -#warning "Using AUDIO_INPUT_PIN defined in mozzi_config.h for audio input." -#endif - -// hack for Teensy 2 (ATmega32U4), which has "adc_mapping" instead of "analog_pin_to_channel_PGM" -#if defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY) -//pasted from hardware/arduino/variants/leonardo/pins_arduino.h, doesn't work as of mozzi 0.01.2a -// __AVR_ATmega32U4__ has an unusual mapping of pins to channels -//extern const uint8_t PROGMEM analog_pin_to_channel_PGM[]; -//#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) ) - -// look at Arduino.app/Contents/Resources/Java/hardware/teensy/cores/teensy/pins_teensy.c - analogRead -// adc_mapping is already declared in pins_teensy.c, but it's static there so we can't access it -static const uint8_t PROGMEM adc_mapping[] = { -// 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8 - 0, 1, 4, 5, 6, 7, 13, 12, 11, 10, 9, 8, 10, 11, 12, 13, 7, 6, 5, 4, 1, 0, 8 -}; -#define analogPinToChannel(P) ( pgm_read_byte( adc_mapping + (P) ) ) -#endif - - -// include this although already in teensy 3 analog.c, because it is static there -#if defined(__MK20DX128__) -static const uint8_t channel2sc1a[] = { - 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, - 0, 19, 3, 21, 26, 22, 23 -}; -#elif defined(__MK20DX256__) -static const uint8_t channel2sc1a[] = { - 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, - 0, 19, 3, 19+128, 26, 18+128, 23, - 5+192, 5+128, 4+128, 6+128, 7+128, 4+192 -// A15 26 E1 ADC1_SE5a 5+64 -// A16 27 C9 ADC1_SE5b 5 -// A17 28 C8 ADC1_SE4b 4 -// A18 29 C10 ADC1_SE6b 6 -// A19 30 C11 ADC1_SE7b 7 -// A20 31 E0 ADC1_SE4a 4+64 -}; -#endif - - // for setupFastAnalogRead() enum ANALOG_READ_SPEED {FAST_ADC,FASTER_ADC,FASTEST_ADC}; +/** @defgroup analog Functions for taking (non-blocking) analog readings. */ + /** @ingroup analog This is automatically called in startMozzi. @@ -121,31 +77,51 @@ and ADC6 do not have digital input buffers, and therefore do not require Digital Input Disable bits. @param channel_num the analog input channel you wish to use. */ -void disconnectDigitalIn(uint8_t channel_num); - +inline void disconnectDigitalIn(uint8_t channel_num) { +#if IS_AVR() + DIDR0 |= 1< -extern STM32ADC adc; -#elif IS_ESP8266() -#include -#endif - -// moved these out of xorshift96() so xorshift96() can be reseeded manually -static unsigned long x=132456789, y=362436069, z=521288629; -// static unsigned long x= analogRead(A0)+123456789; -// static unsigned long y= analogRead(A1)+362436069; -// static unsigned long z= analogRead(A2)+521288629; - -/** @ingroup random -Random number generator. A faster replacement for Arduino's random function, -which is too slow to use with Mozzi. -Based on Marsaglia, George. (2003). Xorshift RNGs. http://www.jstatsoft.org/v08/i14/xorshift.pdf -@return a random 32 bit integer. -@todo check timing of xorshift96(), rand() and other PRNG candidates. - */ - -unsigned long xorshift96() -{ //period 2^96-1 - // static unsigned long x=123456789, y=362436069, z=521288629; - unsigned long t; - - x ^= x << 16; - x ^= x >> 5; - x ^= x << 1; - - t = x; - x = y; - y = z; - z = t ^ x ^ y; - - return z; -} - - -/** @ingroup random -Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used -in Mozzi's rand() function. This can be useful if you want random sequences to -be different on each run of a sketch, by seeding with fairly random input, such -as analogRead() on an unconnected pin (as explained in the Arduino documentation -for randomSeed(). randSeed is the same as xorshift96Seed(), but easier to -remember. -@param seed a number to use as a seed. -*/ -void randSeed(unsigned long seed) -{ - x=seed; -} - - -#if defined (__AVR_ATmega644P__) - -// a less fancy version for gizduino (__AVR_ATmega644P__) which doesn't know INTERNAL -static long longRandom() -{ - return ((long)analogRead(0)+63)*(analogRead(1)+97); // added offsets in case analogRead is 0 -} - -#elif defined (__AVR_ATmega2560__) -/* -longRandom(), used as a seed generator, comes from: -http://arduino.cc/forum/index.php/topic,38091.0.html -// AUTHOR: Rob Tillaart -// PURPOSE: Simple Random functions based upon unreliable internal temp sensor -// VERSION: 0.1 -// DATE: 2011-05-01 -// -// Released to the public domain, use at own risk -// -*/ -static long longRandom() -{ - //analogReference(INTERNAL2V56); - unsigned long rv = 0; - for (uint8_t i=0; i< 32; i++) rv |= ((analogRead(8)+2294) & 1L) << i; // added 2294 in case analogRead is 0 - return rv; -} - -#elif IS_AVR() - -static long longRandom() -{ - //analogReference(INTERNAL); - unsigned long rv = 0; - for (uint8_t i=0; i< 32; i++) rv |= ((analogRead(8)+1171) & 1L) << i; // added 1171 in case analogRead is 0 - return rv; -} - - -#endif - - -/** @ingroup random -Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used -in Mozzi's rand() function. This can be useful if you want random sequences to -be different on each run of a sketch, by seeding with a fairly random input. -randSeed() called without a parameter uses noise from reading the Arduino's -internal temperature as the seed, a technique discussed at -http://arduino.cc/forum/index.php/topic,38091.0.html, borrowing code put there -by Rob Tillaart. -@note It's not perfect, as discussed in the forum thread. -It might only work with some processors: (from the thread) -"...ATmega328P in DIP, possibly others but the duemilanove and uno will do it at least." -So far, gizduino's __AVR_ATmega644P__ chip doesn't like it, so we use (long)analogRead(0)*analogRead(1) for that instead. -It works to some degree on STM32 chips, but the produced seed is not very random at all. Again, using an appropriate -analogRead() (preferably on one or two floating input pins) is much more effective. -@todo add Teensy 3 code -*/ -void randSeed() { -#if IS_AVR() - ADCSRA &= ~ (1 << ADIE); // adc Disable Interrupt, re-enable at end - // this attempt at remembering analog_reference stops it working - // maybe needs a delay after changing analog reference in longRandom (Arduino reference suggests this) - // because the analog reads return 0 - //uint8_t analog_reference_orig = ADMUX&192; // analog_reference is high 2 bits of ADMUX, store this because longRandom sets it to internal - x=longRandom(); - y=longRandom(); - z=longRandom(); - //analogReference(analog_reference_orig); // change back to original - ADCSRA |= (1 << ADIE); // adc re-Enable Interrupt -#elif IS_STM32MAPLE() - // Unfortunately the internal temp sensor on STM32s does _not_ appear to create a lot of noise. - // Ironically, the calls to calibrate help induce some random noise. You're still fairly likely to produce two equal - // random seeds in two subsequent runs, however. - adc.enableInternalReading(); - float dummy = adc.readTemp(); - int* dummy_int = (int*) &dummy; - x=*dummy_int; - adc.calibrate(); - dummy = adc.readTemp(); - y=*dummy_int; - adc.calibrate(); - dummy = adc.readTemp(); - z=*dummy_int; -#elif IS_ESP8266() - x = RANDOM_REG32; - y = random (0xFFFFFFFF) ^ RANDOM_REG32; - z = random (0xFFFFFFFF) ^ RANDOM_REG32; -#else -#warning Automatic random seeding not implemented on this platform -#endif -} - - - -/** @ingroup random -Initialises Mozzi's (pseudo)random number generator xorshift96() with a chosen seed number. -@param seed a number to use as a seed. -*/ -void xorshiftSeed(unsigned long seed) -{ - x=seed; -} - - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param minval the minimum signed byte value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. -@param maxval the maximum signed byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random signed byte between minval and maxval-1 inclusive. -*/ -int8_t rand(int8_t minval, int8_t maxval) -{ - return (int8_t) ((((int) (lowByte(xorshift96()))) * (maxval-minval))>>8) + minval; -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param minval the minimum unsigned byte value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. -@param maxval the maximum unsigned byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random unsigned byte between minval and maxval-1 inclusive. -*/ -uint8_t rand(uint8_t minval, uint8_t maxval) -{ - return (uint8_t) ((((unsigned int) (lowByte(xorshift96()))) * (maxval-minval))>>8) + minval; -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param minval the minimum signed int value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. -@param maxval the maximum signed int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random int between minval and maxval-1 inclusive. -*/ -int rand( int minval, int maxval) -{ - return (int) ((((xorshift96() & 0xFFFF) * (maxval-minval))>>16) + minval); -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param minval the minimum unsigned int value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. -@param maxval the maximum unsigned int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random unsigned int between minval and maxval-1 inclusive. -*/ -unsigned int rand(unsigned int minval, unsigned int maxval) -{ - return (unsigned int) ((((xorshift96() & 0xFFFF) * (maxval-minval))>>16) + minval); -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param maxval the maximum signed byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random signed byte between 0 and maxval-1 inclusive. -*/ -int8_t rand(int8_t maxval) -{ - return (int8_t) ((((int) (lowByte(xorshift96()))) * maxval)>>8); -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param maxval the maximum unsigned byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random unsigned byte between 0 and maxval-1 inclusive. -*/ -uint8_t rand(uint8_t maxval) -{ - return (uint8_t) ((((unsigned int) (lowByte(xorshift96()))) * maxval)>>8); -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param maxval the maximum signed int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random int between 0 and maxval-1 inclusive. -*/ -int rand(int maxval) -{ - return (int) (((xorshift96() & 0xFFFF) * maxval)>>16); -} - - -/** @ingroup random -Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. -@param maxval the maximum unsigned int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. -@return a random unsigned int between 0 and maxval-1 inclusive. -*/ -unsigned int rand(unsigned int maxval) -{ - return (unsigned int) (((xorshift96() & 0xFFFF) * maxval)>>16); -} - - -/** @ingroup random -Generates a random number in the range for midi notes. -@return a random value between 0 and 127 inclusive -*/ -uint8_t randMidiNote() -{ - return lowByte(xorshift96())>>1; -} diff --git a/mozzi_rand.h b/mozzi_rand.h index ac75db24f..ef83de745 100644 --- a/mozzi_rand.h +++ b/mozzi_rand.h @@ -1,31 +1,151 @@ #ifndef MOZZI_RAND_H_ #define MOZZI_RAND_H_ -#if ARDUINO >= 100 - #include "Arduino.h" -#else - #include "WProgram.h" -#endif +#include +#include "internal/mozzi_rand_p.h" +/** @ingroup random +Random number generator. A faster replacement for Arduino's random function, +which is too slow to use with Mozzi. +Based on Marsaglia, George. (2003). Xorshift RNGs. http://www.jstatsoft.org/v08/i14/xorshift.pdf +@return a random 32 bit integer. +@todo check timing of xorshift96(), rand() and other PRNG candidates. + */ +inline uint32_t xorshift96() { return MozziPrivate::MozziRandPrivate::xorshift96(); }; -unsigned long xorshift96(); +/** @ingroup random +Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used +in Mozzi's rand() function. This can be useful if you want random sequences to +be different on each run of a sketch, by seeding with fairly random input, such +as analogRead() on an unconnected pin (as explained in the Arduino documentation +for randomSeed(). randSeed is the same as xorshift96Seed(), but easier to +remember. +@param seed a number to use as a seed. +*/ +inline void randSeed(uint32_t seed) { MozziPrivate::randSeed(seed); }; -void xorshiftSeed(unsigned long seed); -void randSeed(unsigned long seed); -void randSeed(); +/** @ingroup random +Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used +in Mozzi's rand() function. This can be useful if you want random sequences to +be different on each run of a sketch, by seeding with a fairly random input. +randSeed() called without a parameter uses noise from reading the Arduino's +internal temperature as the seed, a technique discussed at +http://arduino.cc/forum/index.php/topic,38091.0.html, borrowing code put there +by Rob Tillaart. -int8_t rand(int8_t minval, int8_t maxval); -int8_t rand(int8_t maxval); +@note Intialization of the random seed is done differently on different MCUs, + but is nowhere near perfect for most (and for some it is not even implemented at all). + Many implementations (e.g. on AVR, STM32) simply rely on reading a (hopefully noisy) + internal temperature sensor. + You will often get better results by calling analogRead() - @em not mozziAnalogRead(0), in this case! - + on one or two floating (non-connected) analog pins. +*/ +inline void randSeed() { MozziPrivate::MozziRandPrivate::autoSeed(); }; -uint8_t rand(uint8_t minval, uint8_t maxval); -uint8_t rand(uint8_t maxval); +/** @ingroup random +Initialises Mozzi's (pseudo)random number generator xorshift96() with a chosen seed number. +@param seed a number to use as a seed. +// TODO: duplicate deprecate / remove +*/ +inline void xorshiftSeed(uint32_t seed) { randSeed(seed); }; -int rand(int minval, int maxval); -int rand(int maxval); +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param minval the minimum signed byte value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. +@param maxval the maximum signed byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random signed byte between minval and maxval-1 inclusive. +*/ +inline int8_t rand(int8_t minval, int8_t maxval) +{ + return (int8_t) ((((int) (lowByte(xorshift96()))) * (maxval-minval))>>8) + minval; +} -unsigned int rand(unsigned int minval, unsigned int maxval); -unsigned int rand(unsigned int maxval); +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param maxval the maximum signed byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random signed byte between 0 and maxval-1 inclusive. +*/ +inline int8_t rand(int8_t maxval) +{ + return (int8_t) ((((int) (lowByte(xorshift96()))) * maxval)>>8); +} -uint8_t randMidiNote(); +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param minval the minimum unsigned byte value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. +@param maxval the maximum unsigned byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random unsigned byte between minval and maxval-1 inclusive. +*/ +inline uint8_t rand(uint8_t minval, uint8_t maxval) +{ + return (uint8_t) ((((unsigned int) (lowByte(xorshift96()))) * (maxval-minval))>>8) + minval; +} + +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param maxval the maximum unsigned byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random unsigned byte between 0 and maxval-1 inclusive. +*/ +inline uint8_t rand(uint8_t maxval) +{ + return (uint8_t) ((((unsigned int) (lowByte(xorshift96()))) * maxval)>>8); +} + +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param minval the minimum signed int value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. +@param maxval the maximum signed int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random int between minval and maxval-1 inclusive. + +@note The returned value is always in the 16 bit range, even on platforms where int is wider. If you need 32 bits, call xorshift96(), directly. +*/ +inline int rand(int minval, int maxval) +{ + return (int) ((((xorshift96() & 0xFFFF) * (maxval-minval))>>16) + minval); +} + +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param maxval the maximum signed int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random int between 0 and maxval-1 inclusive. + +@note The returned value is always in the 16 bit range, even on platforms where int is wider. If you need 32 bits, call xorshift96(), directly. +*/ +inline int rand(int maxval) +{ + return (int) (((xorshift96() & 0xFFFF) * maxval)>>16); +} + +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param minval the minimum unsigned int value of the range to be chosen from. Minval will be the minimum value possibly returned by the function. +@param maxval the maximum unsigned int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random unsigned int between minval and maxval-1 inclusive. +*/ +inline unsigned int rand(unsigned int minval, unsigned int maxval) +{ + return (unsigned int) ((((xorshift96() & 0xFFFF) * (maxval-minval))>>16) + minval); +} + +/** @ingroup random +Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. +@param maxval the maximum unsigned int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function. +@return a random unsigned int between 0 and maxval-1 inclusive. + +@note The returned value is always in the 16 bit range, even on platforms where int is wider. If you need 32 bits, call xorshift96(), directly. +*/ +inline unsigned int rand(unsigned int maxval) +{ + return (unsigned int) (((xorshift96() & 0xFFFF) * maxval)>>16); +} + +/** @ingroup random +Generates a random number in the range for midi notes. +@return a random value between 0 and 127 inclusive +*/ +inline uint8_t randMidiNote() +{ + return lowByte(xorshift96())>>1; +} #endif /* MOZZI_RAND_H_ */ diff --git a/mozzi_utils.cpp b/mozzi_utils.cpp deleted file mode 100644 index 52dafd600..000000000 --- a/mozzi_utils.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "mozzi_utils.h" - -/** @ingroup util -Given a power of 2, work out the number to shift right by to do a divide by the number, or shift left to multiply. -@param a power of 2, or any other number for that matter -@return the number of trailing zeros on the right hand end -*/ - -/* NOTE: previous version is super-nifty, but pulls in software float code on AVR (the platform where it makes a difference), leading to bloat and a compile time warning. - * Sine the only use-case I could find works on a compile-time constant (template-parameter), I added a constexpr function in mozzi_utils.h, instead. I renamed it, too, - * so, we'll learn about any use-case outside of this scope. If there are no reports of breakage, the following can probably be removed for good. -long trailingZeros(unsigned long v) { - // find the number of trailing zeros in v, from http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast - // there are faster methods on the bit twiddling site, but this is short - float f = (float)(v & -v); // cast the least significant bit in v to a float - return (*(uint32_t *)&f >> 23) - 0x7f; -} - -Here's an alternate, trivial version: -uint8_t trailingZeros(uint16_t v) { - uint8_t ret = 0; - while ((v % 2) == 0) { - v = v >> 1; - ++ret; - } - return ret; -} */ - - - /** Convert BPM to milliseconds, which can be used to set the delay between beats for Metronome. - @param bpm beats per minute - */ - unsigned int BPMtoMillis(float bpm){ - float seconds_per_beat = 60.f/bpm; - return (unsigned int) (seconds_per_beat*1000); - } diff --git a/mozzi_utils.h b/mozzi_utils.h index 5ee8671c5..a21f4b03b 100644 --- a/mozzi_utils.h +++ b/mozzi_utils.h @@ -59,8 +59,39 @@ void setPin13Low() } +/** @ingroup util +Given a power of 2, work out the number to shift right by to do a divide by the number, or shift left to multiply. +@param a power of 2, or any other number for that matter +@return the number of trailing zeros on the right hand end +*/ constexpr uint8_t trailingZerosConst(unsigned long v) { return ((v % 2) ? 0 : 1+trailingZerosConst(v >> 1)); } -//uint8_t trailingZeros(uint16_t v); -unsigned int BPMtoMillis(float bpm); +/* NOTE: previous version of the above is super-nifty, but pulls in software float code on AVR (the platform where it makes a difference), leading to bloat and a compile time warning. + * Sine the only use-case I could find works on a compile-time constant (template-parameter), I added a constexpr function in mozzi_utils.h, instead. I renamed it, too, + * so, we'll learn about any use-case outside of this scope. If there are no reports of breakage, the following can probably be removed for good. +long trailingZeros(unsigned long v) { + // find the number of trailing zeros in v, from http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightFloatCast + // there are faster methods on the bit twiddling site, but this is short + float f = (float)(v & -v); // cast the least significant bit in v to a float + return (*(uint32_t *)&f >> 23) - 0x7f; +} + +Here's an alternate, trivial version: +uint8_t trailingZeros(uint16_t v) { + uint8_t ret = 0; + while ((v % 2) == 0) { + v = v >> 1; + ++ret; + } + return ret; +} */ + + +/** Convert BPM to milliseconds, which can be used to set the delay between beats for Metronome. +@param bpm beats per minute +*/ +constexpr uint16_t BPMtoMillis(float bpm){ + //float seconds_per_beat = 60.f/bpm; + return (uint16_t) (((float) 60.f/bpm)*1000); +} #endif /* UTILS_H_ */ diff --git a/utility/TimerZero.cpp b/utility/TimerZero.cpp deleted file mode 100644 index 9cccc6db3..000000000 --- a/utility/TimerZero.cpp +++ /dev/null @@ -1,140 +0,0 @@ -/* - * TimerZero.cpp - * - * Copyright 2012 Tim Barrass - * - * This file is part of TimerZero, a library for Arduino. - * - * TimerZero is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * TimerZero is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with TimerZero. If not, see . - * - */ - -// Based on TimerTwo, -// downloaded from https://bitbucket.org/johnmccombs, 4/2/2012 -// -// TB2012 added Arduino.h include -// TB2012 replaced Timer 2 prescale factors with ones for Timer 0 -// TB2012 replaced all Timer 2 register names with ones for timer 0 -// TB2012 search/replaced TimerTwo with TimerZero -// TB2012 changed preScale array to suit Timer0 - -// Added by TB2014 for Mozzi library, to hide code from Teensy 3.1 -#if defined (__AVR__) - -#include -#include -#include -#include - -// allowed prescale factors -/* -#define PS1 (1 << CS20) -#define PS8 (1 << CS21) -#define PS32 (1 << CS21) | (1 << CS20) -#define PS64 (1 << CS22) -#define PS128 (1 << CS22) | (1 << CS20) -#define PS256 (1 << CS22) | (1 << CS21) -#define PS1024 (1 << CS22) | (1 << CS21) | (1 << CS20) -*/ -#define PS1 (1 << CS00) -#define PS8 (1 << CS01) -#define PS64 (1 << CS01) | (1 << CS00) -#define PS256 (1 << CS02) -#define PS1024 (1 << CS02) | (1 << CS00) - -// table by prescale = 2^n where n is the table index -static const unsigned char __attribute__((section(".progmem.data"))) preScale[] = - { - PS1, 0, 0, PS8, 0, 0, PS64, 0, PS256, 0, PS1024 - }; - -bool TimerZero::reset_; -void (*TimerZero::f_)(); -unsigned TimerZero::period_; -//------------------------------------------------------------------------------ -// initialize timer 0 -unsigned char TimerZero::init(unsigned usec, void (*f)(), bool reset) -{ - f_ = f; - reset_ = reset; - // assume F_CPU is a multiple of 1000000 - // number of clock ticks to delay usec microseconds - unsigned long ticks = usec * (F_CPU/1000000); - // determine prescale factor and TOP/OCR2A value - // use minimum prescale factor - unsigned char ps, i; - for (i = 0; i < sizeof(preScale); i++) - { - ps = pgm_read_byte(&preScale[i]); - if (ps && (ticks >> i) <= 256) - break; - } - //return error if usec is too large - if (i == sizeof(preScale)) - return false; - period_ = ((long)(ticks >> i) * (1 << i))/ (F_CPU /1000000); - // Serial.println(i, DEC); - // disable timer 0 interrupts - TIMSK0 = 0; - // use system clock (clkI/O) - //ASSR &= ~(1 << AS2); - // Clear Timer on Compare Match (CTC) mode - TCCR0A = (1 << WGM01); - - // only need prescale bits in TCCR0B - TCCR0B = ps; - - // set TOP so timer period is (ticks >> i) - OCR0A = (ticks >> i) - 1; - return true; -} -//------------------------------------------------------------------------------ -// Start timer zero interrupts -void TimerZero::start() -{ - TIMSK0 |= (1 << OCIE0A); -} -//------------------------------------------------------------------------------ -// Stop timer 2 interrupts -void TimerZero::stop() -{ - TIMSK0 = 0; -} -//------------------------------------------------------------------------------ -// ISR for timer 0 Compare A interrupt -// TB2012 added ISR_NOBLOCK so it can be interrupted by Timer 1 (audio) -ISR(TIMER0_COMPA_vect, ISR_NOBLOCK) -{ - // disable timer 0 interrupts - TIMSK0 = 0; - // call user function - (*TimerZero::f_)(); - // in case f_ enabled interrupts - cli(); - // clear counter if reset_ is true - if (TimerZero::reset_) - { - // reset counter - TCNT0 = 0; - // clear possible pending interrupt - TIFR0 |= (1 << OCF0A); - } - // enable timer 2 COMPA interrupt - TIMSK0 |= (1 << OCIE0A); -} - -#endif - - - diff --git a/utility/TimerZero.h b/utility/TimerZero.h deleted file mode 100644 index dff630f5f..000000000 --- a/utility/TimerZero.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * TimerZero.h - * - * Copyright 2012 Tim Barrass, adapted from TimerTwo by John McCombs (date?). - * - * This file is part of TimerZero, a library for Arduino. - * - * TimerZero is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * TimerZero is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with TimerZero. If not, see . - * - */ - -// Based on TimerTwo, -// downloaded from https://bitbucket.org/johnmccombs, 4/2/2012 -// -// TB2012 search/replaced TimerTwo with TimerZero - -#ifndef TimerZero_h -#define TimerZero_h - -namespace TimerZero -{ -extern unsigned period_; -// timer reset flag -extern bool reset_; -// user function -extern void (*f_)(); -// call f every usec microseconds if reset == false -// call f after delay of usec microseconds from call return if reset == true -// max delay is 256*1024 clock cycles or 16,384 microseconds for a 16 MHz CPU -unsigned char init(unsigned usec, void (*f)(), bool reset = false); -// period in usec -inline unsigned period() -{ - return period_; -} -// start calls -void start(); -// stop calls -void stop(); -} - -#endif // TimerZero -