示例#1
0
func main() {
	wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
	if err != nil {
		log.Fatal(err)
	}
	oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg")
	if err != nil {
		log.Fatal(err)
	}
	const sampleRate = 22050
	const bytesPerSample = 4 // TODO: This should be defined in audio package
	audioContext, err = audio.NewContext(sampleRate)
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		s, err := wav.Decode(audioContext, wavF)
		if err != nil {
			log.Fatal(err)
			return
		}
		seCh <- s
		close(seCh)
	}()
	go func() {
		s, err := vorbis.Decode(audioContext, oggF)
		if err != nil {
			log.Fatal(err)
			return
		}
		p, err := audio.NewPlayer(audioContext, s)
		if err != nil {
			log.Fatal(err)
			return
		}
		musicCh <- &Player{
			audioPlayer: p,
			total:       time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
		}
		close(musicCh)
		// TODO: Is this goroutine-safe?
		p.Play()
	}()
	if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
		log.Fatal(err)
	}
	if musicPlayer != nil {
		if err := musicPlayer.close(); err != nil {
			log.Fatal(err)
		}
	}
}
示例#2
0
func parseFont() error {
	f, err := ebitenutil.OpenFile("_resources/fonts/mplus-1p-regular.ttf")
	if err != nil {
		return err
	}
	defer func() {
		_ = f.Close()
	}()
	b, err := ioutil.ReadAll(f)
	if err != nil {
		return err
	}
	tt, err := truetype.Parse(b)
	if err != nil {
		return err
	}
	w, h := textImage.Size()
	dst := image.NewRGBA(image.Rect(0, 0, w, h))
	const size = 24
	const dpi = 72
	d := &font.Drawer{
		Dst: dst,
		Src: image.White,
		Face: truetype.NewFace(tt, &truetype.Options{
			Size:    size,
			DPI:     dpi,
			Hinting: font.HintingFull,
		}),
	}
	y := size
	for _, s := range text {
		d.Dot = fixed.P(0, y)
		d.DrawString(s)
		y += size
	}
	return textImage.ReplacePixels(dst.Pix)
}
示例#3
0
func main() {
	wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
	if err != nil {
		log.Fatal(err)
	}
	oggF, err := ebitenutil.OpenFile("_resources/audio/game.ogg")
	if err != nil {
		log.Fatal(err)
	}
	// This sample rate doesn't match with wav/ogg's sample rate,
	// but decoders adjust them.
	const sampleRate = 48000
	const bytesPerSample = 4 // TODO: This should be defined in audio package
	audioContext, err = audio.NewContext(sampleRate)
	if err != nil {
		log.Fatal(err)
	}
	go func() {
		s, err := wav.Decode(audioContext, wavF)
		if err != nil {
			log.Fatal(err)
			return
		}
		b, err := ioutil.ReadAll(s)
		if err != nil {
			log.Fatal(err)
			return
		}
		seCh <- b
		close(seCh)
	}()
	go func() {
		s, err := vorbis.Decode(audioContext, oggF)
		if err != nil {
			log.Fatal(err)
			return
		}
		p, err := audio.NewPlayer(audioContext, s)
		if err != nil {
			log.Fatal(err)
			return
		}
		musicCh <- &Player{
			audioPlayer: p,
			total:       time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
		}
		close(musicCh)
		// TODO: Is this goroutine-safe?
		if err := p.Play(); err != nil {
			log.Fatal(err)
			return
		}
	}()
	if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
		log.Fatal(err)
	}
	if musicPlayer != nil {
		if err := musicPlayer.close(); err != nil {
			log.Fatal(err)
		}
	}
}