P-unit_model/my_util/Sounds.py
2021-01-09 23:59:34 +01:00

40 lines
861 B
Python

from pyaudio import PyAudio
import numpy as np
BITRATE = 20000
LENGTH = 0.5
def play_finished_sound():
global BITRATE
global LENGTH
frequency = 261
num_of_frames = int(BITRATE*LENGTH)
frames = np.arange(0, num_of_frames, 1)
wave_data_numeric = np.sin(frames / ((BITRATE / frequency) / np.pi)) * 127 + 128
wave_data_numeric = wave_data_numeric.astype(int)
wave_data_chr = "".join([chr(x) for x in wave_data_numeric])
rest_frames = num_of_frames % BITRATE
rest = [chr(128)]*rest_frames
wave_data_chr.join(rest)
p = PyAudio()
stream = p.open(
format=p.get_format_from_width(1),
channels=1,
rate=BITRATE,
output=True,
)
stream.write(wave_data_chr)
stream.stop_stream()
stream.close()
p.terminate()
if __name__ == '__main__':
play_finished_sound()