hornetseye-alsa 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/hornetseye-alsa/alsainput.rb +47 -0
- data/lib/hornetseye-alsa/alsaoutput.rb +25 -11
- data/lib/hornetseye-alsa/docs.rb +33 -3
- metadata +2 -2
data/Rakefile
CHANGED
@@ -17,12 +17,39 @@
|
|
17
17
|
# Namespace of Hornetseye computer vision library
|
18
18
|
module Hornetseye
|
19
19
|
|
20
|
+
# Class for capturing audio samples from an ALSA device
|
21
|
+
#
|
22
|
+
# @see http://www.alsa-project.org/
|
20
23
|
class AlsaInput
|
21
24
|
|
22
25
|
class << self
|
23
26
|
|
27
|
+
# Alias for native constructor
|
28
|
+
#
|
29
|
+
# @return [AlsaInput] An object for accessing a microphone.
|
30
|
+
#
|
31
|
+
# @private
|
24
32
|
alias_method :orig_new, :new
|
25
33
|
|
34
|
+
# Open a sound device for input
|
35
|
+
#
|
36
|
+
# Open the specified sound device for reading. Note that the desired sample rate
|
37
|
+
# may not be supported. In that case the sound library will choose a sampling
|
38
|
+
# rate near the desired one.
|
39
|
+
#
|
40
|
+
# @example Open standard microphone device
|
41
|
+
# require 'hornetseye_alsa'
|
42
|
+
# include Hornetseye
|
43
|
+
# microphone = AlsaInput.new 'default:0', 44_100, 2
|
44
|
+
#
|
45
|
+
# @param [String] pcm_name Name of the PCM device
|
46
|
+
# @param [Integer] rate Desired sampling rate.
|
47
|
+
# @param [Integer] channels Number of channels (1=mono, 2=stereo).
|
48
|
+
# @param [Integer] periods Number of audio frames of the output buffer.
|
49
|
+
# @param [Integer] frames Size of the audio frames of the output buffer.
|
50
|
+
# @return [AlsaInput] An object for accessing the microphone.
|
51
|
+
#
|
52
|
+
# @see #rate
|
26
53
|
def new( pcm_name = 'default:0', rate = 48000, channels = 2, periods = 16,
|
27
54
|
frames = 1024 )
|
28
55
|
orig_new pcm_name, rate, channels, periods, frames
|
@@ -30,8 +57,28 @@ module Hornetseye
|
|
30
57
|
|
31
58
|
end
|
32
59
|
|
60
|
+
# Alias for native method
|
61
|
+
#
|
62
|
+
# @return [Node] A two-dimensional array with short-integer audio samples.
|
63
|
+
#
|
64
|
+
# @private
|
33
65
|
alias_method :orig_read, :read
|
34
66
|
|
67
|
+
# Read specified number of samples from the sound device
|
68
|
+
#
|
69
|
+
# Audio data is read from the input buffer.
|
70
|
+
#
|
71
|
+
# A blocking read operation is used. I.e. the program is blocked until there is
|
72
|
+
# sufficient data available in the audio output buffer.
|
73
|
+
#
|
74
|
+
# @example Read 3 seconds of audio samples
|
75
|
+
# require 'hornetseye_alsa'
|
76
|
+
# include Hornetseye
|
77
|
+
# microphone = AlsaInput.new 'default:0', 44_100, 2
|
78
|
+
# data = microphone.read 3 * 44_100
|
79
|
+
#
|
80
|
+
# @param [Integer] samples Number of samples to read.
|
81
|
+
# @return [Node] A two-dimensional array with short-integer audio samples.
|
35
82
|
def read( samples )
|
36
83
|
Hornetseye::MultiArray( SINT, channels, samples ).
|
37
84
|
new orig_read( samples ).memory
|
@@ -24,23 +24,30 @@ module Hornetseye
|
|
24
24
|
|
25
25
|
class << self
|
26
26
|
|
27
|
+
# Alias for native constructor
|
28
|
+
#
|
29
|
+
# @return [AlsaOutput] An object for accessing the speakers.
|
30
|
+
#
|
31
|
+
# @private
|
27
32
|
alias_method :orig_new, :new
|
28
33
|
|
29
|
-
# Open a sound device
|
34
|
+
# Open a sound device for output
|
30
35
|
#
|
31
36
|
# Open the specified sound device for writing. Note that the desired sample rate
|
32
|
-
# may not be supported. In that case the sound library will
|
37
|
+
# may not be supported. In that case the sound library will select a sampling
|
33
38
|
# rate near the desired one.
|
34
39
|
#
|
35
|
-
# @example Open
|
36
|
-
#
|
40
|
+
# @example Open default speakers
|
41
|
+
# require 'hornetseye_alsa'
|
42
|
+
# include Hornetseye
|
43
|
+
# speaker = AlsaOutput.new 'default:0', 44_100, 2
|
37
44
|
#
|
38
45
|
# @param [String] pcm_name Name of the PCM device
|
39
46
|
# @param [Integer] rate Desired sampling rate.
|
40
47
|
# @param [Integer] channels Number of channels (1=mono, 2=stereo).
|
41
48
|
# @param [Integer] periods Number of audio frames of the output buffer.
|
42
49
|
# @param [Integer] frames Size of the audio frames of the output buffer.
|
43
|
-
# @return [AlsaOutput] An object for accessing the
|
50
|
+
# @return [AlsaOutput] An object for accessing the speakers.
|
44
51
|
#
|
45
52
|
# @see #rate
|
46
53
|
def new( pcm_name = 'default:0', rate = 48000, channels = 2, periods = 16,
|
@@ -50,6 +57,9 @@ module Hornetseye
|
|
50
57
|
|
51
58
|
end
|
52
59
|
|
60
|
+
# Alias for native method
|
61
|
+
#
|
62
|
+
# @private
|
53
63
|
alias_method :orig_write, :write
|
54
64
|
|
55
65
|
# Write an audio frame to the sound device
|
@@ -62,14 +72,17 @@ module Hornetseye
|
|
62
72
|
# A blocking write operation is used. I.e. the program is blocked until there is
|
63
73
|
# sufficient space in the audio output buffer.
|
64
74
|
#
|
65
|
-
# @example
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
75
|
+
# @example Play a 400Hz tune for 3 seconds
|
76
|
+
# require 'hornetseye_alsa'
|
77
|
+
# include Hornetseye
|
78
|
+
# speaker = AlsaOutput.new 'default:0', 44_100, 2
|
79
|
+
# L = 44_100 / 400
|
80
|
+
# wave = lazy( 2, L ) { |j,i| Math.sin( i * 2 * Math::PI / L ) * 0x7FFF }.to_sint
|
81
|
+
# ( 3 * 400 ).times { speaker.write wave }
|
71
82
|
#
|
72
83
|
# @param [Node] frame A two-dimensional array of short-integer audio samples.
|
84
|
+
#
|
85
|
+
# @return [Node] Returns the parameter +frame+.
|
73
86
|
def write( frame )
|
74
87
|
if frame.typecode != SINT
|
75
88
|
raise "Audio data must be of type SINT (but was #{frame.typecode})"
|
@@ -82,6 +95,7 @@ module Hornetseye
|
|
82
95
|
"#{frame.shape.first}"
|
83
96
|
end
|
84
97
|
orig_write Hornetseye::Sequence( UBYTE, 2 * frame.size ).new( frame.memory )
|
98
|
+
frame
|
85
99
|
end
|
86
100
|
|
87
101
|
end
|
data/lib/hornetseye-alsa/docs.rb
CHANGED
@@ -19,19 +19,41 @@ module Hornetseye
|
|
19
19
|
|
20
20
|
class AlsaInput
|
21
21
|
|
22
|
+
# Get the sampling rate of the sound device
|
23
|
+
#
|
24
|
+
# The sampling rate may be different to the desired sampling rate specified in
|
25
|
+
# the constructor.
|
26
|
+
#
|
27
|
+
# @return [Integer] The sampling rate of the sound device.
|
22
28
|
attr_reader :rate
|
23
29
|
|
30
|
+
# Number of audio channels
|
31
|
+
#
|
32
|
+
# @return [Integer] Number of audio channels (1=mono, 2=stereo).
|
24
33
|
attr_reader :channels
|
25
34
|
|
35
|
+
# Close the audio device
|
36
|
+
#
|
37
|
+
# @return [AlsaInput] Returns +self+.
|
26
38
|
def close
|
27
39
|
end
|
28
40
|
|
41
|
+
# Space available for recording to the audio buffer
|
42
|
+
#
|
43
|
+
# @return [Integer] Number of audio samples left for recording before the buffer
|
44
|
+
# overflows.
|
29
45
|
def avail
|
30
46
|
end
|
31
47
|
|
48
|
+
# Number of samples available for retrieval
|
49
|
+
#
|
50
|
+
# @return [Integer] Number of audio samples available for retrieval.
|
32
51
|
def delay
|
33
52
|
end
|
34
53
|
|
54
|
+
# Reset the sound device
|
55
|
+
#
|
56
|
+
# @return [AlsaInput] Returns +self+.
|
35
57
|
def prepare
|
36
58
|
end
|
37
59
|
|
@@ -53,18 +75,24 @@ module Hornetseye
|
|
53
75
|
attr_reader :channels
|
54
76
|
|
55
77
|
# Close the audio device
|
78
|
+
#
|
79
|
+
# @return [AlsaOutput] Returns +self+.
|
56
80
|
def close
|
57
81
|
end
|
58
82
|
|
59
83
|
# Drop content of audio output buffer
|
84
|
+
#
|
85
|
+
# @return [AlsaOutput] Returns +self+.
|
60
86
|
def drop
|
61
87
|
end
|
62
88
|
|
63
89
|
# Wait until audio buffer underflows
|
90
|
+
#
|
91
|
+
# @return [AlsaOutput] Returns +self+.
|
64
92
|
def drain
|
65
93
|
end
|
66
94
|
|
67
|
-
# Space available for writing
|
95
|
+
# Space available for writing to the audio buffer
|
68
96
|
#
|
69
97
|
# @return [Integer] Number of audio samples which can be written to the audio
|
70
98
|
# buffer.
|
@@ -80,10 +108,12 @@ module Hornetseye
|
|
80
108
|
def delay
|
81
109
|
end
|
82
110
|
|
83
|
-
# Reset the sound device
|
111
|
+
# Reset the sound device
|
84
112
|
#
|
85
113
|
# One needs to call this method if one wants to resume playing audio samples after
|
86
|
-
# calling #drop
|
114
|
+
# calling #drop.
|
115
|
+
#
|
116
|
+
# @return [AlsaOutput] Returns +self+.
|
87
117
|
def prepare
|
88
118
|
end
|
89
119
|
|