audio: use int constants (C style enums) instead of string.
Change-Id: Ifb6db04c4bbf6c66e5a63dcaeee704ab02e711b0 Reviewed-on: https://go-review.googlesource.com/6423 Reviewed-by: Andrew Gerrand <adg@golang.org> Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
parent
388f1b8302
commit
fc5f7c74c8
@ -18,33 +18,60 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Format represents an PCM data format.
|
// Format represents an PCM data format.
|
||||||
type Format string
|
type Format int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Mono8 = Format("mono8")
|
Mono8 Format = iota
|
||||||
Mono16 = Format("mono16")
|
Mono16
|
||||||
Stereo8 = Format("stereo8")
|
Stereo8
|
||||||
Stereo16 = Format("stereo16")
|
Stereo16
|
||||||
)
|
)
|
||||||
|
|
||||||
var formatToCode = map[Format]int32{
|
func (f Format) String() string { return formatStrings[f] }
|
||||||
|
|
||||||
|
// formatBytes is the product of bytes per sample and number of channels.
|
||||||
|
var formatBytes = [...]int64{
|
||||||
|
Mono8: 1,
|
||||||
|
Mono16: 2,
|
||||||
|
Stereo8: 2,
|
||||||
|
Stereo16: 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var formatCodes = [...]uint32{
|
||||||
Mono8: al.FormatMono8,
|
Mono8: al.FormatMono8,
|
||||||
Mono16: al.FormatMono16,
|
Mono16: al.FormatMono16,
|
||||||
Stereo8: al.FormatStereo8,
|
Stereo8: al.FormatStereo8,
|
||||||
Stereo16: al.FormatStereo16,
|
Stereo16: al.FormatStereo16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var formatStrings = [...]string{
|
||||||
|
Mono8: "mono8",
|
||||||
|
Mono16: "mono16",
|
||||||
|
Stereo8: "stereo8",
|
||||||
|
Stereo16: "stereo16",
|
||||||
|
}
|
||||||
|
|
||||||
// State indicates the current playing state of the player.
|
// State indicates the current playing state of the player.
|
||||||
type State string
|
type State int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Unknown = State("unknown")
|
Unknown State = iota
|
||||||
Initial = State("initial")
|
Initial
|
||||||
Playing = State("playing")
|
Playing
|
||||||
Paused = State("paused")
|
Paused
|
||||||
Stopped = State("stopped")
|
Stopped
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (s State) String() string { return stateStrings[s] }
|
||||||
|
|
||||||
|
var stateStrings = [...]string{
|
||||||
|
Unknown: "unknown",
|
||||||
|
Initial: "initial",
|
||||||
|
Playing: "playing",
|
||||||
|
Paused: "paused",
|
||||||
|
Stopped: "stopped",
|
||||||
|
}
|
||||||
|
|
||||||
var codeToState = map[int32]State{
|
var codeToState = map[int32]State{
|
||||||
0: Unknown,
|
0: Unknown,
|
||||||
al.Initial: Initial,
|
al.Initial: Initial,
|
||||||
@ -121,7 +148,7 @@ func (p *Player) prepare(offset int64, force bool) error {
|
|||||||
if n > 0 {
|
if n > 0 {
|
||||||
size += int64(n)
|
size += int64(n)
|
||||||
b := al.GenBuffers(1)
|
b := al.GenBuffers(1)
|
||||||
b[0].BufferData(uint32(formatToCode[p.t.format]), buf[:n], int32(p.t.samplesPerSecond))
|
b[0].BufferData(formatCodes[p.t.format], buf[:n], int32(p.t.samplesPerSecond))
|
||||||
bufs = append(bufs, b[0])
|
bufs = append(bufs, b[0])
|
||||||
}
|
}
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
@ -259,26 +286,11 @@ func (p *Player) Destroy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func byteOffsetToDur(t *track, offset int64) time.Duration {
|
func byteOffsetToDur(t *track, offset int64) time.Duration {
|
||||||
samples := offset
|
return time.Duration(offset * formatBytes[t.format] * int64(time.Second) / t.samplesPerSecond)
|
||||||
if t.format == Mono16 || t.format == Stereo16 {
|
|
||||||
samples /= 2
|
|
||||||
}
|
|
||||||
if t.format == Stereo8 || t.format == Stereo16 {
|
|
||||||
samples /= 2
|
|
||||||
}
|
|
||||||
return time.Duration(samples * int64(time.Second) / t.samplesPerSecond)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func durToByteOffset(t *track, dur time.Duration) int64 {
|
func durToByteOffset(t *track, dur time.Duration) int64 {
|
||||||
size := t.samplesPerSecond * int64(dur) / int64(time.Second)
|
return int64(dur) * t.samplesPerSecond / (formatBytes[t.format] * int64(time.Second))
|
||||||
if t.format == Mono16 || t.format == Stereo16 {
|
|
||||||
// Each sample is represented by 16-bits. Move twice further.
|
|
||||||
size *= 2
|
|
||||||
}
|
|
||||||
if t.format == Stereo8 || t.format == Stereo16 {
|
|
||||||
size *= 2
|
|
||||||
}
|
|
||||||
return size
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// lastErr returns the last error or nil if the last operation
|
// lastErr returns the last error or nil if the last operation
|
||||||
|
Loading…
x
Reference in New Issue
Block a user