Skip to content

Commit 6829e5a

Browse files
odeliytheacodes
andauthored
examples: Add CV select sample (#9)
* Add files via upload * Update CV_select.py * firmware: Add select_from_list_using_cv helper * examples: Add CV select example Co-authored-by: Thea Flowers <me@thea.codes>
1 parent 97f7345 commit 6829e5a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

examples/cv_select.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This example determines which sample to play based on the
2+
# pitch CV input- useful for things like drum samples.
3+
4+
import winterbloom_bhb
5+
6+
bhb = winterbloom_bhb.BigHonkingButton()
7+
8+
samples = [
9+
bhb.load_sample("samples/clap.wav"),
10+
bhb.load_sample("samples/dist.wav"),
11+
bhb.load_sample("samples/go.wav"),
12+
bhb.load_sample("samples/honk.wav"),
13+
bhb.load_sample("samples/kick.wav"),
14+
bhb.load_sample("samples/reverse.wav"),
15+
bhb.load_sample("samples/snare.wav"),
16+
]
17+
18+
while True:
19+
if bhb.triggered:
20+
# This selects a sample from the list based on the pitch CV input.
21+
# The samples are evenly distributed across the CV range,
22+
# -5v to +5v. So if you send -5v it'll select the first sample and
23+
# if you send +5v it'll select the last one, with voltages in-between
24+
# will select the closest sample in the list.
25+
sample = bhb.select_from_list_using_cv(samples, bhb.pitch_in)
26+
27+
# By default, this uses the full CV range but you can change it by
28+
# specifying low and high - useful if you only have a positive CV
29+
# source.
30+
31+
# sample = bhb.select_from_list_using_cv(samples, bhb.pitch_in, low=0, high=5)
32+
33+
bhb.play(sample)
34+
bhb.gate_out = True
35+
36+
if bhb.released:
37+
bhb.gate_out = False

firmware/winterbloom_bhb/bhb.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ def __init__(self):
7171
self._pitch_in.direct_calibration(
7272
{4068: -5.0, 3049: -2.5, 2025: 0, 1001: 2.5, 8: 5.0}
7373
)
74+
self.min_cv = -5.0
75+
self.max_cv = 5.0
7476
else:
7577
self._pitch_in.direct_calibration(
7678
{4068: -2.0, 3049: -1.0, 2025: 0, 1001: 1.0, 8: 2.0}
7779
)
80+
self.min_cv = -2.0
81+
self.max_cv = 2.0
7882

7983
self.audio_out = audioio.AudioOut(board.HONK_OUT)
8084

@@ -141,3 +145,16 @@ def play(self, sample, pitch_cv=None, loop=False):
141145

142146
def stop(self):
143147
self.audio_out.stop()
148+
149+
def select_from_list_using_cv(self, list, cv, low=None, high=None):
150+
if low is None:
151+
low = self.min_cv
152+
if high is None:
153+
high = self.max_cv
154+
155+
cv = min(high, max(low, cv))
156+
count = len(list)
157+
span = high - low
158+
value = cv - low
159+
index = int((value / span) * count)
160+
return list[index]

user_guide/docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Big Honking Button can do all sorts of things! We've made a few examples to get
101101

102102
1. [Cycle example](https://github.com/wntrblm/Big_Honking_Button/blob/main/examples/cycle.py): Shows how to load multiple samples and cycle between them.
103103
1. [Random example](https://github.com/wntrblm/Big_Honking_Button/blob/main/examples/random.py): Shows how to load multiple samples and choose one at random.
104+
1. [CV select example](https://github.com/wntrblm/Big_Honking_Button/blob/main/examples/cv_select.py): Shows how to load multiple samples and use the CV input to select which one to play.
104105
1. [Tap tempo example](https://github.com/wntrblm/Big_Honking_Button/blob/main/examples/tap_tempo.py): Shows how to use the button to set the tempo and have the module play back a sample at each beat.
105106
1. [Sine example](https://github.com/wntrblm/Big_Honking_Button/blob/main/examples/sine.py): An advanced example that shows how to generate a custom waveform.
106107
1. [Noise example](https://github.com/wntrblm/Big_Honking_Button/blob/main/examples/noise.py): An advanced example that shows how to generate noise.

0 commit comments

Comments
 (0)