Documentation
¶
Index ¶
- Variables
- func ByteCountFromPlayTime(t time.Duration) int64
- func F32ToUnsignedPCM16(fs []float32, outBuf []byte) []byte
- func Init(sr SampleRate, chanCount SoundChannelCount, bitDepth SoundBitDepth) error
- func PauseAllSounds()
- func PlayTimeFromByteCount(byteCount int64) time.Duration
- func ReadAllFromReader(reader io.Reader, readingBufSize, ouputBufSize uint64) ([]byte, error)
- func ResumeAllSounds()
- type OggStreamer
- type SampleRate
- type Sound
- func (s *Sound) Close() error
- func (s *Sound) IsClosed() bool
- func (s *Sound) IsPlaying() bool
- func (s *Sound) LoopAsync(timesToPlay int)
- func (s *Sound) Pause()
- func (s *Sound) PlayAsync()
- func (s *Sound) PlaySync()
- func (s *Sound) RemainingTime() time.Duration
- func (s *Sound) SeekToPercent(percent float64)
- func (s *Sound) SeekToTime(t time.Duration)
- func (s *Sound) SetVolume(newVol float64)
- func (s *Sound) TotalTime() time.Duration
- func (s *Sound) Volume() float64
- func (s *Sound) Wait()
- func (s *Sound) WaitLoop()
- type SoundBitDepth
- type SoundBuffer
- type SoundChannelCount
- type SoundInfo
- type SoundMode
- type SoundType
- type WavStreamer
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidWhence = errors.New("invalid whence value. Must be: io.SeekStart, io.SeekCurrent, or io.SeekEnd") ErrNegativeSeekPos = errors.New("negative seeker position") )
Pre-defined errors
var ( Ctx *oto.Context SamplingRate SampleRate ChanCount SoundChannelCount BitDepth SoundBitDepth BytesPerSample int64 BytesPerSecond int64 )
Those values are set after Init
Functions ¶
func ByteCountFromPlayTime ¶
PlayTimeFromByteCount returns how many bytes are needed to produce a sound that takes t time to play
func F32ToUnsignedPCM16 ¶ added in v1.11.0
F32ToUnsignedPCM16 takes PCM data stored as float32 between [-1, 1] and returns a byte array of uint16, where each two subsequent bytes represent one uint16.
func Init ¶
func Init(sr SampleRate, chanCount SoundChannelCount, bitDepth SoundBitDepth) error
Init prepares the default audio device and does any required setup. It must be called before loading any sounds
func PauseAllSounds ¶
func PauseAllSounds()
func PlayTimeFromByteCount ¶
PlayTimeFromByteCount returns the time taken to play this many bytes
func ReadAllFromReader ¶
ReadAllFromReader takes an io.Reader and reads until error or io.EOF.
If io.EOF is reached then read bytes are returned with a nil error. If the reader returns an error that's not io.EOF then everything read till that point is returned along with the error
readingBufSize is the buffer used to read from reader.Read(). Bigger values might read more efficiently. If readingBufSize<4096 then readingBufSize is set to 4096
ouputBufSize is used to set the capacity of the final buffer to be returned. This can greatly improve performance if you know the size of the output. It is allowed to have an outputBufSize that's smaller or larger than what the reader ends up returning
func ResumeAllSounds ¶
func ResumeAllSounds()
Types ¶
type OggStreamer ¶ added in v1.12.0
func NewOggStreamer ¶ added in v1.12.0
func NewOggStreamer(f *os.File, dec *oggvorbis.Reader) *OggStreamer
func (*OggStreamer) Read ¶ added in v1.12.0
func (ws *OggStreamer) Read(outBuf []byte) (floatsRead int, err error)
func (*OggStreamer) Seek ¶ added in v1.12.0
func (ws *OggStreamer) Seek(offset int64, whence int) (int64, error)
func (*OggStreamer) Size ¶ added in v1.12.0
func (ws *OggStreamer) Size() int64
Size returns number of bytes
type SampleRate ¶
type SampleRate int
const ( SampleRate_44100 SampleRate = 44100 SampleRate_48000 SampleRate = 48000 )
type Sound ¶
type Sound struct {
Player oto.Player
PlayerSeeker io.Seeker
Info SoundInfo
// File is the file descriptor of the sound file being streamed.
// This is only set if sound is streamed, and is kept to ensure GC doesn't hit it
File *os.File
// Data is an io.ReadSeeker over an open file or over a buffer containing the uncompressed sound file.
// Becomes nil after close
Data io.ReadSeeker
IsLooping bool
}
func ClipInMemSoundPercent ¶
ClipInMemSoundPercent is like CopyInMemSound but produces a sound that plays only between from and to. fromPercent and toPercent must be between 0 and 1
func CopyInMemSound ¶
CopyInMemSound returns a new sound object that has identitcal info and uses the same underlying data, but with independent play controls (e.g. one playing at the start while one is in the middle). Since the sound data is not copied this function is very fast.
Panics if the sound is not in-memory
func NewSoundMem ¶
NewSoundMem loads the entire sound file into memory
func NewSoundStreaming ¶
NewSoundStreaming plays sound by streaming from a file, so no need to load the entire file into memory. Good for large sound files
func (*Sound) Close ¶
Close will clean underlying resources, and the 'Ctx' and 'Bytes' fields will be made nil. Repeated calls are no-ops
func (*Sound) LoopAsync ¶
LoopAsync plays the sound 'timesToPlay' times. If timesToPlay<0 then it is played indefinitely until paused If timesToPlay==0 then the sound is not played. If a sound is already playing then it will be paused then resumed in a looping manner
func (*Sound) PlayAsync ¶
func (s *Sound) PlayAsync()
PlayAsync plays the sound in the background and returns.
func (*Sound) RemainingTime ¶
RemainingTime returns the time left in the clip, which is affected by pausing/resetting/seeking of the sound. Returns zero after close
func (*Sound) SeekToPercent ¶
SeekToPercent moves the current position of the sound to the given percentage of the total sound length. For example, if a sound is 10s long and percent=0.5 then when the sound is played it will start from 5s.
This can be used while the sound is playing.
percent is clamped [0,1], so passing <0 is the same as zero, and >1 is the same as 1
func (*Sound) SeekToTime ¶
SeekToTime moves the current position of the sound to the given duration. For example if you use t=5*time.Second then play you will start from 5th second.
This can be used while the sound is playing.
t is clamped between [0, totalTime]
func (*Sound) SetVolume ¶
SetVolume must be between 0 and 1 (both inclusive). Other values will panic. The default volume is 1.
func (*Sound) TotalTime ¶
TotalTime returns the time taken to play the entire sound. Safe to use after close
func (*Sound) Wait ¶
func (s *Sound) Wait()
Wait blocks until sound finishes playing. If the sound is not playing Wait returns immediately. In the worst case (Wait sleeping then sound immediately paused), Wait will block ~4% of the total play time. In most other cases Wait should be accurate to ~1ms.
If you want to wait for all loops to finish then use WaitLoop
type SoundBitDepth ¶
type SoundBitDepth int
const ( SoundBitDepth_1 SoundBitDepth = 1 SoundBitDepth_2 SoundBitDepth = 2 )
type SoundBuffer ¶
func (*SoundBuffer) Copy ¶
func (sb *SoundBuffer) Copy() *SoundBuffer
Copy returns a new SoundBuffer that uses the same `Data` but with an independent ReadSeeker. This allows you to have many readers all reading from different positions of the same buffer.
The new buffer will have its starting position set to io.SeekStart (`Pos=0`)
func (*SoundBuffer) Read ¶
func (sb *SoundBuffer) Read(outBuf []byte) (bytesRead int, err error)
Read only returns io.EOF when bytesRead==0 and no more input is available
func (*SoundBuffer) Seek ¶
func (sb *SoundBuffer) Seek(offset int64, whence int) (int64, error)
Seek returns the new position. An error is only returned if the whence is invalid or if the resulting position is negative.
If the resulting position is >=len(SoundBuffer.Data) then future Read() calls will return io.EOF
type SoundChannelCount ¶
type SoundChannelCount int
const ( SoundChannelCount_1 SoundChannelCount = 1 SoundChannelCount_2 SoundChannelCount = 2 )
type WavStreamer ¶ added in v1.12.0
func NewWavStreamer ¶ added in v1.12.0
func (*WavStreamer) Read ¶ added in v1.12.0
func (ws *WavStreamer) Read(outBuf []byte) (bytesRead int, err error)
func (*WavStreamer) Seek ¶ added in v1.12.0
func (ws *WavStreamer) Seek(offset int64, whence int) (int64, error)
func (*WavStreamer) Size ¶ added in v1.12.0
func (ws *WavStreamer) Size() int64
Size returns number of bytes