Class: Hornetseye::AlsaInput

Inherits:
Object
  • Object
show all
Defined in:
docs/hornetseye-alsa/lib/hornetseye-alsa/alsainput.rb,
docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb

Overview

Class for capturing audio samples from an ALSA device

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#channelsInteger (readonly)

Number of audio channels

Returns:

  • (Integer)

    Number of audio channels (1=mono, 2=stereo).



33
34
35
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb', line 33

def channels
  @channels
end

#rateInteger (readonly)

Get the sampling rate of the sound device

The sampling rate may be different to the desired sampling rate specified in the constructor.

Returns:

  • (Integer)

    The sampling rate of the sound device.



28
29
30
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb', line 28

def rate
  @rate
end

Class Method Details

.new(pcm_name = 'default', rate = 48000, channels = 2) ⇒ AlsaInput

Open a sound device for input

Open the specified sound device for reading. Note that the desired sample rate may not be supported. In that case the sound library will choose a sampling rate near the desired one.

Examples:

Open standard microphone device

require 'hornetseye_alsa'
include Hornetseye
microphone = AlsaInput.new 'default', 44_100, 2

Parameters:

  • pcm_name (String) (defaults to: 'default')

    Name of the PCM device

  • rate (Integer) (defaults to: 48000)

    Desired sampling rate.

  • channels (Integer) (defaults to: 2)

    Number of channels (1=mono, 2=stereo).

Returns:

  • (AlsaInput)

    An object for accessing the microphone.

See Also:



51
52
53
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/alsainput.rb', line 51

def new(pcm_name = 'default', rate = 48000, channels = 2)
  orig_new pcm_name, rate, channels
end

Instance Method Details

#availInteger

Space available for recording to the audio buffer

Returns:

  • (Integer)

    Number of audio samples left for recording before the buffer overflows.



45
46
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb', line 45

def avail
end

#closeAlsaInput

Close the audio device

Returns:



38
39
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb', line 38

def close
end

#delayInteger

Number of samples available for retrieval

Returns:

  • (Integer)

    Number of audio samples available for retrieval.



51
52
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb', line 51

def delay
end

#prepareAlsaInput

Reset the sound device

Returns:



57
58
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/docs.rb', line 57

def prepare
end

#read(samples) ⇒ Node

Read specified number of samples from the sound device

Audio data is read from the input buffer.

A blocking read operation is used. I.e. the program is blocked until there is sufficient data available in the audio input buffer.

Examples:

Read 3 seconds of audio samples

require 'hornetseye_alsa'
include Hornetseye
microphone = AlsaInput.new 'default', 44_100, 2
data = microphone.read 3 * 44_100

Parameters:

  • samples (Integer)

    Number of samples to read.

Returns:

  • (Node)

    A two-dimensional array with short-integer audio samples.



79
80
81
# File 'docs/hornetseye-alsa/lib/hornetseye-alsa/alsainput.rb', line 79

def read(samples)
  MultiArray.import SINT, orig_read(samples).memory, channels, samples
end