Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Jan Kruse
potify
Commits
cc75c7b6
Commit
cc75c7b6
authored
Sep 11, 2017
by
Andre Julius
👀
Browse files
Restructure player to be threaded, polling data from the song queue
parent
69b39b8e
Changes
1
Hide whitespace changes
Inline
Side-by-side
api/player.py
View file @
cc75c7b6
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
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment