Beispiel #1
0
func update(buf audio.PCM32Samples, dec audio.Decoder) error {
	n, err := dec.Read(buf)
	if err != nil {
		return err
	}
	fmt.Println(len(buf), n)
	if n == 0 {
		return errutil.NewNoPos("EOF")
	}

	// The x scale is relative to the number of frames.
	xscale := len(buf) / width

	// The y scale is relative to the range of values from the frames.
	yscale := Range(buf) / height

	i := image.NewRGBA(image.Rect(0, 0, width, height))
	draw.Draw(i, i.Bounds(), &image.Uniform{black}, image.ZP, draw.Src)

	oldx, oldy := 0, height/2
	for x := 0; x < width; x += 2 {
		// loudness from the frame scaled down to the image.
		loudness := int(buf[x*xscale]) / (yscale + 1)

		// To center the oscilloscope on the y axis we add height/2.
		y := loudness + height/2

		// draw a line between (x, y) and (oldx, oldy)
		line(i, x, y, oldx, oldy)

		// Update old x/y.
		oldx, oldy = x, y
	}

	// Change image.Image type to win.Image.
	img, err := win.ReadImage(i)
	if err != nil {
		return errutil.Err(err)
	}
	// Draw image to window.
	err = win.Draw(image.ZP, img)
	if err != nil {
		return errutil.Err(err)
	}

	// Display window updates on screen.
	return win.Update()
}
Beispiel #2
0
// simple demonstrates how to draw images using the Draw and DrawRect methods.
// It also gives an example of a basic event loop.
func simple() (err error) {
	runtime.GOMAXPROCS(runtime.NumCPU())
	defer profile.Start(profile.CPUProfile).Stop()

	// Open the window.
	err = win.Open(width, height, win.Resizeable)
	if err != nil {
		return err
	}
	defer win.Close()

	// Load image resources.
	err = loadResources()
	if err != nil {
		return err
	}
	defer freeResources()

	// start and frames will be used to calculate the average FPS of the
	// application.
	start := time.Now()
	frames := 0

	// Render the images onto the window.
	err = render()
	if err != nil {
		return err
	}

	// Update and event loop.
	for {
		// Display window updates on screen.
		err = win.Update()
		if err != nil {
			return err
		}
		frames++

		// Poll events until the event queue is empty.
		for e := win.PollEvent(); e != nil; e = win.PollEvent() {
			fmt.Printf("%T event: %v\n", e, e)
			switch obj := e.(type) {
			case we.KeyPress:
				r := obj.Key.String()
				err = zz(r)
			case we.KeyRepeat:
				r := obj.Key.String()
				err = zz(r)
			case we.KeyRune:
				r := obj.String()
				err = zz(r)
			}
			if err != nil {
				return err
			}
			switch e.(type) {
			case we.Close:
				displayFPS(start, frames)
				// Close the application.
				return nil
			case we.Resize:
				// Rerender the images onto the window after resize events.
				err = render()
				if err != nil {
					return err
				}
			}
		}

		// Cap refresh rate at 30 FPS.
		time.Sleep(time.Second / 30)
	}
}