sbire 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/audio_converter.rb +63 -7
- data/lib/audio_recorder.rb +6 -11
- data/lib/command_manager.rb +5 -3
- data/lib/pid_manager.rb +12 -0
- data/lib/save_manager.rb +1 -1
- data/lib/sbire.rb +17 -20
- data/lib/sbire_config.rb +43 -11
- metadata +5 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 584201d6ad6c79033ac7fd2531c95198d2e1dea4
|
4
|
+
data.tar.gz: ef3dd012fcef79e09cd29dda6fa502297290f75e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b73366ac9da504c1177c4ce2938bc4c6e7a162582bf1828b4bc9d7d5aeed985ef7504fad019f15cbe967ef441f85f8150e03b9d641f185d8ef1cac059e62b59
|
7
|
+
data.tar.gz: 4e7df1d3741837d0436ff9c6c71670d29a236d116c652a18b3f2eed70d24df73b7136709daa0063324c7cf0b26d712ff94fc82e298d6fd67dbe7f3498115b3ea
|
data/README.md
CHANGED
data/lib/audio_converter.rb
CHANGED
@@ -1,15 +1,71 @@
|
|
1
|
-
require 'curb'
|
2
1
|
require 'json'
|
3
|
-
require '
|
2
|
+
require 'fileutils'
|
3
|
+
require 'rest_client'
|
4
4
|
|
5
5
|
class AudioConverter
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :pid_manager
|
7
|
+
def initialize(pid_manager)
|
8
|
+
@pid_manager = pid_manager
|
9
|
+
end
|
10
|
+
|
11
|
+
def start(&block)
|
12
|
+
FileUtils.rm_rf("#{SbireConfig.out_path}/.")
|
13
|
+
pid = listen_audio(block)
|
14
|
+
pid_manager.store(self, pid)
|
15
|
+
end
|
16
|
+
|
17
|
+
def stop
|
18
|
+
pid_manager.kill(self)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def write_pid(pid)
|
23
|
+
File.open(SbireConfig.converter_pid_file, 'w') {|file| file.write(pid)}
|
24
|
+
end
|
25
|
+
|
26
|
+
def listen_audio(block)
|
27
|
+
fork do
|
28
|
+
index = 1
|
29
|
+
stop = false
|
30
|
+
|
31
|
+
while stop == false
|
32
|
+
if File.exist?(filename(index + 1))
|
33
|
+
hypotheses = send_to_google(filename(index))
|
34
|
+
block.call(hypotheses)
|
35
|
+
index += 1
|
36
|
+
|
37
|
+
stop = true if ENV["mode"] == "test"
|
38
|
+
end
|
39
|
+
sleep 0.1
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def send_to_google(path)
|
45
|
+
file = File.read(path)
|
46
|
+
begin
|
47
|
+
response = RestClient.post url,
|
48
|
+
file,
|
49
|
+
content_type: 'audio/x-flac; rate=16000'
|
50
|
+
read_response(response.to_str) if response.code == 200
|
51
|
+
rescue => e
|
52
|
+
puts e
|
53
|
+
return [""]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def filename(index)
|
58
|
+
index = "%03d" % index
|
59
|
+
"#{SbireConfig.out_file}#{index}.flac"
|
60
|
+
end
|
7
61
|
|
8
|
-
def
|
9
|
-
|
62
|
+
def read_response(text)
|
63
|
+
data = JSON.parse(text)
|
64
|
+
data['hypotheses'].map {|ut| [ut['utterance'], ut['confidence']] }.first
|
10
65
|
end
|
11
66
|
|
12
|
-
def
|
13
|
-
|
67
|
+
def url
|
68
|
+
lang = SbireConfig.lang
|
69
|
+
"https://www.google.com/speech-api/v1/recognize?lang=#{lang}"
|
14
70
|
end
|
15
71
|
end
|
data/lib/audio_recorder.rb
CHANGED
@@ -1,26 +1,21 @@
|
|
1
1
|
class AudioRecorder
|
2
|
-
attr_accessor :path
|
3
|
-
def initialize(path)
|
2
|
+
attr_accessor :path, :pid_manager
|
3
|
+
def initialize(path, pid_manager)
|
4
4
|
@path = path
|
5
|
+
@pid_manager = pid_manager
|
5
6
|
end
|
6
7
|
|
7
8
|
def start
|
8
9
|
pid = record_audio
|
9
|
-
|
10
|
+
pid_manager.store(self, pid)
|
10
11
|
end
|
11
12
|
|
12
13
|
def stop
|
13
|
-
|
14
|
-
system("kill #{pid}")
|
14
|
+
pid_manager.kill(self)
|
15
15
|
end
|
16
16
|
|
17
17
|
private
|
18
|
-
def write_pid(pid)
|
19
|
-
File.open(Sbire::PID_FILE, 'w') {|file| file.write(pid)}
|
20
|
-
end
|
21
|
-
|
22
18
|
def record_audio
|
23
|
-
fork { exec "
|
24
|
-
`ps aux | grep ffmpeg | grep Sl | grep -v grep | awk -F" " '{print $2}'`
|
19
|
+
fork { exec "sox -t alsa -r 22050 default #{path}.flac -q silence -l 1 0 1% 1 1.0 1% rate 16k : newfile : restart" }
|
25
20
|
end
|
26
21
|
end
|
data/lib/command_manager.rb
CHANGED
@@ -8,9 +8,11 @@ class CommandManager
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def execute(hypotheses)
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
unless hypotheses.first.empty?
|
12
|
+
command = find(hypotheses)
|
13
|
+
system("#{command} &")
|
14
|
+
return command
|
15
|
+
end
|
14
16
|
end
|
15
17
|
|
16
18
|
private
|
data/lib/pid_manager.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
class PidManager
|
2
|
+
def store(object, pid)
|
3
|
+
class_name = object.class.name
|
4
|
+
File.open("/tmp/#{class_name}.pid", 'w') {|file| file.write(pid)}
|
5
|
+
end
|
6
|
+
|
7
|
+
def kill(object)
|
8
|
+
class_name = object.class.name
|
9
|
+
pid = File.read("/tmp/#{class_name}.pid")
|
10
|
+
system("kill #{pid}")
|
11
|
+
end
|
12
|
+
end
|
data/lib/save_manager.rb
CHANGED
data/lib/sbire.rb
CHANGED
@@ -4,18 +4,11 @@ require_relative 'command_manager'
|
|
4
4
|
require_relative 'audio_recorder'
|
5
5
|
require_relative 'save_manager'
|
6
6
|
require_relative 'sbire_config'
|
7
|
+
require_relative 'pid_manager'
|
7
8
|
require 'rest_client'
|
8
9
|
|
9
10
|
class Sbire
|
10
11
|
|
11
|
-
BASE_DIRECTORY = "#{Dir.home}/.sbire"
|
12
|
-
OUT_FILE = "#{BASE_DIRECTORY}/.audiofile.wav"
|
13
|
-
PID_FILE = "#{BASE_DIRECTORY}/.pid"
|
14
|
-
TEXT_FILE = "#{BASE_DIRECTORY}/sbire.txt"
|
15
|
-
CONFIG_PATH = "#{BASE_DIRECTORY}/config.yml"
|
16
|
-
COMMAND_PATH = "#{BASE_DIRECTORY}/commands.yml"
|
17
|
-
CONFIG = SbireConfig.new(CONFIG_PATH)
|
18
|
-
|
19
12
|
attr_accessor :command
|
20
13
|
|
21
14
|
def self.run(argv)
|
@@ -36,38 +29,42 @@ class Sbire
|
|
36
29
|
|
37
30
|
private
|
38
31
|
def start
|
39
|
-
audio_recorder.start
|
40
32
|
show("Sbire is listening your voice")
|
33
|
+
audio_recorder.start
|
34
|
+
audio_converter.start { |result| command_manager.execute(result) }
|
41
35
|
end
|
42
36
|
|
43
37
|
def stop
|
44
|
-
show("Sbire is analyzing your voice")
|
45
38
|
audio_recorder.stop
|
46
|
-
|
47
|
-
show(command_manager.execute(hypotheses))
|
39
|
+
audio_converter.stop
|
48
40
|
end
|
49
41
|
|
50
42
|
def save
|
51
|
-
show("Sbire is
|
52
|
-
audio_recorder.
|
53
|
-
|
54
|
-
|
55
|
-
|
43
|
+
show("Sbire is listening your voice")
|
44
|
+
audio_recorder.start
|
45
|
+
FileUtils.rm(SbireConfig.text_file)
|
46
|
+
audio_converter.start do |result|
|
47
|
+
File.open(SbireConfig.text_file, 'ab+') {|f| f.write(result.first + " ") }
|
48
|
+
end
|
56
49
|
end
|
57
50
|
|
58
51
|
def command_manager
|
59
|
-
@command_manager ||= CommandManager.new(
|
52
|
+
@command_manager ||= CommandManager.new(SbireConfig.command_path)
|
60
53
|
end
|
61
54
|
|
62
55
|
def audio_recorder
|
63
|
-
@audio_recorder ||= AudioRecorder.new(
|
56
|
+
@audio_recorder ||= AudioRecorder.new(SbireConfig.out_file, pid_manager)
|
64
57
|
end
|
65
58
|
|
66
59
|
def audio_converter
|
67
|
-
@audio_converter ||= AudioConverter.new(
|
60
|
+
@audio_converter ||= AudioConverter.new(pid_manager)
|
68
61
|
end
|
69
62
|
|
70
63
|
def show(message)
|
71
64
|
Notifier.call(message)
|
72
65
|
end
|
66
|
+
|
67
|
+
def pid_manager
|
68
|
+
@pid_manager ||= PidManager.new
|
69
|
+
end
|
73
70
|
end
|
data/lib/sbire_config.rb
CHANGED
@@ -1,18 +1,50 @@
|
|
1
1
|
require 'yaml'
|
2
2
|
|
3
3
|
class SbireConfig
|
4
|
-
|
5
|
-
|
6
|
-
@config = YAML.load_file(path)
|
4
|
+
def self.config_path
|
5
|
+
"#{base_directory}/config.yml"
|
7
6
|
end
|
8
|
-
end
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
def self.base_directory
|
9
|
+
"/home/dougui/.sbire"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.tmp_directory
|
13
|
+
"/tmp"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.lang
|
17
|
+
config["lang"] ||= "en-US"
|
18
|
+
end
|
13
19
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
20
|
+
def self.out_path
|
21
|
+
config["out_path"] ||= "#{base_directory}/out/"
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.out_file
|
25
|
+
config["out_file"] ||= "#{out_path}.audiofile"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.record_pid_file
|
29
|
+
config["record_pid_file"] ||= "#{tmp_directory}/.recorder.pid"
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.converter_pid_file
|
33
|
+
config["converter_pid_file"] ||= "#{tmp_directory}/.converter.pid"
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.text_file
|
37
|
+
config["text_file"] ||= "#{base_directory}/text"
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.command_path
|
41
|
+
config["command_path"] ||= "#{base_directory}/commands.yml"
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def self.config
|
47
|
+
return @config if @config
|
48
|
+
@config = YAML.load_file(config_path) || {}
|
49
|
+
end
|
18
50
|
end
|
metadata
CHANGED
@@ -1,31 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sbire
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Guirec Corbel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '>='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: speech2text
|
14
|
+
name: rest-client
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
17
|
- - '>='
|
@@ -54,10 +40,11 @@ files:
|
|
54
40
|
- lib/save_manager.rb
|
55
41
|
- lib/sbire_config.rb
|
56
42
|
- lib/notifier.rb
|
43
|
+
- lib/pid_manager.rb
|
57
44
|
- README.md
|
58
45
|
homepage: https://github.com/GCorbel/sbire
|
59
46
|
licenses:
|
60
|
-
-
|
47
|
+
- MIT
|
61
48
|
metadata: {}
|
62
49
|
post_install_message:
|
63
50
|
rdoc_options: []
|