Add rate limit to file descriptor streaming

This commit is contained in:
Trevor Slocum 2020-01-23 17:46:05 -08:00
parent df10c1312c
commit 83f3c4f1c8
4 changed files with 37 additions and 11 deletions

View file

@ -3,9 +3,11 @@ configuration options and their defaults.
# Options
* **--buffer-size** Audio buffer size (default 500ms)
* **--fd** Write audio in WAV format to the specified file descriptor. This allows ditty to be used over ssh:
* **--buffer-size** Audio buffer size, defaults to 500ms.
* **--restrict-library** Restrict access to a folder and its subfolders.
* **--fd** Write audio in WAV format to the specified file descriptor instead. This allows ditty to be used over ssh:
* `ssh ditty.rocketnine.space -t 'ditty --fd=2' 2> >(aplay --quiet)`
* Note: Writing to a file descriptor disables pause support.
# Default keybindings

View file

@ -30,10 +30,6 @@ var (
playingFormat beep.Format
playingSampleRate beep.SampleRate
nextStreamer beep.StreamSeekCloser
nextFormat beep.Format
nextFileName string
volume *effects.Volume
ctrl *beep.Ctrl
@ -155,8 +151,9 @@ func play(audioFile *audioFile) {
streamFd = os.NewFile(uintptr(streamFdInt), "out")
go func() {
rateLimitedStreamer := NewRateLimitedStreamer(ctrl, bufferSize/64)
for {
_ = wav.Encode(streamFd, ctrl, playingFormat)
_ = wav.Encode(streamFd, rateLimitedStreamer, playingFormat)
time.Sleep(250 * time.Millisecond)
}
}()

View file

@ -3,8 +3,6 @@ package main
import (
"path"
"time"
"github.com/faiface/beep"
)
func listPrevious() {
@ -34,8 +32,6 @@ func listSelect() {
browseFolder(path.Join(mainBufferDirectory, ".."))
return
}
nextStreamer = nil
nextFormat = beep.Format{}
entry := selectedEntry()
if entry.File.IsDir() {

31
streamer.go Normal file
View file

@ -0,0 +1,31 @@
package main
import (
"time"
"github.com/faiface/beep"
)
type RateLimitedStreamer struct {
streamer beep.Streamer
rate time.Duration
last time.Time
}
func NewRateLimitedStreamer(streamer beep.Streamer, rate time.Duration) *RateLimitedStreamer {
s := RateLimitedStreamer{streamer: streamer, rate: rate}
return &s
}
func (s *RateLimitedStreamer) Stream(samples [][2]float64) (n int, ok bool) {
if time.Since(s.last) < s.rate {
time.Sleep(time.Until(s.last.Add(s.rate)))
}
s.last = time.Now()
return s.streamer.Stream(samples)
}
func (s *RateLimitedStreamer) Err() error {
return s.streamer.Err()
}