ebiten/_docs/public/examples/piano.html
2016-04-22 01:17:05 +09:00

263 lines
6.4 KiB
HTML

<!DOCTYPE html>
<!--
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
<link rel="icon" href="../favicon.png" type="image/png" >
<title>Ebiten example - piano</title>
<style>
body {
font-family: sans-serif;
}
iframe {
border-color: #999;
border-style: solid;
border-width: 1px;
overflow: hidden;
}
pre {
background: #eee;
padding: 1em;
}
</style>
<nav><a href="..">Ebiten</a></nav>
<h1>Ebiten example - piano</h1>
<iframe src="piano.content.html" width="640" height="480"></iframe>
<pre><code>package main
import (
&#34;bytes&#34;
&#34;fmt&#34;
&#34;image/color&#34;
&#34;log&#34;
&#34;math&#34;
&#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;
)
const (
screenWidth = 320
screenHeight = 240
sampleRate = 44100
)
var audioContext *audio.Context
func init() {
var err error
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
}
var pcm = make([]float64, 4*sampleRate)
const baseFreq = 220
func init() {
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
}
}
var (
noteCache = map[int][]byte{}
)
func toBytes(l, r []int16) []byte {
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
}
type stream struct {
*bytes.Reader
}
func (s *stream) Close() error {
s.Reader = nil
return nil
}
func addNote(freq float64, vol float64) error {
f := int(freq)
if n, ok := noteCache[f]; ok {
p, err := audio.NewPlayer(audioContext, &amp;stream{bytes.NewReader(n)})
if err != nil {
return err
}
p.Play()
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
p, err := audio.NewPlayer(audioContext, &amp;stream{bytes.NewReader(n)})
if err != nil {
return err
}
p.Play()
return nil
}
var keys = []ebiten.Key{
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,
}
var keyStates = map[ebiten.Key]int{}
func init() {
for _, key := range keys {
keyStates[key] = 0
}
}
func updateInput() {
for _, key := range keys {
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
}
keyStates[key]&#43;&#43;
}
}
var (
imagePiano *ebiten.Image
)
func init() {
var err error
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil {
panic(err)
}
imageEmpty.Fill(color.White)
imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
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)
imagePiano.DrawImage(imageEmpty, op)
common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.Black)
}
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)
imagePiano.DrawImage(imageEmpty, op)
common.ArcadeFont.DrawText(imagePiano, k, x&#43;8, y&#43;height-16, 1, color.White)
}
}
func update(screen *ebiten.Image) error {
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
}
}
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
screen.DrawImage(imagePiano, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf(&#34;FPS: %0.2f&#34;, ebiten.CurrentFPS()))
if err := audioContext.Update(); err != nil {
return err
}
return nil
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Piano (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
}
</code></pre>
<footer>© 2014 Hajime Hoshi</footer>