mixmastamyk / boombox

A simple cross-platform audio-file player module for Python.
GNU Lesser General Public License v3.0
12 stars 0 forks source link

Boom Box

::

             ┏┓ ┏━┓┏━┓┏┳┓┏┓ ┏━┓╻ ╻
    ╺━╸╺━╸   ┣┻┓┃ ┃┃ ┃┃┃┃┣┻┓┃ ┃┏╋┛   ╺━╸╺━╸
       ╺━╸   ┗━┛┗━┛┗━┛╹ ╹┗━┛┗━┛╹ ╹   ╺━╸

               ╭══════════════╮
╭───────────┬──┴──────────────┴──┬───────────╮
│(0) ___    │ ⯀  [:::::::] [] ( )│    ___ (0)│
│  /:::::\  │────────────────────│  /:::::\  │
│ |:::::::| │  │   ──    ──   │  │ |:::::::| │
│ |:::::::| │  │ ─(⚙)────(⚙)─ │  │ |:::::::| │
│  \:::::/  │  │────▯▯▯▯▯▯────│♫ │  \:::::/  │
╰────────────────────────────────┴───────────╯

.. image:: https://raw.githubusercontent.com/mixmastamyk/boombox/master/media/electric-boogaloo-sm.jpg :align: center :alt: Image: There's no stoppin' us! :target: https://www.youtube.com/watch?v=bFaPBFd6QRk :width: 90%

|

This is a small cross-platform audio-file player module, useful for plain-to-fancy system sound events, rings, beeps, and the like. I couldn't find a good one for this. "playsound" was very close at first glance but had a number of issues and has not been updated in a while.

BoomBox can wait for the file to finish or play in the background. It tries hard to support Windows, Mac, and Linux, and mostly succeeds. Though you could play an eight-minute Grateful Dead jam with it, you probably wouldn't want to.

.. ~ It's a one file pure-python module that can easily be copied into a project .. ~ if need be. NOT ANYMORE .. ~ ┏┓ ┏━┓┏━┓┏┳┓┏┓ ┏━┓╻ ╻ .. ~ ┣┻┓┃ ┃┃ ┃┃┃┃┣┻┓┃ ┃┏╋┛ .. ~ ┗━┛┗━┛┗━┛╹ ╹┗━┛┗━┛╹ ╹

Usage

Quick start—a cross-platform player looks like this:

.. code-block:: python

from boombox import BoomBox  # power on

boombox = BoomBox("There's_no_stoppin_us.ogg")  # load cassette

boombox.play()  # hit the ⏯ button

The play function also returns the instance, so if in a hurry one could do:

.. code-block:: python

boombox = BoomBox(slow_jam).play()  # or
BoomBox(slow_jam).play()

The latter less efficient for multiple calls, of course. There are a number of other keyword parameters that can be passed. Such as:

Not all arguments are supported on every implementation, but they will not balk if given.

Implementations

There are a number of underlying implementations if you'd like to pick a specific one and bypass the platform default:

.. ~ spacer

.. ~ spacer

Simply add an import to your script to choose a different one:

.. code-block:: python

from boombox import PyAudioBoomBox as BoomBox

You may have to install one of the audio libraries above for all of the functionality of Boom Box to work.

::

⏵ pip install --user boombox[all]  # or pyaudio, pyobjc, pygobject, etc

Playback Control

::

╭───────────────────────────────────────────╮
│ ╭───────────────────────────────────────╮ │
│ │ ╭───────────────────────────────────╮ │ │
│ │ │ /\ :  Electric Boogaloo     90 min│ │ │
│ │ │/──\: .....................  NR [✓]│ │ │
│ │ ╰───────────────────────────────────╯ │ │
│ │      //─\\   ╭....:....╮   //─\\      │ │
│ │     ││( )││  │)       (│  ││( )││     │ │
│ │      \\─//   ╰....:....╯   \\─//      │ │
│ │       _ _ ._  _ _ .__|_ _.._  _       │ │
│ │      (_(_)│ |(_(/_│  │_(_||_)(/_      │ │
│ │               low noise   |           │ │
│ ╰─────── ─────────────────────── ───────╯ │
│        /    []             []    \        │
│       /  ()                   ()  \       │
╰──────/─────────────────────────────\──────╯

A simple playback interface is returned by the instance:

.. code-block:: python

boombox.stop()  # Enough!
boombox.play()  # One more time!

Tone Generation

Tones may be generated like so:

.. code-block:: python

    boombox.play_tone(frequency_hz, duration_ms, volume=.1)

::

▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
╲▂▂▂▂╱╲▂▂▂▂╱╲▂▂▂
▔╲▂▂▂╱▔╲▂▂▂╱▔╲▂▂
▔▔╲▂▂╱▔▔╲▂▂╱▔▔╲▂
▔▔▔╲▂╱▔▔▔╲▂╱▔▔▔╲
▔▔▔▔╲╱▔▔▔▔╲╱▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

┏┓╻┏━┓   ┏━┓┏┓╻┏━╸   ╺┳┓┏━┓┏━╸┏━┓   ╻╺┳╸   ┏┓ ┏━╸╺┳╸╺┳╸┏━╸┏━┓   ╻
┃┗┫┃ ┃   ┃ ┃┃┗┫┣╸     ┃┃┃ ┃┣╸ ┗━┓   ┃ ┃    ┣┻┓┣╸  ┃  ┃ ┣╸ ┣┳┛   ╹
╹ ╹┗━┛   ┗━┛╹ ╹┗━╸   ╺┻┛┗━┛┗━╸┗━┛   ╹ ╹    ┗━┛┗━╸ ╹  ╹ ┗━╸╹┗╸   ╹