alsa

package module
v0.0.0-...-deca482 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2012 License: GPL-3.0 Imports: 4 Imported by: 2

README

NAME
    alsa is a Go wrapper package for C alsa library.

AUTHORS
    Viacheslav Chumushuk <voice@root.ua>

COPYING
    This programm is released under the GNU General Public License version 3 or later, which is
    distributed in the COPYING file. You should have received a copy of the GNU General Public License along with
    this program.  If not, see <http://www.gnu.org/licenses/>.

Documentation

Overview

alsa package is the simple wrapper for C alsa binding library.

Index

Examples

Constants

View Source
const (
	// Playback stream
	StreamTypePlayback = C.SND_PCM_STREAM_PLAYBACK
	// Capture stream
	StreamTypeCapture = C.SND_PCM_STREAM_CAPTURE
)

Stream type constants.

View Source
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
)
View Source
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 New

func New() *Handle

New returns newly initialized ALSA handler.

func (*Handle) ApplyHwParams

func (handle *Handle) ApplyHwParams() error

ApplyHwParams changes ALSA hardware parameters for the current stream.

func (*Handle) AvailUpdate

func (handle *Handle) AvailUpdate() (freeBytes int, err error)

AvailUpdate returns number of bytes ready to be read/written.

func (*Handle) Close

func (handle *Handle) Close()

Close closes stream and release the handler.

func (*Handle) Delay

func (handle *Handle) Delay() (int, error)

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

func (handle *Handle) Drain() error

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

func (handle *Handle) Drop() error

Drop stream, this function stops the PCM immediately. The pending samples on the buffer are ignored.

func (*Handle) FrameSize

func (handle *Handle) FrameSize() int

FrameSize returns size of one frame in bytes.

func (*Handle) MaxSampleRate

func (handle *Handle) MaxSampleRate() (int, error)

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) Pause

func (handle *Handle) Pause() error

Pause PCM.

func (*Handle) Read

func (handle *Handle) Read(buf []byte) (n int, err error)

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

func (handle *Handle) SampleSize() int

SampleSize returns one sample size in bytes.

func (*Handle) SkipFrames

func (handle *Handle) SkipFrames(frames int) (int, error)

Skip certain number of frames

func (*Handle) Unpause

func (handle *Handle) Unpause() error

Unpause PCM.

func (*Handle) Wait

func (handle *Handle) Wait(maxDelay int) (ok bool, err error)

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

func (handle *Handle) Write(buf []byte) (wrote int, err error)

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:

type SampleFormat

type SampleFormat C.snd_pcm_format_t

Sample type.

type StreamType

type StreamType C.snd_pcm_stream_t

Alsa stream type. Playback or capture.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL