Class: Pocketsphinx::AudioFile

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

Overview

Implements Recordable interface (#start_recording, #stop_recording 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 = 2048) ⇒ 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



9
10
11
12
13
14
15
16
17
18
# File 'lib/pocketsphinx/audio_file.rb', line 9

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

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

#start_recordingObject



20
21
22
# File 'lib/pocketsphinx/audio_file.rb', line 20

def start_recording
  self.file = File.open(file_path, 'rb')
end

#stop_recordingObject



24
25
26
27
28
29
# File 'lib/pocketsphinx/audio_file.rb', line 24

def stop_recording
  if file
    file.close
    self.file = nil
  end
end