Sound.jl

Provide `sound` & `record` functions for playing & recording audio in Julia. Also Julia version of phase vocoder.
Popularity
25 Stars
Updated Last
6 Months Ago
Started In
November 2021

Sound

https://github.com/JeffFessler/Sound.jl

docs-stable docs-dev action status pkgeval status codecov license code-style

This Julia repo exports the functions sound and soundsc that play an audio signal through a computer's audio output device, such as speakers or headphones. These functions are designed to be similar to that of Matlab commands sound and soundsc to facilitate code migration.

Getting started

Sound is a registered package, so installation is easy:

import Pkg; Pkg.add("Sound")

Example

using Sound
S = 8192 # sampling rate in Hz
x = 0.7*cos.(2π*(1:S÷2)*440/S)
y = 0.8*sin.(2π*(1:S÷2)*660/S)
sound(x, S) # monophonic
sound([x y], S) # stereo
soundsc([x y], S) # scale to unit amplitude

See the documentation.

Matlab's audioplayer has the same arguments as sound, so you can type audioplayer = sound and then call audioplayer(x, S) if desired, albeit without any of other features of audioplayer.

As a nod towards the Julia way of doing things, both sound and soundsc also support the SampleBuf type in the SampledSignals.jl package, via Requires.jl. That type carries the sampling rate along with the signal data, which is attractive compared to having two separate variables.

using Sound
using SampledSignals: SampleBuf # you may need to add this package
S = 8192 # sampling rate in Hz
x = 0.7*cos.(2π*(1:S÷2)*440/S)
y = 0.8*sin.(2π*(1:S÷2)*660/S)
sb = SampleBuf([x y], S) # stereo data
sound(sb)
soundsc(sb) # scale to maximum volume

By default, the audio output is routed to the sound output device specified in system-wide settings, e.g., via "System Preferences" on a Mac. There is keyword option to override that setting.

Audio recording

There is also a simple record method here for recording from the system-wide default audio input device (typically a built-in microphone). It returns a Vector of the sample data and the audio system default sampling rate.

using Sound: record
data, S = record(4) # record 4 seconds of audio data

Again there is keyword argument for selecting the audio input device.

See the examples/ directory for an example of creating a Record/Stop/Play GUI using Gtk.jl.

Phase vocoder

Also exported is the function phase_vocoder that provides a Julia version of a phase vocoder, translated from this Matlab code for audio time scaling. See the documentation for example use.

Compatibility

Tested with Julia ≥ 1.10.

Caveats

Because Julia code is compiled, the first time you call an audio function the sound can be jittery. Subsequent calls (with the same argument types) usually work as expected.

On MacOS, if you run Julia from an xterm in XQuartz, then (at least as of XQuartz v2.8.1) no audio will be recorded because XQuartz does not ask for permission to access the microphone. Running Julia within the Terminal app is required because Terminal will properly request microphone permissions.

Related packages

Used By Packages

No packages found.