Replace 'beep' with pulse to enable pulseaudio playback

This commit is contained in:
Adriano Caloiaro 2020-11-18 09:45:04 -07:00
parent c106bda7d4
commit a11810e45f
No known key found for this signature in database
GPG key ID: 9FFD0E7601F166AB
5 changed files with 29 additions and 23 deletions

View file

@ -2,14 +2,25 @@
A simple terminal UI player for [di.fm Premium](http://di.fm)
![App Screenshot](https://user-images.githubusercontent.com/3331648/81481515-bb668400-91fe-11ea-8a7c-39e1bb76c55d.png)
# Caveat
This player is a somewhat crude proof-of-concept that was written over the course of ~8 hours and slightly improved upon since. There are not doubt bugs. Help me improve it if you find it useful.
# Dependencies
## PulseAudio
Both linux and MacOS depend on pulseaudio to be running.
### MacOS
By default, pulseaudio on MacOS runs as "root", which is not ideal. PulseAudio is best run by non-root users. By symbolically linking the pulseaudio plist file into your user's `~/Library/LaunchAgents/`, it runs as your user.
```
brew install pulseaudio
ln -s $(brew info pulseaudio | grep "/usr/local/Cellar" | awk '{print $1}')//homebrew.mxcl.pulseaudio.plist ~/Library/LaunchAgents
brew services start pulseaudio
```
# Install
This app has been tested on Linux and Mac, but not Windows. However, it should also build on Windows.
## Binary Releases
There are binary builds available in [releases](https://github.com/acaloiaro/di-tui/releases).

View file

@ -178,9 +178,9 @@ func Stream(url string, ctx *context.AppContext) (format beep.Format) {
return
}
player.Play(ctx, resp.Body)
go func() { player.Play(ctx, resp.Body) }()
if err != nil {
ctx.SetStatusMessage("There was a problem streaming audio.")
ctx.SetStatusMessage(fmt.Sprintf("There was a problem streaming audio: %s", err.Error()))
return
}

2
go.mod
View file

@ -26,3 +26,5 @@ require (
gopkg.in/ini.v1 v1.52.0
gopkg.in/yaml.v2 v2.2.8 // indirect
)
replace github.com/jfreymuth/pulse => github.com/acaloiaro/pulse v1.0.1

4
go.sum
View file

@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/acaloiaro/pulse v1.0.1 h1:mK0E6qJGklcvLZKrVcT0y/5XucCaDzAxdUSu8NyvB9I=
github.com/acaloiaro/pulse v1.0.1/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
@ -69,8 +71,6 @@ github.com/hajimehoshi/oto v0.3.4/go.mod h1:PgjqsBJff0efqL2nlMJidJgVJywLn6M4y8PI
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jfreymuth/oggvorbis v1.0.0/go.mod h1:abe6F9QRjuU9l+2jek3gj46lu40N4qlYxh2grqkLEDM=
github.com/jfreymuth/pulse v0.0.0-20201014123913-1e525c426c93 h1:gDcaH96SZ7q1JU6hj0tSv8FiuqadFERU17lLxhphLa8=
github.com/jfreymuth/pulse v0.0.0-20201014123913-1e525c426c93/go.mod h1:cpYspI6YljhkUf1WLXLLDmeaaPFc3CnGLjDZf9dZ4no=
github.com/jfreymuth/vorbis v1.0.0/go.mod h1:8zy3lUAm9K/rJJk223RKy6vjCZTWC61NA2QD06bfOE0=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=

View file

@ -3,8 +3,6 @@ package player
import (
"fmt"
"io"
"log"
"os"
"github.com/acaloiaro/di-tui/context"
"github.com/hajimehoshi/go-mp3"
@ -12,10 +10,10 @@ import (
"github.com/jfreymuth/pulse/proto"
)
func Play(ctx *context.AppContext, stream io.Reader) {
func Play(ctx *context.AppContext, stream io.Reader) (err error) {
d, err := mp3.NewDecoder(stream)
if err != nil {
log.Println("Unable to decode mp3 file")
return
}
c, err := pulse.NewClient()
@ -33,16 +31,11 @@ func Play(ctx *context.AppContext, stream io.Reader) {
pulse.PlaybackBufferSize(16482),
)
if err != nil {
fmt.Println(err)
return
}
go func() {
ctx.AudioStream.Start()
ctx.AudioStream.Drain()
if ctx.AudioStream.Underflow() {
log.Println("Underflow!")
os.Exit(1)
}
}()
return
}