aio

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2022 License: MIT Imports: 11 Imported by: 1

README

aio

A simple Audio I/O library written in Go. This library relies on FFmpeg, FFProbe and FFPlay which must be downloaded before usage and added to the system path.

For Video I/O using FFmpeg. see the Vidio project.

Installation

go get github.com/AlexEidt/aio

Buffers

aio uses byte buffers to transport audio data. Audio data can take on many forms, including floating point, unsigned integer and signed integer. These types may be larger than a byte and would have to be split. Learn more about available audio types from the FFmpeg Wiki. alaw and mulaw codecs are currently not supported.

As an example, if there is stereo sound (two channels) encoded in the s16le (signed 16 bit integers, little endian) format with a sampling rate of 44100 Hz, one second of audio would be

44100 * 2 (channels) * 2 (bytes per sample) = 176400 bytes

Continuing on with this example, since this is stereo audio with 2 channels, one frame of audio is represented by 2 consecutive integers, one for each channel. Each integer is 16 bits, which means one frame of audio would be represented by 4 consecutive bytes.

Options

The Options struct is used to specify optional parameters for Audio I/O.

type Options struct {
	SampleRate int    // Sample rate in Hz.
	Channels   int    // Number of channels.
	Bitrate    int    // Bitrate.
	Format     string // Format of audio.
	Codec      string // Audio Codec.
	Video      string // Video file to use.
}

Audio

Audio is used to read audio from files. It can also be used to gather audio metadata from a file. By default, the audio buffer has a length of

sample rate * channels * bytes per sample

which corresponds to 1 second of audio data.

The user may pass in options to set the desired sampling rate, format and channels of the audio. If options is nil, then the channels and sampling rate from the file will be used, with a default format of "s16le".

aio.NewAudio(filename string, options *aio.Options) (*aio.Audio, error)

FileName() string
SampleRate() int
Channels() int
Bitrate() int
Duration() float64
Format() string
Codec() string
BitsPerSample() int
Buffer() []byte
SetBuffer(buffer []byte)

Read() bool
Close()

AudioWriter

AudioWriter is used to write audio to files from a byte buffer. It comes with an Options struct that can be used to specify certain metadata of the output audio file. If options is nil, the defaults used are a sampling rate of 44100 Hz, with 2 channels in the "s16le" format.

aio.NewAudioWriter(filename string, options *aio.Options) (*aio.AudioWriter, error)

FileName() string
SampleRate() int
Channels() int
Bitrate() int
Format() string
Codec() string
Video() string

Write(buffer []byte) error
Close()

Microphone

Microphone is similar to the Audio struct, the only difference being that it reads audio from the microphone. The stream parameter is used to specify the microphone stream index, which will differ depending on the platform. For Windows (dshow) and MacOS (avfoundation), find the stream index by entering the following command

ffmpeg -f [dshow | avfoundation] -list_devices true -i dummy

and selecting the desired stream. For linux, see this page on the FFmpeg Wiki.

Additionally, an options parameter may be passed to specify the format, sampling rate and audio channels the microphone should record at. Any other options are ignored.

aio.NewMicrophone(stream int, options *aio.Options) (*aio.Microphone, error)

Name() string
SampleRate() int
Channels() int
Format() string
BitsPerSample() int
Buffer() []byte
SetBuffer(buffer []byte)

Read() bool
Close()

Player

Player is used to play audio from a byte buffer.

aio.NewPlayer(channels, sampleRate int, format string) (*aio.Player, error)

Play(buffer []byte) error
Close()

Additionally, files may be played directly using the Play function:

aio.Play(filename string) error

Examples

Copy input.wav to output.mp3.

audio, _ := aio.NewAudio("input.wav", nil)

options := aio.Options{
	SampleRate: audio.SampleRate(),
	Channels:   audio.Channels(),
	Bitrate:    audio.Bitrate(),
	Format:     audio.Format(),
}

writer, _ := aio.NewAudioWriter("output.mp3", &options)
defer writer.Close()

for audio.Read() {
	writer.Write(audio.Buffer())
}

Capture 10 seconds of audio from the microphone. Audio is recorded at 44100 Hz stereo and is in signed 16 bit format.

micOptions := aio.Options{Format: "s16le", Channels: 2, SampleRate: 44100}
mic, _ := aio.NewMicrophone(0, &micOptions)
defer mic.Close()

writerOptions := aio.Options{
	SampleRate: mic.SampleRate(),
	Channels:   mic.Channels(),
	Format:     mic.Format(),
}

writer, _ := aio.NewAudioWriter("output.wav", &writerOptions)
defer writer.Close()

seconds := 0
for mic.Read() && seconds < 10 {
	writer.Write(mic.Buffer())
	seconds++
}

Play input.mp4.

audio, _ := aio.NewAudio("input.mp4", nil)
player, _ := aio.NewPlayer(
	audio.Channels(),
	audio.SampleRate(),
	audio.Format(),
)
defer player.Close()

for audio.Read() {
	player.Play(audio.Buffer())
}

Combine sound.wav and movie.mp4 into output.mp4.

audio, _ := aio.NewAudio("sound.wav", nil)

options := aio.Options{
	SampleRate: audio.SampleRate(),
	Channels:   audio.Channels(),
	Bitrate:    audio.Bitrate(),
	Format:     audio.Format(),
	Codec:      audio.Codec(),
	Video:      "movie.mp4",
}

writer, _ := aio.NewAudioWriter("output.mp4", &options)
defer writer.Close()

for audio.Read() {
	writer.Write(audio.Buffer())
}

Play Microphone audio. Use default microphone settings for recording.

mic, _ := aio.NewMicrophone(0, nil)
defer mic.Close()

player, _ := aio.NewPlayer(
	mic.Channels(),
	mic.SampleRate(),
	mic.Format(),
)
defer player.Close()

for mic.Read() {
	player.Play(mic.Buffer())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Play

func Play(filename string) error

Types

type Audio

type Audio struct {
	// contains filtered or unexported fields
}

func NewAudio

func NewAudio(filename string, options *Options) (*Audio, error)

func (*Audio) Bitrate

func (audio *Audio) Bitrate() int

func (*Audio) BitsPerSample

func (audio *Audio) BitsPerSample() int

func (*Audio) Buffer

func (audio *Audio) Buffer() []byte

func (*Audio) Channels

func (audio *Audio) Channels() int

func (*Audio) Close

func (audio *Audio) Close()

Closes the pipe and stops the ffmpeg process.

func (*Audio) Codec

func (audio *Audio) Codec() string

func (*Audio) Duration

func (audio *Audio) Duration() float64

func (*Audio) FileName

func (audio *Audio) FileName() string

func (*Audio) Format

func (audio *Audio) Format() string

func (*Audio) Read

func (audio *Audio) Read() bool

Reads the next frame from of audio and stores it in the buffer. If the last frame has been read, returns false, otherwise true.

func (*Audio) SampleRate

func (audio *Audio) SampleRate() int

func (*Audio) SetBuffer

func (audio *Audio) SetBuffer(buffer []byte)

type AudioWriter

type AudioWriter struct {
	// contains filtered or unexported fields
}

func NewAudioWriter

func NewAudioWriter(filename string, options *Options) (*AudioWriter, error)

func (*AudioWriter) Bitrate

func (writer *AudioWriter) Bitrate() int

func (*AudioWriter) Channels

func (writer *AudioWriter) Channels() int

func (*AudioWriter) Close

func (writer *AudioWriter) Close()

Closes the pipe and stops the ffmpeg process.

func (*AudioWriter) Codec

func (writer *AudioWriter) Codec() string

func (*AudioWriter) FileName

func (writer *AudioWriter) FileName() string

func (*AudioWriter) Format

func (writer *AudioWriter) Format() string

func (*AudioWriter) SampleRate

func (writer *AudioWriter) SampleRate() int

func (*AudioWriter) Video

func (writer *AudioWriter) Video() string

func (*AudioWriter) Write

func (writer *AudioWriter) Write(buffer []byte) error

Writes the given buffer to the audio file.

type Microphone

type Microphone struct {
	// contains filtered or unexported fields
}

func NewMicrophone

func NewMicrophone(stream int, options *Options) (*Microphone, error)

func (*Microphone) BitsPerSample

func (mic *Microphone) BitsPerSample() int

func (*Microphone) Buffer

func (mic *Microphone) Buffer() []byte

func (*Microphone) Channels

func (mic *Microphone) Channels() int

func (*Microphone) Close

func (mic *Microphone) Close()

Closes the pipe and stops the ffmpeg process.

func (*Microphone) Format

func (mic *Microphone) Format() string

func (*Microphone) Name

func (mic *Microphone) Name() string

func (*Microphone) Read

func (mic *Microphone) Read() bool

Reads the next audio sample from the microphone and stores in the buffer.

func (*Microphone) SampleRate

func (mic *Microphone) SampleRate() int

func (*Microphone) SetBuffer

func (mic *Microphone) SetBuffer(buffer []byte)

Sets the framebuffer to the given byte array. Note that "buffer" must be large enough to store one frame of mic data.

type Options

type Options struct {
	SampleRate int    // Sample rate in Hz.
	Channels   int    // Number of channels.
	Bitrate    int    // Bitrate.
	Format     string // Format of audio.
	Codec      string // Audio Codec.
	Video      string // Video file to use.
}

type Player

type Player struct {
	// contains filtered or unexported fields
}

func NewPlayer

func NewPlayer(channels, sampleRate int, format string) (*Player, error)

func (*Player) Close

func (player *Player) Close()

Closes the pipe and stops the ffplay process.

func (*Player) Play

func (player *Player) Play(buffer []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL