Mozzi  version v1.1.0
sound synthesis library for Arduino
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 
22 #define PHASOR_MAX_VALUE_UL 4294967295UL
23 
24 /** Phasor repeatedly generates a high resolution ramp at a variable frequency.
25 The output of Phasor.next() is an unsigned number between 0 and 4294967295, the
26 maximum that can be expressed by an unsigned 32 bit integer.
27 @tparam UPDATE_RATE the rate at which the Phasor will be updated,
28 usually CONTROL_RATE or AUDIO_RATE.
29 */
30 
31 template <unsigned int UPDATE_RATE>
32 class Phasor
33 {
34 private:
35  uint32_t current_value;
36  volatile uint32_t step_size;
37 
38 public:
39  /** Constructor. "Phasor <AUDIO_RATE> myphasor;"
40  makes a Phasor which updates at AUDIO_RATE.
41  */
42  Phasor (){
43  ;
44  }
45 
46  /** Increments one step along the phase.
47  @return the next value.
48  */
49  inline
51  {
52  current_value += step_size; // will wrap
53  return current_value;
54  }
55 
56  /** Set the current value of the phasor. The Phasor will continue incrementing from this
57  value using any previously calculated step size.
58  */
59  inline
60  void set(uint32_t value)
61  {
62  current_value=value;
63  }
64 
65 
66  /** Set the Phasor frequency with an unsigned int.
67  @param frequency is how many times per second to count from
68  0 to the maximum uint32_t value 4294967295.
69  @note Timing 8us
70  */
71  inline
72  void setFreq( int frequency)
73  {
74  step_size = ((((uint32_t)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
75  }
76 
77 
78  /** Set the Phasor frequency with a float.
79  @param frequency is how many times per second to count from
80  0 to the maximum uint32_t value 4294967295.
81  */
82  inline
83  void setFreq(float frequency)
84  { // 1 us - using float doesn't seem to incur measurable overhead with the oscilloscope
85  step_size = (uint32_t)(((float)PHASOR_MAX_VALUE_UL/UPDATE_RATE)*frequency);
86  }
87 
88  /** phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
89  Instead of recalculating the phase increment for each frequency in between, you
90  can just calculate the phase increment for each end frequency with
91  phaseIncFromFreq(), then use a Line to interpolate on the fly and use
92  setPhaseInc() to set the phase increment at each step. (Note: I should really
93  profile this with the oscilloscope to see if it's worth the extra confusion!)
94  @param frequency for which you want to calculate a phase increment value.
95  @return the phase increment value which will produce a given frequency.
96  */
97  inline
99  {
100  return ((((uint32_t)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
101  }
102 
103 
104  /** Set a specific phase increment. See phaseIncFromFreq().
105  @param stepsize a phase increment value as calculated by phaseIncFromFreq().
106  */
107  inline
108  void setPhaseInc(uint32_t stepsize)
109  {
110  step_size = stepsize;
111  }
112 
113 };
114 
115 /**
116 @example 06.Synthesis/PWM_Phasing/PWM_Phasing.ino
117 This example demonstrates the Phasor class.
118 */
119 
120 #endif /* PHASOR_H_ */
void set(uint32_t value)
Set the current value of the phasor.
Definition: Phasor.h:60
void setFreq(int frequency)
Set the Phasor frequency with an unsigned int.
Definition: Phasor.h:72
Phasor()
Constructor.
Definition: Phasor.h:42
void setFreq(float frequency)
Set the Phasor frequency with a float.
Definition: Phasor.h:83
#define PHASOR_MAX_VALUE_UL
Definition: Phasor.h:22
uint32_t phaseIncFromFreq(int frequency)
phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies...
Definition: Phasor.h:98
uint32_t next()
Increments one step along the phase.
Definition: Phasor.h:50
Phasor repeatedly generates a high resolution ramp at a variable frequency.
Definition: Phasor.h:32
void setPhaseInc(uint32_t stepsize)
Set a specific phase increment.
Definition: Phasor.h:108