Class: Pocketsphinx::AudioFile

Inherits:
Struct
  • Object
show all
Defined in:
lib/pocketsphinx/audio_file.rb

Overview

Implements Recordable interface (#record and #read_audio)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_pathObject

Returns the value of attribute file_path

Returns:

  • (Object)

    the current value of file_path



3
4
5
# File 'lib/pocketsphinx/audio_file.rb', line 3

def file_path
  @file_path
end

Instance Method Details

#read_audio(buffer, max_samples = 4096) ⇒ Fixnum

Read next block of audio samples from file; up to max samples into buffer.

Parameters:

  • buffer (FFI::Pointer)

    16bit buffer of at least max_samples in size

Returns:

  • (Fixnum)

    Samples actually read; nil if EOF



17
18
19
20
21
22
23
24
25
26
# File 'lib/pocketsphinx/audio_file.rb', line 17

def read_audio(buffer, max_samples = 4096)
  if file.nil?
    raise "Can't read audio: use AudioFile#record to open the file first"
  end

  if data = file.read(max_samples * 2)
    buffer.write_string(data)
    data.length / 2
  end
end

#recordObject



4
5
6
7
8
9
10
# File 'lib/pocketsphinx/audio_file.rb', line 4

def record
  File.open(file_path, 'rb') do |file|
    self.file = file
    yield
    self.file = nil
  end
end