// MinMaxFloat returns the smallest and biggest samples in the buffer
344657
func MinMaxFloat(buf *audio.PCMBuffer) (min, max float64) {
	if buf == nil || buf.Len() == 0 {
		return 0, 0
	}
	buf.SwitchPrimaryType(audio.Float)
	min = buf.Floats[0]

	for _, v := range buf.Floats {
		if v > max {
			max = v
		} else if v < min {
			min = v
		}
	}

	return min, max
}
1149027
func MinMaxIndex(real []float64, timePeriod int32) ([]int32, []int32) {
	var outBegIdx C.int
	var outNBElement C.int
	outMinIdx := make([]int32, len(real))
	outMaxIdx := make([]int32, len(real))
	C.TA_MINMAXINDEX(0, C.int(len(real)-1), (*C.double)(unsafe.Pointer(&real[0])), C.int(timePeriod), &outBegIdx, &outNBElement, (*C.int)(unsafe.Pointer(&outMinIdx[0])), (*C.int)(unsafe.Pointer(&outMaxIdx[0])))
	return outMinIdx[:outNBElement], outMaxIdx[:outNBElement]
}
1373404
func NewFloatEncoder() *FloatEncoder {
	s := FloatEncoder{
		first:   true,
		leading: ^uint64(0),
	}

	s.bw = bitstream.NewWriter(&s.buf)
	s.buf.WriteByte(floatCompressedGorilla << 4)

	return &s
}
1318913
func (enc *Encoder) encodeFloat(n float64) ([]byte, error) {
	enc.buf = strconv.AppendFloat(enc.buf, n, 'f', -1, 64)
	return enc.buf, nil
}
588712
func Max(x, y float32) float32 {
	return float32(math.Max(float64(x), float64(y)))
}
1340895
func formatFloat(f float64, maxPrec int) string {
	v := strconv.FormatFloat(f, 'f', -1, 64)

	idx := strings.LastIndex(v, ".")
	if idx == -1 {
		return v
	}

	sublen := idx + maxPrec + 1
	if sublen > len(v) {
		sublen = len(v)
	}

	return v[:sublen]
}
16505
func (c C) Min(other Input) Input {
	if v, ok := other.(C); ok {
		return C(minFloat32(float32(c), float32(v)))
	}
	return other.Min(c)
}
752463
func Clamp(x, min, max float64) float64 {
	if x < min {
		return min
	}
	if x > max {
		return max
	}
	return x
}
1367606
func SixvalFloat64(data []float64, p int) (min, lq, med, avg, uq, max float64) {
	n := len(data)

	// Special cases 0 and 1
	if n == 0 {
		return
	}

	if n == 1 {
		min = data[0]
		lq = data[0]
		med = data[0]
		avg = data[0]
		uq = data[0]
		max = data[0]
		return
	}

	// First pass (min, max, coarse average)
	var sum float64
	min, max = math.MaxFloat64, -math.MaxFloat64
	for _, v := range data {
		if v < min {
			min = v
		}
		if v > max {
			max = v
		}
		sum += v
	}
	avg = sum / float64(n)

	// Second pass: Correct average
	var corr float64
	for _, v := range data {
		corr += v - avg
	}
	avg += corr / float64(n)

	// Median
	sort.Float64s(data)
	if n%2 == 1 {
		med = data[(n-1)/2]
	} else {
		med = (data[n/2] + data[n/2-1]) / 2
	}

	// Percentiles
	if p < 0 {
		p = 0
	}
	if p > 100 {
		p = 100
	}
	lq = percentilFloat64(data, p)
	uq = percentilFloat64(data, 100-p)
	return
}
1053033
func (p *Params) MustFloat64(key string, defaults ...float64) float64 {
	r, err := strconv.ParseFloat(p.Get(key), 64)
	if len(defaults) > 0 && err != nil {
		return defaults[0]
	}
	return r
}
367594
func MinMaxIndex(inReal []float64, inTimePeriod int) ([]float64, []float64) {

	outMinIdx := make([]float64, len(inReal))
	outMaxIdx := make([]float64, len(inReal))

	nbInitialElementNeeded := (inTimePeriod - 1)
	startIdx := nbInitialElementNeeded
	outIdx := startIdx
	today := startIdx
	trailingIdx := startIdx - nbInitialElementNeeded
	highestIdx := -1
	highest := 0.0
	lowestIdx := -1
	lowest := 0.0
	for today < len(inReal) {
		tmpLow, tmpHigh := inReal[today], inReal[today]
		if highestIdx < trailingIdx {
			highestIdx = trailingIdx
			highest = inReal[highestIdx]
			i := highestIdx
			i++
			for i <= today {
				tmpHigh = inReal[i]
				if tmpHigh > highest {
					highestIdx = i
					highest = tmpHigh
				}
				i++
			}
		} else if tmpHigh >= highest {
			highestIdx = today
			highest = tmpHigh
		}
		if lowestIdx < trailingIdx {
			lowestIdx = trailingIdx
			lowest = inReal[lowestIdx]
			i := lowestIdx
			i++
			for i <= today {
				tmpLow = inReal[i]
				if tmpLow < lowest {
					lowestIdx = i
					lowest = tmpLow
				}
				i++
			}
		} else if tmpLow <= lowest {
			lowestIdx = today
			lowest = tmpLow
		}
		outMaxIdx[outIdx] = float64(highestIdx)
		outMinIdx[outIdx] = float64(lowestIdx)
		outIdx++
		trailingIdx++
		today++
	}
	return outMinIdx, outMaxIdx
}
823164
func sampleFloat64DecodeFunc(bitsPerSample int) (func([]byte) float64, error) {
	bytesPerSample := bitsPerSample / 8
	switch bytesPerSample {
	case 1:
		// 8bit values are unsigned
		return func(s []byte) float64 {
			return float64(uint8(s[0]))
		}, nil
	case 2:
		return func(s []byte) float64 {
			return float64(int(s[0]) + int(s[1])<<8)
		}, nil
	case 3:
		return func(s []byte) float64 {
			var output int32
			output |= int32(s[2]) << 0
			output |= int32(s[1]) << 8
			output |= int32(s[0]) << 16
			return float64(output)
		}, nil
	case 4:
		// TODO: fix the float64 conversion (current int implementation)
		return func(s []byte) float64 {
			return float64(int(s[0]) + int(s[1])<<8 + int(s[2])<<16 + int(s[3])<<24)
		}, nil
	default:
		return nil, fmt.Errorf("unhandled byte depth:%d", bitsPerSample)
	}
}
1035897
func (u *Ugen) Max(other Input) Input {
	return binOpMax(u.Rate, u, other, u.NumOutputs)
}
1064423
func (b *PCMBuffer) AsFloatBuffer() *FloatBuffer {
	newB := &FloatBuffer{}
	newB.Data = b.AsF64()
	if b.Format != nil {
		newB.Format = &Format{
			NumChannels: b.Format.NumChannels,
			SampleRate:  b.Format.SampleRate,
		}
	}
	return newB
}
773787
func Minmax(target uint32, internalformat uint32, sink bool) {
	C.glowMinmax(gpMinmax, (C.GLenum)(target), (C.GLenum)(internalformat), (C.GLboolean)(boolToInt(sink)))
}
1244708
func SampleMax(values []int64) int64 {
	if 0 == len(values) {
		return 0
	}
	var max int64 = math.MinInt64
	for _, v := range values {
		if max < v {
			max = v
		}
	}
	return max
}
49107
func MinOf(a []float64) (retVal float64) {
	if len(a) < 1 {
		panic("Cannot find the min of an empty slice")
	}
	return Reduce(min, a[0], a[1:]...)
}