• Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎵 Python sound notifications made easy

chime

Python sound notifications made easy.


Table of contents

Motivation

I made this because I wanted a simple auditory cue system to tell me when a long-running number crunching script had finished. I didn't want to have to fiddle with the command-line, and also wanted a cross-platform solution. Thus was born chime!

Installation

pip install chime

This library has no dependencies. The IPython/Jupyter functionality is only imported if you've installed the ipython library. It should work for any Python version above or equal to 3.6.

Basic usage

chime puts four functions at your disposal:

>>> import chime

>>> chime.success()
>>> chime.warning()
>>> chime.error()
>>> chime.info()

Calling any of the above functions will play a sound. Note that the sounds are played in asynchronous processes, and are thus non-blocking. Each function should take around 2ms to execute, regardless of the sound length. You're free to use each sound notification in any way you see fit. I'm not your mama.

Theming

The sounds that are played depend on which theme is being used.

>>> chime.theme()  # return the current theme
'chime'

Several themes are available:

>>> chime.themes()
['big-sur', 'chime', 'future', 'mario', 'material', 'pokemon', 'sonic', 'zelda']

The theme can be changed by passing a theme name to the theme function:

>>> chime.theme('zelda')

A couple of things to note:

  • You can listen to the sounds interactively via this soundboard, which is made with Streamlit.
  • A random theme will be picked each time you play a sound if you set the theme to 'random'.

IPython/Jupyter magic

Load the extension as so:

%load_ext chime

You can wrap a line:

%chime print("I'm a line")

You can also wrap an entire cell:

%%chime

print("I'm a cell")

The magic command will call chime.success when the line/cell finishes successfully. Otherwise, chime.error is called whenever an exception is raised.

Exception notifications

If you run chime.notify_exceptions, then chime.error will be called whenever an exception is raised.

chime.notify_exceptions()

raise ValueError("I'm going to make some noise")

Command-line usage

You can run chime from the command-line:

$ chime

By default, this will play the success sound. You can also choose which sound to play, like so:

$ chime info

You can also choose which theme to use:

$ chime info --theme zelda

If you're using bash, then you can use chime to notify you when a program finishes:

$ echo "Hello world!"; chime

This will play the sound regardless of the fact that the first command succeeded or not. If you're running on Windows, then you can run the following equivalent:

> echo "Hello world!" & chime

Platform support

Under the hood, chime runs a command in the shell to play a .wav file. The command-line program that is used depends on the platform that you're using. Platform information is available in the sys.platform variable as well as the platform module from the standard library. Currently, the supported platforms are:

  • Darwin
  • Linux
  • Windows

A UserWarning is raised if you run a chime sound on an unsupported platform. Feel free to get in touch or issue a pull request if you want to add support for a specific platform. Likewise, don't hesitate if you're encountering trouble with one of the above platforms. I won't bite.

I can't hear anything 🙉

Did you check if you turned your sound on? Just kidding. 😜

This library is designed to be non-invasive. By default, sounds are played asynchronously in unchecked processes. Therefore, if something goes wrong, the process dies silently. If you can't hear anything and you think that the issue is coming from chime, then set the sync parameter when you play a sound:

>>> chime.info(sync=True)

This will play the sound synchronously and issue a warning if something goes wrong, which should allow you to debug the issue. You can also raise an exception instead of sending a warning by setting the raise_error parameter:

>>> chime.info(sync=True, raise_error=True)

Note that setting raise_error won't do anything if sync is set to False.

Setting a default theme

To change the default theme a configuration file may be created in ~/.config/chime/chime.conf on Unix or %APPDATA%\chime\chime.ini on Windows.

For example, to change the default theme to 'zelda', the configuration file would contain:

[chime]
theme = zelda

Command-line arguments

Chime works by running commands in the CLI. For instance, aplay is used on Linux systems, while afplay is used on Darwin systems. Arguments can be specified by setting the RUN_ARGS variable. For example, here's how to select a specific sound card, assuming a Linux system using aplay:

>>> chime.RUN_ARGS = "--device sysdefault:CARD=PCH"

You can also specify this as a default configuration in the configuration file:

[chime]
cli_args = '--device sysdefault:CARD=PCH'

At present, it isn't possible to pass CLI arguments on Windows, due to a limitation of the winsound module.

Adding a new theme

I have toyed with the idea of allowing users to add their own theme(s), but at the moment I rather keep things minimal. However, I'm happy to integrate new themes into the library. You can propose a new theme by opening a pull request that adds the necessary .wav files to the themes directory. A theme is made up of four files: success.wav, warning.wav, error.wav, and info.wav. That's all you need to do: the theme will picked up be automatically once the necessary files are provided.

Be creative! 👩‍🎨

Things to do

  • Some mechanism to automatically call chime.warning when a warning occurs.
  • Make it work with a remote machine. For instance a Jupyter Notebook hosted on a remote machine.
  • More themes!

Acknowledgements

  • Special thanks to Michael Vlah for being a gentleman by giving up the "chime" name on PyPI.
  • Thanks to u/Pajke on reddit for helping me debug Windows support.
  • Thanks to David Chen for adding Linux support by suggesting the use of aplay.
  • Thanks to Vincent Warmerdam for suggesting a command-line interface.
  • Calmcode made a video introduction to chime ❤️
  • Thanks to Paulo S. Costa for contributing in many different ways.
  • Thanks to d34d_m8 for adding OpenBSD support.

License

As you would probably expect, this is MIT licensed.

More Repositories

1

prince

👑 Multivariate exploratory data analysis in Python — PCA, CA, MCA, MFA, FAMD, GPA
Python
1,245
star
2

eaopt

🍀 Evolutionary optimization library for Go (genetic algorithm, partical swarm optimization, differential evolution)
Go
881
star
3

xam

🎯 Personal data science and machine learning toolbox
Python
362
star
4

flask-boilerplate

🚀 Fully fledged Flask boilerplate code
Python
354
star
5

sorobn

🧮 Bayesian networks in Python
Python
234
star
6

kaggle-recruit-restaurant

🏆 Kaggle 8th place solution
Jupyter Notebook
106
star
7

procedural-art

🌌 Procedural art with vanilla JavaScript
HTML
96
star
8

pytorch-resample

🎲 Iterable dataset resampling in PyTorch
Python
89
star
9

xgp

🔮 Symbolic regression library
Go
61
star
10

flask-sse-no-deps

An example of server-sent events in Flask without extra dependencies
Python
59
star
11

clavier

🔤 Measure edit distance based on keyboard layout
Python
58
star
12

halfgone

🔳 Black and white digital halftoning
Go
47
star
13

taxi-demo-rp-mz-rv-rd-st

🚕 Self-contained demo using Redpanda, Materialize, River, Redis, and Streamlit to predict taxi trip durations
Python
44
star
14

pointu

✏️ Pointillisme tool based on Weighted Voronoi Stippling
Go
37
star
15

carre

👌 Image simplifier
Go
33
star
16

kaggle-vsb-power

⚡ 13th place solution
Jupyter Notebook
31
star
17

arcgonaut

🌀 Golang arc diagrams
Go
29
star
18

eaopt-examples

🍀 eaopt examples
Go
28
star
19

starboost

⭐🚀 Gradient boosting on steroids
Python
26
star
20

naked

The simplest way to deploy a machine learning model
Python
23
star
21

idao-2020-qualifier

Solution of team "Data O Plomo" to the qualification phase of the 2020 edition of the International Data Analysis Olympiad (IDAO)
Jupyter Notebook
18
star
22

genetic-curve-fitting

📈
Python
17
star
23

orc

🧌 Parsing structured information from OCR outputs
Jupyter Notebook
17
star
24

myriade

✨🌲 Hierarchical extreme multiclass and multi-label classification.
Python
16
star
25

bike-sharing-history

🚲 Git scraping for bike sharing APIs
Python
16
star
26

data-science-tutorials

Jupyter Notebook
15
star
27

maxhalford.github.io

🏡 Personal website
HTML
13
star
28

tuna

🐟 A streaming ETL for fish
Go
13
star
29

bbc-weather-honolulu

☀️ Measuring the accuracy of BBC weather forecasts in Honolulu, USA
Python
12
star
30

yamp

Yet Another MkDocs Parser
Python
11
star
31

tartine

🍞 Manipulate dynamic spreadsheets with arbitrary layouts using Python
Python
11
star
32

spotgeo-challenge

🛰️ My solution to the Kelvins spotGEO challenge
Python
10
star
33

openbikes

🚲 Collecting and publishing bike sharing data stored at https://github.com/MaxHalford/openbikes-data
Python
9
star
34

gago

Old version of eaopt, will eventually be removed
Go
9
star
35

project-euler-python

🐍
Python
9
star
36

ikea-store-locations

🇸🇪 Retrieval and analysis of IKEA store locations
Python
9
star
37

directory-architecture

📁 Mimicking the tree command
Python
8
star
38

xgp-python

XGP Python package with a scikit-learn interface
Python
8
star
39

idao-2020-final

Solution of team "Data O Plomo" to the final phase of the 2020 edition of the International Data Analysis Olympiad (IDAO)
Jupyter Notebook
7
star
40

svg2stl

🛹 Turn an SVG into an STL for stencil creation purposes
Python
6
star
41

inverted-index-search-engine

Python
5
star
42

streaming-cdf-benchmark

A benchmark to compare algorithms for estimating cumulative density functions (CDF) on streaming data
Python
5
star
43

vose

Cython implementation of Vose's Alias method
Cython
5
star
44

jan

💤 Just Another Neural network
Python
5
star
45

bitcoin-analysis-m1sid

💰 Master 1 project
Python
5
star
46

kaggle-march-madness-2019

🏀 Men and women solutions for the 2019 edition of the Kaggle March Madness competition
Jupyter Notebook
4
star
47

kaggle-DSG18-qualifier

Jupyter Notebook
3
star
48

kaggle-DSG17-qualifier

Python
3
star
49

kaggle-plasticc-astro-classification

Jupyter Notebook
3
star
50

andor-faq-llm

🎲 Answering tabletop game questions using an LLM
Python
2
star
51

kaggle-answer-correctness

🤔 Solution to the Riiid! Answer Correctness Prediction competition on Kaggle
Python
2
star
52

postgres-job-docker

🐳 Docker setup for PostgreSQL + Join Order Benchmark (JOB)
Shell
2
star
53

dotfiles

🧘 Because it's the healthy thing to do
Shell
2
star
54

ziboinboin.com

🍂 Old Ziboinboin website
HTML
1
star
55

openbikes-data

🚲 Git storage for https://github.com/MaxHalford/openbikes
1
star
56

where-to-live

Jupyter Notebook
1
star
57

cochleas-L3SID

Python
1
star
58

chrome-infinite-scrolling-robot

JavaScript
1
star
59

kaggle-avito-demand

Python
1
star
60

tldks-2020

Jupyter Notebook
1
star