Skip to content

Commit

Permalink
Audio back in working order, without dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
200sc committed Jul 7, 2019
1 parent f87eb3e commit a07be93
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions audio/encode_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
package audio

import (
"fmt"

"github.com/pkg/errors"
"github.com/yobert/alsa"
)

type alsaAudio struct {
*Encoding
*alsa.Device
bytesPerPeriod int
period int
playProgress int
stopCh chan struct{}
playing bool
playCh chan error
playAmount int
playProgress int
stopCh chan struct{}
playing bool
playCh chan error
period int
}

func (aa *alsaAudio) Play() <-chan error {
Expand All @@ -31,14 +29,14 @@ func (aa *alsaAudio) Play() <-chan error {
go func() {
for {
var data []byte
if len(aa.Encoding.Data)-aa.playProgress <= aa.period {
if len(aa.Encoding.Data)-aa.playProgress <= aa.playAmount {
data = aa.Encoding.Data[aa.playProgress:]
if aa.Loop {
delta := aa.period - (len(aa.Encoding.Data) - aa.playProgress)
delta := aa.playAmount - (len(aa.Encoding.Data) - aa.playProgress)
data = append(data, aa.Encoding.Data[:delta]...)
}
} else {
data = aa.Encoding.Data[aa.playProgress : aa.playProgress+aa.period]
data = aa.Encoding.Data[aa.playProgress : aa.playProgress+aa.playAmount]
}
if len(data) != 0 {
err := aa.Device.Write(data, aa.period)
Expand All @@ -50,7 +48,7 @@ func (aa *alsaAudio) Play() <-chan error {
break
}
}
aa.playProgress += aa.period
aa.playProgress += aa.playAmount
if aa.playProgress > len(aa.Encoding.Data) {
if aa.Loop {
aa.playProgress %= len(aa.Encoding.Data)
Expand Down Expand Up @@ -117,10 +115,6 @@ func EncodeBytes(enc Encoding) (Audio, error) {
if err != nil {
return nil, err
}
//err := handle.Open("default", alsa.StreamTypePlayback, alsa.ModeBlock)
//if err != nil {
// return nil, err
//}
// Todo: annotate these errors with more info
format, err := alsaFormat(enc.Bits)
if err != nil {
Expand Down Expand Up @@ -152,11 +146,11 @@ func EncodeBytes(enc Encoding) (Audio, error) {
return nil, err
}
return &alsaAudio{
Encoding: &enc,
Device: handle,
period: period,
stopCh: make(chan struct{}),
bytesPerPeriod: period * (int(enc.Bits) / 8),
playAmount: period * int(enc.Bits) / 4,
period: period,
Encoding: &enc,
Device: handle,
stopCh: make(chan struct{}),
}, nil
}

Expand All @@ -166,14 +160,12 @@ func openDevice() (*alsa.Device, error) {
return nil, err
}
for i, c := range cards {
fmt.Println("Card", c)
dvcs, err := c.Devices()
if err != nil {
alsa.CloseCards([]*alsa.Card{c})
continue
}
for _, d := range dvcs {
fmt.Println("Device", d)
if d.Type != alsa.PCM || !d.Play {
continue
}
Expand Down

0 comments on commit a07be93

Please sign in to comment.