lib/scissor/echonest.rb in scissor-echonest-0.0.8 vs lib/scissor/echonest.rb in scissor-echonest-0.1.0
- old
+ new
@@ -14,17 +14,27 @@
def echonest
@echonest ||= Echonest(echonest_api_key)
end
end
+ def bars
+ analyze do |analysis|
+ bars = analysis.bars
+ bars.inject([]) do |chunks, bar|
+ chunk = self[bar.start, bar.duration]
+ chunk.set_delegate(bar)
+ chunks << chunk
+ chunks
+ end
+ end
+ end
+
def beats
- tempfile_for_echonest do |tmpfile|
+ analyze do |analysis|
chunks = []
- scissor = to_file(tmpfile, :bitrate => '64k')
+ beats = analysis.beats
- beats = self.class.echonest.get_beats(tmpfile)
-
if beats.size != 0
chunk = self[0, beats.first.start]
beat = Beat.new(0.0, beats.first.start, 1.0)
chunk.set_delegate(beat)
chunks << chunk
@@ -40,14 +50,12 @@
chunks
end
end
def segments
- tempfile_for_echonest do |tmpfile|
- scissor = to_file(tmpfile, :bitrate => '64k')
-
- segments = self.class.echonest.get_segments(tmpfile)
+ analyze do |analysis|
+ segments = analysis.segments
segments.inject([]) do |chunks, segment|
chunk = self[segment.start, segment.duration]
chunk.set_delegate(segment)
chunks << chunk
chunks
@@ -55,13 +63,15 @@
end
end
private
- def tempfile_for_echonest
+ def analyze
tmpfile = Pathname.new('/tmp/scissor_echonest_temp_' + $$.to_s + '.mp3')
- yield tmpfile
+ scissor = to_file(tmpfile, :bitrate => '64k')
+
+ yield self.class.echonest.track.analysis(tmpfile)
ensure
- tmpfile.unlink
+ tmpfile.unlink if tmpfile.exist?
end
end
end