decode.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2017 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package mp3 provides MP3 decoder.
  15. //
  16. // On desktops and mobiles, a pure Go decoder is used.
  17. // On browsers, a native decoder on the browser is used.
  18. package mp3
  19. import (
  20. "io"
  21. "github.com/hajimehoshi/go-mp3"
  22. "github.com/hajimehoshi/ebiten/v2/audio"
  23. "github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
  24. )
  25. // Stream is a decoded stream.
  26. type Stream struct {
  27. orig *mp3.Decoder
  28. resampling *convert.Resampling
  29. }
  30. // Read is implementation of io.Reader's Read.
  31. func (s *Stream) Read(buf []byte) (int, error) {
  32. if s.resampling != nil {
  33. return s.resampling.Read(buf)
  34. }
  35. return s.orig.Read(buf)
  36. }
  37. // Seek is implementation of io.Seeker's Seek.
  38. func (s *Stream) Seek(offset int64, whence int) (int64, error) {
  39. if s.resampling != nil {
  40. return s.resampling.Seek(offset, whence)
  41. }
  42. return s.orig.Seek(offset, whence)
  43. }
  44. // Length returns the size of decoded stream in bytes.
  45. func (s *Stream) Length() int64 {
  46. if s.resampling != nil {
  47. return s.resampling.Length()
  48. }
  49. return s.orig.Length()
  50. }
  51. // DecodeWithSampleRate decodes MP3 source and returns a decoded stream.
  52. //
  53. // DecodeWithSampleRate returns error when decoding fails or IO error happens.
  54. //
  55. // DecodeWithSampleRate automatically resamples the stream to fit with sampleRate if necessary.
  56. //
  57. // The returned Stream's Seek is available only when src is an io.Seeker.
  58. //
  59. // A Stream doesn't close src even if src implements io.Closer.
  60. // Closing the source is src owner's responsibility.
  61. func DecodeWithSampleRate(sampleRate int, src io.Reader) (*Stream, error) {
  62. d, err := mp3.NewDecoder(src)
  63. if err != nil {
  64. return nil, err
  65. }
  66. var r *convert.Resampling
  67. if d.SampleRate() != sampleRate {
  68. r = convert.NewResampling(d, d.Length(), d.SampleRate(), sampleRate)
  69. }
  70. s := &Stream{
  71. orig: d,
  72. resampling: r,
  73. }
  74. return s, nil
  75. }
  76. // Decode decodes MP3 source and returns a decoded stream.
  77. //
  78. // Decode returns error when decoding fails or IO error happens.
  79. //
  80. // Decode automatically resamples the stream to fit with the audio context if necessary.
  81. //
  82. // The returned Stream's Seek is available only when src is an io.Seeker.
  83. //
  84. // A Stream doesn't close src even if src implements io.Closer.
  85. // Closing the source is src owner's responsibility.
  86. //
  87. // Deprecated: as of v2.1. Use DecodeWithSampleRate instead.
  88. func Decode(context *audio.Context, src io.Reader) (*Stream, error) {
  89. return DecodeWithSampleRate(context.SampleRate(), src)
  90. }