Mozzi  alpha 0.01.1m
sound synthesis library for Arduino
 All Classes Functions
examples/_19_PWM_Phasing/build-uno/_19_PWM_Phasing.cpp
00001 #include <Arduino.h>
00002 /*  Example of pulse width modulation,
00003  *  using Mozzi sonification library.
00004  *   
00005  *  Based Miller Puckette's j03.pulse.width.mod example 
00006  *  in the Pure Data documentation, subtracting 2 sawtooth 
00007  *  waves with slightly different tunings to produce a 
00008  *  varying phase difference.
00009  *
00010  *  Demonstrates Phasor().
00011  *
00012  *  Circuit: Audio output on digital pin 9.
00013  *
00014  *  Mozzi help/discussion/announcements:
00015  *  https://groups.google.com/forum/#!forum/mozzi-users
00016  *
00017  *  Tim Barrass 2012.
00018  *  This example code is in the public domain.
00019  */
00020 
00021 #include <MozziGuts.h>
00022 #include <Phasor.h>
00023 
00024 #define CONTROL_RATE 64 // powers of 2 please
00025 
00026 Phasor <AUDIO_RATE> aPhasor1;
00027 Phasor <AUDIO_RATE> aPhasor2;
00028 
00029 float freq = 330.f;
00030 
00031 void setup(){
00032   aPhasor1.setFreq(freq);
00033   aPhasor2.setFreq(freq+0.2f);  
00034   startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)
00035 }
00036 
00037 
00038 void updateControl(){
00039 }
00040 
00041 
00042 int updateAudio(){
00043   char asig1 = (char)(aPhasor1.next()>>24);
00044   char asig2 = (char)(aPhasor2.next()>>24);
00045   return ((int)asig1-asig2)/2;
00046 }
00047 
00048 
00049 void loop(){
00050   audioHook(); // required here
00051 }
00052 
00053 
00054 
00055 
00056 
00057 
00058