Sha256: 99a05f99e321b44d152eb31fab0195525563189a13f5c80093dea35b1c80f081

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'scissor'
require 'echonest'
require 'scissor/echonest/chunk_ext.rb'

module Scissor
  def self.echonest_api_key=(echonest_api_key)
    Scissor::Chunk.echonest_api_key = echonest_api_key
  end

  class Chunk
    class << self
      attr_accessor :echonest_api_key

      def echonest
        @echonest ||= Echonest(echonest_api_key)
      end
    end

    def beats
      tempfile_for_echonest do |tmpfile|
        chunks = []
        scissor = to_file(tmpfile, :bitrate => '64k')

        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
        end

        beats.inject do |m, beat|
          chunk = self[m.start, beat.start - m.start]
          chunk.set_delegate(m)
          chunks << chunk
          beat
        end

        chunks
      end
    end

    def segments
      tempfile_for_echonest do |tmpfile|
        scissor = to_file(tmpfile, :bitrate => '64k')

        segments = self.class.echonest.get_segments(tmpfile)
        segments.inject([]) do |chunks, segment|
          chunk = self[segment.start, segment.duration]
          chunk.set_delegate(segment)
          chunks << chunk
          chunks
        end
      end
    end

    private

    def tempfile_for_echonest
      tmpfile = Pathname.new('/tmp/scissor_echonest_temp_' + $$.to_s + '.mp3')
      yield tmpfile
    ensure
      tmpfile.unlink
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scissor-echonest-0.0.8 lib/scissor/echonest.rb