// MonoDownmix converts the buffer to a mono buffer
// by downmixing the channels together.
512986
func MonoDownmix(buf *audio.PCMBuffer) error {
	if buf == nil || buf.Format == nil {
		return audio.ErrInvalidBuffer
	}
	nChans := buf.Format.NumChannels
	if nChans < 2 {
		return nil
	}
	nChansF := float64(nChans)

	frameCount := buf.Size()
	newData := make([]float64, frameCount)
	buf.SwitchPrimaryType(audio.Float)
	for i := 0; i < frameCount; i++ {
		newData[i] = 0
		for j := 0; j < nChans; j++ {
			newData[i] += buf.Floats[i*nChans+j]
		}
		newData[i] /= nChansF
	}
	buf.Floats = newData
	buf.Format.NumChannels = 1

	return nil
}
337232
func FindCommonSuffix(input []string) string {
	rv := ""
	if len(input) < 2 {
		return ""
	}
	from := input
	for i := len(input[0]); i > 0; i-- {
		common := true
		suffix := input[0][i:]
		for _, s := range from {
			if !strings.HasSuffix(s, suffix) {
				common = false
				break
			}
		}
		if common {
			rv = suffix
		}
	}
	return rv
}
637590
func (m *MailYak) buildMimeWithBoundaries(mb, ab string) (*bytes.Buffer, error) {
	var buf bytes.Buffer

	if err := m.writeHeaders(&buf); err != nil {
		return nil, err
	}

	// Start our multipart/mixed part
	mixed := multipart.NewWriter(&buf)
	if err := mixed.SetBoundary(mb); err != nil {
		return nil, err
	}
	defer mixed.Close()

	fmt.Fprintf(&buf, "Content-Type: multipart/mixed;\r\n\tboundary=\"%s\"; charset=UTF-8\r\n\r\n", mixed.Boundary())

	ctype := fmt.Sprintf("multipart/alternative;\r\n\tboundary=\"%s\"", ab)

	altPart, err := mixed.CreatePart(textproto.MIMEHeader{"Content-Type": {ctype}})
	if err != nil {
		return nil, err
	}

	if err := m.writeBody(altPart, ab); err != nil {
		return nil, err
	}

	if err := m.writeAttachments(mixed, lineSplitterBuilder{}); err != nil {
		return nil, err
	}

	return &buf, nil
}
974081
func (l *Live) AddMultipleStreamMixService(mixStream MixStreamParam, resp interface{}) (err error) {
	req := l.cloneRequest(AddMultipleStreamMixServiceAction)
	if mixStream.Main.DomainName != "" {
		req.DomainName = mixStream.Main.DomainName
	}
	if mixStream.Main.AppName != "" {
		req.AppName = mixStream.Main.AppName
	}
	req.SetArgs("StreamName", mixStream.Main.StreamName)
	req.SetArgs("MixDomainName", mixStream.Mix.DomainName)
	req.SetArgs("MixAppName", mixStream.Mix.AppName)
	req.SetArgs("MixStreamName", mixStream.Mix.StreamName)
	err = l.rpc.Query(req, resp)
	return
}
18550
func (s *VisvalingamSimplifier) MultiLineString(mls orb.MultiLineString) orb.MultiLineString {
	return multiLineString(s, mls)
}
444285
func (m *DataStream) Decode(buf []byte) {
	data := bytes.NewBuffer(buf)
	binary.Read(data, binary.LittleEndian, &m.MESSAGE_RATE)
	binary.Read(data, binary.LittleEndian, &m.STREAM_ID)
	binary.Read(data, binary.LittleEndian, &m.ON_OFF)
}
735010
func (m *M) Match(mediatype string) (string, map[string]string, MinifierFunc) {
	m.mutex.RLock()
	defer m.mutex.RUnlock()

	mimetype, params := parse.Mediatype([]byte(mediatype))
	if minifier, ok := m.literal[string(mimetype)]; ok { // string conversion is optimized away
		return string(mimetype), params, minifier.Minify
	}

	for _, minifier := range m.pattern {
		if minifier.pattern.Match(mimetype) {
			return minifier.pattern.String(), params, minifier.Minify
		}
	}
	return string(mimetype), params, nil
}
587141
func (res *ExecResult) Combined() string {
	return res.outBuffer.String() + res.errBuffer.String()
}
13087
func DecodeFormats(mediaTypes media.Types, maps ...map[string]interface{}) (Formats, error) {
	f := make(Formats, len(DefaultFormats))
	copy(f, DefaultFormats)

	for _, m := range maps {
		for k, v := range m {
			found := false
			for i, vv := range f {
				if strings.EqualFold(k, vv.Name) {
					// Merge it with the existing
					if err := decode(mediaTypes, v, &f[i]); err != nil {
						return f, err
					}
					found = true
				}
			}
			if !found {
				var newOutFormat Format
				newOutFormat.Name = k
				if err := decode(mediaTypes, v, &newOutFormat); err != nil {
					return f, err
				}

				// We need values for these
				if newOutFormat.BaseName == "" {
					newOutFormat.BaseName = "index"
				}
				if newOutFormat.Rel == "" {
					newOutFormat.Rel = "alternate"
				}

				f = append(f, newOutFormat)
			}
		}
	}

	sort.Sort(f)

	return f, nil
}
820646
func (o *Osc) Fill(buf *audio.PCMBuffer) error {
	if o == nil {
		return nil
	}
	len := buf.Len()
	for i := 0; i < len; i++ {
		switch buf.DataType {
		case audio.Integer:
			buf.Ints[i] = int(o.Sample())
		case audio.Float:
			buf.Floats[i] = o.Sample()
		case audio.Byte:
			// TODO: check the format bitdepth and endianess and convert
			return errors.New("bytes buffer not yet supported")
		}
	}
	return nil
}
750552
func (eBase *elementBase) hasMixinDecs() bool {
	for _, mi := range eBase.mixins {
		if decs, _ := mi.decsParams(); len(decs) > 0 {
			return true
		}
	}

	return false
}
907563
func (d *Decoder) PCMBuffer(buf *audio.IntBuffer) (n int, err error) {
	if buf == nil {
		return 0, nil
	}

	if !d.pcmDataAccessed {
		err := d.FwdToPCM()
		if err != nil {
			return 0, d.err
		}
	}
	if d.PCMChunk == nil {
		return 0, ErrPCMChunkNotFound
	}

	format := &audio.Format{
		NumChannels: int(d.NumChans),
		SampleRate:  int(d.SampleRate),
	}

	buf.SourceBitDepth = int(d.BitDepth)
	decodeF, err := sampleDecodeFunc(int(d.BitDepth))
	if err != nil {
		return 0, fmt.Errorf("could not get sample decode func %v", err)
	}

	bPerSample := bytesPerSample(int(d.BitDepth))
	// populate a file buffer to avoid multiple very small reads
	// we need to cap the buffer size to not be bigger than the pcm chunk.
	size := len(buf.Data) * bPerSample
	tmpBuf := make([]byte, size)
	var m int
	m, err = d.PCMChunk.R.Read(tmpBuf)
	if err != nil {
		if err == io.EOF {
			return m, nil
		}
		return m, err
	}
	if m == 0 {
		return m, nil
	}
	bufR := bytes.NewReader(tmpBuf[:m])
	sampleBuf := make([]byte, bPerSample, bPerSample)
	var misaligned bool
	if m%bPerSample > 0 {
		misaligned = true
	}

	// Note that we populate the buffer even if the
	// size of the buffer doesn't fit an even number of frames.
	for n = 0; n < len(buf.Data); n++ {
		buf.Data[n], err = decodeF(bufR, sampleBuf)
		if err != nil {
			// the last sample isn't a full sample but just padding.
			if misaligned {
				n--
			}
			break
		}
	}
	buf.Format = format
	if err == io.EOF {
		err = nil
	}

	return n, err
}
1319824
func (h *handler) ConvertFieldsDown(ctx ttnlog.Interface, appDown *types.DownlinkMessage, ttnDown *pb_broker.DownlinkMessage, _ *device.Device) error {
	if appDown.PayloadFields == nil || len(appDown.PayloadFields) == 0 {
		return nil
	}

	if appDown.PayloadRaw != nil {
		return errors.NewErrInvalidArgument("Downlink", "Both Fields and Payload provided")
	}

	app, err := h.applications.Get(appDown.AppID)
	if err != nil {
		return nil
	}

	var encoder PayloadEncoder
	switch app.PayloadFormat {
	case application.PayloadFormatCustom:
		encoder = &CustomDownlinkFunctions{
			Encoder: app.CustomEncoder,
			Logger:  functions.Ignore,
		}
	case application.PayloadFormatCayenneLPP:
		encoder = &cayennelpp.Encoder{}
	default:
		return nil
	}

	raw, _, err := encoder.Encode(appDown.PayloadFields, appDown.FPort)
	if err != nil {
		return err
	}

	appDown.PayloadRaw = raw

	return nil
}
148665
func (d *Decoder) stereo3() {
	// The standard is fairly vague on this step, and the reference
	// implementation is garbage. So this function may or may not be correct,
	// but at least it's correct enough to pass the compliance test.

	if d.mode != ModeJointStereo || d.modeExtension == 0 {
		return
	}

	ms, intensity := d.modeExtension&2 != 0, d.modeExtension&1 != 0

	for gr := 0; gr < 2; gr++ {
		longLow, longHigh := 0, 22
		shortLow, shortHigh := 0, 13
		if d.blockType[gr][1] == 2 {
			if d.mixedBlockFlag[gr][1] == 1 {
				longHigh = 8
				shortLow = 3
			} else {
				longLow = 22
			}
		} else {
			shortLow = 13
		}

		zeroPartStartS := [3]int{shortLow, shortLow, shortLow}
		nonzeroShort := false
		for window := 0; window < 3; window++ {
		sfbShort:
			for sfb := shortHigh - 1; sfb >= shortLow; sfb-- {
				low := scfBandsS[d.samplingFreq][sfb]
				length := scfBandsS[d.samplingFreq][sfb+1] - low
				winLow := 3*low + window*length
				winHigh := winLow + length
				for f := winLow; f < winHigh; f++ {
					if d.huffmanData[gr][1][f] != 0 {
						zeroPartStartS[window] = sfb + 1
						nonzeroShort = true
						break sfbShort
					}
				}
			}
		}

		zeroPartStartL := longLow
		if nonzeroShort {
			zeroPartStartL = longHigh
		} else {
		sfbLong:
			for sfb := longHigh - 1; sfb >= longLow; sfb-- {
				low := scfBandsL[d.samplingFreq][sfb]
				high := scfBandsL[d.samplingFreq][sfb+1]
				for f := low; f < high; f++ {
					if d.huffmanData[gr][1][f] != 0 {
						zeroPartStartL = sfb + 1
						break sfbLong
					}
				}
			}
		}

		for window := 0; window < 3; window++ {
			for sfb := shortLow; sfb < shortHigh; sfb++ {
				low := scfBandsS[d.samplingFreq][sfb]
				length := scfBandsS[d.samplingFreq][sfb+1] - low
				winLow := 3*low + window*length
				winHigh := winLow + length
				isPos := sfb
				if isPos == 12 {
					isPos = 11
				}
				isPos = d.scalefacS[gr][1][isPos][window]
				if intensity && sfb >= zeroPartStartS[window] && isPos < 7 {
					L := intensityFactors[isPos]
					R := 1 - L
					for f := winLow; f < winHigh; f++ {
						d.huffmanData[gr][1][f] = R * d.huffmanData[gr][0][f]
						d.huffmanData[gr][0][f] *= L
					}
				} else if ms {
					for f := winLow; f < winHigh; f++ {
						M, S := d.huffmanData[gr][0][f], d.huffmanData[gr][1][f]
						d.huffmanData[gr][0][f] = (M + S) / sqrt2
						d.huffmanData[gr][1][f] = (M - S) / sqrt2
					}
				}
			}
		}

		for sfb := longLow; sfb < longHigh; sfb++ {
			low := scfBandsL[d.samplingFreq][sfb]
			high := scfBandsL[d.samplingFreq][sfb+1]
			isPos := sfb
			if isPos == 21 {
				isPos = 20
			}
			isPos = d.scalefacL[gr][1][isPos]
			if intensity && sfb >= zeroPartStartL && isPos < 7 {
				L := intensityFactors[isPos]
				R := 1 - L
				for f := low; f < high; f++ {
					d.huffmanData[gr][1][f] = R * d.huffmanData[gr][0][f]
					d.huffmanData[gr][0][f] *= L
				}
			} else if ms {
				for f := low; f < high; f++ {
					M, S := d.huffmanData[gr][0][f], d.huffmanData[gr][1][f]
					d.huffmanData[gr][0][f] = (M + S) / sqrt2
					d.huffmanData[gr][1][f] = (M - S) / sqrt2
				}
			}
		}
	}
}
268881
func (as AnnotatedSentence) LoweredStringSlice() []string {
	retVal := make([]string, len(as), len(as))
	for i, a := range as {
		retVal[i] = a.Lowered
	}
	return retVal
}
760313
func decodeCompoundMessage(buf []byte) (trunc int, parts [][]byte, err error) {
	if len(buf) < 1 {
		err = fmt.Errorf("missing compound length byte")
		return
	}
	numParts := uint8(buf[0])
	buf = buf[1:]

	// Check we have enough bytes
	if len(buf) < int(numParts*2) {
		err = fmt.Errorf("truncated len slice")
		return
	}

	// Decode the lengths
	lengths := make([]uint16, numParts)
	for i := 0; i < int(numParts); i++ {
		lengths[i] = binary.BigEndian.Uint16(buf[i*2 : i*2+2])
	}
	buf = buf[numParts*2:]

	// Split each message
	for idx, msgLen := range lengths {
		if len(buf) < int(msgLen) {
			trunc = int(numParts) - idx
			return
		}

		// Extract the slice, seek past on the buffer
		slice := buf[:msgLen]
		buf = buf[msgLen:]
		parts = append(parts, slice)
	}
	return
}
643793
func Mix(rate int8, inputs []Input) Input {
	CheckRate(rate)
	switch len(inputs) {
	case 0:
		panic("can not mix empty inputs slice")
	case 1:
		return inputs[0]
	case 2:
		return inputs[0].Add(inputs[1])
	case 3:
		return Sum3(rate, inputs[0], inputs[1], inputs[2])
	case 4:
		return Sum4(rate, inputs[0], inputs[1], inputs[2], inputs[3])
	default:
		head := []Input{Sum4(rate, inputs[0], inputs[1], inputs[2], inputs[3])}
		return Mix(rate, append(head, inputs[4:]...))
	}
}