Archive
Play a sound file from memory
Problem
I wanted to play short sound files a lot of times. Calling an external program each time to play a file would have been too slow. How to read sound files in the memory and play them from there?
Solution
First I tried the package simpleaudio. It worked well but it turned out that it couldn’t play every .wav file. VLC could play my file correctly, but simpleaudio just produced some noise. So I needed another solution.
And that’s how I found soundfile and sounddevice. Their usage is very simple:
import soundfile as sf
import sounddevice as sd
samples, samplerate = sf.read('file.wav')
sd.play(samples, samplerate)
sd.wait()
To use them, you also need to install numpy.
More info here: https://stackoverflow.com/a/42387773/232485
Update:
I had some .wav files that were not played correctly with soundfile+sounddevice. So I ended by using soundfile+sounddevice for certain files and by using simpleaudio for some other files.
Links
- Playing and Recording Sound in Python (this is where I read about
simpleaudio) - https://github.com/jabbalaci/keysound (my project, in which I needed to play sound files)
Handle extended file attributes
Problem
You want to handle extended file attributes from Python.
Solution
If you are not familiar with extended file attributes, you can read more about it here. I also have a blog post (see here), in which I show how to set/remove extended file attributes using the attr Linux command.
To handle the extended file attributes from Python, you can use the pyxattr package: https://github.com/iustin/pyxattr
Its usage is very simple:
>>> import xattr
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
>>> xattr.getxattr("main.py", "user.com.dropbox.attrs")
b'\n\x12...'
>>> xattr.setxattr("main.py", "user.com.dropbox.ignored", "1")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs', b'user.com.dropbox.ignored']
>>> xattr.getxattr("main.py", "user.com.dropbox.ignored")
b'1'
>>> xattr.removexattr("main.py", "user.com.dropbox.ignored")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
A typical use case: you want Dropbox to ignore your venv/ folder inside your project.
Links
- https://en.wikipedia.org/wiki/Extended_file_attributes
- https://help.dropbox.com/sync/ignored-files
- https://github.com/iustin/pyxattr (I used this in this post)
- https://github.com/xattr/xattr (an alternative)
keysound
Problem
I have a non-mechanical keyboard. However, when I press keys, I also want to hear some insane clickytty clicks. What to do?
Solution
If you don’t want to buy a new keyboard, you can try my keysound project. When you press a key, a short sound file is played.
At the bottom of the README of this project’s GitHub page, I also collected some related work, thus there are several alternatives available.
