-
Notifications
You must be signed in to change notification settings - Fork 3
/
Beep.py
99 lines (84 loc) · 2.54 KB
/
Beep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Class adapted from
'THeK3nger' original github upload
to include a "speed" functionality
for the sound played, a blip
for TamuHack 2020 by Jack Wang
"""
import os
import wave
import threading
import sys
# PyAudio Library
import pyaudio
import time
# Pydub for .mp3 to .wav
import pydub
from pydub import AudioSegment
class WavePlayerLoop(threading.Thread):
"""
A simple class based on PyAudio to play wave loop.
It's a threading class. You can play audio while your application
continues to do its stuff. :)
"""
CHUNK = 1024
def __init__(self, filepath, loop=True):
"""
Initialize `WavePlayerLoop` class.
PARAM:
-- filepath (String) : File Path to wave file.
-- loop (boolean) : True if you want loop playback.
False otherwise.
"""
super(WavePlayerLoop, self).__init__()
if ".mp3" in os.path.abspath(filepath):
self.mp3_to_wav(os.path.abspath(filepath))
self.filepath = os.path.abspath(filepath[:-4]+".wav")
self.loop = loop
self.speed = 1.0
def run(self):
# Open Wave File and start play!
wf = wave.open(self.filepath, 'rb')
player = pyaudio.PyAudio()
# Open Output Stream (basen on PyAudio tutorial)
stream = player.open(format=player.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
# PLAYBACK LOOP
data = wf.readframes(self.CHUNK)
# lock = threading.Lock()
# cv = threading.Condition(lock)
# cv.acquire()
while self.loop:
stream.write(data)
data = wf.readframes(self.CHUNK)
if data == b'': # If file is over then rewind.
wf.rewind()
data = wf.readframes(self.CHUNK)
time.sleep(.75*self.speed)
# cv.wait(timeout=.75 *self.speed)
stream.close()
player.terminate()
def play(self):
"""
Just another name for self.start()
"""
self.start()
def stop(self):
"""
Stop playback.
"""
self.loop = False
def change_speed(self, speed):
""""
changes speed
"""
self.speed = speed
def mp3_to_wav(self, file):
"""
Changes .mp3 file to .wav file
"""
sound = AudioSegment.from_mp3(file)
file = file[:-4]
sound.export(file+".wav", format="wav")