-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsine.py
More file actions
45 lines (34 loc) · 1.24 KB
/
Copy pathsine.py
File metadata and controls
45 lines (34 loc) · 1.24 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
# This advanced example shows how to create a custom waveform
# for the button to output. In this case, it generates a
# sine wave. You can use this example as a basis for very
# basic oscillators.
# See also noise.py
import array
import math
import audiocore
import winterbloom_bhb
bhb = winterbloom_bhb.BigHonkingButton()
# This generates a raw set of samples that represents one full
# cycle of a sine wave. If you wanted different waveforms, you
# could change the formula here to generate that instead.
def generate_sine_wave(volume=1.0):
volume = volume * (2 ** 15 - 1) # Increase this to increase the volume of the tone.
length = 100
samples = array.array("H", [0] * length)
for i in range(length):
samples[i] = int((1 + math.sin(math.pi * 2 * i / length)) * volume)
return samples
sine_wave = generate_sine_wave(0.8)
sample = audiocore.RawSample(sine_wave)
# Change this to play different notes. You can also
# check the CV input using `bhb.pitch_in` and re-adjust
# the sample rate.
frequency = 440
sample.sample_rate = frequency * len(sine_wave)
while bhb.update():
if bhb.triggered:
bhb.gate_out = True
bhb.play(sample, loop=True)
if bhb.released:
bhb.gate_out = False
bhb.stop()