// ConvertSampleRate converts the sample rate
198273
func ConvertSampleRate(srcSamples []int32, srcSampleRate, dstSampleRate int) (dstSamples []int32, err error) {
	// Do nothing
	if srcSampleRate == dstSampleRate {
		dstSamples = srcSamples
		return
	}

	// For now we don't handle data loss
	if srcSampleRate < dstSampleRate {
		err = errors.New("astiaudio: src sample rate < dst sample rate")
		return
	}

	// Get ratio
	ratio := float64(srcSampleRate) / float64(dstSampleRate)

	// Loop
	var idx float64
	for {
		// Take floor of index
		floor := int(math.Floor(idx))

		// Out of range
		if floor >= len(srcSamples) {
			break
		}

		// Append sample
		dstSamples = append(dstSamples, srcSamples[floor])

		// Increment index
		idx += ratio
	}
	return
}
771182
func GetMPEGAudioDuration(r readerutil.SizeReaderAt) (time.Duration, error) {
	var header uint32
	if err := binary.Read(io.NewSectionReader(r, 0, r.Size()), binary.BigEndian, &header); err != nil {
		return 0, fmt.Errorf("Failed to read MPEG frame header: %v", err)
	}
	getBits := func(startBit, numBits uint) uint32 {
		return (header << startBit) >> (32 - numBits)
	}

	if getBits(0, 11) != 0x7ff {
		return 0, errors.New("Missing sync bits in MPEG frame header")
	}
	var version mpegVersion
	var ok bool
	if version, ok = mpegVersionsByID[getBits(11, 2)]; !ok {
		return 0, errors.New("Invalid MPEG version index")
	}
	var layer mpegLayer
	if layer, ok = mpegLayersByIndex[getBits(13, 2)]; !ok {
		return 0, errors.New("Invalid MPEG layer index")
	}
	bitrate := mpegBitrates[version][layer][getBits(16, 4)]
	if bitrate == 0 {
		return 0, errors.New("Invalid MPEG bitrate")
	}
	samplingRate := mpegSamplingRates[version][getBits(20, 2)]
	if samplingRate == 0 {
		return 0, errors.New("Invalid MPEG sample rate")
	}
	samplesPerFrame := mpegSamplesPerFrame[version][layer]

	var xingHeaderStart int64 = 4
	// Skip "side information".
	if getBits(24, 2) == 0x3 { // Channel mode; 0x3 is mono.
		xingHeaderStart += 17
	} else {
		xingHeaderStart += 32
	}
	// Skip 16-bit CRC if present.
	if getBits(15, 1) == 0x0 { // 0x0 means "has protection".
		xingHeaderStart += 2
	}

	b := make([]byte, 12)
	if _, err := r.ReadAt(b, xingHeaderStart); err != nil {
		return 0, fmt.Errorf("Unable to read Xing header at %d: %v", xingHeaderStart, err)
	}
	var ms int64
	if bytes.Equal(b[0:4], xingHeaderName) || bytes.Equal(b[0:4], infoHeaderName) {
		r := bytes.NewReader(b[4:])
		var xingFlags uint32
		binary.Read(r, binary.BigEndian, &xingFlags)
		if xingFlags&0x1 == 0x0 {
			return 0, fmt.Errorf("Xing header at %d lacks number of frames", xingHeaderStart)
		}
		var numFrames uint32
		binary.Read(r, binary.BigEndian, &numFrames)
		ms = int64(samplesPerFrame) * int64(numFrames) * 1000 / int64(samplingRate)
	} else {
		// Okay, no Xing VBR header. Assume that the file has a constant bitrate.
		// (The other alternative is to read the whole file and examine each frame.)
		ms = r.Size() / int64(bitrate) * 8
	}
	return time.Duration(ms) * time.Millisecond, nil
}
1371167
func (s *AiffSettings) SetSampleRate(v int64) *AiffSettings {
	s.SampleRate = &v
	return s
}
4862
func (s *Mp2Settings) SetSampleRate(v float64) *Mp2Settings {
	s.SampleRate = &v
	return s
}
1252575
func (c *StatsdClient) TimingWithSampling(stat string, delta int64, sampleRate float32) error {
	if err := checkSampleRate(sampleRate); err != nil {
		return err
	}

	if !shouldFire(sampleRate) {
		return nil // ignore this call
	}

	return c.send(stat, "%d|ms", delta, sampleRate)
}
682277
func (s *AudioParameters) SetSampleRate(v string) *AudioParameters {
	s.SampleRate = &v
	return s
}
1049729
func (wav *Reader) ReadRawSample() ([]byte, error) {
	if wav.samplesRead > wav.numSamples {
		return nil, io.EOF
	}

	buf := make([]byte, wav.bytesPerSample)
	n, err := wav.input.Read(buf)
	if err != nil {
		return nil, err
	}

	if n != int(wav.bytesPerSample) {
		return nil, fmt.Errorf("Read %d bytes, should have read %d", n, wav.bytesPerSample)
	}

	wav.samplesRead++

	return buf, nil
}
1194768
func (l SamplingEventLogger) SetRootSampleRate(root ID, p float64) {
	l.m.Lock()
	defer l.m.Unlock()

	l.rootRates[root] = p
}
1154378
func (s *SynthesizeSpeechInput) SetSampleRate(v string) *SynthesizeSpeechInput {
	s.SampleRate = &v
	return s
}
940118
func (s *GetIntentOutput) SetSampleUtterances(v []*string) *GetIntentOutput {
	s.SampleUtterances = v
	return s
}
919750
func (c *Memory) StartSampling(samplingInterval int, suppressRandomness bool) (*gcdmessage.ChromeResponse, error) {
	var v MemoryStartSamplingParams
	v.SamplingInterval = samplingInterval
	v.SuppressRandomness = suppressRandomness
	return c.StartSamplingWithParams(&v)
}
80556
func MemProfileRate(rate int) func(*Profile) {
	return func(p *Profile) {
		p.memProfileRate = rate
		p.mode = memMode
	}
}
1080185
func (d *Decoder) decodeSamples1() error {
	for s := 0; s < 12; s++ {
		for sb := 0; sb < 32; sb++ {
			nch := d.nChannels
			if sb >= d.bound {
				nch = 1
			}

			for ch := 0; ch < nch; ch++ {
				if d.allocation[ch][sb] != 0 {
					sample, err := d.stream.readBits(d.allocation[ch][sb])
					if err != nil {
						return err
					}
					d.sample[ch][32*s+sb] = float32(sample)
				}
			}
		}

		for sb := d.bound; sb < 32; sb++ {
			d.sample[1][32*s+sb] = d.sample[0][32*s+sb]
		}
	}
	return nil
}
741490
func ExtractSamples(o *DecodeOptions, fams ...*dto.MetricFamily) (model.Vector, error) {
	var (
		all     model.Vector
		lastErr error
	)
	for _, f := range fams {
		some, err := extractSamples(f, o)
		if err != nil {
			lastErr = err
			continue
		}
		all = append(all, some...)
	}
	return all, lastErr
}
388746
func (e *RecordEncoder) Samples(samples []RefSample, b []byte) []byte {
	buf := encoding.Encbuf{B: b}
	buf.PutByte(byte(RecordSamples))

	if len(samples) == 0 {
		return buf.Get()
	}

	// Store base timestamp and base reference number of first sample.
	// All samples encode their timestamp and ref as delta to those.
	first := samples[0]

	buf.PutBE64(first.Ref)
	buf.PutBE64int64(first.T)

	for _, s := range samples {
		buf.PutVarint64(int64(s.Ref) - int64(first.Ref))
		buf.PutVarint64(s.T - first.T)
		buf.PutBE64(math.Float64bits(s.V))
	}
	return buf.Get()
}
958169
func (s *WavSettings) SetSampleRate(v int64) *WavSettings {
	s.SampleRate = &v
	return s
}
43681
func (l SamplingEventLogger) UnsetRootSampleRate(root ID) {
	l.m.Lock()
	defer l.m.Unlock()

	delete(l.rootRates, root)
}