• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Go で書き直した Ikemen


Commit MetaInfo

修订版7bd84dc38b5304f074bea4ad7819601cfb52df0e (tree)
时间2019-01-16 23:40:16
作者neatunsou <sisiy4excite@gmai...>
Commiterneatunsou

Log Message

mp3にとりあえず対応

更改概述

差异

--- a/get.sh
+++ b/get.sh
@@ -5,3 +5,6 @@ go get -u github.com/go-gl/glfw/v3.2/glfw
55 go get -u github.com/go-gl/gl/v2.1/gl
66 go get -u github.com/jfreymuth/go-vorbis/ogg/vorbis
77 go get -u github.com/timshannon/go-openal/openal
8+go get -u github.com/faiface/beep
9+go get -u github.com/hajimehoshi/oto
10+go get -u github.com/hajimehoshi/go-mp3
--- a/src/sound.go
+++ b/src/sound.go
@@ -3,11 +3,18 @@ package main
33 import (
44 "encoding/binary"
55 "fmt"
6- "github.com/jfreymuth/go-vorbis/ogg/vorbis"
7- "github.com/timshannon/go-openal/openal"
86 "io"
97 "math"
108 "os"
9+ "path/filepath"
10+ "time"
11+
12+ "github.com/jfreymuth/go-vorbis/ogg/vorbis"
13+ "github.com/timshannon/go-openal/openal"
14+
15+ "github.com/faiface/beep"
16+ "github.com/faiface/beep/mp3"
17+ "github.com/faiface/beep/speaker"
1118 )
1219
1320 const (
@@ -15,6 +22,11 @@ const (
1522 audioFrequency = 48000
1623 )
1724
25+// ------------------------------------------------------------------
26+// Audio Source
27+
28+// AudioSource structure.
29+// It contains OpenAl's sound destination and buffer
1830 type AudioSource struct {
1931 Src openal.Source
2032 bufs openal.Buffers
@@ -40,6 +52,9 @@ func (s *AudioSource) Delete() {
4052 s.Src.Delete()
4153 }
4254
55+// ------------------------------------------------------------------
56+// Mixer
57+
4358 type Mixer struct {
4459 buf [audioOutLen * 2]float32
4560 sendBuf []int16
@@ -150,6 +165,9 @@ func (m *Mixer) Mix(wav []byte, fidx float64, bytesPerSample, channels int,
150165 return float64(len(wav))
151166 }
152167
168+// ------------------------------------------------------------------
169+// Normalizer
170+
153171 type Normalizer struct {
154172 mul float64
155173 l, r *NormalizerLR
@@ -210,6 +228,9 @@ func (n *NormalizerLR) process(bai float64, sam *float32) float64 {
210228 return bai
211229 }
212230
231+// ------------------------------------------------------------------
232+// Vorbis
233+
213234 type Vorbis struct {
214235 dec *vorbis.Vorbis
215236 fp *os.File
@@ -222,9 +243,12 @@ type Vorbis struct {
222243 func newVorbis() *Vorbis {
223244 return &Vorbis{openReq: make(chan string, 1), normalizer: NewNormalizer()}
224245 }
246+
247+// Opens a file asynchronously
225248 func (v *Vorbis) Open(file string) {
226249 v.openReq <- file
227250 }
251+
228252 func (v *Vorbis) openFile(file string) bool {
229253 v.clear()
230254 var err error
@@ -233,6 +257,7 @@ func (v *Vorbis) openFile(file string) bool {
233257 }
234258 return v.restart()
235259 }
260+
236261 func (v *Vorbis) restart() bool {
237262 if v.fp == nil {
238263 return false
@@ -246,6 +271,7 @@ func (v *Vorbis) restart() bool {
246271 v.buf = nil
247272 return true
248273 }
274+
249275 func (v *Vorbis) clear() {
250276 if v.dec != nil {
251277 v.dec = nil
@@ -255,6 +281,7 @@ func (v *Vorbis) clear() {
255281 v.fp = nil
256282 }
257283 }
284+
258285 func (v *Vorbis) samToAudioOut(buf [][]float32) (out []int16) {
259286 var o1i int
260287 if len(buf) == 1 {
@@ -276,6 +303,7 @@ func (v *Vorbis) samToAudioOut(buf [][]float32) (out []int16) {
276303 v.bufi -= float64(int(v.bufi))
277304 return
278305 }
306+
279307 func (v *Vorbis) read() []int16 {
280308 select {
281309 case file := <-v.openReq:
@@ -302,6 +330,67 @@ func (v *Vorbis) read() []int16 {
302330 return sys.nullSndBuf[:]
303331 }
304332
333+// ------------------------------------------------------------------
334+// Bgm
335+
336+type Bgm struct {
337+ filename string
338+ vorbis *Vorbis
339+ ctrlmp3 *beep.Ctrl
340+}
341+
342+func newBgm() *Bgm {
343+ return &Bgm{
344+ vorbis: newVorbis(),
345+ }
346+}
347+
348+func (bgm *Bgm) IsVorbis() bool {
349+ return filepath.Ext(bgm.filename) == ".ogg"
350+}
351+
352+func (bgm *Bgm) IsMp3() bool {
353+ return filepath.Ext(bgm.filename) == ".mp3"
354+}
355+
356+func (bgm *Bgm) Open(filename string) {
357+ if filepath.Base(bgm.filename) != filepath.Base(filename) {
358+ bgm.filename = filename
359+ speaker.Clear()
360+
361+ if bgm.IsVorbis() {
362+ bgm.vorbis.Open(filename)
363+ } else if bgm.IsMp3() {
364+ bgm.ReadMp3()
365+ }
366+ }
367+}
368+
369+func (bgm *Bgm) ReadMp3() {
370+ f, _ := os.Open(bgm.filename)
371+ s, format, err := mp3.Decode(f)
372+ if err != nil {
373+ return
374+ }
375+ streamer := beep.Loop(-1, s)
376+ bgm.ctrlmp3 = &beep.Ctrl{Streamer: streamer}
377+ speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/20))
378+ speaker.Play(bgm.ctrlmp3)
379+ return
380+}
381+func (bgm *Bgm) Mp3Paused() {
382+ speaker.Lock()
383+ bgm.ctrlmp3.Paused = true
384+ speaker.Unlock()
385+ return
386+}
387+func (bgm *Bgm) ReadVorbis() []int16 {
388+ return bgm.vorbis.read()
389+}
390+
391+// ------------------------------------------------------------------
392+// Wave
393+
305394 type Wave struct {
306395 SamplesPerSec uint32
307396 Channels uint16
@@ -400,12 +489,18 @@ func ReadWave(f *os.File, ofs int64) (*Wave, error) {
400489 return &w, nil
401490 }
402491
492+// ------------------------------------------------------------------
493+// Snd
494+
403495 type Snd struct {
404496 table map[[2]int32]*Wave
405497 ver, ver2 uint16
406498 }
407499
408-func newSnd() *Snd { return &Snd{table: make(map[[2]int32]*Wave)} }
500+func newSnd() *Snd {
501+ return &Snd{table: make(map[[2]int32]*Wave)}
502+}
503+
409504 func LoadSnd(filename string) (*Snd, error) {
410505 s := newSnd()
411506 f, err := os.Open(filename)
@@ -478,6 +573,9 @@ func (s *Snd) play(gn [2]int32) bool {
478573 return c.sound != nil
479574 }
480575
576+// ------------------------------------------------------------------
577+// Sound
578+
481579 type Sound struct {
482580 sound *Wave
483581 volume int16
--- a/src/system.go
+++ b/src/system.go
@@ -35,7 +35,7 @@ var sys = System{
3535 lifeMul: 1, team1VS2Life: 1,
3636 turnsRecoveryRate: 1.0 / 300,
3737 mixer: *newMixer(),
38- bgm: *newVorbis(),
38+ bgm: *newBgm(),
3939 sounds: newSounds(16),
4040 allPalFX: *newPalFX(),
4141 bgPalFX: *newPalFX(),
@@ -87,7 +87,7 @@ type System struct {
8787 debugScript string
8888 debugDraw bool
8989 mixer Mixer
90- bgm Vorbis
90+ bgm Bgm
9191 audioContext *openal.Context
9292 nullSndBuf [audioOutLen * 2]int16
9393 sounds Sounds
@@ -392,7 +392,15 @@ func (s *System) soundWrite() {
392392 if bgmSrc.Src.BuffersProcessed() > 0 {
393393 out := s.nullSndBuf[:]
394394 if !s.nomusic {
395- out = s.bgm.read()
395+ if s.bgm.IsVorbis() {
396+ out = s.bgm.ReadVorbis()
397+ } else if s.bgm.IsMp3() && s.bgm.ctrlmp3 != nil {
398+ s.bgm.ctrlmp3.Paused = false
399+ }
400+ } else {
401+ if s.bgm.IsMp3() && s.bgm.ctrlmp3 != nil {
402+ s.bgm.Mp3Paused()
403+ }
396404 }
397405 buf := bgmSrc.Src.UnqueueBuffer()
398406 buf.SetDataInt16(openal.FormatStereo16, out, audioFrequency)