Commit cc75c7b6 authored by Andre Julius's avatar Andre Julius 👀
Browse files

Restructure player to be threaded, polling data from the song queue

parent 69b39b8e
import wave
import pyaudio
import api.index
import api.queue
import threading
import logging
from config import datadir
def play(filename):
wave_file = wave.open(datadir + "/" + filename)
p = pyaudio.PyAudio()
chunk = 1024
stream = p.open(
format=p.get_format_from_width(
wave_file.getsampwidth()
),
channels=wave_file.getnchannels(),
rate=wave_file.getframerate(),
output=True
)
data = wave_file.readframes(chunk)
while len(data) > 0:
stream.write(data)
class Player(threading.Thread):
"""docstring for Player"""
def __init__(self):
super().__init__(target=self)
self._logger = logging.getLogger("player")
def play(self, filename):
self._logger.info("start playing {}".format(filename))
wave_file = wave.open(datadir + "/" + filename)
p = pyaudio.PyAudio()
chunk = 1024
stream = p.open(
format=p.get_format_from_width(
wave_file.getsampwidth()
),
channels=wave_file.getnchannels(),
rate=wave_file.getframerate(),
output=True
)
data = wave_file.readframes(chunk)
while len(data) > 0:
stream.write(data)
data = wave_file.readframes(chunk)
def run(self):
self._logger.info("start polling song queue")
while True:
filename = api.queue.q.get()
self.play(filename)
player = Player()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment