Playing audio

To begin with, we have added 24 sound samples in .wav format in a folder named sounds in this chapter's code folder. These audio files correspond to the 24 notes on our keyboard. The audio files are named according to the note name it represents.

In order to keep the audio processing separate from the GUI code, we create a new file called audio.py (7.03). The code is defined as follows:

import simpleaudio as safrom _thread import start_new_threadimport timedef play_note(note_name): wave_obj = sa.WaveObject.from_wave_file('sounds/' + note_name + '.wav') wave_obj.play()def play_scale(scale): for note in scale:   play_note(note)   time.sleep(0.5)def play_scale_in_new_thread(scale):  start_new_thread(play_scale,(scale,))def play_chord(scale): ...

Get Tkinter GUI Application Development Blueprints - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.