@@ -12,14 +12,14 @@ import (
1212)
1313
1414type Output interface {
15- Bins (... int ) int
15+ Bins (int ) int
1616 Write ([][]float64 , int ) error
1717}
1818
1919type 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
2525type 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+ }
0 commit comments