Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Binozo committed Nov 16, 2023
1 parent 1791a53 commit eb95426
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
13 changes: 9 additions & 4 deletions pkg/pcm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package pcm
import "github.com/Binozo/GoTinyAlsa/internal/tinypcm"

type Config struct {
Channels int
SampleRate int
PeriodSize int
PeriodCount int
// The number of audio channels
Channels int
// Sample rate (the higher, the better)
SampleRate int
// Number of frames in a period
PeriodSize int
// Number of periods
PeriodCount int
// IO Format
Format tinypcm.Format
StartThreshold int
StopThreshold int
Expand Down
41 changes: 28 additions & 13 deletions pkg/pcm/info.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
package pcm

type Info struct {
Access uint
Format0 uint
Format1 uint
FormatNames []string
Subformat uint
RateMin uint
RateMax uint
ChannelsMin uint
ChannelsMax uint
SampleBitsMin uint
SampleBitsMax uint
PeriodSizeMin uint
PeriodSizeMax uint
// Mask that represents the type of read or write methods available
Access uint
// Mask that represents the PCM_FORMAT available (e.g. PCM_FORMAT_32_LE)
Format0 uint
// Mask that represents the PCM_FORMAT available (e.g. PCM_FORMAT_32_LE)
Format1 uint
// Pcm formats available
FormatNames []string
// Mask that represents the subformat available
Subformat uint
// Minimum available rate
RateMin uint
// Maximum available rate
RateMax uint
// Minimum available channels
ChannelsMin uint
// Maximum available channels
ChannelsMax uint
// Minimum available sample bits
SampleBitsMin uint
// Maximum available sample bits
SampleBitsMax uint
// Minimum available period size
PeriodSizeMin uint
// Maximum available period size
PeriodSizeMax uint
// Minimum available period count
PeriodCountMin uint
// Maximum available period count
PeriodCountMax uint
}
2 changes: 2 additions & 0 deletions pkg/tinyalsa/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const PCM_FORMAT_S32_LE = tinyapi.PCM_FORMAT_S32_LE
const PCM_FORMAT_S32_BE = tinyapi.PCM_FORMAT_S32_BE
const ErrorTolerance = 10 // defines how many error frames are allowed to be read without stopping reading the next ones

// GetAudioStream Listens to the input of the defined device
func (d *AlsaDevice) GetAudioStream(config pcm.Config, audioData chan []byte) error {
pcmDevice, err := tinyapi.PcmOpen(d.Card, d.Device, PCM_IN, config)
if err != nil {
Expand Down Expand Up @@ -61,6 +62,7 @@ FrameReader:
return nil
}

// SendAudioStream plays the given audio data (usually wav) to the device
func (d *AlsaDevice) SendAudioStream(audioData []byte) error {
pcmDevice, err := tinyapi.PcmOpen(d.Card, d.Device, PCM_OUT, d.DeviceConfig)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/tinyalsa/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type AlsaDevice struct {
DeviceConfig pcm.Config
}

// NewDevice defines a new device you want to interact with
func NewDevice(cardNr int, deviceNr int, deviceConfig pcm.Config) AlsaDevice {
return AlsaDevice{
Card: cardNr,
Expand All @@ -20,6 +21,7 @@ func NewDevice(cardNr int, deviceNr int, deviceConfig pcm.Config) AlsaDevice {
}
}

// BestDeviceConfig return you the best device config to use
func BestDeviceConfig(cardNr int, deviceNr int, format int) pcm.Config {
device := AlsaDevice{
Card: cardNr,
Expand All @@ -42,6 +44,7 @@ func BestDeviceConfig(cardNr int, deviceNr int, format int) pcm.Config {
}
}

// GetInfo returns information data about the given device's input & output
func (d *AlsaDevice) GetInfo() DeviceInfo {
inInfo, outInfo := tinyapi.GetParams(d.Card, d.Device)
return DeviceInfo{
Expand Down
6 changes: 5 additions & 1 deletion pkg/tinyalsa/deviceinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package tinyalsa
import "github.com/Binozo/GoTinyAlsa/pkg/pcm"

type DeviceInfo struct {
// Output info (e.g. speaker)
Out pcm.Info
In pcm.Info
// Input info (e.g. microphone)
In pcm.Info
}

// HasOutput returns true, if the given device has an output (e.g. speaker)
func (i *DeviceInfo) HasOutput() bool {
return i.Out.Access != 0 && i.Out.RateMin != 0
}

// HasInput returns true, if the given device has an input (e.g. microphone)
func (i *DeviceInfo) HasInput() bool {
return i.In.Access != 0 && i.In.RateMin != 0
}

0 comments on commit eb95426

Please sign in to comment.