@@ -168,7 +168,7 @@ func (d *Display) Stop() error {
168168func (d * Display ) Write (buffers [][]float64 , channels int ) error {
169169
170170 peak := 0.0
171- bins := d .Bins (channels )
171+ bins := d .binsInternal (channels , bufferLength ( buffers ) )
172172
173173 for i := 0 ; i < channels ; i ++ {
174174 for _ , val := range buffers [i ][:bins ] {
@@ -311,6 +311,18 @@ func (d *Display) Bins(chCount int) int {
311311 }
312312}
313313
314+ func bufferLength (buffers [][]float64 ) int {
315+ return len (buffers [0 ])
316+ }
317+
318+ func (d * Display ) binsInternal (chCount , bufLen int ) int {
319+ bins := d .Bins (chCount )
320+ if bins >= bufLen {
321+ bins = bufLen - 1
322+ }
323+ return bins
324+ }
325+
314326func (d * Display ) inputProcessor () {
315327 if d .cancel != nil {
316328 defer d .cancel ()
@@ -474,7 +486,7 @@ func sizeAndCap(value float64, space int, zeroBase bool, baseRune rune) (int, ru
474486
475487// drawUp will draw up.
476488func (d * Display ) drawUp (bins [][]float64 , channelCount int , scale float64 ) {
477- binCount := d .Bins (channelCount )
489+ binCount := d .binsInternal (channelCount , bufferLength ( bins ) )
478490 barSpace := intMax (d .termHeight - d .baseSize , 0 )
479491 scale = float64 (barSpace ) / scale
480492
@@ -515,7 +527,7 @@ func (d *Display) drawUp(bins [][]float64, channelCount int, scale float64) {
515527
516528// drawUpDown will draw up and down.
517529func (d * Display ) drawUpDown (bins [][]float64 , channelCount int , scale float64 ) {
518- binCount := d .Bins (channelCount )
530+ binCount := d .binsInternal (channelCount , bufferLength ( bins ) )
519531 centerStart := intMax ((d .termHeight - d .baseSize )/ 2 , 0 )
520532 centerStop := centerStart + d .baseSize
521533
@@ -563,7 +575,7 @@ func (d *Display) drawUpDown(bins [][]float64, channelCount int, scale float64)
563575// drawUpDownSplit will draw up and down split down the middle for left and
564576// right channels.
565577func (d * Display ) drawUpDownSplit (bins [][]float64 , channelCount int , scale float64 ) {
566- binCount := d .Bins ( 2 )
578+ binCount := d .binsInternal ( 2 , bufferLength ( bins ) )
567579 centerStart := intMax ((d .termHeight - d .baseSize )/ 2 , 0 )
568580 centerStop := centerStart + d .baseSize
569581
@@ -616,7 +628,7 @@ func (d *Display) drawUpDownSplit(bins [][]float64, channelCount int, scale floa
616628// drawUpDownSplitVert will draw up and down split down the middle for left and
617629// right channels.
618630func (d * Display ) drawUpDownSplitVert (bins [][]float64 , channelCount int , scale float64 ) {
619- binCount := d .Bins ( 2 )
631+ binCount := d .binsInternal ( 2 , bufferLength ( bins ) )
620632 centerStart := intMax ((d .termHeight - d .baseSize )/ 2 , 0 )
621633 centerStop := centerStart + d .baseSize
622634
@@ -668,7 +680,7 @@ func (d *Display) drawUpDownSplitVert(bins [][]float64, channelCount int, scale
668680
669681// drawDown will draw down.
670682func (d * Display ) drawDown (bins [][]float64 , channelCount int , scale float64 ) {
671- binCount := d .Bins (channelCount )
683+ binCount := d .binsInternal (channelCount , bufferLength ( bins ) )
672684 barSpace := intMax (d .termHeight - d .baseSize , 0 )
673685 scale = float64 (barSpace ) / scale
674686
@@ -712,7 +724,7 @@ func (d *Display) drawDown(bins [][]float64, channelCount int, scale float64) {
712724}
713725
714726func (d * Display ) drawLeft (bins [][]float64 , channelCount int , scale float64 ) {
715- binCount := d .Bins (channelCount )
727+ binCount := d .binsInternal (channelCount , bufferLength ( bins ) )
716728 barSpace := intMax (d .termWidth - d .baseSize , 0 )
717729 scale = float64 (barSpace ) / scale
718730
@@ -753,7 +765,7 @@ func (d *Display) drawLeft(bins [][]float64, channelCount int, scale float64) {
753765
754766// drawLeftRight will draw left and right.
755767func (d * Display ) drawLeftRight (bins [][]float64 , channelCount int , scale float64 ) {
756- binCount := d .Bins (channelCount )
768+ binCount := d .binsInternal (channelCount , bufferLength ( bins ) )
757769 centerStart := intMax ((d .termWidth - d .baseSize )/ 2 , 0 )
758770 centerStop := centerStart + d .baseSize
759771
@@ -803,7 +815,7 @@ func (d *Display) drawLeftRight(bins [][]float64, channelCount int, scale float6
803815
804816// drawLeftRight will draw left and right.
805817func (d * Display ) drawLeftRightSplit (bins [][]float64 , channelCount int , scale float64 ) {
806- binCount := d .Bins ( 2 )
818+ binCount := d .binsInternal ( 2 , bufferLength ( bins ) )
807819 centerStart := intMax ((d .termWidth - d .baseSize )/ 2 , 0 )
808820 centerStop := centerStart + d .baseSize
809821
@@ -854,7 +866,7 @@ func (d *Display) drawLeftRightSplit(bins [][]float64, channelCount int, scale f
854866}
855867
856868func (d * Display ) drawRight (bins [][]float64 , channelCount int , scale float64 ) {
857- binCount := d .Bins (channelCount )
869+ binCount := d .binsInternal (channelCount , bufferLength ( bins ) )
858870 barSpace := intMax (d .termWidth - d .baseSize , 0 )
859871 scale = float64 (barSpace ) / scale
860872
0 commit comments