2018-07-03 14:09:12 +02:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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
|
2018-07-04 19:27:23 +02:00
|
|
|
// +build !wasm
|
2018-07-03 14:09:12 +02:00
|
|
|
|
|
|
|
package vorbis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/vorbis/internal/stb"
|
2018-07-03 14:09:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type decoderImpl struct {
|
2018-10-05 16:59:30 +02:00
|
|
|
samples *stb.Samples
|
2018-07-03 14:09:12 +02:00
|
|
|
channels int
|
|
|
|
sampleRate int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoderImpl) Read(buf []float32) (int, error) {
|
2018-10-05 16:59:30 +02:00
|
|
|
return d.samples.Read(buf)
|
2018-07-03 14:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoderImpl) Length() int64 {
|
2018-10-05 16:59:30 +02:00
|
|
|
return d.samples.Length()
|
2018-07-03 14:09:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoderImpl) Channels() int {
|
|
|
|
return d.channels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoderImpl) SampleRate() int {
|
|
|
|
return d.sampleRate
|
|
|
|
}
|
|
|
|
|
2019-01-14 17:07:21 +01:00
|
|
|
func (d *decoderImpl) SetPosition(pos int64) error {
|
|
|
|
return d.samples.SetPosition(pos)
|
|
|
|
}
|
|
|
|
|
2018-07-03 14:09:12 +02:00
|
|
|
func newDecoder(in audio.ReadSeekCloser) (decoder, error) {
|
|
|
|
buf, err := ioutil.ReadAll(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-05 16:59:30 +02:00
|
|
|
samples, channels, sampleRate, err := stb.DecodeVorbis(buf)
|
2018-07-03 14:09:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-05 16:59:30 +02:00
|
|
|
return &decoderImpl{samples, channels, sampleRate}, nil
|
2018-07-03 14:09:12 +02:00
|
|
|
}
|