github.com/noriah/catnip@v1.8.5/cmd/catnip/config.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/noriah/catnip/dsp"
     7  	"github.com/noriah/catnip/graphic"
     8  	"github.com/noriah/catnip/input"
     9  )
    10  
    11  // Config is a temporary struct to define parameters
    12  type config struct {
    13  	// Backend is the backend name from list-backends
    14  	backend string
    15  	// Device is the device name from list-devices
    16  	device string
    17  	// SampleRate is the rate at which samples are read
    18  	sampleRate float64
    19  	// SmoothFactor factor of smooth
    20  	smoothFactor float64
    21  	// Smoothing method used to do time smoothing.
    22  	smoothingMethod int
    23  	// Size of window used for averaging methods.
    24  	smoothingAverageWindowSize int
    25  	// SampleSize is how much we draw. Play with it
    26  	sampleSize int
    27  	// FrameRate is the number of frames to draw every second (0 draws it every
    28  	// perfect sample)
    29  	frameRate int
    30  	// BaseSize number of cells wide/high the base is
    31  	baseSize int
    32  	// BarSize is the size of bars, in columns/rows
    33  	barSize int
    34  	// SpaceSize is the size of spaces, in columns/rows
    35  	spaceSize int
    36  	// ChannelCount is the number of channels we want to look at. DO NOT TOUCH
    37  	channelCount int
    38  	// DrawType is the draw type
    39  	drawType int
    40  	// Combine determines if we merge streams (stereo -> mono)
    41  	combine bool
    42  	// Don't run math.Log on the output of the analyzer
    43  	dontNormalize bool
    44  	// Use threaded processor
    45  	useThreaded bool
    46  	// Invert the order of bin drawing
    47  	invertDraw bool
    48  	// Styles is the configuration for bar color styles
    49  	styles graphic.Styles
    50  
    51  	// Use the number writer instead of
    52  	useNumberWriter bool
    53  	// Number of bins to use for the number writer per channel (0 uses default 50)
    54  	numberWriterBins int
    55  }
    56  
    57  // NewZeroConfig returns a zero config
    58  // it is the "default"
    59  func newZeroConfig() config {
    60  	return config{
    61  		backend:                    input.DefaultBackend(),
    62  		sampleRate:                 44100,
    63  		sampleSize:                 1024,
    64  		smoothFactor:               64.15,
    65  		smoothingMethod:            int(dsp.SmoothDefault),
    66  		smoothingAverageWindowSize: 0, // if zero, will be calculated
    67  		frameRate:                  0,
    68  		baseSize:                   1,
    69  		barSize:                    1,
    70  		spaceSize:                  1,
    71  		channelCount:               2,
    72  		drawType:                   int(graphic.DrawDefault),
    73  		dontNormalize:              false,
    74  		combine:                    false,
    75  		useThreaded:                false,
    76  		invertDraw:                 false,
    77  		useNumberWriter:            false,
    78  		numberWriterBins:           50,
    79  	}
    80  }
    81  
    82  // Sanitize cleans things up
    83  func (cfg *config) validate() error {
    84  
    85  	if cfg.sampleRate < float64(cfg.sampleSize) {
    86  		return errors.New("sample rate lower than sample size")
    87  	}
    88  
    89  	if cfg.sampleSize < 4 {
    90  		return errors.New("sample size too small (4+ required)")
    91  	}
    92  
    93  	switch {
    94  
    95  	case cfg.channelCount > 2:
    96  		return errors.New("too many channels (2 max)")
    97  
    98  	case cfg.channelCount < 1:
    99  		return errors.New("too few channels (1 min)")
   100  
   101  	}
   102  
   103  	switch {
   104  	case cfg.smoothFactor > 99.99:
   105  		cfg.smoothFactor = 0.9999
   106  	case cfg.smoothFactor < 0.00001:
   107  		cfg.smoothFactor = 0.00001
   108  	default:
   109  		cfg.smoothFactor /= 100.0
   110  	}
   111  
   112  	if cfg.numberWriterBins <= 0 {
   113  		cfg.numberWriterBins = 50
   114  	}
   115  
   116  	return nil
   117  }