Sample: working with audio data

Sample: a class representing audio data (wraps a numpy float array).

A Sample contains the audio (read from a soundfile or synthesized) as a numpy float array. It aware of its sr, original format and encoding, etc. It can also perform many operations on the sample data (fade-in/out, cut, insert, reverse, normalize, etc) and implements most math operations valid for audio data (+, -, *, /).

Note

Some operations are performed in place while others return a copy. In general, whenever the number of samples or the number of channels is modified (e.g. `mixdown` or `stripLeft`), a copy is returned. Otherwise the operation is performed in-place

Demo Notebooks

Sample - Basic Operations
_images/audiosample-demo-notebook.png
  • Loading a soundfile from disk

  • Plotting / Spectrogram

  • Basic operations (fade, normalization, mixing, etc.)

  • Playback

Sample - Feature Extraction
assets/audiosample-feature-extraction.png

A Sample implements multiple feature extraction algorithms, like onset detection, fundamental tracking, etc.


maelzel.snd.audiosample Module

audiosample

This module is based on the Sample class, which contains the audio of a soundfile as a numpy array and it aware of its sr, original format and encoding, etc. It can also perform simple actions (fade-in/out, cut, insert, reverse, normalize, etc) on its own audio destructively or return a new Sample. It implements most math operations valid for audio data (+, -, *, /)

Note

All operations are samplerate-aware: any operation involving multiple Sample instances will broadcast these to the highest samplerate used

Examples

# load a Sample, fade it, play and write
from maelzel.snd.audiosample import *
s = Sample("snd/Numbers_EnglishFemale.flac")
s.fade(0.5)
s.play(speed=0.5, block=True)
# Plot a 6 second fragment startine at time=1
s[1:7].plotSpectrogram(fftsize=4096, overlap=8, mindb=-100, maxfreq=8000)
_images/audiosample-plot-spectrogram.png
samples = [Sample("soundA.wav"),
           Sample("soundB.aif"),
           Sample("soundC.flac")]
a, b, c = broadcastSamplerate(samples)
# mix them down
out = a.prependSilence(2) + b + c
out.write("mixed.wav")

Classes

Sample(sound[, sr, start, end, readonly, engine])

A class representing audio data