Created
April 14, 2022 18:24
-
-
Save vsinha/2a3c2cd5796a15d85a1e8ff304455c32 to your computer and use it in GitHub Desktop.
Git gud at guitar. Speaks a random note so you can practice finding the note by name. Ideally put this script in its own folder and run it from there, as it uses the filesystem to store the created note MP3 files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import time | |
from gtts import gTTS | |
import os | |
sleep_sec = 0.25 | |
natural_notes = ["A", "B", "C", "D", "E", "F", "G"] | |
prev = None | |
def filename(note): | |
return note + ".mp3" | |
def generate_file(note): | |
obj = gTTS(note, lang="en", slow=False) | |
obj.save(filename(note)) | |
def pick_random(notes): | |
global prev | |
note = random.choice(notes) | |
if prev is not None and prev == note: | |
# Reduce the chance of repeats | |
note = random.choice(notes) | |
prev = note | |
return note | |
def speak(note): | |
print(note) | |
file = filename(note) | |
if not os.path.exists(file): | |
generate_file(note) | |
os.system("mpg321 --quiet " + file) | |
while True: | |
note = pick_random(natural_notes) | |
speak(note) | |
time.sleep(sleep_sec) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment