https://github.com/JeffFessler/Sound.jl
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.
Sound is a registered package,
so installation is easy:
import Pkg; Pkg.add("Sound")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 amplitudeSee 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 volumeBy 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.
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 dataAgain 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.
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.
Tested with Julia ≥ 1.10.
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.
- https://github.com/haberdashPI/SignalBase.jl
supports a
frameratemethod that serves as the default sampling rate here. - https://github.com/haberdashPI/SignalOperators.jl has useful audio processing operations.
- https://github.com/dancasimiro/WAV.jl
has a similar
wavplayfunction - https://github.com/JuliaAudio has a collection of audio packages.
- https://github.com/JuliaAudio/PortAudio.jl
Currently, thesoundfunction here is just a wrapper around functions in this package. However that could change in the future to support other audio back-ends, much like howPlots.jlprovides a common interface to various plotting back-ends.