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
20
/** Phasor repeatedly generates a high resolution ramp at a variable frequency.
21
The output of Phasor.next() is an unsigned number between 0 and 4294967295, the
22
maximum that can be expressed by an unsigned 32 bit integer.
23
@tparam UPDATE_RATE the rate at which the Phasor will be updated,
24
usually MOZZI_CONTROL_RATE or MOZZI_AUDIO_RATE.
25
*/
26
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
:
35
/** Constructor. "Phasor <MOZZI_AUDIO_RATE> myphasor;"
36
makes a Phasor which updates at MOZZI_AUDIO_RATE.
37
*/
38
Phasor
(){
39
;
40
}
41
42
/** Increments one step along the phase.
43
@return the next value.
44
*/
45
inline
46
uint32_t
next
()
47
{
48
current_value += step_size;
// will wrap
49
return
current_value;
50
}
51
52
/** Set the current value of the phasor. The Phasor will continue incrementing from this
53
value using any previously calculated step size.
54
*/
55
inline
56
void
set
(uint32_t value)
57
{
58
current_value=value;
59
}
60
61
62
/** Set the Phasor frequency with an unsigned int.
63
@param frequency is how many times per second to count from
64
0 to the maximum uint32_t value 4294967295.
65
@note Timing 8us
66
*/
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
74
/** Set the Phasor frequency with a float.
75
@param frequency is how many times per second to count from
76
0 to the maximum uint32_t value 4294967295.
77
*/
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
84
/** phaseIncFromFreq() and setPhaseInc() are for saving processor time when sliding between frequencies.
85
Instead of recalculating the phase increment for each frequency in between, you
86
can just calculate the phase increment for each end frequency with
87
phaseIncFromFreq(), then use a Line to interpolate on the fly and use
88
setPhaseInc() to set the phase increment at each step. (Note: I should really
89
profile this with the oscilloscope to see if it's worth the extra confusion!)
90
@param frequency for which you want to calculate a phase increment value.
91
@return the phase increment value which will produce a given frequency.
92
*/
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
100
/** Set a specific phase increment. See phaseIncFromFreq().
101
@param stepsize a phase increment value as calculated by phaseIncFromFreq().
102
*/
103
inline
104
void
setPhaseInc
(uint32_t stepsize)
105
{
106
step_size = stepsize;
107
}
108
109
};
110
111
/**
112
@example 06.Synthesis/PWM_Phasing/PWM_Phasing.ino
113
This example demonstrates the Phasor class.
114
*/
115
116
#
endif
/* PHASOR_H_ */
Generated automatically using Doxygen. If info on this page is outdated, incomplete, or wrong, please open an issue at https://github.com/sensorium/Mozzi/issues