Skip to main content

poordub

A minimalistic PyDub clone.

This small package was written out of curiosity about the audioop module in the Python standard library. It implements a subset of PyDub's features.

You probably should not use it, but it works and illustrates how to use audioop correctly.

PcmAudio objects

The main class in this module is PcmAudio, which is a shrink-wrapped version of PyDub's AudioSegment, but a bit less capable than the original:

  • Supports only file formats that Python understands, i.e. Wave, AIFF, and SunAudio.
  • Does everyting in-memory
  • Has no fancy audio effects (other than fades)

Usage

To read a file from disk, use PcmAudio.from_file(audio_file, audio_format=wave), where audio_file can be a path or a file-like object, and audio_format can be either wave, aifc, or sunau.

Approximately a second of a simple sine wave can be obtained with PcmAudio.sine(hz), and silence with PcmAudio.silence(millis=0).

To add audio parts, use + like so: part1 + part2. To loop, multiply with the number of repetitions: audio * 3

To extract a segment from an audio clip, slice it at the desired time: audio[start:end], where start and end should be in milliseconds, defaulting to 0 and the total audio length, respectively. The total audio length can be obtained with len(audio).

To change the amplitude, add or subtract the desired amplitude change in dB: audio - 3.

To overlay two audio clips, use clip1 & clip2, but careful: This adds the signals, which might result in ugly noise if the sum of amplitudes is greater than the maximum possible amplitude for the sample width. If that happens, add some negative gain to the clips before overlaying them.

Also make certain that audios have the same length before overlaying them. Otherwise, the longer part will be clipped.

A rough measure of the signal strength can be obtained with audio.dbfs(). If it is too low, audio.normalize(headroom=0.1) will scale it to the max with a safety margin, given in dB by headroom.

To write audio files, use audio.to_file(audio_file, audio_format=wave, compression=None). audio_file and audio_format are as above, and compression is only supported for AIFF files.

A memory buffer containing an audio file can be obtained with audio.to_buffer(self, audio_format=wave, compression=None). To play a clip, call audio.play() which returns a simpleaudio.PlayObject.

Fades

The only included effects are fades:

  • audio.fade_in(duration, threshold=float('-inf'))
  • audio.fade_out(duration, threshold=float('-inf'))
  • audio.cross_fade(other, duration, gap=0, threshold=float('-inf'))

For these, duration is in milliseconds as usual, and threshold is the minimum amplitude that needs to be exceeded in the portion being faded before the actual fade is applied.

While this sounds somewhat technical, it improves the audible result of cross-fades: If one part is already very low at the beginning or the end, it needs no additional fade, but can be used as-is in the overlay. Try -9dB for testing.

Finally, the gap is the duration (in milliseconds) of additional silence that is inserted at each end of the audio parts during cross-fades. It can be used to make the transition from one clip to another audibly clearer.

Misc

audio.to_mono() and audio.to_stereo() do what their names suggest. Additionally, there are two technical operations to_framerate() and to_sample_width() which are used internally to ensure consistency between clips before appending or overlaying.

Playback and recording

Playback and recording use PyAudio, which in turn depends on the cross-platform portaudio library. The former can be installed with pip3 install PyAudio, the latter with apt-get, brew or similar. PyAudio is not installed by default as a dependency, it should be done manually before microphones or speakers are used.

The AudioStream wrapper takes care of frame I/O in blocking mode. The open method expects PcmAudio.Params with the desired number of channels, sample width and frame rate. By default, it opens a stream in playback mode. Pass input=True for recording.

AudioStream can be used as a context manager that opens the stream with default settings and closes it on exit.

Example code:

with AudioStream( AudioStream.CD_AUDIO) as out:
    out.play( some_audio)

with AudioStream( AudioStream.MONO_16KHZ).open( input=True) as in_out:
    recording = in_out.record( 3000)
    in_out.play( recording)

Release files for poordub 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for poordub 0.1.5
File Size Uploaded
poordub-0.1.5.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for poordub 0.1.5
File Interpreter ABI Platform
poordub-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / poordub-0.1.5.tar.gz

Download URL poordub-0.1.5.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7641998dda46663b25c50799063aa512af92798eeeb65b14c0251f991e82c3e5
BLAKE2b-256 checksum
How to use checksums
70af7402b5edfa50ffbd89ce99867629b4762dd493aefd9297f248a56b19a88b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / poordub-0.1.5-py3-none-any.whl

Download URL poordub-0.1.5-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f718e50decb204025d7bcb1a87734c39671e34c56886e30f6eea2338d29aa65
BLAKE2b-256 checksum
How to use checksums
d75b7d0f595a382b872b10e662e5f1edfa288b2ef8ce367626f42a0f67f81949
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page