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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c3335ce18ab067308516df60def16e7c4c331c1
4
- data.tar.gz: 90fbf28718ed3fa48dc57f20a77632ad90c1915b
3
+ metadata.gz: 584201d6ad6c79033ac7fd2531c95198d2e1dea4
4
+ data.tar.gz: ef3dd012fcef79e09cd29dda6fa502297290f75e
5
5
  SHA512:
6
- metadata.gz: 8c2dec5cec66f79ca36428829768b21b9968150ceb1b6334ddbfd092294bc9d7eecfc6f3c733d64a19e8ff61c54ba42d699554385825ba46127246f1331281eb
7
- data.tar.gz: 72d4d5f937a50444b003212efe844a9cd67521b240d4010a96ecbfd795f1ea076437e1d34f3b0bf6291cf92633da602cc138a6425259ea27bff2960e6d6e09bb
6
+ metadata.gz: 7b73366ac9da504c1177c4ce2938bc4c6e7a162582bf1828b4bc9d7d5aeed985ef7504fad019f15cbe967ef441f85f8150e03b9d641f185d8ef1cac059e62b59
7
+ data.tar.gz: 4e7df1d3741837d0436ff9c6c71670d29a236d116c652a18b3f2eed70d24df73b7136709daa0063324c7cf0b26d712ff94fc82e298d6fd67dbe7f3498115b3ea
data/README.md CHANGED
@@ -6,7 +6,7 @@ In french, a "Sbire" is a henchman. It's the guy who do what you say. Sbire is p
6
6
 
7
7
  In Ubuntu :
8
8
 
9
- sudo apt-get install ffmpeg notify-osd ruby1.9.1
9
+ sudo apt-get install sox notify-osd ruby1.9.1
10
10
  gem install sbire
11
11
  mkdir ~\.sbire\
12
12
 
@@ -1,15 +1,71 @@
1
- require 'curb'
2
1
  require 'json'
3
- require 'speech'
2
+ require 'fileutils'
3
+ require 'rest_client'
4
4
 
5
5
  class AudioConverter
6
- attr_accessor :audio
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 initialize(file_path)
9
- @audio = Speech::AudioToText.new(file_path)
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 results
13
- [audio.to_text(1, Sbire::CONFIG.lang)]
67
+ def url
68
+ lang = SbireConfig.lang
69
+ "https://www.google.com/speech-api/v1/recognize?lang=#{lang}"
14
70
  end
15
71
  end
@@ -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
- write_pid(pid)
10
+ pid_manager.store(self, pid)
10
11
  end
11
12
 
12
13
  def stop
13
- pid = File.readlines(Sbire::PID_FILE)[0]
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 "ffmpeg -loglevel panic -f alsa -ac 2 -i pulse -y #{path} -r 22050 >/dev/null 2>&1" }
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
@@ -8,9 +8,11 @@ class CommandManager
8
8
  end
9
9
 
10
10
  def execute(hypotheses)
11
- command = find(hypotheses)
12
- system("#{command} &")
13
- return command
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
@@ -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
@@ -6,6 +6,6 @@ class SaveManager
6
6
  end
7
7
 
8
8
  def save
9
- File.open(Sbire::TEXT_FILE, 'w') {|file| file.write(hypotheses.first)}
9
+ File.open(SbireConfig.text_file, 'w') {|file| file.write(hypotheses.first)}
10
10
  end
11
11
  end
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
- hypotheses = audio_converter.results
47
- show(command_manager.execute(hypotheses))
39
+ audio_converter.stop
48
40
  end
49
41
 
50
42
  def save
51
- show("Sbire is writing what you said")
52
- audio_recorder.stop
53
- hypotheses = audio_converter.results
54
- SaveManager.new(hypotheses).save
55
- show("Sbire has ended to write your voice")
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(COMMAND_PATH)
52
+ @command_manager ||= CommandManager.new(SbireConfig.command_path)
60
53
  end
61
54
 
62
55
  def audio_recorder
63
- @audio_recorder ||= AudioRecorder.new(OUT_FILE)
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(OUT_FILE)
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
- def initialize(path)
5
- if File.exist?(path)
6
- @config = YAML.load_file(path)
4
+ def self.config_path
5
+ "#{base_directory}/config.yml"
7
6
  end
8
- end
9
7
 
10
- def lang
11
- config["lang"] ||= "en-US"
12
- end
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
- private
15
- def config
16
- @config ||= {}
17
- end
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.3
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-10-29 00:00:00.000000000 Z
11
+ date: 2013-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: curb
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
- - license.md
47
+ - MIT
61
48
  metadata: {}
62
49
  post_install_message:
63
50
  rdoc_options: []