Skip to content

Commit

Permalink
[stream] Add in "ThrottleSecondsPerSecond"
Browse files Browse the repository at this point in the history
This is a very light refactor of 'Throttle', where it will allow the
user to control the duration to send a seconds-worth of IQ samples
over. As a result, Throttle is now refactored to be
'ThrottleSecondsPerSecond(r, time.Second)'.
  • Loading branch information
paultag committed May 3, 2023
1 parent 856fde5 commit b2c83a5
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions stream/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ import (
"hz.tools/sdr"
)

// Throttle will read the sdr.Reader's SampleRate, and throttle the stream
// to play the Reader back "real time".
// Throttle will read the sdr.Reader's SampleRate, and throttle
// the stream to play the Reader back "real time".
func Throttle(r sdr.Reader) (sdr.Reader, error) {
return ThrottleSecondsPerSecond(r, time.Second)
}

// ThrottleSecondsPerSecond will read the sdr.Reader's SampleRate, and throttle
// the stream to play the Reader back where the duration 'd' passes every
// second.
func ThrottleSecondsPerSecond(r sdr.Reader, d time.Duration) (sdr.Reader, error) {
// Let's search for an easy window to size to use.
var windowRate uint = 20
for ; r.SampleRate()%windowRate == 0; windowRate++ {
Expand All @@ -40,7 +47,7 @@ func Throttle(r sdr.Reader) (sdr.Reader, error) {
}
go func() {
defer pipeWriter.Close()
clock := time.NewTicker(time.Second / time.Duration(windowRate))
clock := time.NewTicker(d / time.Duration(windowRate))
defer clock.Stop()
for {
_, err := sdr.ReadFull(r, buf)
Expand Down

0 comments on commit b2c83a5

Please sign in to comment.