mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
docs: Add example/piano
This commit is contained in:
parent
eed7f50662
commit
6dd88923e8
@ -158,6 +158,7 @@ var examples = []example{
|
||||
{Name: "mosaic"},
|
||||
{Name: "paint"},
|
||||
{Name: "perspective"},
|
||||
{Name: "piano"},
|
||||
{Name: "rotate"},
|
||||
{Name: "blocks"},
|
||||
}
|
||||
|
39
_docs/public/example/piano.content.html
Normal file
39
_docs/public/example/piano.content.html
Normal file
@ -0,0 +1,39 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2015 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.
|
||||
-->
|
||||
<script>
|
||||
'use strict';
|
||||
window.addEventListener('load', function() {
|
||||
function isProduction() {
|
||||
var l = window.top.location;
|
||||
if (l.hash === '#_production') {
|
||||
return true;
|
||||
}
|
||||
if (l.hostname === 'localhost' || l.hostname === '127.0.0.1') {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
var s = document.createElement('script');
|
||||
var src = 'piano.js';
|
||||
if (isProduction()) {
|
||||
src = 'http://hajimehoshi.github.io/ebiten.pagestorage/master/' + src;
|
||||
}
|
||||
s.src = src;
|
||||
document.body.appendChild(s);
|
||||
});
|
||||
</script>
|
174
_docs/public/example/piano.html
Normal file
174
_docs/public/example/piano.html
Normal file
@ -0,0 +1,174 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Copyright 2015 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 (
|
||||
"fmt"
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"github.com/hajimehoshi/ebiten/example/common"
|
||||
"github.com/hajimehoshi/ebiten/exp/audio"
|
||||
"image/color"
|
||||
"log"
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
screenWidth = 320
|
||||
screenHeight = 240
|
||||
)
|
||||
|
||||
var pcm = make([]float64, 4*audio.SampleRate())
|
||||
|
||||
const baseFreq = 220
|
||||
|
||||
func init() {
|
||||
s := float64(audio.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 < len(pcm); i++ {
|
||||
v := 0.0
|
||||
twoPiF := 2.0 * math.Pi * baseFreq
|
||||
for j := 0; j < len(amp); j++ {
|
||||
a := amp[j] * math.Exp(-5*float64(i)/(x[j]*s))
|
||||
v += a * math.Sin(float64(i)*twoPiF*float64(j+1)/s)
|
||||
}
|
||||
pcm[i] = v / 5.0
|
||||
}
|
||||
}
|
||||
|
||||
func addNote(freq float64, vol float64) {
|
||||
f := int(freq)
|
||||
length := len(pcm) * baseFreq / f
|
||||
l := make([]int16, length)
|
||||
r := make([]int16, length)
|
||||
j := 0
|
||||
jj := 0
|
||||
for i := 0; i < len(l); i++ {
|
||||
p := pcm[j]
|
||||
l[i] = int16(p * vol * math.MaxInt16)
|
||||
r[i] = l[i]
|
||||
jj += f
|
||||
j = jj / baseFreq
|
||||
}
|
||||
audio.Play(-1, l, r)
|
||||
}
|
||||
|
||||
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]++
|
||||
}
|
||||
}
|
||||
|
||||
func update(screen *ebiten.Image) error {
|
||||
updateInput()
|
||||
for i, key := range keys {
|
||||
if keyStates[key] != 1 {
|
||||
continue
|
||||
}
|
||||
addNote(220*math.Exp2(float64(i-1)/12.0), 1.0)
|
||||
}
|
||||
|
||||
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
|
||||
|
||||
whiteKeys := []string{"A", "S", "D", "F", "G", "H", "J", "K", "L"}
|
||||
width := 24
|
||||
y := 48
|
||||
for i, k := range whiteKeys {
|
||||
x := i*width + 36
|
||||
height := 112
|
||||
screen.DrawFilledRect(x, y, width-1, height, color.White)
|
||||
common.ArcadeFont.DrawText(screen, k, x+8, y+height-16, 1, color.Black)
|
||||
}
|
||||
|
||||
blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
|
||||
for i, k := range blackKeys {
|
||||
if k == "" {
|
||||
continue
|
||||
}
|
||||
x := i*width + 24
|
||||
height := 64
|
||||
screen.DrawFilledRect(x, y, width-1, height, color.Black)
|
||||
common.ArcadeFont.DrawText(screen, k, x+8, y+height-16, 1, color.White)
|
||||
}
|
||||
|
||||
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
|
||||
|
||||
<footer>© 2015 Hajime Hoshi</footer>
|
@ -72,6 +72,8 @@ pre {
|
||||
|
||||
<a href="example/perspective.html"><img src="images/example/perspective.png" width="320" height="240" alt="Ebiten example: perspective" class="example"></a>
|
||||
|
||||
<a href="example/piano.html"><img src="images/example/piano.png" width="320" height="240" alt="Ebiten example: piano" class="example"></a>
|
||||
|
||||
<a href="example/rotate.html"><img src="images/example/rotate.png" width="320" height="240" alt="Ebiten example: rotate" class="example"></a>
|
||||
|
||||
<a href="example/blocks.html"><img src="images/example/blocks.png" width="256" height="240" alt="Ebiten example: blocks" class="example"></a>
|
||||
|
Loading…
Reference in New Issue
Block a user