faiface / beep

A little package that brings sound to any Go application. Suitable for playback and audio-processing.
MIT License
2.08k stars 152 forks source link

How do i play the same file multiple times? #19

Closed KidLinus closed 6 years ago

KidLinus commented 6 years ago

Hi, I cant figure out how to play the same sound effect multiple times, like every 5 seconds. Can you point me in the right direction?

faiface commented 6 years ago

Hi! Yes, a beep.Streamer gets drained when played and thus can naturally only be played once. However, there are two good ways to solve this:

  1. The loaded/decoded streamer is a beep.StreamSeekCloser, which means you can seek it = rewind to an arbitrary point. Doing streamer.Seek(0) rewinds it to the beginning and it can be played again.
  2. A more general approach, and also an approach which allows you to play the same sound in multiple instances over one another is to use a beep.Buffer. Buffer allows you to store audio data in memory (the streamer loaded from file streams directly from the file without loading to memory) and then play it as you wish. You create a buffer, you use buffer.Append(streamer) to add audio data to the buffer, and then you use buffer.Streamer(start, stop) to create a streamer that plays from start to stop. For example, buffer.Streamer(0, buffer.Len()) plays the entire content of the buffer. The obtained streamer can be then played through the speaker. To play the audio multiple times, you just create a new streamer every time you need it. Look at the docs for more info.
KidLinus commented 6 years ago

I see, thank you! (Close this issue)

gonutz commented 6 years ago

I was looking for this, only to find it while creating a new issue for it (Github pointed me to this one when I was typing in my keywords). Please consider adding this to your readme and as an example. I think this is of utmost importance as many people use your library alongside your pixel engine to create games and playing a certain sound effect in this way is the foremost use case for small WAV sound effects usually.

faiface commented 6 years ago

@gonutz You're right. I'll extend the README to a more fledged tutorial covering most features of Beep as soon as I can.

gonutz commented 6 years ago

Cool, thank you! :-)