Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

inital checkin

  • Loading branch information...
commit 68461e2704794afe7f211b5883d01b99c9a436cf 0 parents
Aaron Patterson tenderlove authored
1  .gitignore
@@ -0,0 +1 @@
+.*.swp
6 History.txt
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2008-08-01
+
+* 1 major enhancement
+
+ * Birthday!
+
7 Manifest.txt
@@ -0,0 +1,7 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+bin/earworm
+lib/earworm.rb
+test/test_earworm.rb
48 README.txt
@@ -0,0 +1,48 @@
+= earworm
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 FIX
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 Rakefile
@@ -0,0 +1,13 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
+require 'earworm.rb'
+
+Hoe.new('earworm', Earworm::VERSION) do |p|
+ p.developer('Aaron Patterson', 'aaronp@rubyforge.org')
+end
+
+# vim: syntax=Ruby
0  bin/earworm
No changes.
98 lib/earworm.rb
@@ -0,0 +1,98 @@
+require 'icanhasaudio'
+require 'tempfile'
+require 'earworm_lib'
+require 'rexml/document'
+require 'rexml/parsers'
+
+class Earworm
+ VERSION = '0.0.1'
+ URL = 'http://ofa.musicdns.org/ofa/1/track'
+
+ class << self
+ def ofa_version
+ major = DL.malloc(DL.sizeof('I'))
+ minor = DL.malloc(DL.sizeof('I'))
+ rev = DL.malloc(DL.sizeof('I'))
+ major.struct!('I', 'val')
+ minor.struct!('I', 'val')
+ rev.struct!('I', 'val')
+ EarwormLib.ofa_get_version(major, minor, rev)
+ "#{major['val']}.#{minor['val']}.#{rev['val']}"
+ end
+ end
+
+ attr_accessor :client_id
+ def initialize(client_id)
+ @client_id = client_id
+ end
+
+ def identify(options = {})
+ post_opts = nil
+ if options.key?(:file)
+ fpt = fingerprint_to_hash(options[:file])
+ post_opts = {
+ 'cid' => @client_id,
+ 'cvr' => 'Example 0.9.3',
+ 'fpt' => fpt[:fpt],
+ 'art' => 'unknown',
+ 'ttl' => 'unknown',
+ 'alb' => 'unknown',
+ 'tnm' => 0,
+ 'gnr' => 'unknown',
+ 'yrr' => 0,
+ 'brt' => 0,
+ 'fmt' => 'wav',
+ 'dur' => fpt[:milliseconds],
+ 'rmd' => 1,
+ 'enc' => '',
+ }
+ end
+ xml = REXML::Document.new(
+ Net::HTTP.post_form(URI.parse(URL), post_opts).body
+ )
+ p xml
+ end
+
+ def fingerprint(filename)
+ fingerprint_to_hash(filename)[:fpt]
+ end
+
+ private
+ def fingerprint_to_hash(filename)
+ if filename.is_a?(IO)
+ return fingerprint_io(f)
+ else
+ tmpfile = case filename
+ when /mp3$/
+ decode_mp3(filename)
+ end
+ File.open(tmpfile, 'rb') { |f|
+ return fingerprint_io(f)
+ }
+ end
+ end
+
+ def decode_mp3(filename)
+ reader = Audio::MPEG::Decoder.new
+ name = File.join(Dir::tmpdir, "#{File.basename(filename, '.mp3')}.wav")
+ File.open(filename, 'rb') { |input|
+ File.open(name, 'wb') { |tmpfile|
+ reader.decode(input, tmpfile)
+ }
+ }
+ name
+ end
+
+ def fingerprint_io(io)
+ header = io.read(4).unpack('N').first
+ raise unless header == Audio::MPEG::Encoder::WAV_ID_RIFF
+ info = Audio::MPEG::Encoder.parse_wave_header(io)
+ bytes_in_seconds = 135 * info[:in_samplerate] * 2 * info[:num_channels]
+ data = io.read(bytes_in_seconds)
+ info[:fpt] = EarwormLib.ofa_create_print( data,
+ 0,
+ bytes_in_seconds/2,
+ info[:in_samplerate], 1)
+ info
+ end
+end
7 lib/earworm_lib.rb
@@ -0,0 +1,7 @@
+require 'dl/import'
+module EarwormLib # :nodoc:
+ extend DL::Importable
+ dlload('/opt/local/lib/libofa.dylib')
+ extern "void ofa_get_version(int *, int *, int *)"
+ extern "const char * ofa_create_print(const char *, int, long, int, int)"
+end
20 test/test_earworm.rb
@@ -0,0 +1,20 @@
+require 'rubygems'
+require 'test/unit'
+require 'earworm'
+
+class EarwormTest < Test::Unit::TestCase
+ def test_ofa_version
+ assert Earworm.ofa_version
+ end
+
+ def test_fingerprint
+ ew = Earworm.new('123')
+ key = ew.fingerprint('/tmp/kl.mp3')
+ assert key
+ end
+
+ def test_identify
+ ew = Earworm.new('a7f6063296c0f1c9b75c7f511861b89b')
+ assert ew.identify(:file => '/tmp/kl.mp3')
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.