Class: Hornetseye::AlsaInput

Inherits:
Object
  • Object
show all
Defined in:
lib/hornetseye-alsa/alsainput.rb,
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 '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 'lib/hornetseye-alsa/docs.rb', line 28

def rate
  @rate
end

Class Method Details

.new(pcm_name = 'default:0', rate = 48000, channels = 2, periods = 8, frames = 1024) ⇒ 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:0', 44_100, 2

Parameters:

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

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

  • periods (Integer) (defaults to: 8)

    Number of audio frames of the input buffer.

  • frames (Integer) (defaults to: 1024)

    Size of the audio frames of the input buffer.

Returns:

  • (AlsaInput)

    An object for accessing the microphone.

See Also:



53
54
55
56
# File 'lib/hornetseye-alsa/alsainput.rb', line 53

def new( pcm_name = 'default:0', rate = 48000, channels = 2, periods = 8,
         frames = 1024 )
  orig_new pcm_name, rate, channels, periods, frames
end

.orig_newAlsaInput

Alias for native constructor

Returns:

  • (AlsaInput)

    An object for accessing a microphone.



32
# File 'lib/hornetseye-alsa/alsainput.rb', line 32

alias_method :orig_new, :new

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 'lib/hornetseye-alsa/docs.rb', line 45

def avail
end

#closeAlsaInput

Close the audio device

Returns:



38
39
# File '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 'lib/hornetseye-alsa/docs.rb', line 51

def delay
end

#orig_readNode

Alias for native method

Returns:

  • (Node)

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



65
# File 'lib/hornetseye-alsa/alsainput.rb', line 65

alias_method :orig_read, :read

#prepareAlsaInput

Reset the sound device

Returns:



57
58
# File '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:0', 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.



82
83
84
# File 'lib/hornetseye-alsa/alsainput.rb', line 82

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