mirror of https://bitbucket.org/ausocean/av.git
audio: renamed AudioDevice to ADPCMDevice
This commit is contained in:
parent
b66f415e1c
commit
733785254e
|
@ -61,8 +61,8 @@ const (
|
||||||
stopped
|
stopped
|
||||||
)
|
)
|
||||||
|
|
||||||
// AudioDevice holds everything we need to know about the audio input stream and implements io.Reader.
|
// ADPCMDevice holds everything we need to know about the audio input stream and implements io.Reader.
|
||||||
type AudioDevice struct {
|
type ADPCMDevice struct {
|
||||||
l Logger // Logger for device's routines to log to.
|
l Logger // Logger for device's routines to log to.
|
||||||
mode uint8 // Operating mode, either running, paused, or stopped.
|
mode uint8 // Operating mode, either running, paused, or stopped.
|
||||||
mu sync.Mutex // Provides synchronisation when changing modes concurrently.
|
mu sync.Mutex // Provides synchronisation when changing modes concurrently.
|
||||||
|
@ -74,7 +74,7 @@ type AudioDevice struct {
|
||||||
Config // Configuration parameters for this device.
|
Config // Configuration parameters for this device.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config provides parameters used by AudioDevice.
|
// Config provides parameters used by ADPCMDevice.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
SampleRate int
|
SampleRate int
|
||||||
Channels int
|
Channels int
|
||||||
|
@ -93,14 +93,14 @@ type Logger interface {
|
||||||
// OpenError is used to determine whether an error has originated from attempting to open a device.
|
// OpenError is used to determine whether an error has originated from attempting to open a device.
|
||||||
type OpenError error
|
type OpenError error
|
||||||
|
|
||||||
// NewAudioDevice initializes and returns a AudioDevice which has its logger set as the given logger.
|
// NewADPCMDevice initializes and returns an ADPCMDevice which has its logger set as the given logger.
|
||||||
func NewAudioDevice(l Logger) *AudioDevice { return &AudioDevice{l: l} }
|
func NewADPCMDevice(l Logger) *ADPCMDevice { return &ADPCMDevice{l: l} }
|
||||||
|
|
||||||
// Set will take a Config struct, check the validity of the relevant fields
|
// Set will take a Config struct, check the validity of the relevant fields
|
||||||
// and then performs any configuration necessary. If fields are not valid,
|
// and then performs any configuration necessary. If fields are not valid,
|
||||||
// an error is added to the multiError and a default value is used.
|
// an error is added to the multiError and a default value is used.
|
||||||
// It then initialises the AudioDevice which can then be started, read from, and stopped.
|
// It then initialises the ADPCMDevice which can then be started, read from, and stopped.
|
||||||
func (d *AudioDevice) Set(c config.Config) error {
|
func (d *ADPCMDevice) Set(c config.Config) error {
|
||||||
var errs device.MultiError
|
var errs device.MultiError
|
||||||
if c.SampleRate <= 0 {
|
if c.SampleRate <= 0 {
|
||||||
errs = append(errs, fmt.Errorf("invalid sample rate: %v", c.SampleRate))
|
errs = append(errs, fmt.Errorf("invalid sample rate: %v", c.SampleRate))
|
||||||
|
@ -162,8 +162,8 @@ func (d *AudioDevice) Set(c config.Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start will start recording audio and writing to the ringbuffer.
|
// Start will start recording audio and writing to the ringbuffer.
|
||||||
// Once a AudioDevice has been stopped it cannot be started again. This is likely to change in future.
|
// Once a ADPCMDevice has been stopped it cannot be started again. This is likely to change in future.
|
||||||
func (d *AudioDevice) Start() error {
|
func (d *ADPCMDevice) Start() error {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
mode := d.mode
|
mode := d.mode
|
||||||
d.mu.Unlock()
|
d.mu.Unlock()
|
||||||
|
@ -184,15 +184,15 @@ func (d *AudioDevice) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop will stop recording audio and close the device.
|
// Stop will stop recording audio and close the device.
|
||||||
// Once a AudioDevice has been stopped it cannot be started again. This is likely to change in future.
|
// Once a ADPCMDevice has been stopped it cannot be started again. This is likely to change in future.
|
||||||
func (d *AudioDevice) Stop() {
|
func (d *ADPCMDevice) Stop() {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
d.mode = stopped
|
d.mode = stopped
|
||||||
d.mu.Unlock()
|
d.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChunkSize returns the number of bytes written to the ringbuffer per d.RecPeriod.
|
// ChunkSize returns the number of bytes written to the ringbuffer per d.RecPeriod.
|
||||||
func (d *AudioDevice) ChunkSize() int {
|
func (d *ADPCMDevice) ChunkSize() int {
|
||||||
return d.chunkSize
|
return d.chunkSize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,7 +218,7 @@ func validate(c *Config) error {
|
||||||
|
|
||||||
// open the recording device with the given name and prepare it to record.
|
// open the recording device with the given name and prepare it to record.
|
||||||
// If name is empty, the first recording device is used.
|
// If name is empty, the first recording device is used.
|
||||||
func (d *AudioDevice) open() error {
|
func (d *ADPCMDevice) open() error {
|
||||||
// Close any existing device.
|
// Close any existing device.
|
||||||
if d.dev != nil {
|
if d.dev != nil {
|
||||||
d.l.Log(logger.Debug, pkg+"closing device", "title", d.title)
|
d.l.Log(logger.Debug, pkg+"closing device", "title", d.title)
|
||||||
|
@ -351,7 +351,7 @@ func (d *AudioDevice) open() error {
|
||||||
|
|
||||||
// input continously records audio and writes it to the ringbuffer.
|
// input continously records audio and writes it to the ringbuffer.
|
||||||
// Re-opens the device and tries again if ASLA returns an error.
|
// Re-opens the device and tries again if ASLA returns an error.
|
||||||
func (d *AudioDevice) input() {
|
func (d *ADPCMDevice) input() {
|
||||||
for {
|
for {
|
||||||
// Check mode.
|
// Check mode.
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
|
@ -402,7 +402,7 @@ func (d *AudioDevice) input() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read reads from the ringbuffer, returning the number of bytes read upon success.
|
// Read reads from the ringbuffer, returning the number of bytes read upon success.
|
||||||
func (d *AudioDevice) Read(p []byte) (int, error) {
|
func (d *ADPCMDevice) Read(p []byte) (int, error) {
|
||||||
// Ready ringbuffer for read.
|
// Ready ringbuffer for read.
|
||||||
_, err := d.rb.Next(rbNextTimeout)
|
_, err := d.rb.Next(rbNextTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -414,7 +414,7 @@ func (d *AudioDevice) Read(p []byte) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// formatBuffer returns audio that has been converted to the desired format.
|
// formatBuffer returns audio that has been converted to the desired format.
|
||||||
func (d *AudioDevice) formatBuffer() alsa.Buffer {
|
func (d *ADPCMDevice) formatBuffer() alsa.Buffer {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// If nothing needs to be changed, return the original.
|
// If nothing needs to be changed, return the original.
|
||||||
|
|
|
@ -47,9 +47,9 @@ func TestDevice(t *testing.T) {
|
||||||
}
|
}
|
||||||
n := 2 // Number of periods to wait while recording.
|
n := 2 // Number of periods to wait while recording.
|
||||||
|
|
||||||
// Create a new audio Device, start, read/lex, and then stop it.
|
// Create a new ADPCMDevice, start, read/lex, and then stop it.
|
||||||
l := logger.New(logger.Debug, os.Stderr, true)
|
l := logger.New(logger.Debug, os.Stderr, true)
|
||||||
ai := NewAudioDevice(l)
|
ai := NewADPCMDevice(l)
|
||||||
err := ai.Set(c)
|
err := ai.Set(c)
|
||||||
// If there was an error opening the device, skip this test.
|
// If there was an error opening the device, skip this test.
|
||||||
if _, ok := err.(OpenError); ok {
|
if _, ok := err.(OpenError); ok {
|
||||||
|
|
|
@ -29,7 +29,7 @@ import (
|
||||||
// It returns a function that can be used to stop the device and any errors that occur.
|
// It returns a function that can be used to stop the device and any errors that occur.
|
||||||
func (r *Revid) startAudioDevice() (func() error, error) {
|
func (r *Revid) startAudioDevice() (func() error, error) {
|
||||||
// Create audio device.
|
// Create audio device.
|
||||||
ai := audio.NewAudioDevice(r.cfg.Logger)
|
ai := audio.NewADPCMDevice(r.cfg.Logger)
|
||||||
|
|
||||||
err := ai.Set(r.cfg)
|
err := ai.Set(r.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue