@@ -4,18 +4,16 @@ import (
44 "context"
55 "fmt"
66 "log"
7- "sync"
87
8+ "github.com/noriah/catnip"
99 "github.com/noriah/catnip/dsp"
1010 "github.com/noriah/catnip/dsp/window"
1111 "github.com/noriah/catnip/graphic"
1212 "github.com/noriah/catnip/input"
13- "github.com/noriah/catnip/processor"
1413
1514 _ "github.com/noriah/catnip/input/all"
1615
1716 "github.com/integrii/flaggy"
18- "github.com/pkg/errors"
1917)
2018
2119// AppName is the app name
@@ -38,110 +36,58 @@ func main() {
3836 return
3937 }
4038
41- chk (cfg .Sanitize (), "invalid config" )
39+ chk (cfg .validate (), "invalid config" )
4240
43- chk (catnip (& cfg ), "failed to run catnip" )
44- }
45-
46- // Catnip starts to draw the processor on the termbox screen.
47- func catnip (cfg * config ) error {
48-
49- display := & graphic.Display {}
50-
51- // PROCESSOR SETUP
52-
53- inputBuffers := input .MakeBuffers (cfg .channelCount , cfg .sampleSize )
41+ display := graphic .NewDisplay ()
5442
55- procConfig := processor.Config {
43+ catnipCfg := catnip.Config {
44+ Backend : cfg .backend ,
45+ Device : cfg .device ,
5646 SampleRate : cfg .sampleRate ,
5747 SampleSize : cfg .sampleSize ,
5848 ChannelCount : cfg .channelCount ,
59- FrameRate : cfg .frameRate ,
60- Buffers : inputBuffers ,
49+ ProcessRate : cfg .frameRate ,
50+ Combine : cfg .combine ,
51+ UseThreaded : cfg .useThreaded ,
52+ SetupFunc : func () error {
53+ if err := display .Init (cfg .sampleRate , cfg .sampleSize ); err != nil {
54+ return err
55+ }
56+
57+ display .SetSizes (cfg .barSize , cfg .spaceSize )
58+ display .SetBase (cfg .baseSize )
59+ display .SetDrawType (graphic .DrawType (cfg .drawType ))
60+ display .SetStyles (cfg .styles )
61+ display .SetInvertDraw (cfg .invertDraw )
62+
63+ return nil
64+ },
65+ StartFunc : func (ctx context.Context ) (context.Context , error ) {
66+ ctx = display .Start (ctx )
67+
68+ return ctx , nil
69+ },
70+ CleanupFunc : func () error {
71+ display .Stop ()
72+ display .Close ()
73+ return nil
74+ },
75+ Output : display ,
76+ Windower : window .Lanczos ,
6177 Analyzer : dsp .NewAnalyzer (dsp.AnalyzerConfig {
6278 SampleRate : cfg .sampleRate ,
6379 SampleSize : cfg .sampleSize ,
6480 SquashLow : true ,
6581 BinMethod : dsp .MaxSamples ,
6682 }),
67- Output : display ,
6883 Smoother : dsp .NewSmoother (dsp.SmootherConfig {
6984 SampleSize : cfg .sampleSize ,
7085 ChannelCount : cfg .channelCount ,
7186 SmoothingFactor : cfg .smoothFactor ,
7287 }),
73- Windower : window .Lanczos ,
74- }
75-
76- var vis processor.Processor
77-
78- if cfg .useThreaded {
79- vis = processor .NewThreaded (procConfig )
80- } else {
81- vis = processor .New (procConfig )
82- }
83-
84- // INPUT SETUP
85-
86- backend , err := input .InitBackend (cfg .backend )
87- if err != nil {
88- return err
89- }
90-
91- sessConfig := input.SessionConfig {
92- FrameSize : cfg .channelCount ,
93- SampleSize : cfg .sampleSize ,
94- SampleRate : cfg .sampleRate ,
95- }
96-
97- if sessConfig .Device , err = input .GetDevice (backend , cfg .device ); err != nil {
98- return err
99- }
100-
101- audio , err := backend .Start (sessConfig )
102- defer backend .Close ()
103-
104- if err != nil {
105- return errors .Wrap (err , "failed to start the input backend" )
106- }
107-
108- // DISPLAY SETUP
109-
110- if err = display .Init (cfg .sampleRate , cfg .sampleSize ); err != nil {
111- return err
112- }
113- defer display .Close ()
114-
115- display .SetSizes (cfg .barSize , cfg .spaceSize )
116- display .SetBase (cfg .baseSize )
117- display .SetDrawType (graphic .DrawType (cfg .drawType ))
118- display .SetStyles (cfg .styles )
119- display .SetInvertDraw (cfg .invertDraw )
120-
121- // Root Context
122- ctx , cancel := context .WithCancel (context .Background ())
123- defer cancel ()
124-
125- ctx = display .Start (ctx )
126- defer display .Stop ()
127-
128- ctx = vis .Start (ctx )
129- defer vis .Stop ()
130-
131- kickChan := make (chan bool , 1 )
132-
133- mu := & sync.Mutex {}
134-
135- // Start the processor
136- go vis .Process (ctx , kickChan , mu )
137-
138- if err := audio .Start (ctx , inputBuffers , kickChan , mu ); err != nil {
139- if ! errors .Is (ctx .Err (), context .Canceled ) {
140- return errors .Wrap (err , "failed to start input session" )
141- }
14288 }
14389
144- return nil
90+ chk ( catnip . Catnip ( & catnipCfg ), "failed to run catnip" )
14591}
14692
14793func doFlags (cfg * config ) bool {
0 commit comments