- commit
- e6ccffa152ccb565022f03b1df6b187d3a0de026
- parent
- 36d3de7cb8f5b45f426c777eee7f791fd1a5b25b
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2021-07-04 09:35
replace alsaaudio by soundcard
Diffstat
| M | README.md | 4 | ---- |
| M | player.py | 25 | ++++++++----------------- |
2 files changed, 8 insertions, 21 deletions
diff --git a/README.md b/README.md
@@ -28,10 +28,6 @@ Any help would be appreciated 28 28 possible, e.g. to prevent the song from looping through the same part again 29 29 and again. 30 3031 -1 - **Better audio output.** alsaaudio was the first library I got to work, but32 -1 it has many flaws. It blocks a lot and it is impossible to know which beat33 -1 is currently playing.34 -135 31 - **Visual Feedback.** The original infinite jukebox has a great UI. This one 36 32 has currently none. 37 33
diff --git a/player.py b/player.py
@@ -5,12 +5,11 @@ import argparse 5 5 import gzip 6 6 import os 7 7 import pickle8 -1 import struct9 8 10 9 from scipy.misc import imresize11 -1 import alsaaudio12 10 import librosa 13 11 import numpy -1 12 import soundcard 14 13 15 14 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) 16 15 @@ -80,10 +79,7 @@ def compute_buffers(y, beat_frames): 80 79 81 80 buffers = [] 82 81 for start, end in iter_beat_slices(raw, beat_frames):83 -1 samples = raw[start:end]84 -1 data = struct.pack("h" * len(samples), *samples)85 -1 duration = librosa.samples_to_time(end - start)86 -1 buffers.append((data, duration))-1 82 buffers.append(y.T[start:end]) 87 83 88 84 return buffers 89 85 @@ -128,21 +124,16 @@ def get_next_position(i, jumps): 128 124 129 125 130 126 def play(buffers, sample_rate, jumps):131 -1 # https://larsimmisch.github.io/pyalsaaudio/libalsaaudio.html#pcm-objects132 -1 pcm = alsaaudio.PCM()133 -1 pcm.setrate(sample_rate)134 -1 pcm.setchannels(1)135 -1136 127 i = 0 137 128 n = len(buffers) 138 129139 -1 while True:140 -1 data, duration = buffers[i]141 -1 pcm.write(data)-1 130 with soundcard.default_speaker().player(samplerate=sample_rate) as sp: -1 131 while True: -1 132 sp.play(buffers[i]) 142 133143 -1 i = get_next_position(i, jumps)144 -1 if i >= n:145 -1 i = 0-1 134 i = get_next_position(i, jumps) -1 135 if i >= n: -1 136 i = 0 146 137 147 138 148 139 def parse_args():