Skip to content

Commit cc8303f

Browse files
committed
Pass sample object and use default sample level
1 parent 18bab8e commit cc8303f

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

circuitpython/perc/code.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
# Settings
1919

20+
LEVEL = 0.25 # Use `SAMPLES[notenum]["level"]` to override
21+
2022
## Velocity is 0->127
2123
HARD = 100
2224
SOFT = 0 # Adjust to act as gate
@@ -27,67 +29,59 @@
2729
HARD: "kick_hard",
2830
SOFT: "kick_soft"
2931
},
30-
"level": 0.25
32+
"level": 0.3
3133
},
3234
38: {
3335
"samples": {
3436
HARD: "snare_hard",
3537
SOFT: "snare_soft"
3638
},
37-
"level": 0.25
39+
"level": 0.3
3840
},
3941
41: {
4042
"samples": {
4143
HARD: "tom_lo_hard",
4244
SOFT: "tom_lo_soft"
43-
},
44-
"level": 0.25
45+
}
4546
},
4647
42: {
4748
"samples": {
4849
SOFT: "hihat_closed"
49-
},
50-
"level": 0.25
50+
}
5151
},
5252
43: {
5353
"samples": {
5454
HARD: "tom_mid_hard",
5555
SOFT: "tom_mid_soft"
56-
},
57-
"level": 0.25
56+
}
5857
},
5958
44: {
6059
"samples": {
6160
SOFT: "hihat_pedal"
62-
},
63-
"level": 0.25
61+
}
6462
},
6563
45: {
6664
"samples": {
6765
HARD: "tom_hi_hard",
6866
SOFT: "tom_hi_soft"
69-
},
70-
"level": 0.25
67+
}
7168
},
7269
46: {
7370
"samples": {
7471
SOFT: "hihat_open"
75-
},
76-
"level": 0.25
72+
}
7773
},
7874
49: {
7975
"samples": {
8076
HARD: "crash_hard",
8177
SOFT: "crash_soft"
82-
},
83-
"level": 0.25
78+
}
8479
},
8580
51: {
8681
"samples": {
8782
HARD: "ride_hard",
8883
SOFT: "ride_soft"
89-
},
90-
"level": 0.25
84+
}
9185
}
9286
}
9387

@@ -111,23 +105,29 @@
111105
mixer.voice[i].play(wav)
112106
mixer.voice[i].level = 0.0
113107

114-
def get_sample(notenum:int, velocity:int = 127) -> tuple[str, float]:
108+
def get_sample(notenum:int, velocity:int = 127) -> tuple[str, float]|None:
115109
if notenum in SAMPLES:
116-
for vel, name in SAMPLES[notenum]["samples"].items():
110+
sample = SAMPLES[notenum].copy()
111+
for vel, name in sample["samples"].items():
117112
if velocity >= vel:
118-
return (name, SAMPLES[notenum]["level"])
113+
sample["name"] = name
114+
del sample["samples"]
115+
return sample
119116
return None
120117

121-
def play_sample(name:str, level:float = 0.25) -> None:
118+
def get_sample_index(name:str) -> int|None:
119+
try:
120+
return list(samples.keys()).index(name)
121+
except ValueError:
122+
return None
123+
124+
def play_sample(sample:dict) -> None:
122125
global samples
123-
if name in samples:
124-
i = 0
125-
for val, wav in samples.items():
126-
if val == name:
127-
mixer.voice[i].level = level
128-
mixer.voice[i].play(wav)
129-
break
130-
i += 1
126+
if "name" in sample and sample["name"] in samples:
127+
i = get_sample_index(sample["name"])
128+
wav = samples[sample["name"]]
129+
mixer.voice[i].level = sample.get("level", LEVEL)
130+
mixer.voice[i].play(wav)
131131

132132
def stop_sample(name:str) -> None:
133133
global samples
@@ -142,10 +142,9 @@ def stop_sample(name:str) -> None:
142142
async def touch_handler():
143143
while True:
144144
for event in hardware.check_touch():
145-
if (notenum := event.key_number + 36) in SAMPLES:
146-
name, level = get_sample(notenum)
145+
if (notenum := event.key_number + 36) in SAMPLES and (sample := get_sample(notenum)):
147146
if event.pressed:
148-
play_sample(name, level)
147+
play_sample(sample)
149148
elif event.released:
150149
pass
151150
# stop_sample(name)
@@ -160,10 +159,9 @@ async def midi_handler():
160159
while True:
161160
for midi in (hardware.midi_uart, hardware.midi_usb):
162161
msg = midi.receive()
163-
if isinstance(msg, (NoteOn, NoteOff)) and msg.note in SAMPLES:
164-
name, level = get_sample(msg.note, msg.velocity)
162+
if isinstance(msg, (NoteOn, NoteOff)) and msg.note in SAMPLES and (sample := get_sample(msg.note, msg.velocity)):
165163
if isinstance(msg, NoteOn) and msg.velocity != 0:
166-
play_sample(name, level)
164+
play_sample(sample)
167165
elif isinstance(msg, NoteOff) or (isinstance(msg, NoteOn) and msg.velocity == 0):
168166
pass
169167
# stop_sample(name)

0 commit comments

Comments
 (0)