-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_formats.go
70 lines (59 loc) · 2.26 KB
/
utils_formats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package edau
import "os"
import "io"
import "fmt"
import "strings"
import "errors"
import "github.com/hajimehoshi/ebiten/v2/audio"
import "github.com/hajimehoshi/ebiten/v2/audio/wav"
import "github.com/hajimehoshi/ebiten/v2/audio/mp3"
import "github.com/hajimehoshi/ebiten/v2/audio/vorbis"
// A common interface that all Ebitengine audio streams conform to.
type StdAudioStream interface {
io.ReadSeeker
Length() int64
}
// Returned by functions that require Ebitengine's audio.NewContext to have
// already been created, typically so the sample rate can be directly obtained
// from it with audio.CurrentContext().SampleRate().
var ErrAudioContextUninitialized = errors.New("Ebitengine's audio context not initialized")
// Loads an .ogg, .mp3 or .wav file as a [StdAudioStream]. Additionally,
// the returned interface also implements [io.Closer], which can be used
// to close the file associated to the stream, e.g.:
// err := audioStream.(io.Closer).Close()
// The sample rate used is taken from Ebitengine's audio.CurrentContext().
// If no audio context has been initialized, [ErrAudioContextUninitialized]
// will be returned.
func LoadAudioFileAsStream(filename string) (StdAudioStream, error) {
ctx := audio.CurrentContext()
if ctx == nil { return nil, ErrAudioContextUninitialized }
file, err := os.Open(filename)
if err != nil { return nil, err }
var stream StdAudioStream
if strings.HasSuffix(filename, ".wav") {
stream, err = wav.DecodeWithSampleRate(ctx.SampleRate(), file)
} else if strings.HasSuffix(filename, ".ogg") {
stream, err = vorbis.DecodeWithSampleRate(ctx.SampleRate(), file)
} else if strings.HasSuffix(filename, ".mp3") {
stream, err = mp3.DecodeWithSampleRate(ctx.SampleRate(), file)
} else {
return nil, fmt.Errorf("unexpected audio format for '%s'", filename)
}
return &streamWithClose{ stream, file }, err
}
type streamWithClose struct {
stream StdAudioStream
file *os.File
}
func (self *streamWithClose) Read(buffer []byte) (int, error) {
return self.stream.Read(buffer)
}
func (self *streamWithClose) Seek(offset int64, whence int) (int64, error) {
return self.stream.Seek(offset, whence)
}
func (self *streamWithClose) Length() int64 {
return self.stream.Length()
}
func (self *streamWithClose) Close() error {
return self.file.Close()
}