audio
is a collection of package to handle audio inputs, outputs and processing in Go.
It currently supports 3 backends:
oto
(https://github.com/ebitengine/oto) [for all OSes, but only playback]portaudio
(https://github.com/gordonklaus/portaudio) [for Windows]pulseaudio
(github.com/jfreymuth/pulse) [for Linux]
And it has various modules for audio processing:
- Basics:
resampler
,planar
. - Noise suppression, also in streaming mode.
- Voice Activity Detector
- For speech processing see also github.com/xaionaro-go/speech.
BEEP using a vorbis audio file
import (
...
"github.com/xaionaro-go/audio/pkg/audio"
// select the backends you want to use:
_ "github.com/xaionaro-go/audio/pkg/audio/backends/oto"
_ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio"
)
func beep(...) {
...
player := audio.NewPlayerAuto(ctx)
defer player.Close()
stream, err := player.PlayVorbis(ctx, vorbisReader) // or use PlayPCM if the byte stream is PCM
...
}
To use a specific backend:
pulsePCMPlayer := pulse.NewPlayerPCM()
player := audio.NewPlayer(pulsePCMPlayer)
defer player.Close()
stream, err := player.PlayVorbis(ctx, vorbisReader)
RECORD
import (
...
"github.com/xaionaro-go/audio/pkg/audio"
_ "github.com/xaionaro-go/audio/pkg/audio/backends/portaudio"
_ "github.com/xaionaro-go/audio/pkg/audio/backends/pulseaudio"
)
func record5Seconds(ctx context.Context, w io.Writer) {
ctx, cancelFn := context.WithCancel(ctx)
recorder := audio.NewRecorderAuto(ctx)
defer recorder.Close()
streamRecord, err := recorder.RecordPCM(ctx, 48000, 2, audio.PCMFormatFloat32LE, w)
defer streamRecord.Close()
time.Sleep(5 * time.Second)
cancelFn()
}