Class: CW::AudioPlayer

Inherits:
Object
  • Object
show all
Includes:
FileDetails, OStest
Defined in:
lib/cw/audio_player.rb

Constant Summary

Constants included from FileDetails

FileDetails::ABBREVIATIONS, FileDetails::AUDIO_DIR, FileDetails::BOOKMARK_FILE, FileDetails::CALLS, FileDetails::CALLS_FILENAME, FileDetails::CODE, FileDetails::CODE_FILENAME, FileDetails::CONFIG_FILENAME, FileDetails::CONFIG_PATH, FileDetails::DASH_FILENAME, FileDetails::DATA, FileDetails::DEF_AUDIO_FILENAME, FileDetails::DICT_DIR, FileDetails::DICT_FILENAME, FileDetails::DOT_AUDIO_DIR, FileDetails::DOT_CW_DIR, FileDetails::DOT_FILENAME, FileDetails::E_SPACE_FILENAME, FileDetails::HERE, FileDetails::Q_CODES, FileDetails::ROOT, FileDetails::SPACE_FILENAME, FileDetails::TEXT, FileDetails::USER_CONFIG_PATH, FileDetails::WORK_DIR

Instance Method Summary collapse

Methods included from OStest

#is_mac?, #is_posix?

Methods included from FileDetails

#audio_dir, #audio_filename, #dash_path, #default_audio_dir, #dot_audio_dir, #dot_cw_dir, #dot_path, #e_space_path, #init_filenames, #process_audio_dir, #process_dot_audio, #process_dot_cw, #progress_file, #space_path, #user_audio_dir

Instance Method Details

#convert_book(words) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/cw/audio_player.rb', line 46

def convert_book words
  words = words.delete("\n")
  File.open(temp_filename_for_ebook2cw, 'w') do |file|
    file.print words
  end
  cl = Cl.new.cl_full(temp_filename_for_ebook2cw)
  ! @dry_run ? `#{cl}` : cl
  File.delete(temp_filename_for_ebook2cw)
  File.rename(play_filename_for_ebook2cw + '0000.mp3', play_filename_for_ebook2cw)
end

#convert_words(words) ⇒ Object



64
65
66
67
# File 'lib/cw/audio_player.rb', line 64

def convert_words words
  tone.generate words if Cfg.config["use_ebook2cw"].nil?
  convert_words_with_ebook2cw words if Cfg.config["use_ebook2cw"]
end

#convert_words_with_ebook2cw(words) ⇒ Object



57
58
59
60
61
62
# File 'lib/cw/audio_player.rb', line 57

def convert_words_with_ebook2cw words
  words = words.delete("\n")
  cl = Cl.new.cl_echo(words)
  ! @dry_run ? `#{cl}` : cl
  File.rename(play_filename + '0000.mp3', play_filename)
end

#os_play_commandObject



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

def os_play_command
  if is_mac?
    'afplay'
  elsif is_posix?
    'ossplay'
  else
    puts 'Error - play_command required in .cw_config'
    exit 1
  end
end

#playObject



74
75
76
77
78
79
80
81
# File 'lib/cw/audio_player.rb', line 74

def play
  cmd = play_command + ' ' + play_filename
  @pid = ! @dry_run ? Process.spawn(cmd) : cmd
  begin
    Process.waitpid(@pid) if @pid.is_a?(1.class)
  rescue Errno::ECHILD
  end
end

#play_cmd_for_psObject



95
96
97
# File 'lib/cw/audio_player.rb', line 95

def play_cmd_for_ps
  '[' << play_command[0] << ']' << play_command[1..-1]
end

#play_commandObject



28
29
30
31
32
33
34
# File 'lib/cw/audio_player.rb', line 28

def play_command
  if Cfg.config["play_command"].nil?
    Cfg.config.params["play_command"] =
      os_play_command
  end
  Cfg.config["play_command"]
end

#play_filenameObject



69
70
71
72
# File 'lib/cw/audio_player.rb', line 69

def play_filename
  return play_filename_for_ebook2cw if Cfg.config["use_ebook2cw"]
  tone.play_filename
end

#play_filename_for_ebook2cwObject



36
37
38
39
40
# File 'lib/cw/audio_player.rb', line 36

def play_filename_for_ebook2cw
  @play_filename ||= File.join(audio_dir, audio_filename)
  puts "@play_filename = #{@play_filename}"
  @play_filename
end

#play_tone(tone) ⇒ Object



91
92
93
# File 'lib/cw/audio_player.rb', line 91

def play_tone tone
  `#{play_command + ' ' + tone}`
end

#still_playing?Boolean

Returns:



99
100
101
102
103
104
# File 'lib/cw/audio_player.rb', line 99

def still_playing?
  cl = "ps -eo  pid,args | grep #{play_cmd_for_ps}"
  ps = `#{cl}`
  return ps.include?("#{play_filename_for_ebook2cw}") if Cfg.config["use_ebook2cw"]
  return ps.include?(tone.play_filename) unless Cfg.config["use_ebook2cw"]
end

#stopObject



83
84
85
86
87
88
89
# File 'lib/cw/audio_player.rb', line 83

def stop
  begin
    Process.kill(:TERM, @pid)
    Process.wait(@pid)
  rescue
  end
end

#temp_filename_for_ebook2cwObject



42
43
44
# File 'lib/cw/audio_player.rb', line 42

def temp_filename_for_ebook2cw
  File.expand_path("tempxxxx.txt", audio_dir)
end

#toneObject



13
14
15
# File 'lib/cw/audio_player.rb', line 13

def tone
  @tone ||= ToneGenerator.new
end