@@ -57,7 +57,7 @@ def cmd_mic_list
57
57
end
58
58
end
59
59
60
- def audio_file_wave_header ( sample_rate_hz , num_channels , bits_per_sample , data_size )
60
+ def audio_file_wave_header ( sample_rate_hz : , num_channels : , bits_per_sample : , data_size : )
61
61
subchunk1_size = 16
62
62
chunk_size = 4 + ( 8 + subchunk1_size ) + ( 8 + data_size )
63
63
byte_rate = sample_rate_hz * num_channels * bits_per_sample / 8
@@ -104,17 +104,17 @@ def cmd_mic_start(*args)
104
104
105
105
mic_start_opts . parse ( args ) do |opt , _idx , val |
106
106
case opt
107
- when "-h"
108
- print_line ( "Usage: mic_start [options]\n " )
109
- print_line ( "Streams and records audio from the target microphone." )
110
- print_line ( mic_start_opts . usage )
111
- return
112
- when "-d"
113
- duration = val . to_i
114
- when "-m"
115
- device_id = val . to_i
116
- when "-s"
117
- saved_audio_path = val
107
+ when "-h"
108
+ print_line ( "Usage: mic_start [options]\n " )
109
+ print_line ( "Streams and records audio from the target microphone." )
110
+ print_line ( mic_start_opts . usage )
111
+ return
112
+ when "-d"
113
+ duration = val . to_i
114
+ when "-m"
115
+ device_id = val . to_i
116
+ when "-s"
117
+ saved_audio_path = val
118
118
end
119
119
end
120
120
@@ -128,14 +128,19 @@ def cmd_mic_start(*args)
128
128
return
129
129
end
130
130
131
+ channel = client . mic . mic_start ( device_id )
132
+ if channel . nil?
133
+ print_error ( "Mic failed to start streaming." )
134
+ return
135
+ end
131
136
print_status ( "Saving to audio file: #{ saved_audio_path } " )
137
+ print_status ( "Streaming started..." )
132
138
total_data_len = 0
133
139
begin
134
- channel = client . mic . mic_start ( device_id )
135
- mic_started = true
136
- print_status ( "Streaming started..." )
137
140
::File . open ( saved_audio_path , 'wb' ) do |outfile |
138
- audio_file_wave_header ( 11025 , 1 , 16 , 2000000000 ) . each { |e | e . write ( outfile ) }
141
+ audio_file_wave_header ( sample_rate_hz : 11025 , num_channels : 1 , bits_per_sample : 16 , data_size : 2_000_000_000 ) . each {
142
+ |e | e . write ( outfile )
143
+ }
139
144
end
140
145
::Timeout . timeout ( duration ) do
141
146
while client do
@@ -145,15 +150,15 @@ def cmd_mic_start(*args)
145
150
end
146
151
rescue ::Timeout ::Error
147
152
ensure
148
- if mic_started
149
- total_data_len += get_data . call ( channel , saved_audio_path )
150
- client . mic . mic_stop
151
- print_status ( "Streaming stopped." )
152
- # Now that we know the actual length of data, update the file header.
153
- :: File . open ( saved_audio_path , 'rb+' ) do | outfile |
154
- outfile . seek ( 0 , :: IO :: SEEK_SET )
155
- audio_file_wave_header ( 11025 , 1 , 16 , total_data_len ) . each { |e | e . write ( outfile ) }
156
- end
153
+ total_data_len += get_data . call ( channel , saved_audio_path )
154
+ client . mic . mic_stop
155
+ print_status ( "Streaming stopped." )
156
+ # Now that we know the actual length of data, update the file header.
157
+ :: File . open ( saved_audio_path , 'rb+' ) do | outfile |
158
+ outfile . seek ( 0 , :: IO :: SEEK_SET )
159
+ audio_file_wave_header ( sample_rate_hz : 11025 , num_channels : 1 , bits_per_sample : 16 , data_size : total_data_len ) . each {
160
+ |e | e . write ( outfile )
161
+ }
157
162
end
158
163
end
159
164
end
@@ -168,13 +173,13 @@ def cmd_listen(*args)
168
173
169
174
listen_opts . parse ( args ) do |opt , _idx , val |
170
175
case opt
171
- when "-h"
172
- print_line ( "Usage: listen -f <filename>\n " )
173
- print_line ( "Plays saved audio from a file." )
174
- print_line ( listen_opts . usage )
175
- return
176
- when "-f"
177
- filename = val
176
+ when "-h"
177
+ print_line ( "Usage: listen -f <filename>\n " )
178
+ print_line ( "Plays saved audio from a file." )
179
+ print_line ( listen_opts . usage )
180
+ return
181
+ when "-f"
182
+ filename = val
178
183
end
179
184
end
180
185
0 commit comments