Part of the series

Several example codes

~1 min read • Updated Sep 22, 2025

Program Overview

This Python program allows free playback of a musical instrument (such as a digital piano or guitar) on a device.
Using audio libraries, the selected instrument sound is played, and the user can hear it through speakers or headphones.


Python Code (Example using winsound for Windows):


import winsound

# Function to play a simple note (e.g., instrument sound)
def play_note(frequency=440, duration=500):
    winsound.Beep(frequency, duration)

# Play a sequence of notes
play_note(440, 500)  # Note A
play_note(494, 500)  # Note B
play_note(523, 500)  # Note C

Sample Output:


Plays three consecutive notes with different frequencies  
User hears the instrument sound through the device's speaker or headphones

Explanation:

- The winsound.Beep() function generates sound at a specified frequency
- Each note is played for a defined duration
- This program runs on devices that support audio playback


Written & researched by Dr. Shahin Siami