Skip to main content

OpenSesame Plugin: Audio Low Latency

An OpenSesame plugin for playing and recording audio files with low, predictable latency.

Copyright (C) 2017-2026 Bob Rosbag and contributors

About

The main goal of this plugin is to play and record audio with minimal and predictable latencies, for a high accuracy and precision. The init items offer two modules:

  • PyAlsaAudio uses the Linux ALSA audio system directly; it gave the best results within Python (Linux only).
  • SoundDevice (PortAudio) is cross-platform: Linux and Windows (macOS not tested).

This plugin consists of playback and recording items as well as foreground and background (multi-threaded) items. Both playback and recording have their own init item. These should be placed at the beginning of an experiment.

Difference between foreground and background:

  • Foreground items play/record until the file ends or the set duration has passed.
  • Background items consist of a 'bg_start', 'bg_wait' and 'bg_stop' item (and 'bg_pause' and 'bg_resume'). These are fully multi-threaded. After the start of the playback/recording, the item will immediately advance to the next item. When it reaches the 'bg_stop' or 'bg_wait' item, it will stop the audio or wait until the file ends or duration has passed.

Seven items for playback:

  • audio_low_latency_play_init initialization of the playback audio device, this should be placed at the beginning of an experiment.

Foreground:

  • audio_low_latency_play_fg starts the playback of audio, it will play the entire audio file or will stop after the set duration has passed before continuing to the next item in the experiment.

Background:

  • audio_low_latency_play_bg_start starts the playback of audio, it will directly advance to the next item in the experiment.
  • audio_low_latency_play_bg_wait waits until the thread from audio_low_latency_play_bg_start is finished (end of audio or surpassing the duration) before advancing to the next item in the experiment.
  • audio_low_latency_play_bg_stop sends a stop signal to the audio_low_latency_play_bg_start thread to stop immediately and checks if the thread has finished.
  • audio_low_latency_play_bg_pause pauses the background playback of audio.
  • audio_low_latency_play_bg_resume resumes the background playback of audio.

Seven items for recording:

  • audio_low_latency_record_init initialization of the recording audio device, this should be placed at the beginning of an experiment.

Foreground:

  • audio_low_latency_record_fg starts the recording of audio, it will record for the set duration before continuing to the next item in the experiment.

Background:

  • audio_low_latency_record_bg_start starts the recording of audio, it will directly advance to the next item in the experiment.
  • audio_low_latency_record_bg_wait waits until the thread from audio_low_latency_record_bg_start is finished (surpassing the duration) before advancing to the next item in the experiment.
  • audio_low_latency_record_bg_stop sends a stop signal to the audio_low_latency_record_bg_start thread to stop immediately and checks if the thread has finished.
  • audio_low_latency_record_bg_pause pauses the background recording of audio.
  • audio_low_latency_record_bg_resume resumes the background recording of audio.

Renamed items (14.0.0). The foreground items were audio_low_latency_play and audio_low_latency_record, and the background items had no "bg_" (e.g. audio_low_latency_play_start is now audio_low_latency_play_bg_start). Experiments made with an older version open and run as they are, and OpenSesame saves them with the new item types; the names of the items, and so the variables in the log file, stay the same. An experiment saved with 14.0.0 or later needs 14.0.0 or later: an older version doesn't know the new item types (OpenSesame 4.1 then runs the experiment without these items, after a warning that a plugin is missing).

Linux (PyAlsaAudio): "hw:" devices use the sound card directly, so the card must support the bit depth, sample rate and number of channels; "plughw:" devices let ALSA convert what the card doesn't support.

A record item can't be used twice as a linked copy (e.g. twice in one sequence): each use needs its own recording, so use a second record item (a copy, not a linked copy) with its own name. A record item in a loop is fine.

Recordings are saved as [Audio Filename].wav in the folder of the log file; a filename with a folder (e.g. recordings/subject_1) saves into that folder below the log folder and creates it when needed, and a full path is used as it is.

Linux (SoundDevice): devices of PortAudio's OSS host API (/dev/dsp, the OSS emulation of the kernel) are not listed: they bypass ALSA, so the sound card and the actual sizes can't be found; use PyAlsaAudio, or the ALSA devices of SoundDevice. An experiment with such a device gives an error.

PyAudio is no longer supported (after 12.0.2): an experiment with module "PyAudio (PortAudio)" gives an error; choose "SoundDevice (PortAudio)", which lists the same devices (the latency may differ).

Variables in the log file ([kind] is play or record):

  • Init items: audio_low_latency_[kind]_module, _device_name, _period_size, _period_time, _period_time_exact, _periods, _buffer_size (the period size times the number of periods), _buffer_time (with SoundDevice, PortAudio is asked for a buffer of that size and rounds it: see _buffer_size_actual), _bitdepth, _samplewidth, _samplerate and channels; and audio_low_latency[kind]_soundcard, _period_size_actual and buffer_size_actual (see below); with SoundDevice also audio_low_latency[kind]portaudio_latency: the latency in ms that PortAudio reports for the stream, and audio_low_latency[kind]host_api: the host API of the device, e.g. "Windows WASAPI" (on all systems, "dummy mode" in dummy mode); with SoundDevice also audio_low_latency[kind]_wasapi_exclusive (yes when WASAPI exclusive mode is used, else no).
  • The fg and bg_start items: the timestamps time_stimulus_onset_[item_name] and time_stimulus_offset_[item_name] (no offset in dummy mode).
  • Buffer underruns and overruns: audio_low_latency_play_underruns_[item_name] (audio_low_latency_play_fg and play_bg_start items) and audio_low_latency_record_overruns[item_name] (audio_low_latency_record_fg and _record_bg_start items): the number of underruns (periods of the sound that were not played) or overruns (parts of the recording that are missing) during that item, set when the playback or recording ends (bg_start items: before the bg_wait or bg_stop item ends). With PyAlsaAudio a number; with SoundDevice "unknown": PortAudio doesn't report them for WASAPI, and with ALSA it reports them one write late, also those of the pause between two sounds, so they can't be counted per item. "dummy mode" in dummy mode. A warning in the debug window still reports them too.
  • Keys: audio_low_latency_[kind]key_presses and audio_low_latency[kind]key_timestamps (fg items), audio_low_latency[kind]start_key_presses and audio_low_latency[kind]_start_key_timestamps (bg_start items; these names come from the item names before 14.0.0), separated by ';'.
  • The audio_low_latency_play_fg and _play_bg_start items: wav_duration and wait_to_finish (in ms; wait_to_finish not in dummy mode).

The host API, WASAPI and PortAudio variables exist only with SoundDevice: a text that shows them (e.g. in a feedback item) stops with an error when the experiment uses PyAlsaAudio. To show a default instead, write e.g. {var.get('audio_low_latency_play_host_api', 'n/a')}.

The sound card (audio_low_latency_[kind]_soundcard) is found as follows:

  • Linux (PyAlsaAudio, and the "(hw:X,Y)" devices of SoundDevice): the codec of an onboard (HD Audio) device, e.g. "Realtek ALC1220", else the name of the card, e.g. "MOTU M2", from /proc/asound. Devices that are not on a sound card, such as "default" with a sound server, "pipewire" or "null", give "unknown".
  • Windows and macOS (SoundDevice): the device name, e.g. "Speakers (Realtek(R) Audio)".
  • In dummy mode: "dummy mode".

The period size and buffer size that are actually used, in frames (audio_low_latency_[kind]_period_size_actual and _buffer_size_actual):

  • Linux (PyAlsaAudio devices on a sound card, also through a plug or dmix device such as "default:CARD=X", and the "(hw:X,Y)" devices of SoundDevice): the hardware parameters of the sound card, from /proc/asound. With PyAlsaAudio, a period size or number of periods other than the chosen one is an error; with SoundDevice, a period size other than the chosen one is a warning in the debug window, and the buffer size is not checked (PortAudio is asked for a buffer of the chosen number of periods and may round it). Other devices, such as "default" with a sound server, "pipewire" or "null", give "unknown".
  • Windows, SoundDevice with a WASAPI device: the buffer size of WASAPI (from PortAudio); the period size is "unknown". Other host APIs (MME, ASIO) give "unknown" for both: PortAudio doesn't report them.
  • macOS: "unknown".
  • In dummy mode: "dummy mode".

Windows (SoundDevice, through PortAudio):

  • Host API: Windows lists most devices once per host API (MME, Windows WASAPI, ...), often with the same name. The init item lists the devices of the chosen host API, so it is always clear which one a device uses. PyAlsaAudio uses ALSA directly: its host API is "ALSA" and can't be changed. Experiments made with 13.1.0 or earlier have the host API "all" (or none), which uses the first device with the chosen name (MME on Windows): they still run with the same device, and opening the init item in the GUI replaces "all" by the host API of that device. MME and WASAPI in shared mode play and record through the Windows mixer: Windows may convert the bit depth and the sample rate, and the latency is higher (MME the highest). WASAPI in shared mode only accepts the sample rate set in the Windows sound settings. The host API choice also works on Linux and macOS.
  • WASAPI exclusive mode (SoundDevice): the device is used by the experiment alone, without the Windows mixer, so the bit depth, sample rate and buffer reach the sound card; the device must support them, or it can't be opened. Other programs can't play sound on that device during the experiment. It can only be switched on with host API "Windows WASAPI"; with another host API it is not used. In exclusive mode the sound card plays its buffer again as long as no new data comes in (between sounds the items write nothing), which sounds as ringing after a sound that doesn't end in silence: the play items therefore write one buffer of silence after each sound and when they pause, so they end about one buffer (e.g. 43 ms) later, when the sound has been played.
  • ASIO: the plugin makes SoundDevice load its PortAudio library with ASIO (it sets the environment variable SD_ENABLE_ASIO before SoundDevice is imported), so ASIO devices are listed. This doesn't work when SoundDevice was imported before the plugin (e.g. by an inline script) or uses another PortAudio library (e.g. from conda). PortAudio loads every installed ASIO driver when SoundDevice is imported, also when an ASIO device isn't used. The ASIO buffer size is set in the driver's control panel; PortAudio doesn't report the buffer it uses, so the actual sizes are "unknown".
  • DirectSound and WDM-KS devices are not listed: the items play and record with blocking reads and writes, which PortAudio does not support for them (with DirectSound nothing was played or recorded, without an error; WDM-KS devices could not be opened). An experiment with such a device gives an error.

Installation

Install the plugin with pip, in the Python environment of OpenSesame:

pip install opensesame-plugin-audio_low_latency

License

This plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version (GPL-3.0-or-later).

The full license text is in the file COPYING, and is also available at https://www.gnu.org/licenses/gpl-3.0.html.

This plugin contains works of others. Icons are derivatives of the Faience icon theme, Faenza icon theme and Papirus icon theme.

Documentation

Installation instructions and documentation on OpenSesame are available on the documentation website:

Metadata

Release files for opensesame-plugin-audio-low-latency 14.1.0

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

Source distribution (sdist)

Source distribution for opensesame-plugin-audio-low-latency 14.1.0
File Size Uploaded Provenance Actions
opensesame_plugin_audio_low_latency-14.1.0.tar.gz 114.6 kB Without provenance Details for opensesame_plugin_audio_low_latency-14.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opensesame-plugin-audio-low-latency 14.1.0
File Interpreter ABI Platform Actions
opensesame_plugin_audio_low_latency-14.1.0-py3-none-any.whl Python 3 none any Details for opensesame_plugin_audio_low_latency-14.1.0-py3-none-any.whl

Total release size: 294.9 kB

Release files / opensesame_plugin_audio_low_latency-14.1.0.tar.gz

Download URL opensesame_plugin_audio_low_latency-14.1.0.tar.gz
Size 114.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4cf458efd5a808a9f5300320026d00f19699a44c83e0e56295ed4d34b5676048
BLAKE2b-256 checksum
How to use checksums
4b43753bece0fe6824e4f24f32917847a3f6e9858c767b076457ab827b52aeb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / opensesame_plugin_audio_low_latency-14.1.0-py3-none-any.whl

Download URL opensesame_plugin_audio_low_latency-14.1.0-py3-none-any.whl
Size 180.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3648118529bfcfeb68de9cc393e3b9e7e985b5e25eeb88f86641a3587a276044
BLAKE2b-256 checksum
How to use checksums
136a5aa45387667d8e0672d5523a33aded41e5cb97aee203aec56de06f7b4e62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Security for opensesame-plugin-audio-low-latency 14.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

14.1.0 This release

2 release files No provenance

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