Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve experience when system does not have PortAudio installed yet #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The following software must be installed for `pymusiclooper` to function correct
Supported audio formats *without* ffmpeg include: WAV, FLAC, Ogg/Vorbis, Ogg/Opus, MP3.
A full list can be found at [libsndfile's supported formats page](https://libsndfile.github.io/libsndfile/formats.html)

Additionally, to use the `play` command on Linux systems, you may need to
install the PortAudio library. On Ubuntu, run `sudo apt install libportaudio2`.

## Installation

### Option 1: Installing using pipx [Recommended]
Expand Down
16 changes: 11 additions & 5 deletions pymusiclooper/playback.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""Module for playback through the terminal"""
import importlib
import logging
import signal
import threading

import numpy as np
import lazy_loader as lazy
from rich.progress import BarColumn, Progress, TextColumn

from pymusiclooper.console import rich_console

# Lazy-load sounddevice
sd = lazy.load("sounddevice")
# Lazy-load sounddevice: call `sd()` to return the module.
# (We use this instead of `lazy-loader`, to get clearer error messages when
# sounddevice is missing dependencies like PortAudio.)
_sd = None
def sd():
global _sd
_sd = _sd or importlib.import_module("sounddevice")
return _sd

class PlaybackHandler:
"""Handler class for initiating looping playback through the terminal."""
Expand Down Expand Up @@ -93,9 +99,9 @@ def callback(outdata, frames, time, status):
self.current_frame += chunksize
if chunksize < frames:
outdata[chunksize:] = 0
raise sd.CallbackStop()
raise sd().CallbackStop()

self.stream = sd.OutputStream(
self.stream = sd().OutputStream(
samplerate=samplerate,
channels=n_channels,
callback=callback,
Expand Down