ebiten/docs/examples/piano.html

283 lines
8.8 KiB
HTML
Raw Normal View History

2016-04-13 18:26:31 +02:00
<!DOCTYPE html>
2016-08-27 17:58:27 +02:00
<script src="../scripts/force-https.js"></script>
2016-04-13 18:26:31 +02:00
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
<link rel="icon" href="../favicon.png" type="image/png" >
<title>Ebiten example - piano</title>
2016-08-27 17:48:15 +02:00
<link rel="stylesheet" href="../stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="../stylesheets/highlight-github.css">
<link rel="stylesheet" href="../stylesheets/ebiten.css">
2016-08-26 19:28:57 +02:00
<header class="navbar"><div class="container">
<div class="navbar-header">
2016-08-27 17:48:15 +02:00
<a class="navbar-brand" href="..">Ebiten</a>
2016-08-26 19:28:57 +02:00
</div>
2016-08-26 19:37:08 +02:00
<nav class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/hajimehoshi/ebiten">GitHub</a></li>
2016-08-27 17:58:27 +02:00
<li><a href="https://godoc.org/github.com/hajimehoshi/ebiten">GoDoc</a></li>
2016-08-26 19:37:08 +02:00
<li><a href="https://github.com/hajimehoshi/ebiten/wiki">Wiki</a>
</ul>
</nav>
2016-08-26 19:28:57 +02:00
</header>
2016-04-13 18:26:31 +02:00
2016-08-26 19:28:57 +02:00
<main><div class="container">
2016-08-26 20:18:16 +02:00
<h2>Ebiten example - piano</h2>
2016-08-26 19:28:57 +02:00
<iframe src="piano.content.html" width="640" height="480"></iframe>
2016-08-27 07:00:06 +02:00
<pre><code class="language-go">// &#43;build example
2016-08-26 16:33:36 +02:00
package main
2016-04-13 18:26:31 +02:00
import (
2016-08-26 22:36:52 +02:00
&#34;fmt&#34;
&#34;image/color&#34;
&#34;log&#34;
&#34;math&#34;
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
&#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
&#34;github.com/hajimehoshi/ebiten/examples/common&#34;
2016-04-13 18:26:31 +02:00
)
const (
2016-08-26 22:36:52 +02:00
screenWidth = 320
screenHeight = 240
sampleRate = 44100
2016-04-13 18:26:31 +02:00
)
var audioContext *audio.Context
func init() {
2016-08-26 22:36:52 +02:00
var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
2016-04-13 18:26:31 +02:00
}
var pcm = make([]float64, 4*sampleRate)
const baseFreq = 220
func init() {
2016-08-26 22:36:52 +02:00
s := float64(sampleRate)
amp := []float64{1.0, 0.8, 0.6, 0.4, 0.2}
x := []float64{4.0, 2.0, 1.0, 0.5, 0.25}
for i := 0; i &lt; len(pcm); i&#43;&#43; {
v := 0.0
twoPiF := 2.0 * math.Pi * baseFreq
for j := 0; j &lt; len(amp); j&#43;&#43; {
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
v &#43;= a * math.Sin(float64(i)*twoPiF*float64(j&#43;1)/s)
}
pcm[i] = v / 5.0
2016-04-13 18:26:31 +02:00
}
}
var (
2016-08-26 22:36:52 +02:00
noteCache = map[int][]byte{}
2016-04-13 18:26:31 +02:00
)
func toBytes(l, r []int16) []byte {
2016-08-26 22:36:52 +02:00
if len(l) != len(r) {
panic(&#34;len(l) must equal to len(r)&#34;)
}
b := make([]byte, len(l)*4)
for i := range l {
b[4*i] = byte(l[i])
b[4*i&#43;1] = byte(l[i] &gt;&gt; 8)
b[4*i&#43;2] = byte(r[i])
b[4*i&#43;3] = byte(r[i] &gt;&gt; 8)
}
return b
2016-04-13 18:26:31 +02:00
}
func addNote(freq float64, vol float64) error {
2016-08-26 22:36:52 +02:00
// TODO: Call Close method of *audio.Player.
// However, this works without Close because Close is automatically called when GC
// collects a *audio.Player object.
f := int(freq)
if n, ok := noteCache[f]; ok {
p, err := audio.NewPlayerFromBytes(audioContext, n)
if err != nil {
return err
}
if err := p.Play(); err != nil {
return err
}
return nil
}
length := len(pcm) * baseFreq / f
l := make([]int16, length)
r := make([]int16, length)
j := 0
jj := 0
for i := 0; i &lt; len(l); i&#43;&#43; {
p := pcm[j]
l[i] = int16(p * vol * math.MaxInt16)
r[i] = l[i]
jj &#43;= f
j = jj / baseFreq
}
n := toBytes(l, r)
noteCache[f] = n
2016-08-01 17:36:43 +02:00
p, err := audio.NewPlayerFromBytes(audioContext, n)
2016-04-13 18:26:31 +02:00
if err != nil {
2016-08-26 22:36:52 +02:00
return err
2016-04-13 18:26:31 +02:00
}
2016-08-26 16:33:36 +02:00
if err := p.Play(); err != nil {
2016-08-26 22:36:52 +02:00
return err
2016-08-26 16:33:36 +02:00
}
2016-04-13 18:26:31 +02:00
return nil
}
var keys = []ebiten.Key{
2016-08-26 22:36:52 +02:00
ebiten.KeyQ,
ebiten.KeyA,
ebiten.KeyW,
ebiten.KeyS,
ebiten.KeyD,
ebiten.KeyR,
ebiten.KeyF,
ebiten.KeyT,
ebiten.KeyG,
ebiten.KeyH,
ebiten.KeyU,
ebiten.KeyJ,
ebiten.KeyI,
ebiten.KeyK,
ebiten.KeyO,
ebiten.KeyL,
2016-04-13 18:26:31 +02:00
}
var keyStates = map[ebiten.Key]int{}
func init() {
2016-08-26 22:36:52 +02:00
for _, key := range keys {
keyStates[key] = 0
}
2016-04-13 18:26:31 +02:00
}
func updateInput() {
2016-08-26 22:36:52 +02:00
for _, key := range keys {
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
}
keyStates[key]&#43;&#43;
2016-04-13 18:26:31 +02:00
}
}
var (
2016-08-26 22:36:52 +02:00
imagePiano *ebiten.Image
2016-04-13 18:26:31 +02:00
)
func init() {
2016-08-26 22:36:52 +02:00
var err error
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil {
panic(err)
2016-08-26 16:33:36 +02:00
}
2016-08-26 22:36:52 +02:00
if err := imageEmpty.Fill(color.White); err != nil {
panic(err)
2016-08-26 16:33:36 +02:00
}
2016-08-26 22:36:52 +02:00
imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
whiteKeys := []string{&#34;A&#34;, &#34;S&#34;, &#34;D&#34;, &#34;F&#34;, &#34;G&#34;, &#34;H&#34;, &#34;J&#34;, &#34;K&#34;, &#34;L&#34;}
width := 24
y := 48
for i, k := range whiteKeys {
x := i*width &#43; 36
height := 112
op := &amp;ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1, 1, 1, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.Black); err != nil {
panic(err)
}
2016-08-26 16:33:36 +02:00
}
2016-08-26 22:36:52 +02:00
blackKeys := []string{&#34;Q&#34;, &#34;W&#34;, &#34;&#34;, &#34;R&#34;, &#34;T&#34;, &#34;&#34;, &#34;U&#34;, &#34;I&#34;, &#34;O&#34;}
for i, k := range blackKeys {
if k == &#34;&#34; {
continue
}
x := i*width &#43; 24
height := 64
op := &amp;ebiten.DrawImageOptions{}
w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.White); err != nil {
panic(err)
}
2016-08-26 16:33:36 +02:00
}
2016-04-13 18:26:31 +02:00
}
func update(screen *ebiten.Image) error {
2016-08-26 22:36:52 +02:00
updateInput()
for i, key := range keys {
if keyStates[key] != 1 {
continue
}
if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil {
return err
}
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil {
return err
}
if err := screen.DrawImage(imagePiano, nil); err != nil {
return err
}
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %0.2f&#34;, ebiten.CurrentFPS())); err != nil {
return err
}
2016-04-21 18:16:58 +02:00
2016-08-26 22:36:52 +02:00
if err := audioContext.Update(); err != nil {
return err
}
return nil
2016-04-13 18:26:31 +02:00
}
func main() {
2016-08-26 22:36:52 +02:00
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Piano (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
2016-04-13 18:26:31 +02:00
}
</code></pre>
2016-08-26 19:28:57 +02:00
</div></main>
<footer><div class="container">
<p>© 2013 Hajime Hoshi</p>
<p>Code is licensed under <a href="https://github.com/hajimehoshi/ebiten/blob/master/LICENSE">Apache License 2.0</a>.</p>
<p>The content of this page is licensed under <a href="https://creativecommons.org/licenses/by/4.0/">the Creative Commons Attribution 4.0 License</a>.</p>
</div></footer>
2016-04-13 18:26:31 +02:00
2016-08-27 17:48:15 +02:00
<script src="../scripts/highlight.pack.js"></script>
2016-08-27 07:00:06 +02:00
<script>hljs.initHighlightingOnLoad();</script>
2016-08-27 17:44:50 +02:00
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-83252440-1', 'auto');
ga('send', 'pageview');
</script>