Created
February 1, 2020 04:25
-
-
Save Roxiun/f5f587cea622740cc808fe4fde791e5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyaudio | |
import wave | |
import sys | |
from playsound import playsound | |
chunk = 1024 | |
class AudioFile: | |
chunk = 1024 | |
def __init__(self, file): | |
""" Init audio stream """ | |
self.wf = wave.open(file, 'rb') | |
self.p = pyaudio.PyAudio() | |
self.stream = self.p.open( | |
format = self.p.get_format_from_width(self.wf.getsampwidth()), | |
channels = self.wf.getnchannels(), | |
rate = self.wf.getframerate(), | |
output = True | |
) | |
def play(self): | |
""" Play entire file """ | |
data = self.wf.readframes(self.chunk) | |
while data != '': | |
self.stream.write(data) | |
data = self.wf.readframes(self.chunk) | |
def close(self): | |
""" Graceful shutdown """ | |
self.stream.close() | |
self.p.terminate() | |
# Usage example for pyaudio | |
name = 'location_goes_here' | |
s = Sound() | |
s.read(name) | |
s.play() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment