speak_slow 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -111,20 +111,23 @@ module SpeakSlow
111
111
  # `mv #{temp_filepath} #{out_filepath}`
112
112
  # end
113
113
  # end
114
-
114
+
115
115
  def apply_sox_to_wav(in_filepath, out_filepath)
116
116
  puts "Applying SoX functions to WAV (this might take a few minutes or more)"
117
117
  # silence = "silence 0 1 0.3 -32d"
118
118
  # silence = "silence 1 0.005 -32d 1 0.3 -32d"
119
119
  silence = "silence 1 0.01 1% 1 0.3 1%"
120
- if @speed and @speed != 1
120
+ if @speed and @speed != 1 and @silence and @silence != 0 #speed and silence
121
121
  speed = "tempo -s #{@speed}"
122
122
  pad = "pad 0 #{@silence.to_f * @speed}"
123
123
  `#{@sox} #{in_filepath} -p #{silence} #{pad} : restart | #{@sox} - -p | #{@sox} - #{out_filepath} #{speed}`
124
- elsif @silence and @silence != 0
124
+ elsif @speed and @speed != 1 #only speed
125
+ speed = "tempo -s #{@speed}"
126
+ `#{@sox} #{in_filepath} #{out_filepath} #{speed}`
127
+ elsif @silence and @silence != 0 #only silence
125
128
  pad = "pad 0 #{@silence.to_f}"
126
129
  `#{@sox} #{in_filepath} -p #{silence} #{pad} : restart | #{@sox} - #{out_filepath} `
127
- else
130
+ else #nothing changed
128
131
  `cp #{in_filepath} #{out_filepath}`
129
132
  end
130
133
  end
@@ -0,0 +1,171 @@
1
+ require "speak_slow/version"
2
+ require "rubygems"
3
+ require "progressbar"
4
+
5
+ SOX = "/usr/local/bin/sox"
6
+ SOXI = "/usr/local/bin/soxi"
7
+
8
+ module SpeakSlow
9
+ DATA_DIR = File.expand_path(File.dirname(__FILE__) + "/../data")
10
+
11
+ class Converter
12
+ def initialize(in_filepath, out_filepath)
13
+
14
+ @sox = check_command(SOX)
15
+ @soxi = check_command(SOXI)
16
+
17
+ if /([^\/]+)\.([^\.]+)\z/ =~ in_filepath and File.exists?(in_filepath)
18
+ @in_filepath = in_filepath
19
+ @basename = File.basename(in_filepath, ".*")
20
+ @in_format = $2
21
+ else
22
+ puts "The filename does not exist or not have a valid format"
23
+ exit
24
+ end
25
+
26
+ if /([^\/]+)\.([^\.]+)\z/ =~ out_filepath
27
+ @out_filepath = out_filepath
28
+ @out_format = $2
29
+ @outdir = File.dirname(out_filepath)
30
+ else
31
+ puts "The filename does not have a valid format"
32
+ exit
33
+ end
34
+
35
+ unless File.exists?(@outdir) && File::ftype(@outdir) == "directory"
36
+ puts "The output directory does not exist"
37
+ exit
38
+ end
39
+
40
+ unless ["wav", "mp3"].index @out_format
41
+ puts "The output format specified is not available"
42
+ exit
43
+ end
44
+ end
45
+
46
+ def execute(speed = 1, silence = 0)
47
+ @silence = silence
48
+ @speed = speed
49
+ temp_wav_original = @outdir + "/" + @basename + "-temp-original.wav"
50
+ temp_wav_result = @outdir + "/" + @basename + "-temp-result.wav"
51
+ if @in_format != "wav" # @in_format == "mp3"
52
+ convert_to_wav(@in_filepath, temp_wav_original)
53
+ elsif @in_format == "wav"
54
+ `cp #{@in_filepath} #{temp_wav_original}`
55
+ end
56
+
57
+ apply_sox_to_wav(temp_wav_original, temp_wav_result)
58
+ # split_wav(temp_wav_original)
59
+ # merge_wav(temp_wav_result)
60
+
61
+ if @out_format == "mp3"
62
+ convert_to_mp3(temp_wav_result, @out_filepath)
63
+ `rm #{temp_wav_original} #{temp_wav_result}`
64
+ elsif @out_format == "wav"
65
+ `mv #{temp_wav_result} #{@out_filepath}; rm #{temp_wav_original}`
66
+ end
67
+ end
68
+
69
+ # def split_wav(in_filepath)
70
+ # puts "Splitting WAV to segments"
71
+ # result = `#{@sox} #{in_filepath} #{@outdir}/split-.wav silence 0 1 0.3 -32d : newfile : restart`
72
+ # end
73
+ #
74
+ # def merge_wav(out_filepath)
75
+ # temp_filepath = @outdir + "/temp.wav"
76
+ # files = []
77
+ # Dir.foreach(@outdir) do |file|
78
+ # next unless /^split\-\d+\.wav$/ =~ file
79
+ # files << @outdir + "/" + file
80
+ # end
81
+ # index = 0
82
+ # puts "Merging segments back to one WAV file"
83
+ # bar = ProgressBar.new(@basename, files.size)
84
+ # files.sort.each do |filepath|
85
+ # length = `#{@soxi} -D #{filepath}`.to_f
86
+ # num_seconds = @silence == "auto" ? length.to_i + 1 : @silence.to_i
87
+ # index += 1
88
+ # bar.inc(1)
89
+ # if index == 1
90
+ # File.rename(filepath, out_filepath)
91
+ # next
92
+ # end
93
+ #
94
+ # if length == 0 or @silence.to_f == 0
95
+ # `#{@sox} #{out_filepath} #{filepath} #{temp_filepath} ; mv #{temp_filepath} #{out_filepath} ; rm #{filepath}`
96
+ # else
97
+ # if @silence == "auto"
98
+ # silence_length = length
99
+ # elsif @speed and @speed != 1
100
+ # silence_length = @silence.to_f / @speed * 1
101
+ # else
102
+ # silence_length = @silence
103
+ # end
104
+ # `#{@sox} #{out_filepath} -p pad 0 #{silence_length} | #{@sox} - #{filepath} #{temp_filepath} ; mv #{temp_filepath} #{out_filepath} ; rm #{filepath}`
105
+ # end
106
+ # end
107
+ # print "\n"
108
+ # puts "Changing speed of the resulting WAV"
109
+ # if @speed and @speed.to_f != 1.0
110
+ # `#{@sox} #{out_filepath} #{temp_filepath} tempo -s #{@speed}`
111
+ # `mv #{temp_filepath} #{out_filepath}`
112
+ # end
113
+ # end
114
+
115
+ def apply_sox_to_wav(in_filepath, out_filepath)
116
+ puts "Applying SoX functions to WAV (this might take a few minutes or more)"
117
+ # silence = "silence 0 1 0.3 -32d"
118
+ # silence = "silence 1 0.005 -32d 1 0.3 -32d"
119
+ silence = "silence 1 0.01 1% 1 0.3 1%"
120
+ if @speed and @speed != 1 and @silence and @silence != 0 #speed and silence
121
+ speed = "tempo -s #{@speed}"
122
+ pad = "pad 0 #{@silence.to_f * @speed}"
123
+ `#{@sox} #{in_filepath} -p #{silence} #{pad} : restart | #{@sox} - -p | #{@sox} - #{out_filepath} #{speed}`
124
+ <<<<<<< HEAD
125
+ elsif @speed and @speed != 1 #only speed
126
+ speed = "tempo -s #{@speed}"
127
+ `#{@sox} #{in_filepath} #{out_filepath} #{speed}`
128
+ elsif @silence and @silence != 0 #only silence
129
+ pad = "pad 0 #{@silence.to_f}"
130
+ `#{@sox} #{in_filepath} -p #{silence} #{pad} : restart | #{@sox} - #{out_filepath} `
131
+ else #nothing changed
132
+ =======
133
+ elsif @silence and @silence != 0
134
+ pad = "pad 0 #{@silence.to_f}"
135
+ `#{@sox} #{in_filepath} -p #{silence} #{pad} : restart | #{@sox} - #{out_filepath} `
136
+ else
137
+ >>>>>>> origin/master
138
+ `cp #{in_filepath} #{out_filepath}`
139
+ end
140
+ end
141
+
142
+ def convert_to_wav(in_filepath, out_filepath)
143
+ basename = File.basename(in_filepath )
144
+ puts "Converting to WAV: #{basename}"
145
+ `#{@sox} #{in_filepath} #{out_filepath}`
146
+ end
147
+
148
+ def convert_to_mp3(in_filepath, out_filepath)
149
+ basename = File.basename(out_filepath)
150
+ puts "Converting WAV to MP3: #{basename}"
151
+ `#{@sox} #{in_filepath} #{out_filepath}`
152
+ end
153
+
154
+ def check_command(command)
155
+ basename = File.basename(command)
156
+ path = ""
157
+ print "Checking #{basename} command: "
158
+ if open("| which #{command} 2>/dev/null"){ |f| path = f.gets }
159
+ puts "detected at #{path}"
160
+ return path.strip
161
+ elsif open("| which #{basename} 2>/dev/null"){ |f| path = f.gets }
162
+ puts "detected at #{path}"
163
+ return path.strip
164
+ else
165
+ puts "not installed to the system"
166
+ exit
167
+ end
168
+ end
169
+
170
+ end # of class
171
+ end # of module
@@ -1,3 +1,3 @@
1
1
  module SpeakSlow
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,7 @@
1
+ module SpeakSlow
2
+ <<<<<<< HEAD
3
+ VERSION = "0.0.3"
4
+ =======
5
+ VERSION = "0.0.2"
6
+ >>>>>>> origin/master
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: speak_slow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-24 00:00:00.000000000 Z
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -77,7 +77,9 @@ files:
77
77
  - data/MLK_Dream.mp3
78
78
  - data/MLK_Dream.wav
79
79
  - lib/speak_slow.rb
80
+ - lib/speak_slow.rb.orig
80
81
  - lib/speak_slow/version.rb
82
+ - lib/speak_slow/version.rb.orig
81
83
  - speak_slow.gemspec
82
84
  - test/speak_slow_test.rb
83
85
  homepage: http://github.com/yohasebe/speak_slow