Class: Hornetseye::AlsaOutput

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

Overview

Class for playing sounds using the ALSA library

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



75
76
77
# File 'lib/hornetseye-alsa/docs.rb', line 75

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.



70
71
72
# File 'lib/hornetseye-alsa/docs.rb', line 70

def rate
  @rate
end

Class Method Details

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

Open a sound device for output

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

Examples:

Open default speakers

require 'hornetseye_alsa'
include Hornetseye
speaker = AlsaOutput.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:

  • (AlsaOutput)

    An object for accessing the speakers.

See Also:



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

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

.orig_newAlsaOutput

Alias for native constructor

Returns:

  • (AlsaOutput)

    An object for accessing the speakers.



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

alias_method :orig_new, :new

Instance Method Details

#availInteger

Space available for writing to the audio buffer

Returns:

  • (Integer)

    Number of audio samples which can be written to the audio buffer.



99
100
# File 'lib/hornetseye-alsa/docs.rb', line 99

def avail
end

#closeAlsaOutput

Close the audio device

Returns:



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

def close
end

#delayInteger

Number of audio samples in the audio buffer

Returns the number of audio samples left in the audio buffer. This can be used to properly synchronise video display with audio output.

Returns:

  • (Integer)

    Number of audio samples left to play.



108
109
# File 'lib/hornetseye-alsa/docs.rb', line 108

def delay
end

#drainAlsaOutput

Wait until audio buffer underflows

Returns:



92
93
# File 'lib/hornetseye-alsa/docs.rb', line 92

def drain
end

#dropAlsaOutput

Drop content of audio output buffer

Returns:



86
87
# File 'lib/hornetseye-alsa/docs.rb', line 86

def drop
end

#orig_writeObject

Alias for native method



60
# File 'lib/hornetseye-alsa/alsaoutput.rb', line 60

alias_method :orig_write, :write

#prepareAlsaOutput

Reset the sound device

One needs to call this method if one wants to resume playing audio samples after calling #drop.

Returns:



117
118
# File 'lib/hornetseye-alsa/docs.rb', line 117

def prepare
end

#write(frame) ⇒ Node

Write an audio frame to the sound device

The audio data is written to the output buffer of the sound device. Playback is resumed if a buffer underflow occurred earlier. The first dimension of the array with the audio data must match the number of channels of the audio device. The second dimension is the number of audio samples.

A blocking write operation is used. I.e. the program is blocked until there is sufficient space in the audio output buffer.

Examples:

Play a 400Hz tune for 3 seconds

require 'hornetseye_alsa'
include Hornetseye
speaker = AlsaOutput.new 'default', 44_100, 2
L = 44_100 / 400
wave = lazy( 2, L ) { |j,i| Math.sin( i * 2 * Math::PI / L ) * 0x7FFF }.to_sint
( 3 * 400 ).times { speaker.write wave }

Parameters:

  • frame (Node)

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

Returns:

  • (Node)

    Returns the parameter frame.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hornetseye-alsa/alsaoutput.rb', line 83

def write( frame )
  if frame.typecode != SINT
    raise "Audio data must be of type SINT (but was #{frame.typecode})"
  end
  if frame.dimension != 2
    raise "Audio frame must have two dimensions (but had #{frame.dimension})"
  end
  if frame.shape.first != channels
    raise "Audio frame must have #{channels} channel(s) but had " +
          "#{frame.shape.first}"
  end
  orig_write Hornetseye::Sequence(UBYTE).new 2 * frame.size, :memory => frame.memory 
  frame
end