Skip to content

Commit 94377b0

Browse files
committed
Add tests and examples for triangle wave
1 parent 11dd02c commit 94377b0

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

example/play_440hz.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def main():
2020
player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
2121
time.sleep(0.5)
2222

23+
print("play triangle wave")
24+
synthesizer = Synthesizer(osc1_waveform=Waveform.triangle, osc1_volume=0.8, use_osc2=False)
25+
player.play_wave(synthesizer.generate_constant_wave(440.0, 3.0))
26+
time.sleep(0.5)
27+
2328
print("play synthesized wave 1")
2429
synthesizer = Synthesizer(
2530
osc1_waveform=Waveform.sawtooth, osc1_volume=1.0,

synthesizer/synthesizer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def generate_wave(self, phases):
4545
phases = np.copy(phases) * self._freq_transpose
4646
return self._volume * self._wave_func(phases)
4747

48-
def gen_triang(self,t,width=0.5):
49-
return scipy.signal.sawtooth(t,width)
48+
@staticmethod
49+
def gen_triang(t, width=0.5):
50+
return scipy.signal.sawtooth(t, width)
51+
5052

5153
class Synthesizer(object):
5254
u""" Virtual analog synthesizer object

test/test_synthesizer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ def test_square_wave():
3131
assert_almost_equal(wave.max(), 1.0, places=3)
3232
assert_almost_equal(wave.min(), -1.0, places=3)
3333
assert_almost_equal(wave.mean(), 0.0, places=3)
34+
35+
36+
def test_triangle_wave():
37+
synthesizer = Synthesizer(osc1_waveform=Waveform.triangle, osc1_volume=1.0, use_osc2=False, rate=RATE)
38+
wave = synthesizer.generate_constant_wave(440.0, 1.0)
39+
eq_(wave.size, RATE)
40+
assert_almost_equal(wave.max(), 1.0, places=3)
41+
assert_almost_equal(wave.min(), -1.0, places=3)
42+
assert_almost_equal(wave.mean(), 0.0, places=3)

0 commit comments

Comments
 (0)