-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOscillator.java
More file actions
84 lines (67 loc) · 2.19 KB
/
Copy pathOscillator.java
File metadata and controls
84 lines (67 loc) · 2.19 KB
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
/**
Oscillator class by default acts as a sine wave oscillator
**/
class Oscillator {
// Constants
protected final double DEFAULT_SINE_CONSTANT = 2.0;
private final static double MIDDLE_C = 261.1; // Freq for middle c
private final static double DEFAULT_AMPLITUDE = 100;
protected int sampleRate;
protected double freq;
protected double amplitude;
// Constructors
/**
Constructor for Oscillator by default sets freq to middle c, and amplitude to 100
@param sampleRate sets the sample rate of the oscillator
**/
protected Oscillator (int sampleRate) {
//frequency default is middle C
this(sampleRate, MIDDLE_C, DEFAULT_AMPLITUDE);
}
/**
Constructor for Oscillator
@param sampleRate sets the sample rate of the oscillator
@param freq sets the frequency of the oscillator
@param amplitude sets the amplitude of the oscillator
**/
protected Oscillator (int sampleRate, double freq, double amplitude) {
this.sampleRate = sampleRate;
this.freq = freq;
this.amplitude = amplitude;
}
/**
Creates the frequency as a byte where at time t
@param t is the time at which the wave is at (1 sec => t=sampleRate)
@return The byte created by the sine oscillator at time t
**/
protected byte createWave(double t) {
return sineWave(t, DEFAULT_SINE_CONSTANT);
}
/**
Creates the frequency of the sine wave oscillator at time t, using the constant
specified
@param t the time of the sine wave to return the frequency at
@param constant default is 2.0, can be used to create square waves using fourier series
@return the byte containing the frequency of the sine wave at time t
**/
protected byte sineWave(double t, double constant) {
double period = (double) this.sampleRate / this.freq;
double angle = constant * Math.PI * (t / period);
return (byte) (Math.sin(angle) * amplitude);
}
// Setter methods
/**
Sets the frequency of the oscillator
@param freq is the frequency the oscillator will be set to
**/
public void setFreq(double freq) {
this.freq = freq;
}
/**
Sets the amplitude of the oscillator
@param amplitude is the amplitude the oscillator will be set to
**/
public void setAmplitude(double amplitude) {
this.amplitude = amplitude;
}
}