carrierwave-audio-waveform 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9b978937b699c8840ed808da400cc75406b850c7
4
- data.tar.gz: e8ea1146faf2a37a1fcdb7c98172c1a241008bfc
3
+ metadata.gz: 6497146ba653815208e4665a1ed1bfad567a9f1b
4
+ data.tar.gz: c727ed3c1b3b5575a026fc42fa6d3635640e2ceb
5
5
  SHA512:
6
- metadata.gz: 763f2a8d2b40333712f256cf8f8cac5e2a75559a30d24857a3075f1f8cb30f5ee1ac825b97d719b8d7bf46f09542fcf1432ba67f2d34a327a1ccf75062e5ff42
7
- data.tar.gz: 377f117014e72cc62fb6c3dafb1a1882a77ce43100b548c10690da7c2f6f79c6564b5f80ca95c4f34503f6df2cf51f1afbb718e30ea7d8c956e2b12de2a116c7
6
+ metadata.gz: f475378005b85801917e5083709056a665e59441b36712b812956287947301bd70f81d671cd7e35adc68fe3ea978cefe02a6cf54b7abb2cdac02800d5bffa976
7
+ data.tar.gz: a26fd903358e1978545d4754524ed3f29f495e3eceac6c5d49d61556024963c035b7a13ca227609c9107c50bee4736fd7069bf8418191b49b9b8b254e77c6f75
@@ -21,7 +21,12 @@ module CarrierWave
21
21
 
22
22
  image_filename = Waveformer.generate(current_path, options)
23
23
  File.rename image_filename, current_path
24
- self.file.instance_variable_set(:@content_type, "image/png")
24
+
25
+ if options[:type] == :svg
26
+ self.file.instance_variable_set(:@content_type, "image/svg+xml")
27
+ else
28
+ self.file.instance_variable_set(:@content_type, "image/png")
29
+ end
25
30
  end
26
31
 
27
32
  def waveform_data options={}
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module AudioWaveform
3
- VERSION = '1.0.3'
3
+ VERSION = '1.0.4'
4
4
  end
5
5
  end
@@ -12,7 +12,8 @@ module CarrierWave
12
12
  :height => 280,
13
13
  :background_color => "#666666",
14
14
  :color => "#00ccff",
15
- :logger => nil
15
+ :logger => nil,
16
+ :type => :png
16
17
  }
17
18
 
18
19
  TransparencyMask = "#00ff00"
@@ -80,7 +81,7 @@ module CarrierWave
80
81
  #
81
82
  def generate(source, options={})
82
83
  options = DefaultOptions.merge(options)
83
- filename = options[:filename] || self.generate_png_filename(source)
84
+ filename = options[:filename] || self.generate_image_filename(source, options[:type])
84
85
 
85
86
  raise ArgumentError.new("No source audio filename given, must be an existing sound file.") unless source
86
87
  raise ArgumentError.new("No destination filename given for waveform") unless filename
@@ -117,7 +118,14 @@ module CarrierWave
117
118
  end
118
119
 
119
120
  image = draw samples, options
120
- image.save filename
121
+
122
+ if options[:type] == :svg
123
+ File.open(filename, 'w') do |f|
124
+ f.puts image
125
+ end
126
+ else
127
+ image.save filename
128
+ end
121
129
  end
122
130
 
123
131
  if source != old_source
@@ -130,10 +138,15 @@ module CarrierWave
130
138
  filename
131
139
  end
132
140
 
133
- def generate_png_filename(source)
141
+ def generate_image_filename(source, image_type)
134
142
  ext = File.extname(source)
135
143
  source_file_path_without_extension = File.join File.dirname(source), File.basename(source, ext)
136
- "#{source_file_path_without_extension}.png"
144
+
145
+ if image_type == :svg
146
+ "#{source_file_path_without_extension}.svg"
147
+ else
148
+ "#{source_file_path_without_extension}.png"
149
+ end
137
150
  end
138
151
 
139
152
  private
@@ -189,8 +202,71 @@ module CarrierWave
189
202
  raise RuntimeError.new("Source audio file #{source} could not be read by RubyAudio library -- Hint: non-WAV files are no longer supported, convert to WAV first using something like ffmpeg (RubyAudio: #{e.message})")
190
203
  end
191
204
 
192
- # Draws the given samples using the given options, returns a ChunkyPNG::Image.
193
205
  def draw(samples, options)
206
+ if options[:type] == :svg
207
+ draw_svg(samples, options)
208
+ else
209
+ draw_png(samples, options)
210
+ end
211
+ end
212
+
213
+ def draw_svg(samples, options)
214
+ image = "<svg viewbox=\"0 0 #{options[:width]} #{options[:height]}\" preserveAspectRatio=\"none\" width=\"100%\" height=\"100%\">"
215
+ if options[:hide_style].nil?
216
+ image+= "<style>"
217
+ image+= "svg {"
218
+ image+= "stroke: #000;"
219
+ image+= "stroke-width: 1;"
220
+ image+= "}"
221
+ image+= "use.waveform-progress {"
222
+ image+= "stroke-width: 2;"
223
+ image+= "clip-path: polygon(0% 0%, 0% 0%, 0% 100%, 0% 100%);"
224
+ image+= "}"
225
+ image+= "svg path {"
226
+ image+= "stroke: inherit;"
227
+ image+= "stroke-width: inherit;"
228
+ image+= "}"
229
+ image+= "</style>"
230
+ end
231
+ image+= "<defs>"
232
+ if options[:gradient]
233
+ options[:gradient].each_with_index do |grad, id|
234
+ image+= "<linearGradient id=\"linear#{id}\" x1=\"0%\" y1=\"0%\" x2=\"100%\" y2=\"0%\">"
235
+ image+= "<stop offset=\"0%\" stop-color=\"#{grad[0]}\"/>"
236
+ image+= "<stop offset=\"100%\" stop-color=\"#{grad[1]}\"/>"
237
+ image+= "</linearGradient>"
238
+ end
239
+ end
240
+ uniqueWaveformID = "waveform-#{SecureRandom.uuid}"
241
+ image+= "<g id=\"#{uniqueWaveformID}\">"
242
+ image+= '<g transform="translate(0, 125.0)">'
243
+ image+= '<path stroke="currrentColor" d="'
244
+
245
+ samples = spaced_samples(samples, options[:sample_width], options[:gap_width]) if options[:sample_width]
246
+ max = samples.reject {|v| v.nil? }.max
247
+ height_factor = (options[:height] / 2.0) / max
248
+
249
+ samples.each_with_index do |sample, pos|
250
+ next if sample.nil?
251
+
252
+ amplitude = sample * height_factor
253
+ top = (0 - amplitude).round
254
+ bottom = (0 + amplitude).round
255
+
256
+ image+= " M#{pos},#{top} V#{bottom}"
257
+ end
258
+
259
+ image+= '"/>'
260
+ image+= "</g>"
261
+ image+= "</g>"
262
+ image+= "</defs>"
263
+ image+= "<use class=\"waveform-base\" href=\"##{uniqueWaveformID}\" />"
264
+ image+= "<use class=\"waveform-progress\" href=\"##{uniqueWaveformID}\" />"
265
+ image+= "</svg>"
266
+ end
267
+
268
+ # Draws the given samples using the given options, returns a ChunkyPNG::Image.
269
+ def draw_png(samples, options)
194
270
  image = ChunkyPNG::Image.new(options[:width], options[:height],
195
271
  options[:background_color] == :transparent ? ChunkyPNG::Color::TRANSPARENT : options[:background_color]
196
272
  )
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave-audio-waveform
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trevor Hinesley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-20 00:00:00.000000000 Z
11
+ date: 2018-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carrierwave
@@ -147,4 +147,3 @@ signing_key:
147
147
  specification_version: 4
148
148
  summary: Generate waveform images from audio files within Carrierwave
149
149
  test_files: []
150
- has_rdoc: