mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 09:37:25 +00:00
88 lines
2.4 KiB
Python
88 lines
2.4 KiB
Python
"""Generic speaker module for playing sounds (e.g. EV3 speaker, Bluetooth speaker, or IDE speaker)."""
|
|
|
|
|
|
class Speaker():
|
|
"""Play beeps and sound files using a speaker."""
|
|
|
|
def __init__(self, device_type):
|
|
"""Device specific speaker initialization."""
|
|
pass
|
|
|
|
def beep(self, frequency=500, duration=100, volume=30):
|
|
"""Play a beep/tone.
|
|
|
|
Arguments:
|
|
frequency (:ref:`frequency`): Frequency of the beep (*Default*: 500).
|
|
duration (:ref:`time`): Duration of the beep (*Default*: 100).
|
|
volume (:ref:`percentage`): Volume of the beep (*Default*: 30).
|
|
::
|
|
|
|
# A simple beep
|
|
brick.sound.beep()
|
|
|
|
# A high pitch (1500 Hz) for one second (1000 ms) at 50% volume
|
|
brick.sound.beep(1500, 1000, 50)
|
|
"""
|
|
pass
|
|
|
|
def beeps(self, number):
|
|
"""Play a number of default beeps with a brief pause in between.
|
|
|
|
Arguments:
|
|
number (int): Number of beeps
|
|
|
|
::
|
|
|
|
# Make 5 simple beeps
|
|
brick.sound.beeps(5)
|
|
"""
|
|
pass
|
|
|
|
def tune(self, frequencies_and_durations, volume=30):
|
|
"""Play a tune composed of beeps.
|
|
|
|
Arguments:
|
|
frequencies_and_durations (list): List of (:ref:`frequency`, :ref:`time`) pairs
|
|
volume (:ref:`percentage`): Volume of the tune (*Default*: 30).
|
|
|
|
::
|
|
|
|
# Play a tune of three notes with increasing
|
|
# pitch (500 Hz, 1000 Hz, 1500 Hz), each 200 ms.
|
|
my_tune = [(500, 200), (1000, 200), (1500, 200)]
|
|
brick.sound.tune(my_tune)
|
|
"""
|
|
pass
|
|
|
|
def file(self, file_name, volume=100):
|
|
"""Play a sound file.
|
|
|
|
Arguments:
|
|
file_name (str): Path to the sound file, including extension.
|
|
volume (:ref:`percentage`): Volume of the sound (*Default*: 100).
|
|
|
|
::
|
|
|
|
# Play one of the built-in sounds
|
|
brick.sound.file(Sound.hello)
|
|
|
|
# Play a sound file from your project folder
|
|
brick.sound.file('mysound.wav')
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
def speak(self, text, volume=30):
|
|
"""Speak a given string of text.
|
|
|
|
Arguments:
|
|
text (str): The text to speak.
|
|
volume (:ref:`percentage`): Volume of the sound (*Default*: 100).
|
|
::
|
|
|
|
# Say "Hi!"
|
|
brick.sound.speak('Hi!')
|
|
"""
|
|
pass
|