Skip to content

Instantly share code, notes, and snippets.

@tinkerstudent
Last active August 28, 2016 20:31
Show Gist options
  • Select an option

  • Save tinkerstudent/c663e9c984cb7a43d211d0bef6e9cf7c to your computer and use it in GitHub Desktop.

Select an option

Save tinkerstudent/c663e9c984cb7a43d211d0bef6e9cf7c to your computer and use it in GitHub Desktop.
import time
import os
import pyaudio
import wave
import os
import platform
import time
twinkle = ['c', 'c', 'g', 'g', 'a', 'a', 'g', '<pause>',
'f', 'f', 'e', 'e', 'd', 'd', 'c', '<pause>',
'g', 'g', 'f', 'f', 'e', 'e', 'd', '<pause>',
'g', 'g', 'f', 'f', 'e', 'e', 'd', '<pause>',
'c', 'c', 'g', 'g', 'a', 'a', 'g', '<pause>',
'f', 'f', 'e', 'e', 'd', 'd', 'c']
def find_key(key):
wav = key + ".wav"
path_to_user = os.path.expanduser('~')
path_to_key = os.path.join(path_to_user, 'Desktop', 'keys', wav)
return path_to_key
def play_note(key):
chunk = 1024
path_to_key_file = find_key(key)
f = wave.open(path_to_key_file,"rb")
p = pyaudio.PyAudio()
s = p.open(format = p.get_format_from_width(f.getsampwidth()),
channels = f.getnchannels(),
rate = f.getframerate(),
output = True)
d = f.readframes(chunk)
while d != '':
s.write(d)
d = f.readframes(chunk)
s.stop_stream()
s.close()
p.terminate()
def play_music():
for note in twinkle:
# add an IF condition below that checks to see if the note is '<pause>'
if note == '<pause>':
time.sleep(0.65)
else:
play_note(note)
play_music()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment