mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
audio/mp3: Start implementing
This commit is contained in:
parent
d8f425962c
commit
bf3f6c9ff6
60
audio/mp3/decode.go
Normal file
60
audio/mp3/decode.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
// Copyright 2017 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.
|
||||||
|
|
||||||
|
// Package mp3 provides MP3 decoder.
|
||||||
|
//
|
||||||
|
// This package is very experimental.
|
||||||
|
package mp3
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/audio"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Stream struct {
|
||||||
|
inner *bytes.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read is implementation of io.Reader's Read.
|
||||||
|
func (s *Stream) Read(p []byte) (int, error) {
|
||||||
|
return s.inner.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seek is implementation of io.Seeker's Seek.
|
||||||
|
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
|
||||||
|
return s.inner.Seek(offset, whence)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read is implementation of io.Closer's Close.
|
||||||
|
func (s *Stream) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size returns the size of decoded stream in bytes.
|
||||||
|
func (s *Stream) Size() int64 {
|
||||||
|
return int64(s.inner.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if err := decode(src, &buf); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s := &Stream{
|
||||||
|
inner: bytes.NewReader(buf.Bytes()),
|
||||||
|
}
|
||||||
|
// TODO: Resampling
|
||||||
|
return s, nil
|
||||||
|
}
|
108
audio/mp3/decode_notjs.go
Normal file
108
audio/mp3/decode_notjs.go
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
// Copyright 2017 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
|
||||||
|
|
||||||
|
package mp3
|
||||||
|
|
||||||
|
// #include "pdmp3.h"
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
eof = 0xffffffff
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
reader io.Reader
|
||||||
|
readerCache []uint8
|
||||||
|
readerPos int
|
||||||
|
readerEOF bool
|
||||||
|
writer io.Writer
|
||||||
|
)
|
||||||
|
|
||||||
|
//export Get_Byte
|
||||||
|
func Get_Byte() C.unsigned {
|
||||||
|
for len(readerCache) == 0 && !readerEOF {
|
||||||
|
buf := make([]uint8, 4096)
|
||||||
|
n, err := reader.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
readerEOF = true
|
||||||
|
} else {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
readerCache = buf[:n]
|
||||||
|
}
|
||||||
|
if readerEOF {
|
||||||
|
return eof
|
||||||
|
}
|
||||||
|
b := readerCache[0]
|
||||||
|
readerCache = readerCache[1:]
|
||||||
|
readerPos++
|
||||||
|
return C.unsigned(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
//export Get_Bytes
|
||||||
|
func Get_Bytes(num C.unsigned, data *C.unsigned) C.unsigned {
|
||||||
|
s := C.unsigned(0)
|
||||||
|
for i := 0; i < int(num); i++ {
|
||||||
|
v := Get_Byte()
|
||||||
|
if v == eof {
|
||||||
|
return eof
|
||||||
|
}
|
||||||
|
*(*C.unsigned)(unsafe.Pointer(uintptr(unsafe.Pointer(data)) + uintptr(i)*unsafe.Sizeof(s))) = v
|
||||||
|
}
|
||||||
|
return C.OK
|
||||||
|
}
|
||||||
|
|
||||||
|
//export Get_Filepos
|
||||||
|
func Get_Filepos() C.unsigned {
|
||||||
|
if len(readerCache) == 0 && readerEOF {
|
||||||
|
return eof
|
||||||
|
}
|
||||||
|
return C.unsigned(readerPos)
|
||||||
|
}
|
||||||
|
|
||||||
|
//export writeToWriter
|
||||||
|
func writeToWriter(data unsafe.Pointer, size C.int) C.size_t {
|
||||||
|
buf := C.GoBytes(data, size)
|
||||||
|
n, err := writer.Write(buf)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return C.size_t(n)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decode(r io.Reader, w io.Writer) error {
|
||||||
|
reader = r
|
||||||
|
writer = w
|
||||||
|
for Get_Filepos() != eof {
|
||||||
|
if C.Read_Frame() == C.OK {
|
||||||
|
C.Decode_L3()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if Get_Filepos() == eof {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return errors.New("mp3: not enough maindata to decode frame")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
2156
audio/mp3/pdmp3.c
Normal file
2156
audio/mp3/pdmp3.c
Normal file
File diff suppressed because it is too large
Load Diff
23
audio/mp3/pdmp3.h
Normal file
23
audio/mp3/pdmp3.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#ifndef PDMP3_H
|
||||||
|
#define PDMP3_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
//#define DEBUG
|
||||||
|
#define IMDCT_TABLES
|
||||||
|
#define IMDCT_NTABLES
|
||||||
|
#define POW34_ITERATE
|
||||||
|
#define POW34_TABLE
|
||||||
|
#define OUTPUT_RAW
|
||||||
|
|
||||||
|
#define OK 0
|
||||||
|
#define ERROR -1
|
||||||
|
|
||||||
|
unsigned Get_Byte(void);
|
||||||
|
unsigned Get_Bytes(unsigned num, unsigned* data_vec);
|
||||||
|
unsigned Get_Filepos(void);
|
||||||
|
int Decode_L3(void);
|
||||||
|
int Read_Frame(void);
|
||||||
|
size_t writeToWriter(void* data, int size);
|
||||||
|
|
||||||
|
#endif
|
BIN
examples/_resources/audio/game2.mp3
Normal file
BIN
examples/_resources/audio/game2.mp3
Normal file
Binary file not shown.
@ -17,6 +17,14 @@ http://mart.kitunebi.com/music_act.html
|
|||||||
Harpie's Feather (ハルピュイアの羽) by Napi
|
Harpie's Feather (ハルピュイアの羽) by Napi
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## game2.mp3
|
||||||
|
|
||||||
|
```
|
||||||
|
http://mart.kitunebi.com/music_act.html
|
||||||
|
|
||||||
|
ノアの羽舟 by Napi
|
||||||
|
```
|
||||||
|
|
||||||
## ragtime.ogg
|
## ragtime.ogg
|
||||||
|
|
||||||
```
|
```
|
||||||
|
BIN
examples/_resources/audio/pd.mp3
Normal file
BIN
examples/_resources/audio/pd.mp3
Normal file
Binary file not shown.
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"github.com/hajimehoshi/ebiten/audio"
|
"github.com/hajimehoshi/ebiten/audio"
|
||||||
"github.com/hajimehoshi/ebiten/audio/vorbis"
|
"github.com/hajimehoshi/ebiten/audio/mp3"
|
||||||
"github.com/hajimehoshi/ebiten/audio/wav"
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
||||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||||
)
|
)
|
||||||
@ -36,7 +36,7 @@ const (
|
|||||||
|
|
||||||
// This sample rate doesn't match with wav/ogg's sample rate,
|
// This sample rate doesn't match with wav/ogg's sample rate,
|
||||||
// but decoders adjust them.
|
// but decoders adjust them.
|
||||||
sampleRate = 48000
|
sampleRate = 44100
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -113,11 +113,11 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg")
|
mp3F, err := ebitenutil.OpenFile("_resources/audio/game2.mp3")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s, err := vorbis.Decode(audioContext, oggF)
|
s, err := mp3.Decode(audioContext, mp3F)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -136,6 +136,9 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|||||||
volume128: 128,
|
volume128: 128,
|
||||||
seCh: make(chan []uint8),
|
seCh: make(chan []uint8),
|
||||||
}
|
}
|
||||||
|
if player.total == 0 {
|
||||||
|
player.total = 1
|
||||||
|
}
|
||||||
player.audioPlayer.Play()
|
player.audioPlayer.Play()
|
||||||
go func() {
|
go func() {
|
||||||
s, err := wav.Decode(audioContext, wavF)
|
s, err := wav.Decode(audioContext, wavF)
|
||||||
|
Loading…
Reference in New Issue
Block a user