Documentation ¶
Overview ¶
alsa package is the simple wrapper for C alsa binding library.
Index ¶
- Constants
- type Handle
- func (handle *Handle) ApplyHwParams() error
- func (handle *Handle) AvailUpdate() (freeBytes int, err error)
- func (handle *Handle) Close()
- func (handle *Handle) Delay() (int, error)
- func (handle *Handle) Drain() error
- func (handle *Handle) Drop() error
- func (handle *Handle) FrameSize() int
- func (handle *Handle) MaxSampleRate() (int, error)
- func (handle *Handle) Open(device string, streamType StreamType, mode int) error
- func (handle *Handle) Pause() error
- func (handle *Handle) Read(buf []byte) (n int, err error)
- func (handle *Handle) SampleSize() int
- func (handle *Handle) SkipFrames(frames int) (int, error)
- func (handle *Handle) Unpause() error
- func (handle *Handle) Wait(maxDelay int) (ok bool, err error)
- func (handle *Handle) Write(buf []byte) (wrote int, err error)
- type SampleFormat
- type StreamType
Examples ¶
Constants ¶
const ( // Playback stream StreamTypePlayback = C.SND_PCM_STREAM_PLAYBACK // Capture stream StreamTypeCapture = C.SND_PCM_STREAM_CAPTURE )
Stream type constants.
const ( // Unknown SampleFormatUnknown = C.SND_PCM_FORMAT_UNKNOWN // Signed 8 bit SampleFormatS8 = C.SND_PCM_FORMAT_S8 // Unsigned 8 bit SampleFormatU8 = C.SND_PCM_FORMAT_U8 // Signed 16 bit Little Endian SampleFormatS16LE = C.SND_PCM_FORMAT_S16_LE // Signed 16 bit Big Endian SampleFormatS16BE = C.SND_PCM_FORMAT_S16_BE // Unsigned 16 bit Little Endian SampleFormatU16LE = C.SND_PCM_FORMAT_U16_LE // Unsigned 16 bit Big Endian SampleFormatU16BE = C.SND_PCM_FORMAT_U16_BE // Signed 24 bit Little Endian using low three bytes in 32-bit word SampleFormatS24LE = C.SND_PCM_FORMAT_S24_LE // Signed 24 bit Big Endian using low three bytes in 32-bit word SampleFormatS24BE = C.SND_PCM_FORMAT_S24_BE // Unsigned 24 bit Little Endian using low three bytes in 32-bit word SampleFormatU24LE = C.SND_PCM_FORMAT_U24_LE // Unsigned 24 bit Big Endian using low three bytes in 32-bit word SampleFormatU24BE = C.SND_PCM_FORMAT_U24_BE // Signed 32 bit Little Endian SampleFormatS32LE = C.SND_PCM_FORMAT_S32_LE // Signed 32 bit Big Endian SampleFormatS32BE = C.SND_PCM_FORMAT_S32_BE // Unsigned 32 bit Little Endian SampleFormatU32LE = C.SND_PCM_FORMAT_U32_LE // Unsigned 32 bit Big Endian SampleFormatU32BE = C.SND_PCM_FORMAT_U32_BE // Signed 24bit Little Endian in 3bytes format SampleFormatS24_3LE = C.SND_PCM_FORMAT_S24_3LE // Signed 24bit Big Endian in 3bytes format SampleFormatS24_3BE = C.SND_PCM_FORMAT_S24_3BE // Unsigned 24bit Little Endian in 3bytes format SampleFormatU24_3LE = C.SND_PCM_FORMAT_U24_3LE // Unsigned 24bit Big Endian in 3bytes format SampleFormatU24_3BE = C.SND_PCM_FORMAT_U24_3BE )
const ( ModeBlock = 0 ModeNonblock = C.SND_PCM_NONBLOCK ModeAsync = C.SND_PCM_ASYNC )
Open mode constants.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handle ¶
type Handle struct { // Used samples format (size, endianness, signed). SampleFormat SampleFormat // Sample rate in Hz. Usual 44100. SampleRate int // Channels in the stream. 2 for stereo. Channels int // The interval between interrupts from the hardware Periods int // Size of buffer in frames Buffersize int // contains filtered or unexported fields }
Handle represents ALSA stream handler.
func (*Handle) ApplyHwParams ¶
ApplyHwParams changes ALSA hardware parameters for the current stream.
func (*Handle) AvailUpdate ¶
AvailUpdate returns number of bytes ready to be read/written.
func (*Handle) Delay ¶
Delay returns the numbers of frames between the time that a frame that is written to the PCM stream and it to be actually audible.
func (*Handle) Drain ¶
Drain stream. For playback wait for all pending frames to be played and then stop the PCM. For capture stop PCM permitting to retrieve residual frames.
func (*Handle) Drop ¶
Drop stream, this function stops the PCM immediately. The pending samples on the buffer are ignored.
func (*Handle) MaxSampleRate ¶
MaxSampleRate returns the maximum samplerate possible for the device
func (*Handle) Open ¶
func (handle *Handle) Open(device string, streamType StreamType, mode int) error
Open opens a stream.
func (*Handle) Read ¶
Read reads PCM data from microphone device Return read value in number of bytes read.
Example ¶
handle := New() err := handle.Open("default", StreamTypeCapture, ModeBlock) if err != nil { fmt.Printf("Open failed. %s", err) } handle.SampleFormat = SampleFormatU8 handle.SampleRate = 8000 handle.Channels = 1 err = handle.ApplyHwParams() if err != nil { fmt.Printf("SetHwParams failed. %s", err) } buf := make([]byte, 1024) n, err := handle.Read(buf) if err != nil { fmt.Printf("Read failed. %s", err) } if n != len(buf) { fmt.Printf("Could not read all data (Read %i, expected %i)", n, len(buf)) } handle.Close()
Output:
func (*Handle) SampleSize ¶
SampleSize returns one sample size in bytes.
func (*Handle) SkipFrames ¶
Skip certain number of frames
func (*Handle) Wait ¶
Wait waits till buffer will be free for some new portion of data or delay time is runs out. true ok value means that PCM stream is ready for I/O, false -- timeout occured.
func (*Handle) Write ¶
Write writes given PCM data. Returns wrote value is total bytes was written.
Example ¶
handle := New() err := handle.Open("default", StreamTypePlayback, ModeBlock) if err != nil { fmt.Printf("Open failed. %s", err) } handle.SampleFormat = SampleFormatU8 handle.SampleRate = 8000 handle.Channels = 1 err = handle.ApplyHwParams() if err != nil { fmt.Printf("SetHwParams failed. %s", err) } buf := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} n, err := handle.Write(buf) if err != nil { fmt.Printf("Write failed %s", err) } if n != len(buf) { fmt.Printf("Did not write all data (Wrote %s, expected %s)", n, len(buf)) } handle.Close()
Output: