fusion_hat.music module
Music
This module provides a class for playing music, sound affect and note control.
Example
Import the module and create an instance
>>> from fusion_hat.music import Music
>>> music = Music()
Play a music file
>>> music.music_play("music.wav")
Play music in a thread
>>> music_thread = threading.Thread(target=music.music_play, args=("music.wav",))
>>> music_thread.start()
Control the music
>>> music.music_pause()
>>> music.music_resume()
>>> music.music_stop()
Play a sound file
>>> music.sound_play("sound.wav")
Play a sound file in a thread
>>> music.sound_play_thread("sound.wav")
- class fusion_hat.music.Music[source]
Bases:
objectPlay music, sound affect and note control
- CHANNELS = 1
- RATE = 44100
- KEY_G_MAJOR = 1
- KEY_D_MAJOR = 2
- KEY_A_MAJOR = 3
- KEY_E_MAJOR = 4
- KEY_B_MAJOR = 5
- KEY_F_SHARP_MAJOR = 6
- KEY_C_SHARP_MAJOR = 7
- KEY_F_MAJOR = -1
- KEY_B_FLAT_MAJOR = -2
- KEY_E_FLAT_MAJOR = -3
- KEY_A_FLAT_MAJOR = -4
- KEY_D_FLAT_MAJOR = -5
- KEY_G_FLAT_MAJOR = -6
- KEY_C_FLAT_MAJOR = -7
- KEY_SIGNATURE_SHARP = 1
- KEY_SIGNATURE_FLAT = -1
- WHOLE_NOTE = 1
- HALF_NOTE = 0.5
- QUARTER_NOTE = 0.25
- EIGHTH_NOTE = 0.125
- SIXTEENTH_NOTE = 0.0625
- NOTE_BASE_FREQ = 440
Base note frequency for calculation (A4)
- NOTE_BASE_INDEX = 69
Base note index for calculation (A4) MIDI compatible
- NOTES = [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, 'A0', 'A#0', 'B0', 'C1', 'C#1', 'D1', 'D#1', 'E1', 'F1', 'F#1', 'G1', 'G#1', 'A1', 'A#1', 'B1', 'C2', 'C#2', 'D2', 'D#2', 'E2', 'F2', 'F#2', 'G2', 'G#2', 'A2', 'A#2', 'B2', 'C3', 'C#3', 'D3', 'D#3', 'E3', 'F3', 'F#3', 'G3', 'G#3', 'A3', 'A#3', 'B3', 'C4', 'C#4', 'D4', 'D#4', 'E4', 'F4', 'F#4', 'G4', 'G#4', 'A4', 'A#4', 'B4', 'C5', 'C#5', 'D5', 'D#5', 'E5', 'F5', 'F#5', 'G5', 'G#5', 'A5', 'A#5', 'B5', 'C6', 'C#6', 'D6', 'D#6', 'E6', 'F6', 'F#6', 'G6', 'G#6', 'A6', 'A#6', 'B6', 'C7', 'C#7', 'D7', 'D#7', 'E7', 'F7', 'F#7', 'G7', 'G#7', 'A7', 'A#7', 'B7', 'C8']
Notes name, MIDI compatible
- time_signature(top: int = None, bottom: int = None) tuple[source]
Set/get time signature
- Parameters:
top (int, optional) – top number of time signature. Defaults to None.
bottom (int, optional) – bottom number of time signature. Defaults to None.
- Returns:
time signature
- Return type:
tuple
- key_signature(key: int = None) int[source]
Set/get key signature
- Parameters:
key (int, optional) – key signature use KEY_XX_MAJOR or String “#”, “##”, or “bbb”, “bbbb”. Defaults to None.
- Returns:
key signature
- Return type:
int
- tempo(tempo: int = None, note_value: float = 0.25) tuple[source]
Set/get tempo beat per minute(bpm)
- Parameters:
tempo (int, optional) – tempo. Defaults to None.
note_value (float, optional) – note value(1, 1/2, Music.HALF_NOTE, etc). Defaults to QUARTER_NOTE.
- Returns:
tempo
- Return type:
tuple
- beat(beat: float) float[source]
Calculate beat delay in seconds from tempo
- Parameters:
beat (float) – beat index
- Returns:
beat delay
- Return type:
float
- note(note: str, natural: bool = False) float[source]
Get frequency of a note
- Parameters:
note (str) – note name(See NOTES)
natural (bool, optional) – if natural note. Defaults to False.
- Returns:
frequency of note
- Return type:
float
- sound_play(filename: str, volume: int = None) None[source]
Play sound effect file
- Parameters:
filename (str) – sound effect file name
volume (int, optional) – volume 0-100, leave empty will not change volume. Defaults to None.
- sound_play_threading(filename: str, volume: int = None) None[source]
Play sound effect in thread(in the background)
- Parameters:
filename (str) – sound effect file name
volume (int, optional) – volume 0-100, leave empty will not change volume. Defaults to None.
- music_play(filename: str, loops: int = 1, start: float = 0.0, volume: int = None) None[source]
Play music file
- Parameters:
filename (str) – sound file name
loops (int, optional) – number of loops, 0:loop forever, 1:play once, 2:play twice, … Defaults to 1.
start (float, optional) – start time in seconds. Defaults to 0.0.
volume (int, optional) – volume 0-100, leave empty will not change volume. Defaults to None.
- sound_length(filename: str) float[source]
Get sound effect length in seconds
- Parameters:
filename (str) – sound effect file name
- Returns:
length in seconds
- Return type:
float
- get_tone_data(freq: float, duration: float) list[source]
Get tone data for playing
Credit to: Aditya Shankar & Gringo Suave https://stackoverflow.com/a/53231212/14827323
- Parameters:
freq (float) – frequency
duration (float) – duration in seconds
- Returns:
tone data
- Return type:
list
- play_tone_for(freq: float, duration: float) None[source]
Play tone for duration seconds Credit to: Aditya Shankar & Gringo Suave https://stackoverflow.com/a/53231212/14827323
- Parameters:
freq (float) – frequency, you can use NOTES to get frequency
duration (float) – duration in seconds