Module micromelon.sounds
Functions to control the onboard buzzer to play tunes or specific frequencies.
Also helpful definitions for musical notes to their frequency and aliases for different preprogrammed tunes.
Expand source code
"""
Functions to control the onboard buzzer to play tunes or specific frequencies.
Also helpful definitions for musical notes to their frequency and aliases for different preprogrammed tunes.
"""
from ._sounds import *
from ._notes import *
__all__ = ["playNote", "play", "off", "NOTES", "TUNES"]
Functions
def off()
-
Turns the buzzer off (sets frequency to zero Hz)
Returns
None
Expand source code
def off(): """ Turns the buzzer off (sets frequency to zero Hz) Returns: None """ return _rc.writeAttribute(OPTYPE.BUZZER_FREQ, [0])
def play(id)
-
Starts playing a preset tune on the robot
Some preset ids can be found in the TUNES dictionary
Will return immediately and the tune will play until finished or stopped with another commandArgs
id
:number
orenum
- the tune / song to play
Returns
None
Expand source code
def play(id): """ Starts playing a preset tune on the robot Some preset ids can be found in the TUNES dictionary Will return immediately and the tune will play until finished or stopped with another command Args: id (number or enum): the tune / song to play Returns: None """ # Allow use of TUNES enum elements as arguments if isinstance(id, Enum): id = id.value return _rc.writeAttribute(OPTYPE.BUZZER_TUNE, [id])
def playNote(freq, secs=None)
-
Sets the buzzer to the given frequency in Hz
If secs argument is provided, the function will block until that number of seconds has elapsedArgs
freq
:positive number
orenum
- frequency to play on the buzzer
secs
:positive number
- optional number of seconds to wait after setting the frequency
Raises
Exception on onvalid arguments
Returns
None
Expand source code
def playNote(freq, secs=None): """ Sets the buzzer to the given frequency in Hz If secs argument is provided, the function will block until that number of seconds has elapsed Args: freq (positive number or enum): frequency to play on the buzzer secs (positive number): optional number of seconds to wait after setting the frequency Raises: Exception on onvalid arguments Returns: None """ # Allow use of NOTES enum as frequency arguments if isinstance(freq, Enum): freq = freq.value if not isNumber(freq) or freq < 0: raise Exception("Note frequency must be a positive number") if secs: secs = restrictTime(secs) # TODO: Is ceil really the right thing to do here? freq = math.ceil(freq) littleEndianFreq = [round(freq) & 0xFF, (round(freq) >> 8) & 0xFF] result = _rc.writeAttribute(OPTYPE.BUZZER_FREQ, littleEndianFreq) if secs: time.sleep(secs) return result
Classes
class NOTES (value, names=None, *, module=None, qualname=None, type=None, start=1)
-
A collection of standard musical note frequencies
As '#' is not valid in a variable name 's' is used instead
Example: F sharp in the 6th octave would be NOTES.Fs6
B flat in the 4th octave would be NOTES.Bb4Ancestors
- enum.Enum
Class variables
var A0
var A1
var A2
var A3
var A4
var A5
var A6
var A7
var A8
var AsBb0
var AsBb1
var AsBb2
var AsBb3
var AsBb4
var AsBb5
var AsBb6
var AsBb7
var AsBb8
var B0
var B1
var B2
var B3
var B4
var B5
var B6
var B7
var B8
var C0
var C1
var C2
var C3
var C4
var C5
var C6
var C7
var C8
var CsDb0
var CsDb1
var CsDb2
var CsDb3
var CsDb4
var CsDb5
var CsDb6
var CsDb7
var CsDb8
var D0
var D1
var D2
var D3
var D4
var D5
var D6
var D7
var D8
var DsEb0
var DsEb1
var DsEb2
var DsEb3
var DsEb4
var DsEb5
var DsEb6
var DsEb7
var DsEb8
var E0
var E1
var E2
var E3
var E4
var E5
var E6
var E7
var E8
var F0
var F1
var F2
var F3
var F4
var F5
var F6
var F7
var F8
var FsGb0
var FsGb1
var FsGb2
var FsGb3
var FsGb4
var FsGb5
var FsGb6
var FsGb7
var FsGb8
var G0
var G1
var G2
var G3
var G4
var G5
var G6
var G7
var G8
var GsAb0
var GsAb1
var GsAb2
var GsAb3
var GsAb4
var GsAb5
var GsAb6
var GsAb7
var GsAb8
class TUNES (value, names=None, *, module=None, qualname=None, type=None, start=1)
-
A collection of pre-programmed tunes the robot can play
TUNES.UP = 1
TUNES.DOWN = 2Expand source code
class TUNES(Enum): """ A collection of pre-programmed tunes the robot can play TUNES.UP = 1 TUNES.DOWN = 2 """ UP = 1 DOWN = 2
Ancestors
- enum.Enum
Class variables
var DOWN
var UP