Mozzi  version v2.0
sound synthesis library for Arduino
Phasor.h
1 /*
2  * Phasor.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 #ifndef PHASOR_H_
13 #define PHASOR_H_
14 
15 #include "Arduino.h"
16 #include "mozzi_fixmath.h"
17 
18 #define PHASOR_MAX_VALUE_UL 4294967295UL
19 
27 template <unsigned int UPDATE_RATE>
28 class Phasor
29 {
30 private:
31  uint32_t current_value;
32  volatile uint32_t step_size;
33 
34 public:
38  Phasor (){
39  ;
40  }
41 
45  inline
46  uint32_t next()
47  {
48  current_value += step_size; // will wrap
49  return current_value;
50  }
51 
55  inline
56  void set(uint32_t value)
57  {
58  current_value=value;
59  }
60 
61 
67  inline
68  void setFreq( int frequency)
69  {
70  step_size = ((((uint32_t)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
71  }
72 
73 
78  inline
79  void setFreq(float frequency)
80  { // 1 us - using float doesn't seem to incur measurable overhead with the oscilloscope
81  step_size = (uint32_t)(((float)PHASOR_MAX_VALUE_UL/UPDATE_RATE)*frequency);
82  }
83 
93  inline
94  uint32_t phaseIncFromFreq(int frequency)
95  {
96  return ((((uint32_t)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
97  }
98 
99 
103  inline
104  void setPhaseInc(uint32_t stepsize)
105  {
106  step_size = stepsize;
107  }
108 
109 };
110 
116 #endif /* PHASOR_H_ */
Phasor repeatedly generates a high resolution ramp at a variable frequency.
Definition: Phasor.h:29
Phasor()
Constructor.
Definition: Phasor.h:38
uint32_t next()
Increments one step along the phase.
Definition: Phasor.h:46
uint32_t phaseIncFromFreq(int frequency)
phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
Definition: Phasor.h:94
void setFreq(float frequency)
Set the Phasor frequency with a float.
Definition: Phasor.h:79
void set(uint32_t value)
Set the current value of the phasor.
Definition: Phasor.h:56
void setPhaseInc(uint32_t stepsize)
Set a specific phase increment.
Definition: Phasor.h:104
void setFreq(int frequency)
Set the Phasor frequency with an unsigned int.
Definition: Phasor.h:68