Skip to content

Instantly share code, notes, and snippets.

@epoz
Created August 3, 2024 10:32
Show Gist options
  • Save epoz/7934c9f24ccf8e4d7f14587897f2bc7a to your computer and use it in GitHub Desktop.
Save epoz/7934c9f24ccf8e4d7f14587897f2bc7a to your computer and use it in GitHub Desktop.
Simple MIDI .wav Python player for FGDP-50 Hang Drum fun
import mido, os, random
import simpleaudio as sa
INPUT = "./Hang Drum Samples"
wavs = {}
for x in os.listdir(INPUT):
if x.endswith(".wav") and len(x) == 7:
wavs.setdefault(x[0], []).append(x)
note_map = {
36: "A",
37: "B",
38: "C",
40: "D",
42: "E",
43: "F",
46: "G",
47: "H",
48: "A",
49: "B",
51: "C",
53: "D",
55: "E",
57: "F",
60: "G",
61: "H",
62: "A",
63: "B",
64: "C",
65: "D",
66: "E",
67: "F",
}
with mido.open_input() as inport:
for msg in inport:
if msg.type == "clock":
continue
try:
if msg.velocity == 0:
continue
except:
continue
wav = wavs.get(note_map.get(msg.note))
if wav:
norm_velocity = msg.velocity / 127
choice = int(norm_velocity * len(wav))
wavv = wav[choice - 1]
sound = sa.WaveObject.from_wave_file(os.path.join(INPUT, wavv))
sound.play()
else:
print("No sound for note", msg.note)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment