-
Notifications
You must be signed in to change notification settings - Fork 186
/
Phasor.h
116 lines (98 loc) · 3.05 KB
/
Phasor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Phasor.h
*
* This file is part of Mozzi.
*
* Copyright 2012-2024 Tim Barrass and the Mozzi Team
*
* Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
*
*/
#ifndef PHASOR_H_
#define PHASOR_H_
#include "Arduino.h"
#include "mozzi_fixmath.h"
#define PHASOR_MAX_VALUE_UL 4294967295UL
/** Phasor repeatedly generates a high resolution ramp at a variable frequency.
The output of Phasor.next() is an unsigned number between 0 and 4294967295, the
maximum that can be expressed by an unsigned 32 bit integer.
@tparam UPDATE_RATE the rate at which the Phasor will be updated,
usually MOZZI_CONTROL_RATE or MOZZI_AUDIO_RATE.
*/
template <unsigned int UPDATE_RATE>
class Phasor
{
private:
uint32_t current_value;
volatile uint32_t step_size;
public:
/** Constructor. "Phasor <MOZZI_AUDIO_RATE> myphasor;"
makes a Phasor which updates at MOZZI_AUDIO_RATE.
*/
Phasor (){
;
}
/** Increments one step along the phase.
@return the next value.
*/
inline
uint32_t next()
{
current_value += step_size; // will wrap
return current_value;
}
/** Set the current value of the phasor. The Phasor will continue incrementing from this
value using any previously calculated step size.
*/
inline
void set(uint32_t value)
{
current_value=value;
}
/** Set the Phasor frequency with an unsigned int.
@param frequency is how many times per second to count from
0 to the maximum uint32_t value 4294967295.
@note Timing 8us
*/
inline
void setFreq( int frequency)
{
step_size = ((((uint32_t)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
}
/** Set the Phasor frequency with a float.
@param frequency is how many times per second to count from
0 to the maximum uint32_t value 4294967295.
*/
inline
void setFreq(float frequency)
{ // 1 us - using float doesn't seem to incur measurable overhead with the oscilloscope
step_size = (uint32_t)(((float)PHASOR_MAX_VALUE_UL/UPDATE_RATE)*frequency);
}
/** phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
Instead of recalculating the phase increment for each frequency in between, you
can just calculate the phase increment for each end frequency with
phaseIncFromFreq(), then use a Line to interpolate on the fly and use
setPhaseInc() to set the phase increment at each step. (Note: I should really
profile this with the oscilloscope to see if it's worth the extra confusion!)
@param frequency for which you want to calculate a phase increment value.
@return the phase increment value which will produce a given frequency.
*/
inline
uint32_t phaseIncFromFreq(int frequency)
{
return ((((uint32_t)((PHASOR_MAX_VALUE_UL>>8)+1))/(UPDATE_RATE))*frequency)<<8;
}
/** Set a specific phase increment. See phaseIncFromFreq().
@param stepsize a phase increment value as calculated by phaseIncFromFreq().
*/
inline
void setPhaseInc(uint32_t stepsize)
{
step_size = stepsize;
}
};
/**
@example 06.Synthesis/PWM_Phasing/PWM_Phasing.ino
This example demonstrates the Phasor class.
*/
#endif /* PHASOR_H_ */