Mozzi  version 2015-05-11-20:23
sound synthesis library for Arduino
 All Classes Functions Typedefs Groups
Phasor.h
1 /*
2  * Phasor.h
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef PHASOR_H_
13 #define PHASOR_H_
14 
15 #if ARDUINO >= 100
16  #include "Arduino.h"
17 #else
18  #include "WProgram.h"
19 #endif
20 #include "mozzi_fixmath.h"
21 #include <util/atomic.h>
22 
23 #define PHASOR_MAX_VALUE_UL 4294967295UL
24 
32 template <unsigned int UPDATE_RATE>
33 class Phasor
34 {
35 private:
36  unsigned long current_value;
37  volatile unsigned long step_size;
38 
39 public:
43  Phasor (){
44  ;
45  }
46 
50  inline
51  unsigned long next()
52  {
53  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
54  {
55  current_value += step_size; // will wrap
56  }
57  return current_value;
58  }
59 
63  inline
64  void set(unsigned long value)
65  {
66  current_value=value;
67  }
68 
69 
75  inline
76  void setFreq( int frequency)
77  {
78  step_size = ((((unsigned long)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
79  }
80 
81 
86  inline
87  void setFreq(float frequency)
88  { // 1 us - using float doesn't seem to incur measurable overhead with the oscilloscope
89  //ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
90  //{
91  step_size = (unsigned long)(((float)PHASOR_MAX_VALUE_UL/UPDATE_RATE)*frequency);
92 
93  //}
94  }
95 
105  inline
106  unsigned long phaseIncFromFreq(int frequency)
107  {
108  return ((((unsigned long)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
109  }
110 
111 
115  inline
116  void setPhaseInc(unsigned long stepsize)
117  {
118  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
119  {
120  step_size = stepsize;
121  }
122  }
123 
124 };
125 
131 #endif /* PHASOR_H_ */
unsigned long phaseIncFromFreq(int frequency)
phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies...
Definition: Phasor.h:106
void setFreq(int frequency)
Set the Phasor frequency with an unsigned int.
Definition: Phasor.h:76
Phasor()
Constructor.
Definition: Phasor.h:43
void setFreq(float frequency)
Set the Phasor frequency with a float.
Definition: Phasor.h:87
void set(unsigned long value)
Set the current value of the phasor.
Definition: Phasor.h:64
void setPhaseInc(unsigned long stepsize)
Set a specific phase increment.
Definition: Phasor.h:116
unsigned long next()
Increments one step along the phase.
Definition: Phasor.h:51
Phasor repeatedly generates a high resolution ramp at a variable frequency.
Definition: Phasor.h:33