Add examples/audio and OggStream struct

This commit is contained in:
Hajime Hoshi 2016-03-08 04:20:59 +09:00
parent 59c2fcd961
commit b964df4f0f
5 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,11 @@
# License
## ragtime.ogg
```
Title: Frog Legs Rag (1906, piano roll)
Artist: James Scott
Album: Frog Legs: Ragtime Era Favorites
Attribution-NonCommercial-ShareAlike: http://creativecommons.org/licenses/by-nc-sa/3.0/
```

Binary file not shown.

58
examples/audio/main.go Normal file
View File

@ -0,0 +1,58 @@
// Copyright 2016 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 (
"fmt"
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/exp/audio"
)
const (
screenWidth = 320
screenHeight = 240
)
var audioContext *audio.Context
func update(screen *ebiten.Image) error {
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
return nil
}
func main() {
// Use a FLAC file so far: I couldn't find any good OGG/Vorbis decoder in pure Go.
f, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg")
if err != nil {
log.Fatal(err)
}
// TODO: sampleRate should be obtained from the ogg file.
audioContext = audio.NewContext(22050)
s, err := audioContext.NewOggStream(f)
if err != nil {
log.Fatal(err)
}
p, err := audioContext.NewPlayer(s)
if err != nil {
log.Fatal(err)
}
p.Play()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "PCM (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

62
exp/audio/ogg.go Normal file
View File

@ -0,0 +1,62 @@
// Copyright 2016 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.
// +build !js
package audio
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"github.com/hajimehoshi/go-vorbis"
)
type OggStream struct {
buf *bytes.Reader
}
func (c *Context) NewOggStream(src io.Reader) (*OggStream, error) {
decoded, channels, sampleRate, err := vorbis.Decode(src)
if err != nil {
return nil, err
}
if channels != 2 {
return nil, errors.New("audio: number of channels must be 2")
}
if sampleRate != c.sampleRate {
return nil, fmt.Errorf("audio: sample rate must be %d but %d", c.sampleRate, sampleRate)
}
// TODO: Read all data once so that Seek can be implemented easily.
// We should look for a wiser way.
b, err := ioutil.ReadAll(decoded)
if err != nil {
return nil, err
}
s := &OggStream{
buf: bytes.NewReader(b),
}
return s, nil
}
func (s *OggStream) Read(p []byte) (int, error) {
return s.buf.Read(p)
}
func (s *OggStream) Seek(offset int64, whence int) (int64, error) {
return s.buf.Seek(offset, whence)
}

69
exp/audio/ogg_js.go Normal file
View File

@ -0,0 +1,69 @@
// Copyright 2016 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.
// +build js
package audio
import (
"bytes"
"io"
"io/ioutil"
"github.com/gopherjs/gopherjs/js"
)
type OggStream struct {
buf *bytes.Reader
}
// TODO: This just uses decodeAudioData can treat audio files other than Ogg/Vorbis.
// TODO: This doesn't work on iOS which doesn't have Ogg/Vorbis decoder.
func (c *Context) NewOggStream(src io.Reader) (*OggStream, error) {
b, err := ioutil.ReadAll(src)
if err != nil {
return nil, err
}
s := &OggStream{}
ch := make(chan struct{})
// TODO: 1 is a correct second argument?
oc := js.Global.Get("OfflineAudioContext").New(2, 1, c.sampleRate)
oc.Call("decodeAudioData", js.NewArrayBuffer(b), func(buf *js.Obbmaiject) {
defer close(ch)
il := buf.Call("getChannelData", 0).Interface().([]float32)
ir := buf.Call("getChannelData", 1).Interface().([]float32)
b := make([]byte, len(il)*4)
for i := 0; i < len(il); i++ {
l := int16(il[i] * (1 << 15))
r := int16(ir[i] * (1 << 15))
b[4*i] = uint8(l)
b[4*i+1] = uint8(l >> 8)
b[4*i+2] = uint8(r)
b[4*i+3] = uint8(r >> 8)
}
s.buf = bytes.NewReader(b)
})
<-ch
return s, nil
}
func (s *OggStream) Read(p []byte) (int, error) {
return s.buf.Read(p)
}
func (s *OggStream) Seek(offset int64, whence int) (int64, error) {
return s.buf.Seek(offset, whence)
}