1
0

audio.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // Copyright 2015 Hajime Hoshi
  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 audio provides audio players.
  15. //
  16. // The stream format must be 16-bit little endian or 32-bit float little endian, and 2 channels. The format is as follows:
  17. //
  18. // [data] = [sample 1] [sample 2] [sample 3] ...
  19. // [sample *] = [channel 1] ...
  20. // [channel *] = [byte 1] [byte 2] ...
  21. //
  22. // An audio context (audio.Context object) has a sample rate you can specify
  23. // and all streams you want to play must have the same sample rate.
  24. //
  25. // An audio context can generate 'players' (audio.Player objects),
  26. // and you can play sound by calling Play function of players.
  27. // When multiple players play, mixing is automatically done.
  28. // Note that too many players may cause distortion.
  29. //
  30. // For the simplest example to play sound, see wav package in the examples.
  31. package audio
  32. import (
  33. "bytes"
  34. "errors"
  35. "fmt"
  36. "io"
  37. "reflect"
  38. "runtime"
  39. "sync"
  40. "time"
  41. "github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
  42. "github.com/hajimehoshi/ebiten/v2/internal/hook"
  43. )
  44. const (
  45. channelCount = 2
  46. bitDepthInBytesInt16 = 2
  47. bitDepthInBytesFloat32 = 4
  48. )
  49. // A Context represents a current state of audio.
  50. //
  51. // At most one Context object can exist in one process.
  52. // This means only one constant sample rate is valid in your one application.
  53. //
  54. // For a typical usage example, see examples/wav/main.go.
  55. type Context struct {
  56. playerFactory *playerFactory
  57. sampleRate int
  58. err error
  59. ready bool
  60. playingPlayers map[*playerImpl]struct{}
  61. m sync.Mutex
  62. semaphore chan struct{}
  63. }
  64. var (
  65. theContext *Context
  66. theContextLock sync.Mutex
  67. )
  68. // NewContext creates a new audio context with the given sample rate.
  69. //
  70. // sampleRate specifies the number of samples that should be played during one second.
  71. // Usual numbers are 44100 or 48000. One context has only one sample rate. You cannot play multiple audio
  72. // sources with different sample rates at the same time.
  73. //
  74. // NewContext panics when an audio context is already created.
  75. func NewContext(sampleRate int) *Context {
  76. theContextLock.Lock()
  77. defer theContextLock.Unlock()
  78. if theContext != nil {
  79. panic("audio: context is already created")
  80. }
  81. c := &Context{
  82. sampleRate: sampleRate,
  83. playerFactory: newPlayerFactory(sampleRate),
  84. playingPlayers: map[*playerImpl]struct{}{},
  85. semaphore: make(chan struct{}, 1),
  86. }
  87. theContext = c
  88. h := getHook()
  89. h.OnSuspendAudio(func() error {
  90. c.semaphore <- struct{}{}
  91. if err := c.playerFactory.suspend(); err != nil {
  92. return err
  93. }
  94. if err := c.onSuspend(); err != nil {
  95. return err
  96. }
  97. return nil
  98. })
  99. h.OnResumeAudio(func() error {
  100. <-c.semaphore
  101. if err := c.playerFactory.resume(); err != nil {
  102. return err
  103. }
  104. if err := c.onResume(); err != nil {
  105. return err
  106. }
  107. return nil
  108. })
  109. h.AppendHookOnBeforeUpdate(func() error {
  110. var err error
  111. theContextLock.Lock()
  112. if theContext != nil {
  113. err = theContext.error()
  114. }
  115. theContextLock.Unlock()
  116. if err != nil {
  117. return err
  118. }
  119. // Initialize the context here in the case when there is no player and
  120. // the program waits for IsReady() to be true (#969, #970, #2715).
  121. ready, err := c.playerFactory.initContextIfNeeded()
  122. if err != nil {
  123. return err
  124. }
  125. if ready != nil {
  126. go func() {
  127. <-ready
  128. c.setReady()
  129. }()
  130. }
  131. return nil
  132. })
  133. // In the current Ebitengine implementation, update might not be called when the window is in background (#3154).
  134. // In this case, an audio player position is not updated correctly with AppendHookOnBeforeUpdate.
  135. // Use a distinct goroutine to update the player states.
  136. go func() {
  137. for {
  138. if err := c.updatePlayers(); err != nil {
  139. c.setError(err)
  140. return
  141. }
  142. time.Sleep(time.Second / 100)
  143. }
  144. }()
  145. return c
  146. }
  147. // CurrentContext returns the current context or nil if there is no context.
  148. func CurrentContext() *Context {
  149. theContextLock.Lock()
  150. c := theContext
  151. theContextLock.Unlock()
  152. return c
  153. }
  154. func (c *Context) setError(err error) {
  155. // TODO: What if c.err already exists?
  156. c.m.Lock()
  157. c.err = err
  158. c.m.Unlock()
  159. }
  160. func (c *Context) error() error {
  161. c.m.Lock()
  162. defer c.m.Unlock()
  163. if c.err != nil {
  164. return c.err
  165. }
  166. return c.playerFactory.error()
  167. }
  168. func (c *Context) setReady() {
  169. c.m.Lock()
  170. c.ready = true
  171. c.m.Unlock()
  172. }
  173. func (c *Context) addPlayingPlayer(p *playerImpl) {
  174. c.m.Lock()
  175. defer c.m.Unlock()
  176. c.playingPlayers[p] = struct{}{}
  177. if !reflect.ValueOf(p.sourceIdent()).Comparable() {
  178. return
  179. }
  180. // Check the source duplication
  181. srcs := map[any]struct{}{}
  182. for p := range c.playingPlayers {
  183. if _, ok := srcs[p.sourceIdent()]; ok {
  184. c.err = errors.New("audio: the same source must not be used by multiple Player objects")
  185. return
  186. }
  187. srcs[p.sourceIdent()] = struct{}{}
  188. }
  189. }
  190. func (c *Context) removePlayingPlayer(p *playerImpl) {
  191. c.m.Lock()
  192. delete(c.playingPlayers, p)
  193. c.m.Unlock()
  194. }
  195. func (c *Context) onSuspend() error {
  196. // A Context must not call playerImpl's functions with a lock, or this causes a deadlock (#2737).
  197. // Copy the playerImpls and iterate them without a lock.
  198. var players []*playerImpl
  199. c.m.Lock()
  200. players = make([]*playerImpl, 0, len(c.playingPlayers))
  201. for p := range c.playingPlayers {
  202. players = append(players, p)
  203. }
  204. c.m.Unlock()
  205. for _, p := range players {
  206. if err := p.Err(); err != nil {
  207. return err
  208. }
  209. p.onContextSuspended()
  210. }
  211. return nil
  212. }
  213. func (c *Context) onResume() error {
  214. // A Context must not call playerImpl's functions with a lock, or this causes a deadlock (#2737).
  215. // Copy the playerImpls and iterate them without a lock.
  216. var players []*playerImpl
  217. c.m.Lock()
  218. players = make([]*playerImpl, 0, len(c.playingPlayers))
  219. for p := range c.playingPlayers {
  220. players = append(players, p)
  221. }
  222. c.m.Unlock()
  223. for _, p := range players {
  224. if err := p.Err(); err != nil {
  225. return err
  226. }
  227. p.onContextResumed()
  228. }
  229. return nil
  230. }
  231. func (c *Context) updatePlayers() error {
  232. // A Context must not call playerImpl's functions with a lock, or this causes a deadlock (#2737).
  233. // Copy the playerImpls and iterate them without a lock.
  234. var players []*playerImpl
  235. c.m.Lock()
  236. players = make([]*playerImpl, 0, len(c.playingPlayers))
  237. for p := range c.playingPlayers {
  238. players = append(players, p)
  239. }
  240. c.m.Unlock()
  241. var playersToRemove []*playerImpl
  242. // Now reader players cannot call removePlayers from themselves in the current implementation.
  243. // Underlying playering can be the pause state after fishing its playing,
  244. // but there is no way to notify this to players so far.
  245. // Instead, let's check the states proactively every frame.
  246. for _, p := range players {
  247. if err := p.Err(); err != nil {
  248. return err
  249. }
  250. p.updatePosition()
  251. if !p.IsPlaying() {
  252. playersToRemove = append(playersToRemove, p)
  253. }
  254. }
  255. c.m.Lock()
  256. for _, p := range playersToRemove {
  257. delete(c.playingPlayers, p)
  258. }
  259. c.m.Unlock()
  260. return nil
  261. }
  262. // IsReady returns a boolean value indicating whether the audio is ready or not.
  263. //
  264. // On some browsers, user interaction like click or pressing keys is required to start audio.
  265. func (c *Context) IsReady() bool {
  266. c.m.Lock()
  267. defer c.m.Unlock()
  268. return c.ready
  269. }
  270. // SampleRate returns the sample rate.
  271. func (c *Context) SampleRate() int {
  272. return c.sampleRate
  273. }
  274. // Player is an audio player which has one stream.
  275. //
  276. // Even when all references to a Player object is gone,
  277. // the object is not GCed until the player finishes playing.
  278. // This means that if a Player plays an infinite stream,
  279. // the object is never GCed unless Close is called.
  280. type Player struct {
  281. p *playerImpl
  282. }
  283. // NewPlayer creates a new player with the given stream.
  284. //
  285. // src's format must be linear PCM (signed 16bits little endian, 2 channel stereo)
  286. // without a header (e.g. RIFF header).
  287. // The sample rate must be same as that of the audio context.
  288. //
  289. // The player is seekable when src is io.Seeker.
  290. // Attempt to seek the player that is not io.Seeker causes panic.
  291. //
  292. // Note that the given src can't be shared with other Player objects.
  293. //
  294. // NewPlayer tries to call Seek of src to get the current position.
  295. // NewPlayer returns error when the Seek returns error.
  296. //
  297. // A Player doesn't close src even if src implements io.Closer.
  298. // Closing the source is src owner's responsibility.
  299. //
  300. // For new code, NewPlayerF32 is preferrable to NewPlayer, since Ebitengine will treat only 32bit float audio internally in the future.
  301. //
  302. // A Player for 16bit integer must be used with 16bit integer version of audio APIs, like vorbis.DecodeWithoutResampling or audio.NewInfiniteLoop, not or vorbis.DecodeF32 or audio.NewInfiniteLoopF32.
  303. func (c *Context) NewPlayer(src io.Reader) (*Player, error) {
  304. _, seekable := src.(io.Seeker)
  305. f32Src := convert.NewFloat32BytesReaderFromInt16BytesReader(src)
  306. pi, err := c.playerFactory.newPlayer(c, f32Src, seekable, src, bitDepthInBytesFloat32)
  307. if err != nil {
  308. return nil, err
  309. }
  310. p := &Player{pi}
  311. runtime.SetFinalizer(p, (*Player).finalize)
  312. return p, nil
  313. }
  314. // NewPlayerF32 creates a new player with the given stream.
  315. //
  316. // src's format must be linear PCM (32bit float, little endian, 2 channel stereo)
  317. // without a header (e.g. RIFF header).
  318. // The sample rate must be same as that of the audio context.
  319. //
  320. // The player is seekable when src is io.Seeker.
  321. // Attempt to seek the player that is not io.Seeker causes panic.
  322. //
  323. // Note that the given src can't be shared with other Player objects.
  324. //
  325. // NewPlayerF32 tries to call Seek of src to get the current position.
  326. // NewPlayerF32 returns error when the Seek returns error.
  327. //
  328. // A Player doesn't close src even if src implements io.Closer.
  329. // Closing the source is src owner's responsibility.
  330. //
  331. // For new code, NewPlayerF32 is preferrable to NewPlayer, since Ebitengine will treat only 32bit float audio internally in the future.
  332. //
  333. // A Player for 32bit float must be used with 32bit float version of audio APIs, like vorbis.DecodeF32 or audio.NewInfiniteLoopF32, not vorbis.DecodeWithoutResampling or audio.NewInfiniteLoop.
  334. func (c *Context) NewPlayerF32(src io.Reader) (*Player, error) {
  335. _, seekable := src.(io.Seeker)
  336. pi, err := c.playerFactory.newPlayer(c, src, seekable, src, bitDepthInBytesFloat32)
  337. if err != nil {
  338. return nil, err
  339. }
  340. p := &Player{pi}
  341. runtime.SetFinalizer(p, (*Player).finalize)
  342. return p, nil
  343. }
  344. // NewPlayer creates a new player with the given stream.
  345. //
  346. // Deprecated: as of v2.2. Use (*Context).NewPlayer instead.
  347. func NewPlayer(context *Context, src io.Reader) (*Player, error) {
  348. return context.NewPlayer(src)
  349. }
  350. // NewPlayerFromBytes creates a new player with the given bytes.
  351. //
  352. // As opposed to NewPlayer, you don't have to care if src is already used by another player or not.
  353. // src can be shared by multiple players.
  354. //
  355. // The format of src should be same as noted at NewPlayer.
  356. func (c *Context) NewPlayerFromBytes(src []byte) *Player {
  357. p, err := c.NewPlayer(bytes.NewReader(src))
  358. if err != nil {
  359. // Errors should never happen.
  360. panic(fmt.Sprintf("audio: %v at NewPlayerFromBytes", err))
  361. }
  362. return p
  363. }
  364. // NewPlayerF32FromBytes creates a new player with the given bytes.
  365. //
  366. // As opposed to NewPlayerF32, you don't have to care if src is already used by another player or not.
  367. // src can be shared by multiple players.
  368. //
  369. // The format of src should be same as noted at NewPlayerF32.
  370. func (c *Context) NewPlayerF32FromBytes(src []byte) *Player {
  371. p, err := c.NewPlayerF32(bytes.NewReader(src))
  372. if err != nil {
  373. // Errors should never happen.
  374. panic(fmt.Sprintf("audio: %v at NewPlayerFromBytesF32", err))
  375. }
  376. return p
  377. }
  378. // NewPlayerFromBytes creates a new player with the given bytes.
  379. //
  380. // Deprecated: as of v2.2. Use (*Context).NewPlayerFromBytes instead.
  381. func NewPlayerFromBytes(context *Context, src []byte) *Player {
  382. return context.NewPlayerFromBytes(src)
  383. }
  384. func (p *Player) finalize() {
  385. runtime.SetFinalizer(p, nil)
  386. if !p.IsPlaying() {
  387. _ = p.Close()
  388. }
  389. }
  390. // Close closes the stream.
  391. //
  392. // When Close is called, the stream owned by the player is NOT closed,
  393. // even if the stream implements io.Closer.
  394. //
  395. // Close returns error when the player is already closed.
  396. func (p *Player) Close() error {
  397. return p.p.Close()
  398. }
  399. // Play plays the stream.
  400. func (p *Player) Play() {
  401. p.p.Play()
  402. }
  403. // IsPlaying returns boolean indicating whether the player is playing.
  404. func (p *Player) IsPlaying() bool {
  405. return p.p.IsPlaying()
  406. }
  407. // Rewind rewinds the current position to the start.
  408. //
  409. // The passed source to NewPlayer must be io.Seeker, or Rewind panics.
  410. //
  411. // Rewind returns error when seeking the source stream returns error.
  412. func (p *Player) Rewind() error {
  413. return p.p.Rewind()
  414. }
  415. // SetPosition sets the position with the given offset.
  416. //
  417. // The passed source to NewPlayer must be io.Seeker, or SetPosition panics.
  418. //
  419. // SetPosition returns error when seeking the source stream returns an error.
  420. func (p *Player) SetPosition(offset time.Duration) error {
  421. return p.p.SetPosition(offset)
  422. }
  423. // Seek seeks the position with the given offset.
  424. //
  425. // Deprecated: as of v2.6. Use SetPosition instead.
  426. func (p *Player) Seek(offset time.Duration) error {
  427. return p.SetPosition(offset)
  428. }
  429. // Pause pauses the playing.
  430. func (p *Player) Pause() {
  431. p.p.Pause()
  432. }
  433. // Position returns the current position in time.
  434. //
  435. // As long as the player continues to play, Position's returning value is increased monotonically,
  436. // even though the source stream loops and its position goes back.
  437. func (p *Player) Position() time.Duration {
  438. return p.p.Position()
  439. }
  440. // Current returns the current position in time.
  441. //
  442. // Deprecated: as of v2.6. Use Position instead.
  443. func (p *Player) Current() time.Duration {
  444. return p.Position()
  445. }
  446. // Volume returns the current volume of this player [0-1].
  447. func (p *Player) Volume() float64 {
  448. return p.p.Volume()
  449. }
  450. // SetVolume sets the volume of this player.
  451. // volume must be in between 0 and 1. SetVolume panics otherwise.
  452. func (p *Player) SetVolume(volume float64) {
  453. p.p.SetVolume(volume)
  454. }
  455. // SetBufferSize adjusts the buffer size of the player.
  456. // If 0 is specified, the default buffer size is used.
  457. // A small buffer size is useful if you want to play a real-time PCM for example.
  458. // Note that the audio quality might be affected if you modify the buffer size.
  459. func (p *Player) SetBufferSize(bufferSize time.Duration) {
  460. p.p.SetBufferSize(bufferSize)
  461. }
  462. type hooker interface {
  463. OnSuspendAudio(f func() error)
  464. OnResumeAudio(f func() error)
  465. AppendHookOnBeforeUpdate(f func() error)
  466. }
  467. var hookerForTesting hooker
  468. func getHook() hooker {
  469. if hookerForTesting != nil {
  470. return hookerForTesting
  471. }
  472. return &hookerImpl{}
  473. }
  474. type hookerImpl struct{}
  475. func (h *hookerImpl) OnSuspendAudio(f func() error) {
  476. hook.OnSuspendAudio(f)
  477. }
  478. func (h *hookerImpl) OnResumeAudio(f func() error) {
  479. hook.OnResumeAudio(f)
  480. }
  481. func (h *hookerImpl) AppendHookOnBeforeUpdate(f func() error) {
  482. hook.AppendHookOnBeforeUpdate(f)
  483. }
  484. // Resample converts the sample rate of the given singed 16bit integer, little-endian, 2 channels (stereo) stream.
  485. // size is the length of the source stream in bytes.
  486. // from is the original sample rate.
  487. // to is the target sample rate.
  488. //
  489. // If the original sample rate equals to the new one, Resample returns source as it is.
  490. func Resample(source io.ReadSeeker, size int64, from, to int) io.ReadSeeker {
  491. if from == to {
  492. return source
  493. }
  494. return convert.NewResampling(source, size, from, to, bitDepthInBytesInt16)
  495. }
  496. // ResampleF32 converts the sample rate of the given 32bit float, little-endian, 2 channels (stereo) stream.
  497. // size is the length of the source stream in bytes.
  498. // from is the original sample rate.
  499. // to is the target sample rate.
  500. //
  501. // If the original sample rate equals to the new one, Resample returns source as it is.
  502. func ResampleF32(source io.ReadSeeker, size int64, from, to int) io.ReadSeeker {
  503. if from == to {
  504. return source
  505. }
  506. return convert.NewResampling(source, size, from, to, bitDepthInBytesFloat32)
  507. }