Skip to main content

MGT-python

PyPi version Python GitHub license CI Documentation

The Musical Gestures Toolbox for Python is a collection of tools for visualising and analysing audio and video files.

MGT python

📖 Documentation & Examples

Quick Start

Installation

pip install musicalgestures

musicalgestures installs its core Python dependencies automatically. You still need a working ffmpeg installation on your system for video processing. For the pose-landmark-trajectory tools (extract_pose_landmarks, see below), add the optional [pose] extra: pip install musicalgestures[pose] (installs MediaPipe).

Basic Usage

import musicalgestures as mg

# Load a video (mp4, avi, mov, … all supported)
v = mg.MgVideo('dance.mp4')

# Create visualizations — call .show() to display the result
v.grid().show()
v.videograms().show()
v.average().show()
v.history().show()
v.heatmap().show()              # where the video changes most

# Motion analysis
v.motion().show()
v.motiontempo().show()          # dominant movement tempo (Hz/BPM)
v.motiondescriptors().show()    # motion energy, smoothness, entropy, spectral descriptors
v.eulerian(mode='motion').show()  # amplify subtle motion (EVM)

# Audio analysis
v.audio.waveform().show()
v.audio.spectrogram().show()
v.audio.mfcc().show()
v.audio.tempo().show()          # tempo + beat tracking
v.sonomotiongram().show()       # sonify the motiongram

# Pose estimation (MediaPipe is GPU-capable on the standard pip OpenCV)
v.pose(model='mediapipe').show()

Display happens via .show() — analysis methods return result objects (MgVideo/MgImage/MgFigure) and do not auto-render.

Runtime Notes

  • import musicalgestures is fast: heavy dependencies are lazy-loaded, so the relevant backends load on first use rather than at import time.
  • ffmpeg is required for video I/O and preprocessing.
  • pose() defaults to the MediaPipe backend and downloads its weights on first use if they are missing; the OpenPose models ('body_25'/'coco'/'mpi') download their larger Caffe weights on first use instead.
  • In notebooks and other non-interactive runs, missing pose weights are downloaded automatically when possible.
  • If device='gpu' is requested but OpenCV CUDA support is unavailable, pose() falls back to CPU execution.
  • flow.dense(), flow.sparse(), and blur_faces() use CPU by default (use_gpu=False). Set use_gpu=True to opt into CUDA acceleration with automatic CPU fallback.
  • get_cuda_device_count() is available to quickly check whether OpenCV sees CUDA devices.
  • blur_faces() returns the generated result object consistently, including when save_data=True.

Try Online

Open In Colab

Quick Links

Scope: MGT and ambiscape

MGT-python and ambiscape are sister toolboxes: MGT owns the pixels (motion analysis, pose, 360° handling, video visualisation), ambiscape owns the samples (soundscape levels, spatial audio, sound-event taxonomies). MGT's audio functions cover quick looks; for serious soundscape work, install the bridge—pip install "musicalgestures[soundscape]"—and pull ambiscape's session features straight into MgFeatures on a shared wall-clock time base (see musicalgestures._soundscape and musicalgestures._timecode).

Features

  • Video Analysis: Motion detection, optical flow, motion vectors, movement tempo, Eulerian Video Magnification, frame-rate/speed resampling (resample()), motion descriptors (energy/smoothness/entropy/spectral via motiondescriptors())
  • Pose Estimation: MediaPipe (default; fast on plain CPU, GPU-capable) and OpenPose (multi-person) backends, with average-pose and trajectory summaries (per-marker quantity of motion + dominant frequency), optional marker motion trails, a 3D pose waterfall (pose_waterfall()), per-segment circular statistics (pose_segments()), centroid centring (pose_center()), and per-marker distance travelled (pose_distance())
  • Audio–movement analysis: Compare a single performer's sound and motion, covering tempo similarity, phase synchrony, structural similarity, per-body-part audio coupling, and loudness/dynamics coupling
  • Sound–movement research toolkit: Lower-level, array-based functions from the author's ro/stillstanding/Westney/cymbal studies, for pulse/cycle segmentation, cross-modal alignment, body-scale-normalised quantity of motion, postural sway metrics, physiology features, mocap I/O, and pose-landmark trajectory extraction (see Sound–Movement Analysis Toolkit below)
  • Audio Processing: Waveforms, spectrograms, MFCC, chromagrams, tempo/beat tracking, spectral descriptors
  • Visualisations: Motiongrams, videograms, motion history, heatmaps, sonomotiongrams (motion → sound)
  • Space-time displays: Stroboscope (chronophotography), silhouette waterfall, Motion History Image, 3D space-time volume, combined motion SSM
  • Integration: Works with NumPy, SciPy, librosa, and Matplotlib ecosystems
  • Cross-platform: Linux, macOS, Windows support

Sound–Movement Analysis Toolkit

Alongside the MgVideo/MgAudio methods above, MGT-python exposes a lower-level toolkit of plain-numpy sound–movement analysis functions, ported from the author's own research pipelines (the ro ritual-drumming study, the stillstanding/standstill-championship posturography study, the Westney with/without-audience piano comparisons, and the cymbal-comparison striking study). They work on arrays—not on MgVideo/MgAudio objects—so they drop straight into notebooks, batch scripts, or your own analysis pipeline, and are all importable directly from musicalgestures:

  • _peakspick_peaks: the one adaptive peak-picker (smoothing, relative threshold, minimum interval, prominence gate) shared by every event detector below.
  • _pulseCycle, group_strokes, segment_cycles, cycle_table, fit_accelerando, motion_onsets: group onsets into rhythmic cycles and fit an exponential accelerando.
  • _alignmentxcorr_lag, envelope_lag, per_cycle_motion_delta, anchor_and_match, offset_stats, sliding_correlation, envelope_agreement: lead/lag and coupling between two (or more) time-aligned signals.
  • _qomband_limited_qom, accel_to_speed, group_qom, pose_qom, body_scale, normalized_qom, grid_qom, envelope, bin_series: quantity-of-motion cores for position, pose-landmark and accelerometer data, including body-scale (framing-invariant) normalisation.
  • _audiofeaturesrms_envelope, spectral_flux, spectral_flux_onsets, energy_onsets, t60_backward_decay, attack_spectral_centroid: scipy-only audio features and onset detectors.
  • _posturecop_sway_metrics, confidence_ellipse_area, convex_hull_area, stabilogram_diffusion, dfa, sample_entropy, spectral_edges, sway_texture, sway_orientation, axial_rayleigh, spatial_extent, principal_axis_projection: standing-sway and postural-control metrics from a centre-of-pressure or marker trace.
  • _physiorespiration_rate, spectral_band_fractions: breathing rate and spectral composition of physiological waveforms.
  • _mocapread_qtm_tsv, compare_modality_envelopes: a robust Qualisys (QTM) TSV reader and cross-modality envelope comparison. (_mocap also defines its own dominant_frequency, a Welch-peak variant kept as musicalgestures._mocap.dominant_frequency rather than re-exported at top level, since it would otherwise shadow the pre-existing musicalgestures.dominant_frequency.)
  • _posetoolsextract_pose_landmarks, midpoint, limb_speed_from_landmarks, impact_events: video → tidy per-landmark trajectory arrays, plus derived limb-speed and impact-event signals. extract_pose_landmarks needs MediaPipe (pip install musicalgestures[pose]), imported lazily, while the derived-signal helpers are numpy-only.
  • motiongram_data (in _motionanalysis) gained an orientation='vertical'|'horizontal' option for the numpy-level motiongram, matching the two motiongrams() render directions.

A few illustrative snippets:

# Pulse-train segmentation: group stroke onsets into rhythmic cycles and fit
# an accelerando (ro study)
import numpy as np
from musicalgestures import segment_cycles, cycle_table, fit_accelerando

onsets = np.array([0.10, 0.34, 1.02, 1.24, 1.85, 2.02, 2.55, 2.68])  # seconds
cycles = segment_cycles(onsets)                  # -> list[Cycle]
table = cycle_table(cycles, clip_id='ro_2023')    # per-cycle DataFrame
ioi0, t_double, r2 = fit_accelerando(table['t'], table['ioi'])
print(f"tempo doubles every {t_double:.1f}s (R²={r2:.2f})")

Pulse segmentation: onsets grouped into stroke cycles with a fitted accelerando

# Quantity of motion + body-scale normalization: framing-invariant QoM in
# body-lengths/second, comparable across recordings/zoom levels (Westney study)
from musicalgestures import extract_pose_landmarks, normalized_qom

traj = extract_pose_landmarks('performance.mp4', fps=25, width=480)
qom, speed, fs_out = normalized_qom(traj['landmarks'][..., :2], traj['fps'])
print(f"{qom:.3f} body-lengths/s")
# Standing-sway metrics from a centre-of-pressure (or marker) trace (stillstanding study)
from musicalgestures import cop_sway_metrics

metrics = cop_sway_metrics(cop_xy, fs=100.0)     # cop_xy: (T, 2) array [ML, AP], mm
print(metrics['path_len'], metrics['area95'], metrics['ap_ml_sd_ratio'])
# Pose-trajectory extraction and impact detection from striking gestures (cymbal study)
from musicalgestures import extract_pose_landmarks, limb_speed_from_landmarks, impact_events

traj = extract_pose_landmarks('strike.mp4', fps=30, width=640)
wrists = traj['landmarks'][:, [15, 16], :2]           # left/right wrist, px
conf = traj['landmarks'][:, [15, 16], 2]
speed = limb_speed_from_landmarks(wrists, conf, traj['fps'])   # px/s, bilateral max
impacts = impact_events(wrists, traj['fps'])                   # acceleration-peak events

See the API reference for full signatures and each function's provenance note.

Presentation

See this short video presentation made for the Nordic Sound and Music Computing Conference 2021:

nordicsmc2021-thumbnail_640

Requirements

Research Background

This toolbox builds on the Musical Gestures Toolbox for Matlab, which again builds on the Musical Gestures Toolbox for Max. Many researchers and research assistants have helped its development over the years, including Balint Laczko, Joachim Poutaraud, Frida Furmyr, Marcus Widmer, Alexander Refsum Jensenius

The software is currently maintained by the fourMs lab at RITMO Centre for Interdisciplinary Studies in Rhythm, Time and Motion at the University of Oslo.

Reference

If you use this toolbox in your research, please cite this article:

@inproceedings{laczkoReflectionsDevelopmentMusical2021,
    title = {Reflections on the Development of the Musical Gestures Toolbox for Python},
    author = {Laczkó, Bálint and Jensenius, Alexander Refsum},
    booktitle = {Proceedings of the Nordic Sound and Music Computing Conference},
    year = {2021},
    address = {Copenhagen},
    url = {http://urn.nb.no/URN:NBN:no-91935}
}

License

This toolbox is released under the GNU General Public License 3.0 license.

Related toolboxes

These four toolboxes come out of the fourMs lab at the University of Oslo. They are separate packages with separate release cycles, but they are built to be used together and share several implementations, so a measure computed in one agrees with the same measure computed in another.

  • ambiscape—soundscapes: the sonic ambience of a place, across level, spectral, spatial, temporal, ecological and source descriptors
  • musiscape—music collections: comparing many tracks and albums held as audio files in folders
  • micromotion—human micromotion: quantity of motion from optical markers, accelerometers, respiration belts and force plates

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

musicalgestures-1.7.1.tar.gz (32.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

musicalgestures-1.7.1-py3-none-any.whl (32.6 MB view details)

Uploaded Python 3

File details

Details for the file musicalgestures-1.7.1.tar.gz.

File metadata

  • Download URL: musicalgestures-1.7.1.tar.gz
  • Upload date:
  • Size: 32.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for musicalgestures-1.7.1.tar.gz
Algorithm Hash digest
SHA256 6b68ac50863fc7ba4a4550a2a2bae626be603e1620bde630bb5a3ea6378921cc
MD5 69a038cd868d709062495c811b9da673
BLAKE2b-256 a242038fc22197b40a44b688b704c1b411760b3446dca971fe5d8f3087a3cfc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for musicalgestures-1.7.1.tar.gz:

Publisher: pypi-publish.yml on fourMs/MGT-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file musicalgestures-1.7.1-py3-none-any.whl.

File metadata

File hashes

Hashes for musicalgestures-1.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0304bd02121ec3d3d714d257ffe13b9f48a6d56ff7418f4dc5c91ba5425d03b2
MD5 0e06aa4a3b14bffc74a27d05022a5f55
BLAKE2b-256 604b074d2f5b4223b3ae03dd323bb5bf6dae21d9658c23950ea343929c303101

See more details on using hashes here.

Provenance

The following attestation bundles were made for musicalgestures-1.7.1-py3-none-any.whl:

Publisher: pypi-publish.yml on fourMs/MGT-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.18.0

2 files

1.17.0

2 files

1.16.0

2 files

1.15.0

2 files

1.14.2

2 files

1.14.1

2 files

1.14.0

2 files

1.13.0

2 files

1.12.1

2 files

1.12.0

2 files

1.11.4

2 files

1.11.3

2 files

1.11.2

2 files

1.11.1

2 files

1.11.0

2 files

1.10.0

2 files

1.9.2

2 files

1.9.1

2 files

1.9.0

2 files

1.8.0

2 files

This release

1.7.1 This release

2 files

1.7.0

2 files

1.6.9

2 files

1.6.8

2 files

1.6.7

2 files

1.6.6

2 files

1.6.5

2 files

1.6.4

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.0

2 files

1.4.9

2 files

1.4.8

2 files

1.4.7

2 files

1.4.6

2 files

1.4.5

2 files

1.4.4

2 files

1.4.3

2 files

1.4.2

2 files

1.4.0

2 files

1.3.3

2 files

1.3.2

2 files

1.3.0

2 files

1.2.9

2 files

1.2.8

2 files

1.2.7

2 files

1.2.6

2 files

1.2.5

2 files

1.2.4

2 files

1.2.3

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.9

2 files

1.1.7

2 files

1.1.6

2 files

1.1.5

2 files

1.1.4

2 files

1.1.3

2 files

1.1.1

2 files

1.1.0

2 files

1.0.999

2 files

1.0.6

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page