Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add Configuration wrapper for cmd_ln.c
- Loading branch information
Showing
7 changed files
with
160 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ tmp | |
*.o | ||
*.a | ||
mkmf.log | ||
.DS_Store | ||
log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'pocketsphinx/configuration/setting_definition' | ||
|
||
module Pocketsphinx | ||
class Configuration | ||
private_class_method :new | ||
|
||
def initialize(ps_arg_defs) | ||
@ps_arg_defs = ps_arg_defs | ||
@setting_definitions = SettingDefinition.from_arg_defs(ps_arg_defs) | ||
|
||
# Sets default settings based on definitions | ||
@ps_config = API::Sphinxbase.cmd_ln_parse_r(nil, ps_arg_defs, 0, nil, 1) | ||
end | ||
|
||
def self.default | ||
new(API::Pocketsphinx.ps_args) | ||
end | ||
|
||
def [](name) | ||
unless definition = @setting_definitions[name] | ||
raise "Configuration setting '#{name}' does not exist" | ||
end | ||
|
||
case definition.type | ||
when :integer | ||
API::Sphinxbase.cmd_ln_int_r(@ps_config, "-#{name}") | ||
when :float | ||
API::Sphinxbase.cmd_ln_float_r(@ps_config, "-#{name}") | ||
when :string | ||
API::Sphinxbase.cmd_ln_str_r(@ps_config, "-#{name}") | ||
when :boolean | ||
API::Sphinxbase.cmd_ln_int_r(@ps_config, "-#{name}") != 0 | ||
when :string_list | ||
raise NotImplementedException | ||
end | ||
end | ||
|
||
def []=(name, value) | ||
unless definition = @setting_definitions[name] | ||
raise "Configuration setting '#{name}' does not exist" | ||
end | ||
|
||
case definition.type | ||
when :integer | ||
raise "Configuration setting '#{name}' must be a Fixnum" unless value.respond_to?(:to_i) | ||
API::Sphinxbase.cmd_ln_set_int_r(@ps_config, "-#{name}", value.to_i) | ||
when :float | ||
raise "Configuration setting '#{name}' must be a Float" unless value.respond_to?(:to_i) | ||
API::Sphinxbase.cmd_ln_set_float_r(@ps_config, "-#{name}", value.to_f) | ||
when :string | ||
API::Sphinxbase.cmd_ln_set_str_r(@ps_config, "-#{name}", value.to_s) | ||
when :boolean | ||
API::Sphinxbase.cmd_ln_set_int_r(@ps_config, "-#{name}", value ? 1 : 0) | ||
when :string_list | ||
raise NotImplementedException | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Pocketsphinx | ||
class Configuration | ||
class SettingDefinition | ||
TYPES = [:integer, :float, :string, :boolean, :string_list] | ||
|
||
def initialize(name, type_code, default, doc) | ||
@name, @type_code, @default, @doc = name, type_code, default, doc | ||
end | ||
|
||
def type | ||
# Remove the required bit if it exists and find type from log2 of code | ||
TYPES[Math.log2(@type_code - @type_code%2) - 1] | ||
end | ||
|
||
def required? | ||
@type_code % 2 == 1 | ||
end | ||
|
||
# Build setting definitions from pocketsphinx argument definitions | ||
# | ||
# @param [FFI::Pointer] ps_arg_defs A pointer to the Pocketsphinx argument definitions | ||
# | ||
# @return [Hash] A hash of setting definitions (name -> definition) | ||
def self.from_arg_defs(ps_arg_defs) | ||
{}.tap do |setting_defs| | ||
arg_array = FFI::Pointer.new(API::Sphinxbase::Argument, ps_arg_defs) | ||
|
||
0.upto(Float::INFINITY) do |i| | ||
arg = API::Sphinxbase::Argument.new(arg_array[i]) | ||
break if arg[:name].nil? | ||
|
||
# Remove '-' from argument name | ||
name = arg[:name][1..-1] | ||
setting_defs[name] = new(name, arg[:type], arg[:deflt], arg[:doc]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
describe Pocketsphinx::Configuration do | ||
subject { Pocketsphinx::Configuration.default } | ||
|
||
it "provides a default pocketsphinx configuration" do | ||
expect(subject).to be_a(Pocketsphinx::Configuration) | ||
end | ||
|
||
it "supports integer settings" do | ||
expect(subject['frate']).to eq(100) | ||
expect(subject['frate']).to be_a(Fixnum) | ||
|
||
subject['frate'] = 50 | ||
expect(subject['frate']).to eq(50) | ||
end | ||
|
||
it "supports float settings" do | ||
expect(subject['samprate']).to eq(16000) | ||
expect(subject['samprate']).to be_a(Float) | ||
|
||
subject['samprate'] = 8000 | ||
expect(subject['samprate']).to eq(8000) | ||
end | ||
|
||
it "supports getting strings" do | ||
expect(subject['warp_type']).to eq('inverse_linear') | ||
|
||
subject['warp_type'] = 'different_type' | ||
expect(subject['warp_type']).to eq('different_type') | ||
end | ||
|
||
it "supports getting booleans" do | ||
expect(subject['smoothspec']).to eq(false) | ||
|
||
subject['smoothspec'] = true | ||
expect(subject['smoothspec']).to eq(true) | ||
end | ||
|
||
it 'raises exceptions when setting with incorrectly typed values' do | ||
expect { subject['frate'] = true }.to raise_exception "Configuration setting 'frate' must be a Fixnum" | ||
end | ||
|
||
it 'raises exceptions when a setting is unknown' do | ||
expect { subject['unknown'] = true }.to raise_exception "Configuration setting 'unknown' does not exist" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters