Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions geiger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package geiger // import "lukechampine.com/geiger"

import (
"math"
"math/rand"
"runtime"
"time"

Expand All @@ -10,17 +11,22 @@ import (
)

const sr = beep.SampleRate(44100)
const memSampleRate = 100 * time.Millisecond
const memSampleRate = 500 * time.Millisecond
const baseline = 160
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally we could measure this empirically somehow, but I suppose this is better than nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it was a bit "twitchy" with 100... At least now if you make a burst of allocations you will feel the pain for half a second :D


func Count() {
var freq, t float64
step := sr.D(1).Seconds()
speaker.Init(sr, sr.N(memSampleRate))
var p int
speaker.Init(sr, sr.N(memSampleRate)/5)
rng := rand.New(rand.NewSource(0x1337))
sign := 1.0
speaker.Play(beep.StreamerFunc(func(samples [][2]float64) (int, bool) {
for i := range samples {
t += step
a := math.Sin(2 * math.Pi * freq * t)
samples[i] = [2]float64{a, a}
if rng.Intn(int(sr)) < p {
samples[i] = [2]float64{sign, sign}
sign *= -1
} else {
samples[i] = [2]float64{0, 0}
}
}
return len(samples), true
}))
Expand All @@ -32,8 +38,13 @@ func Count() {
runtime.ReadMemStats(&ms)
diff := ms.Mallocs - prevAllocs
prevAllocs = ms.Mallocs
// Allocs per second
rate := float64(diff)/memSampleRate.Seconds() - baseline
// If it becomes too high it becomes inaudible.
// Limit at 3.6 Roentgen.
rate = math.Min(rate, float64(sr)/3.6)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not great, not terrible

speaker.Lock()
freq = float64(diff) / memSampleRate.Seconds()
p = int(rate)
speaker.Unlock()
}
}
65 changes: 65 additions & 0 deletions geiger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package geiger

import (
"bytes"
"io"
"io/ioutil"
"testing"
"time"
)

// TestCount requires human ears.
func TestCount(t *testing.T) {
go Count()
tests := []struct {
name string
hz int
per int
}{
{
name: "baseline",
hz: 1,
per: 0,
},
{
name: "5hz",
hz: 5,
per: 1,
}, {
name: "50hz",
hz: 50,
per: 1,
}, {
name: "500hz",
hz: 50,
per: 10,
}, {
name: "5000hz",
hz: 50,
per: 100,
}, {
name: "50000hz",
hz: 50,
per: 1000,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
done := time.After(5 * time.Second)
every := time.NewTicker(time.Second / time.Duration(tt.hz))
for {
select {
case <-done:
return
case <-every.C:
for i := 0; i < tt.per; i++ {
// alloc tmp
tmp := bytes.Buffer{}
io.Copy(ioutil.Discard, &tmp)
}
}
}
})
}
}