2015-01-25 16:09:14 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-02-10 18:04:23 +01:00
|
|
|
"bytes"
|
2015-01-25 16:09:14 +01:00
|
|
|
"fmt"
|
2015-06-07 17:04:21 +02:00
|
|
|
"image/color"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
|
2015-01-25 16:09:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
2016-04-18 18:24:44 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
2015-01-25 16:09:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2016-02-10 19:07:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/examples/common"
|
2015-01-25 16:09:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
2016-02-09 15:04:00 +01:00
|
|
|
sampleRate = 44100
|
2015-01-25 16:09:14 +01:00
|
|
|
)
|
|
|
|
|
2016-03-02 16:48:59 +01:00
|
|
|
var audioContext *audio.Context
|
|
|
|
|
|
|
|
func init() {
|
2016-04-02 19:59:44 +02:00
|
|
|
var err error
|
|
|
|
audioContext, err = audio.NewContext(sampleRate)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-03-02 16:48:59 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 15:04:00 +01:00
|
|
|
var pcm = make([]float64, 4*sampleRate)
|
2015-01-25 16:09:14 +01:00
|
|
|
|
|
|
|
const baseFreq = 220
|
|
|
|
|
|
|
|
func init() {
|
2016-02-09 15:04:00 +01:00
|
|
|
s := float64(sampleRate)
|
2015-01-25 16:09:14 +01:00
|
|
|
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)
|
|
|
|
}
|
2015-01-25 17:20:56 +01:00
|
|
|
pcm[i] = v / 5.0
|
2015-01-25 16:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 03:19:30 +01:00
|
|
|
var (
|
2015-06-07 17:04:21 +02:00
|
|
|
noteCache = map[int][]byte{}
|
2015-02-17 03:19:30 +01:00
|
|
|
)
|
|
|
|
|
2015-06-07 17:04:21 +02:00
|
|
|
func toBytes(l, r []int16) []byte {
|
|
|
|
if len(l) != len(r) {
|
|
|
|
panic("len(l) must equal to len(r)")
|
|
|
|
}
|
2015-06-13 20:27:02 +02:00
|
|
|
b := make([]byte, len(l)*4)
|
|
|
|
for i, _ := range l {
|
|
|
|
b[4*i] = byte(l[i])
|
|
|
|
b[4*i+1] = byte(l[i] >> 8)
|
|
|
|
b[4*i+2] = byte(r[i])
|
|
|
|
b[4*i+3] = byte(r[i] >> 8)
|
2015-06-07 17:04:21 +02:00
|
|
|
}
|
2015-06-13 20:27:02 +02:00
|
|
|
return b
|
2015-06-07 17:04:21 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 18:04:23 +01:00
|
|
|
type stream struct {
|
|
|
|
*bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stream) Close() error {
|
2016-03-28 20:37:28 +02:00
|
|
|
s.Reader = nil
|
2016-02-10 18:04:23 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 13:39:48 +01:00
|
|
|
func addNote(freq float64, vol float64) error {
|
2016-04-21 19:07:43 +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.
|
2015-01-25 16:09:14 +01:00
|
|
|
f := int(freq)
|
2015-06-07 17:04:21 +02:00
|
|
|
if n, ok := noteCache[f]; ok {
|
2016-04-19 16:43:54 +02:00
|
|
|
p, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
|
2016-02-12 13:39:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-10 18:18:39 +01:00
|
|
|
p.Play()
|
2016-02-12 13:39:48 +01:00
|
|
|
return nil
|
2015-02-17 03:19:30 +01:00
|
|
|
}
|
2015-01-25 16:09:14 +01:00
|
|
|
length := len(pcm) * baseFreq / f
|
|
|
|
l := make([]int16, length)
|
|
|
|
r := make([]int16, length)
|
2015-01-26 03:33:37 +01:00
|
|
|
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
|
2015-01-25 16:09:14 +01:00
|
|
|
}
|
2015-06-07 17:04:21 +02:00
|
|
|
n := toBytes(l, r)
|
|
|
|
noteCache[f] = n
|
2016-04-19 16:43:54 +02:00
|
|
|
p, err := audio.NewPlayer(audioContext, &stream{bytes.NewReader(n)})
|
2016-02-12 13:39:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-10 18:18:39 +01:00
|
|
|
p.Play()
|
2016-02-12 13:39:48 +01:00
|
|
|
return nil
|
2015-01-25 16:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var keys = []ebiten.Key{
|
2015-01-26 03:33:37 +01:00
|
|
|
ebiten.KeyQ,
|
2015-01-25 16:09:14 +01:00
|
|
|
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]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 09:45:08 +01:00
|
|
|
var (
|
|
|
|
imagePiano *ebiten.Image
|
|
|
|
)
|
2015-01-28 19:22:36 +01:00
|
|
|
|
2015-02-16 02:33:27 +01:00
|
|
|
func init() {
|
|
|
|
var err error
|
2016-02-06 09:45:08 +01:00
|
|
|
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)
|
2015-02-16 02:33:27 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-28 19:22:36 +01:00
|
|
|
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
|
2016-02-06 09:45:08 +01:00
|
|
|
op := &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+8, y+height-16, 1, color.Black)
|
2015-01-28 19:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
|
|
|
|
for i, k := range blackKeys {
|
|
|
|
if k == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x := i*width + 24
|
|
|
|
height := 64
|
2016-02-06 09:45:08 +01:00
|
|
|
op := &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+8, y+height-16, 1, color.White)
|
2015-01-28 19:22:36 +01:00
|
|
|
}
|
2015-02-16 02:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func update(screen *ebiten.Image) error {
|
|
|
|
updateInput()
|
|
|
|
for i, key := range keys {
|
|
|
|
if keyStates[key] != 1 {
|
|
|
|
continue
|
|
|
|
}
|
2016-02-12 13:39:48 +01:00
|
|
|
if err := addNote(220*math.Exp2(float64(i-1)/12.0), 1.0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-16 02:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
|
2016-02-06 09:45:08 +01:00
|
|
|
screen.DrawImage(imagePiano, nil)
|
2015-01-28 19:22:36 +01:00
|
|
|
|
2015-01-25 16:09:14 +01:00
|
|
|
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
|
2016-04-21 18:11:28 +02:00
|
|
|
|
|
|
|
if err := audioContext.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-01-25 16:09:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Piano (Ebiten Demo)"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|