https://github.com/jefffessler/sound.jl
Provide `sound` & `record` functions for playing & recording audio in Julia. Also Julia version of phase vocoder.
https://github.com/jefffessler/sound.jl
audio julia phase-vocoder sound
Last synced: 18 days ago
JSON representation
Provide `sound` & `record` functions for playing & recording audio in Julia. Also Julia version of phase vocoder.
- Host: GitHub
- URL: https://github.com/jefffessler/sound.jl
- Owner: JeffFessler
- License: mit
- Created: 2021-11-27T13:12:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-22T18:49:49.000Z (about 1 year ago)
- Last Synced: 2025-04-12T23:45:11.439Z (28 days ago)
- Topics: audio, julia, phase-vocoder, sound
- Language: Julia
- Homepage:
- Size: 2.35 MB
- Stars: 28
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sound
https://github.com/JeffFessler/Sound.jl
[![docs-stable][docs-stable-img]][docs-stable-url]
[![docs-dev][docs-dev-img]][docs-dev-url]
[![action status][action-img]][action-url]
[![pkgeval status][pkgeval-img]][pkgeval-url]
[![codecov][codecov-img]][codecov-url]
[![license][license-img]][license-url]
[![code-style][code-blue-img]][code-blue-url]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`](https://www.mathworks.com/help/matlab/ref/sound.html)
and
[`soundsc`](https://www.mathworks.com/help/matlab/ref/soundsc.html)
to facilitate code migration.## Getting started
`Sound` is a registered package,
so installation is easy:```julia
import Pkg; Pkg.add("Sound")
```## Example
```julia
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](https://jefffessler.github.io/Sound.jl/stable).Matlab's
[`audioplayer`](https://www.mathworks.com/help/matlab/ref/audioplayer.html)
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](https://github.com/JuliaAudio/SampledSignals.jl)
package,
via
[Requires.jl](https://github.com/JuliaPackaging/Requires.jl).
That type carries the sampling rate
along with the signal data,
which is attractive
compared to having two separate variables.```julia
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.```julia
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](https://github.com/JeffFessler/Sound.jl/blob/main/examples/gtk-record.jl)
for an example of creating a Record/Stop/Play GUI
using
[Gtk.jl](https://github.com/JuliaGraphics/Gtk.jl).## Phase vocoder
Also exported is the function `phase_vocoder`
that provides a Julia version
of a
[phase vocoder](https://en.wikipedia.org/wiki/Phase_vocoder),
translated from
[this Matlab code](https://sethares.engr.wisc.edu/vocoders/matlabphasevocoder.html)
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
* https://github.com/haberdashPI/SignalBase.jl
supports a `framerate` method 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 `wavplay` function
* https://github.com/JuliaAudio
has a collection of audio packages.
* https://github.com/JuliaAudio/PortAudio.jl
Currently, the `sound` function 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 how
[`Plots.jl`](https://github.com/JuliaPlots/Plots.jl)
provides a common interface to various plotting back-ends.[action-img]: https://github.com/JeffFessler/Sound.jl/workflows/CI/badge.svg
[action-url]: https://github.com/JeffFessler/Sound.jl/actions
[build-img]: https://github.com/JeffFessler/Sound.jl/workflows/CI/badge.svg?branch=main
[build-url]: https://github.com/JeffFessler/Sound.jl/actions?query=workflow%3ACI+branch%3Amain
[pkgeval-img]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/S/Sound.svg
[pkgeval-url]: https://juliaci.github.io/NanosoldierReports/pkgeval_badges/S/Sound.html
[code-blue-img]: https://img.shields.io/badge/code%20style-blue-4495d1.svg
[code-blue-url]: https://github.com/invenia/BlueStyle
[codecov-img]: https://codecov.io/github/JeffFessler/Sound.jl/coverage.svg?branch=main
[codecov-url]: https://codecov.io/github/JeffFessler/Sound.jl?branch=main
[docs-stable-img]: https://img.shields.io/badge/docs-stable-blue.svg
[docs-stable-url]: https://JeffFessler.github.io/Sound.jl/stable
[docs-dev-img]: https://img.shields.io/badge/docs-dev-blue.svg
[docs-dev-url]: https://JeffFessler.github.io/Sound.jl/dev
[license-img]: http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat
[license-url]: LICENSE
[aqua-img]: https://img.shields.io/badge/Aqua.jl-%F0%9F%8C%A2-aqua.svg
[aqua-url]: https://github.com/JuliaTesting/Aqua.jl