Skip to content

Commit de91c51

Browse files
committed
more changes to processor
1 parent c736131 commit de91c51

File tree

8 files changed

+138
-67
lines changed

8 files changed

+138
-67
lines changed

catnip.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/pkg/errors"
1111
)
1212

13+
const MaxChannelCount = 2
14+
const MaxSampleSize = 2048
15+
1316
type SetupFunc func() error
1417
type StartFunc func(ctx context.Context) (context.Context, error)
1518
type CleanupFunc func() error
@@ -79,14 +82,11 @@ func Run(cfg *Config, ctx context.Context) error {
7982
}
8083
}
8184

82-
ctx = vis.Start(ctx)
83-
defer vis.Stop()
84-
8585
kickChan := make(chan bool, 1)
86-
8786
mu := &sync.Mutex{}
8887

89-
go vis.Process(ctx, kickChan, mu)
88+
ctx = vis.Start(ctx, kickChan, mu)
89+
defer vis.Stop()
9090

9191
if err := audio.Start(ctx, inputBuffers, kickChan, mu); err != nil {
9292
if !errors.Is(ctx.Err(), context.Canceled) {

config.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package catnip
22

33
import (
44
"errors"
5+
"fmt"
56

67
"github.com/noriah/catnip/dsp"
78
"github.com/noriah/catnip/dsp/window"
@@ -49,9 +50,6 @@ func NewZeroConfig() Config {
4950
SampleRate: 44100,
5051
SampleSize: 1024,
5152
ChannelCount: 1,
52-
ProcessRate: 0,
53-
Combine: false,
54-
UseThreaded: false,
5553
}
5654
}
5755

@@ -65,13 +63,14 @@ func (cfg *Config) Validate() error {
6563
}
6664

6765
switch {
68-
69-
case cfg.ChannelCount > 2:
70-
return errors.New("too many channels (2 max)")
66+
case cfg.ChannelCount > MaxChannelCount:
67+
return fmt.Errorf("too many channels (%d max)", MaxChannelCount)
7168

7269
case cfg.ChannelCount < 1:
7370
return errors.New("too few channels (1 min)")
7471

72+
case cfg.SampleSize > MaxSampleSize:
73+
return fmt.Errorf("sample size too large (%d max)", MaxSampleSize)
7574
}
7675

7776
return nil

dsp/analyzer.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type Analyzer interface {
3131

3232
// analyzer is an audio spectrum in a buffer
3333
type analyzer struct {
34-
cfg AnalyzerConfig // the analyzer config
35-
bins []bin // bins for processing
36-
binCount int // number of bins we look at
37-
fftSize int // number of fft bins
38-
dontNormalize bool // dont normalize the output
34+
cfg AnalyzerConfig // the analyzer config
35+
bins []bin // bins for processing
36+
binCount int // number of bins we look at
37+
fftSize int // number of fft bins
3938
}
4039

4140
// Bin is a helper struct for spectrum
@@ -109,10 +108,9 @@ func MinNonZeroSampleValue() BinMethod {
109108

110109
func NewAnalyzer(cfg AnalyzerConfig) Analyzer {
111110
return &analyzer{
112-
cfg: cfg,
113-
bins: make([]bin, cfg.SampleSize),
114-
fftSize: cfg.SampleSize/2 + 1,
115-
dontNormalize: cfg.DontNormalize,
111+
cfg: cfg,
112+
bins: make([]bin, cfg.SampleSize),
113+
fftSize: cfg.SampleSize/2 + 1,
116114
}
117115
}
118116

@@ -154,7 +152,7 @@ func (az *analyzer) ProcessBin(idx int, src []complex128) float64 {
154152
return 0.0
155153
}
156154

157-
if !az.dontNormalize {
155+
if !az.cfg.DontNormalize {
158156
mag = math.Log(mag)
159157
}
160158

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/noriah/catnip
22

3-
go 1.18
3+
go 1.20
44

55
require (
66
github.com/integrii/flaggy v1.4.4

graphic/display.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync/atomic"
77

88
"github.com/noriah/catnip/util"
9+
910
"github.com/nsf/termbox-go"
1011
)
1112

@@ -393,19 +394,15 @@ func (d *Display) SetInvertDraw(invert bool) {
393394
}
394395

395396
// Bins returns the number of bars we will draw.
396-
func (d *Display) Bins(sets ...int) int {
397-
x := 1
398-
if len(sets) > 0 {
399-
x = sets[0]
400-
}
397+
func (d *Display) Bins(chCount int) int {
401398

402399
switch d.drawType {
403400
case DrawUp, DrawDown:
404-
return (d.termWidth / d.binSize) / x
401+
return (d.termWidth / d.binSize) / chCount
405402
case DrawUpDown:
406403
return d.termWidth / d.binSize
407404
case DrawLeft, DrawRight:
408-
return (d.termHeight / d.binSize) / x
405+
return (d.termHeight / d.binSize) / chCount
409406
case DrawLeftRight:
410407
return d.termHeight / d.binSize
411408
default:

processor/processor.go

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
)
1313

1414
type Output interface {
15-
Bins(...int) int
15+
Bins(int) int
1616
Write([][]float64, int) error
1717
}
1818

1919
type Processor interface {
20-
Start(ctx context.Context) context.Context
20+
Start(ctx context.Context, kickChan chan bool, mu *sync.Mutex) context.Context
2121
Stop()
22-
Process(context.Context, chan bool, *sync.Mutex)
22+
Process()
2323
}
2424

2525
type Config struct {
@@ -49,6 +49,9 @@ type processor struct {
4949

5050
plans []*fft.Plan
5151

52+
mu *sync.Mutex
53+
ctxCancel context.CancelFunc
54+
5255
anlz dsp.Analyzer
5356
out Output
5457
smth dsp.Smoother
@@ -80,16 +83,20 @@ func New(cfg Config) *processor {
8083
return vis
8184
}
8285

83-
func (vis *processor) Start(ctx context.Context) context.Context {
86+
func (vis *processor) Start(ctx context.Context, kickChan chan bool, mu *sync.Mutex) context.Context {
87+
newCtx, cancel := context.WithCancel(ctx)
88+
vis.ctxCancel = cancel
89+
vis.mu = mu
90+
go vis.run(newCtx, kickChan)
8491

85-
return ctx
92+
return newCtx
8693
}
8794

88-
func (vis *processor) Stop() {}
89-
90-
// Process runs processing on sample sets and calls Write on the output once per sample set.
91-
func (vis *processor) Process(ctx context.Context, kickChan chan bool, mu *sync.Mutex) {
95+
func (vis *processor) Stop() {
96+
vis.ctxCancel()
97+
}
9298

99+
func (vis *processor) run(ctx context.Context, kickChan chan bool) {
93100
if vis.processRate <= 0 {
94101
// if we do not have a framerate set, allow at most 1 second per sampling
95102
vis.processRate = 1
@@ -100,33 +107,7 @@ func (vis *processor) Process(ctx context.Context, kickChan chan bool, mu *sync.
100107
defer ticker.Stop()
101108

102109
for {
103-
mu.Lock()
104-
for idx := range vis.barBufs {
105-
if vis.wndwr != nil {
106-
vis.wndwr(vis.inputBufs[idx])
107-
}
108-
vis.plans[idx].Execute()
109-
}
110-
mu.Unlock()
111-
112-
if n := vis.out.Bins(vis.channelCount); n != vis.bars {
113-
vis.bars = vis.anlz.Recalculate(n)
114-
}
115-
116-
for idx, fftBuf := range vis.fftBufs {
117-
buf := vis.barBufs[idx]
118-
119-
for bIdx := range buf[:vis.bars] {
120-
buf[bIdx] = vis.anlz.ProcessBin(bIdx, fftBuf)
121-
}
122-
}
123-
124-
if vis.smth != nil {
125-
vis.smth.SmoothBuffers(vis.barBufs)
126-
}
127-
128-
vis.out.Write(vis.barBufs, vis.channelCount)
129-
110+
vis.Process()
130111
select {
131112
case <-ctx.Done():
132113
return
@@ -137,3 +118,33 @@ func (vis *processor) Process(ctx context.Context, kickChan chan bool, mu *sync.
137118
ticker.Reset(dur)
138119
}
139120
}
121+
122+
// Process runs processing on sample sets and calls Write on the output once per sample set.
123+
func (vis *processor) Process() {
124+
vis.mu.Lock()
125+
for idx := range vis.barBufs {
126+
if vis.wndwr != nil {
127+
vis.wndwr(vis.inputBufs[idx])
128+
}
129+
vis.plans[idx].Execute()
130+
}
131+
vis.mu.Unlock()
132+
133+
if n := vis.out.Bins(vis.channelCount); n != vis.bars {
134+
vis.bars = vis.anlz.Recalculate(n)
135+
}
136+
137+
for idx, fftBuf := range vis.fftBufs {
138+
buf := vis.barBufs[idx]
139+
140+
for bIdx := range buf[:vis.bars] {
141+
buf[bIdx] = vis.anlz.ProcessBin(bIdx, fftBuf)
142+
}
143+
}
144+
145+
if vis.smth != nil {
146+
vis.smth.SmoothBuffers(vis.barBufs)
147+
}
148+
149+
vis.out.Write(vis.barBufs, vis.channelCount)
150+
}

processor/processor_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package processor
2+
3+
import (
4+
"sync"
5+
"testing"
6+
7+
"github.com/noriah/catnip/input"
8+
)
9+
10+
const (
11+
BinSize = 1024
12+
ChCount = 2
13+
)
14+
15+
type testOutput struct{}
16+
17+
func (tO *testOutput) Bins(int) int {
18+
return BinSize
19+
}
20+
21+
func (tO *testOutput) Write([][]float64, int) error {
22+
return nil
23+
}
24+
25+
type testAnalyzer struct{}
26+
27+
func (tA *testAnalyzer) BinCount() int {
28+
return BinSize
29+
}
30+
31+
func (tA *testAnalyzer) ProcessBin(int, []complex128) float64 {
32+
return 0.0
33+
}
34+
35+
func (ta *testAnalyzer) Recalculate(int) int {
36+
return BinSize
37+
}
38+
39+
type Analyzer interface {
40+
BinCount() int
41+
ProcessBin(int, []complex128) float64
42+
Recalculate(int) int
43+
}
44+
45+
func BenchmarkSlices(b *testing.B) {
46+
47+
inputBuffers := input.MakeBuffers(ChCount, BinSize)
48+
49+
cfg := Config{
50+
SampleRate: 122880.0,
51+
SampleSize: BinSize,
52+
ChannelCount: ChCount,
53+
Buffers: inputBuffers,
54+
Output: &testOutput{},
55+
Analyzer: &testAnalyzer{},
56+
}
57+
58+
proc := New(cfg)
59+
proc.mu = &sync.Mutex{}
60+
61+
b.ResetTimer()
62+
63+
for i := 0; i < b.N; i++ {
64+
proc.Process()
65+
}
66+
}

processor/threaded.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (vis *threadedProcessor) channelProcessor(ch int, kick <-chan bool) {
9090
}
9191
}
9292

93-
func (vis *threadedProcessor) Start(ctx context.Context) context.Context {
93+
func (vis *threadedProcessor) Start(ctx context.Context, kickChan chan bool, mu *sync.Mutex) context.Context {
9494
vis.ctx, vis.cancel = context.WithCancel(ctx)
9595

9696
for i, kick := range vis.kicks {
@@ -107,7 +107,7 @@ func (vis *threadedProcessor) Stop() {
107107
}
108108

109109
// Process runs one draw refresh with the visualizer on the termbox screen.
110-
func (vis *threadedProcessor) Process(ctx context.Context, kickChan chan bool, mu *sync.Mutex) {
110+
func (vis *threadedProcessor) Process() {
111111
if n := vis.out.Bins(vis.channelCount); n != vis.bars {
112112
vis.bars = vis.anlz.Recalculate(n)
113113
}

0 commit comments

Comments
 (0)