Commit 83f3c4f1 authored by Trevor Slocum's avatar Trevor Slocum

Add rate limit to file descriptor streaming

parent df10c131
Pipeline #111888763 passed with stage
in 1 minute and 5 seconds
......@@ -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
......
......@@ -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)
}
}()
......
......@@ -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() {
......
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()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment