Skip to content

Commit b6aceee

Browse files
committed
add invert flag to change bin drawing direction.
1 parent 59a7106 commit b6aceee

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

catnip.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func catnip(cfg *config) error {
6363
SampleSize: cfg.sampleSize,
6464
ChannelCount: cfg.channelCount,
6565
FrameRate: cfg.frameRate,
66+
InvertDraw: cfg.invertDraw,
6667
Buffers: inputBuffers,
6768
Analyzer: dsp.NewAnalyzer(anlzConfig),
6869
Smoother: dsp.NewSmoother(anlzConfig),
@@ -233,6 +234,7 @@ func doFlags(cfg *config) bool {
233234
parser.Int(&cfg.spaceSize, "sw", "space", "space width [0, +Inf)")
234235
parser.Int(&cfg.drawType, "dt", "draw", "draw type (1, 2, 3, 4, 5, 6)")
235236
parser.Bool(&cfg.useThreaded, "t", "threaded", "use the threaded processor")
237+
parser.Bool(&cfg.invertDraw, "i", "invert", "invert the direction of bin drawing")
236238

237239
fg, bg, center := graphic.DefaultStyles().AsUInt16s()
238240
parser.UInt16(&fg, "fg", "foreground",

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type config struct {
4141
combine bool
4242
// Use threaded processor
4343
useThreaded bool
44+
// Invert the order of bin drawing
45+
invertDraw bool
4446
// DrawType is the draw type
4547
styles graphic.Styles
4648
}

graphic/display.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,28 +259,28 @@ func (d *Display) fillStyleBuffer(left, center, right int) {
259259
}
260260

261261
// Draw takes data and draws.
262-
func (d *Display) Draw(bufs [][]float64, channels, count int, scale float64) error {
262+
func (d *Display) Draw(bufs [][]float64, channels, count int, scale float64, invert bool) error {
263263

264264
d.wg.Add(channels)
265265

266266
switch d.drawType {
267267
case DrawUp:
268-
d.DrawUp(bufs, count, scale)
268+
d.DrawUp(bufs, count, scale, invert)
269269

270270
case DrawUpDown:
271-
d.DrawUpDown(bufs, count, scale)
271+
d.DrawUpDown(bufs, count, scale, invert)
272272

273273
case DrawDown:
274-
d.DrawDown(bufs, count, scale)
274+
d.DrawDown(bufs, count, scale, invert)
275275

276276
case DrawLeft:
277-
d.DrawLeft(bufs, count, scale)
277+
d.DrawLeft(bufs, count, scale, invert)
278278

279279
case DrawLeftRight:
280-
d.DrawLeftRight(bufs, count, scale)
280+
d.DrawLeftRight(bufs, count, scale, invert)
281281

282282
case DrawRight:
283-
d.DrawRight(bufs, count, scale)
283+
d.DrawRight(bufs, count, scale, invert)
284284

285285
default:
286286
return nil
@@ -384,7 +384,7 @@ func sizeAndCap(value float64, space int, zeroBase bool, baseRune rune) (int, ru
384384
// DRAWING METHODS
385385

386386
// DrawUp will draw up.
387-
func (d *Display) DrawUp(bins [][]float64, count int, scale float64) {
387+
func (d *Display) DrawUp(bins [][]float64, count int, scale float64, invert bool) {
388388

389389
barSpace := intMax(d.termHeight-d.baseSize, 0)
390390
scale = float64(barSpace) / scale
@@ -400,6 +400,11 @@ func (d *Display) DrawUp(bins [][]float64, count int, scale float64) {
400400
for xBar := 0; xBar < count; xBar++ {
401401

402402
xBin := (xBar * (1 - xSet)) + (((count - 1) - xBar) * xSet)
403+
404+
if invert {
405+
xBin = count - 1 - xBin
406+
}
407+
403408
start, bCap := sizeAndCap(chBins[xBin]*scale, barSpace, true, BarRuneV)
404409

405410
xCol := (xBar * d.binSize) + (channelWidth * xSet) + edgeOffset
@@ -420,7 +425,7 @@ func (d *Display) DrawUp(bins [][]float64, count int, scale float64) {
420425
}
421426

422427
// DrawUpDown will draw up and down.
423-
func (d *Display) DrawUpDown(bins [][]float64, count int, scale float64) {
428+
func (d *Display) DrawUpDown(bins [][]float64, count int, scale float64, invert bool) {
424429

425430
centerStart := intMax((d.termHeight-d.baseSize)/2, 0)
426431
centerStop := centerStart + d.baseSize
@@ -440,7 +445,12 @@ func (d *Display) DrawUpDown(bins [][]float64, count int, scale float64) {
440445
rCap = BarRune
441446
}
442447

443-
xCol := xBar*d.binSize + edgeOffset
448+
xCol := xBar
449+
if invert {
450+
xCol = count - 1 - xCol
451+
}
452+
453+
xCol = xCol*d.binSize + edgeOffset
444454
lCol := intMin(xCol+d.barSize, d.termWidth)
445455

446456
for ; xCol < lCol; xCol++ {
@@ -462,7 +472,7 @@ func (d *Display) DrawUpDown(bins [][]float64, count int, scale float64) {
462472
}
463473

464474
// DrawDown will draw down.
465-
func (d *Display) DrawDown(bins [][]float64, count int, scale float64) {
475+
func (d *Display) DrawDown(bins [][]float64, count int, scale float64, invert bool) {
466476

467477
barSpace := intMax(d.termHeight-d.baseSize, 0)
468478
scale = float64(barSpace) / scale
@@ -478,6 +488,11 @@ func (d *Display) DrawDown(bins [][]float64, count int, scale float64) {
478488
for xBar := 0; xBar < count; xBar++ {
479489

480490
xBin := (xBar * (1 - xSet)) + (((count - 1) - xBar) * xSet)
491+
492+
if invert {
493+
xBin = count - 1 - xBin
494+
}
495+
481496
stop, bCap := sizeAndCap(chBins[xBin]*scale, barSpace, false, BarRune)
482497
if stop += d.baseSize; stop >= d.termHeight {
483498
stop = d.termHeight
@@ -501,7 +516,7 @@ func (d *Display) DrawDown(bins [][]float64, count int, scale float64) {
501516
}
502517
}
503518

504-
func (d *Display) DrawLeft(bins [][]float64, count int, scale float64) {
519+
func (d *Display) DrawLeft(bins [][]float64, count int, scale float64, invert bool) {
505520
barSpace := intMax(d.termWidth-d.baseSize, 0)
506521
scale = float64(barSpace) / scale
507522

@@ -516,6 +531,11 @@ func (d *Display) DrawLeft(bins [][]float64, count int, scale float64) {
516531
for xBar := 0; xBar < count; xBar++ {
517532

518533
xBin := (xBar * (1 - xSet)) + (((count - 1) - xBar) * xSet)
534+
535+
if invert {
536+
xBin = count - 1 - xBin
537+
}
538+
519539
start, bCap := sizeAndCap(chBins[xBin]*scale, barSpace, true, BarRune)
520540

521541
xRow := (xBar * d.binSize) + (channelWidth * xSet) + edgeOffset
@@ -536,7 +556,7 @@ func (d *Display) DrawLeft(bins [][]float64, count int, scale float64) {
536556
}
537557

538558
// DrawLeftRight will draw left and right.
539-
func (d *Display) DrawLeftRight(bins [][]float64, count int, scale float64) {
559+
func (d *Display) DrawLeftRight(bins [][]float64, count int, scale float64, invert bool) {
540560
centerStart := intMax((d.termWidth-d.baseSize)/2, 0)
541561
centerStop := centerStart + d.baseSize
542562

@@ -558,7 +578,13 @@ func (d *Display) DrawLeftRight(bins [][]float64, count int, scale float64) {
558578
rCap = BarRuneH
559579
}
560580

561-
xRow := xBar*d.binSize + edgeOffset
581+
xRow := xBar
582+
if invert {
583+
xRow = count - 1 - xRow
584+
}
585+
586+
xRow = xRow*d.binSize + edgeOffset
587+
562588
lRow := intMin(xRow+d.barSize, d.termHeight)
563589

564590
for ; xRow < lRow; xRow++ {
@@ -578,7 +604,7 @@ func (d *Display) DrawLeftRight(bins [][]float64, count int, scale float64) {
578604
}
579605
}
580606

581-
func (d *Display) DrawRight(bins [][]float64, count int, scale float64) {
607+
func (d *Display) DrawRight(bins [][]float64, count int, scale float64, invert bool) {
582608
barSpace := intMax(d.termWidth-d.baseSize, 0)
583609
scale = float64(barSpace) / scale
584610

@@ -593,6 +619,11 @@ func (d *Display) DrawRight(bins [][]float64, count int, scale float64) {
593619
for xBar := 0; xBar < count; xBar++ {
594620

595621
xBin := (xBar * (1 - xSet)) + (((count - 1) - xBar) * xSet)
622+
623+
if invert {
624+
xBin = count - 1 - xBin
625+
}
626+
596627
stop, bCap := sizeAndCap(chBins[xBin]*scale, barSpace, false, BarRuneH)
597628
if stop += d.baseSize; stop >= d.termWidth {
598629
stop = d.termWidth

processor/processor.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Smoother interface {
3737

3838
type Display interface {
3939
Bars(...int) int
40-
Draw([][]float64, int, int, float64) error
40+
Draw([][]float64, int, int, float64, bool) error
4141
}
4242

4343
type Processor interface {
@@ -51,6 +51,7 @@ type Config struct {
5151
SampleSize int // number of samples per buffer
5252
ChannelCount int // number of channels
5353
FrameRate int // target framerate
54+
InvertDraw bool // invert the direction of bin drawing
5455
Buffers [][]input.Sample // sample buffers
5556
Analyzer Analyzer // audio analyzer
5657
Smoother Smoother // time smoother
@@ -63,6 +64,8 @@ type processor struct {
6364

6465
bars int
6566

67+
invertDraw bool
68+
6669
slowWindow *util.MovingWindow
6770
fastWindow *util.MovingWindow
6871

@@ -88,6 +91,7 @@ func New(cfg Config) *processor {
8891
vis := &processor{
8992
channelCount: cfg.ChannelCount,
9093
frameRate: cfg.FrameRate,
94+
invertDraw: cfg.InvertDraw,
9195
slowWindow: util.NewMovingWindow(slowSize),
9296
fastWindow: util.NewMovingWindow(fastSize),
9397
fftBufs: make([][]complex128, cfg.ChannelCount),
@@ -177,7 +181,7 @@ func (vis *processor) Process(ctx context.Context, kickChan chan bool, mu *sync.
177181
}
178182
}
179183

180-
vis.disp.Draw(vis.barBufs, vis.channelCount, vis.bars, scale)
184+
vis.disp.Draw(vis.barBufs, vis.channelCount, vis.bars, scale, vis.invertDraw)
181185

182186
select {
183187
case <-ctx.Done():

processor/threaded.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type threadedProcessor struct {
1616

1717
bars int
1818

19+
invertDraw bool
20+
1921
slowWindow *util.MovingWindow
2022
fastWindow *util.MovingWindow
2123

@@ -45,6 +47,7 @@ func NewThreaded(cfg Config) *threadedProcessor {
4547

4648
vis := &threadedProcessor{
4749
channelCount: cfg.ChannelCount,
50+
invertDraw: cfg.InvertDraw,
4851
slowWindow: util.NewMovingWindow(slowSize),
4952
fastWindow: util.NewMovingWindow(fastSize),
5053
fftBufs: make([][]complex128, cfg.ChannelCount),
@@ -161,5 +164,5 @@ func (vis *threadedProcessor) Process(ctx context.Context, kickChan chan bool, m
161164
}
162165
}
163166

164-
vis.disp.Draw(vis.barBufs, vis.channelCount, vis.bars, scale)
167+
vis.disp.Draw(vis.barBufs, vis.channelCount, vis.bars, scale, vis.invertDraw)
165168
}

0 commit comments

Comments
 (0)