Ejemplo n.º 1
0
func main() {
	w := new(wav.Wav)
	w.Channels = 1
	w.SamplesPerSecond = 44100
	w.Samples[wav.Mono] = make([]wav.Sample8, w.SamplesPerSecond*5)

	// put in a square wave

	halfWave := 1000
	high := wav.Sample8(255)
	low := wav.Sample8(0)

	value := low
	for i := 0; i < len(w.Samples[wav.Mono]); i++ {
		if i%halfWave == 0 {
			if halfWave > 10 {
				halfWave -= 1
			}
			if value == low {
				value = high
			} else {
				value = low
			}
		}
		w.Samples[wav.Mono][i] = value
	}

	w.Write(os.Stdout)
}
Ejemplo n.º 2
0
func (f *Frame) RenderTo8Bit(out []wav.Sample8) {
	if len(out) != len(f) {
		panic("mismatched buffers")
	}
	for i := 0; i < len(f); i++ {
		in := f[i]

		// clip at [-1.0, 1.0]
		if in > 1.0 {
			in = 1.0
		}
		if in < -1.0 {
			in = -1.0
		}

		if in == 1.0 {
			// special case because 1.0 -> 0 otherwise (due to overflow)
			out[i] = 255
		} else {
			out[i] = wav.Sample8((in + 1) / 2 * 256)
		}
	}
}