Example #1
0
func oscilloscope(filename string) (err error) {
	// Open the window.
	err = win.Open(width, height, win.Resizeable)
	if err != nil {
		return errutil.Err(err)
	}
	defer win.Close()

	f, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer f.Close()
	br := bufio.NewReader(f)

	dec, magic, err := audio.NewDecoder(br)
	if err != nil {
		return err
	}
	fmt.Println("magic:", magic)

	// Info about the sound, number of samples, sample rate etc.
	conf := dec.Config()
	fmt.Println(conf)

	// Play the music.
	go play(filename)

	nsamples := conf.Channels * conf.SampleRate
	buf := make(audio.PCM32Samples, nsamples/fps)

	// Update and event loop.
	for {
		err := update(buf, dec)
		if err != nil {
			return errutil.Err(err)
		}

		// Poll events until the event queue is empty.
		for e := win.PollEvent(); e != nil; e = win.PollEvent() {
			switch e.(type) {
			case we.Close:
				os.Exit(0)
			}
		}
		time.Sleep(time.Second / (fps * 2))
	}
	return nil
}
Example #2
0
File: a.go Project: karlek/vanilj
// 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)
	}
}