Skip to content
Browse files

adding a client and fingerprint class

  • Loading branch information...
1 parent 491aaeb commit 4b2a34368ebfc6892b02064596aba09f6293df5a @tenderlove tenderlove committed Aug 2, 2008
Showing with 121 additions and 102 deletions.
  1. +3 −99 lib/earworm.rb
  2. +39 −0 lib/earworm/client.rb
  3. +76 −0 lib/earworm/fingerprint.rb
  4. +1 −1 lib/earworm/track.rb
  5. +2 −2 test/test_earworm.rb
View
102 lib/earworm.rb
@@ -2,13 +2,14 @@
require 'tempfile'
require 'earworm_lib'
require 'earworm/track'
+require 'earworm/fingerprint'
+require 'earworm/client'
require 'rexml/document'
require 'rexml/parsers/pullparser'
-class Earworm
+module Earworm
VERSION = '0.0.1'
URL = 'http://ofa.musicdns.org/ofa/1/track'
-
class << self
def ofa_version
major = DL.malloc(DL.sizeof('I'))
@@ -21,101 +22,4 @@ def ofa_version
"#{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 = Net::HTTP.post_form(URI.parse(URL), post_opts).body
- parser = REXML::Parsers::PullParser.new(xml)
- track = Track.new
- while parser.has_next?
- thing = parser.pull
- if thing.start_element?
- case thing[0]
- when 'title'
- track.title = parser.pull[0]
- when 'name'
- track.artist_name = parser.pull[0]
- when 'puid'
- track.puid_list << thing[1]['id']
- end
- end
- end
- track
- 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)
- when /wav$/
- 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]
- read_bytes =
- if info[:bytes_in_seconds] > bytes_in_seconds
- bytes_in_seconds
- else
- info[:bytes_in_seconds]
- end
-
- data = io.read(read_bytes)
- info[:fpt] = EarwormLib.ofa_create_print( data,
- 0,
- read_bytes/2,
- info[:in_samplerate], 1)
- info
- end
end
View
39 lib/earworm/client.rb
@@ -0,0 +1,39 @@
+module Earworm
+ class Client
+ attr_accessor :client_id
+ def initialize(client_id)
+ @client_id = client_id
+ end
+
+ def identify(options = {})
+ post_opts = nil
+ if options.key?(:file)
+ post_opts = {
+ 'cid' => @client_id,
+ 'cvr' => 'Example 0.9.3',
+ }.merge(Fingerprint.new(options[:file]).to_hash)
+ end
+ xml = Net::HTTP.post_form(URI.parse(URL), post_opts).body
+ parser = REXML::Parsers::PullParser.new(xml)
+ track = Track.new
+ while parser.has_next?
+ thing = parser.pull
+ if thing.start_element?
+ case thing[0]
+ when 'title'
+ track.title = parser.pull[0]
+ when 'name'
+ track.artist_name = parser.pull[0]
+ when 'puid'
+ track.puid_list << thing[1]['id']
+ end
+ end
+ end
+ track
+ end
+
+ def fingerprint(filename)
+ Fingerprint.new(filename).to_s
+ end
+ end
+end
View
76 lib/earworm/fingerprint.rb
@@ -0,0 +1,76 @@
+module Earworm
+ class Fingerprint
+ def initialize(thing)
+ @thing = thing
+ @hash = nil
+ end
+
+ def to_hash
+ return @hash if @hash
+ info = nil
+ if @thing.is_a?(IO)
+ info = wav_info_for(@thing)
+ else
+ tmpfile = case @thing
+ when /mp3$/
+ decode_mp3(@thing)
+ when /wav$/
+ @thing
+ end
+ File.open(tmpfile, 'rb') { |f|
+ info = wav_info_for(f)
+ }
+ end
+ @hash = {
+ 'fpt' => info[:fpt],
+ 'art' => 'unknown',
+ 'ttl' => 'unknown',
+ 'alb' => 'unknown',
+ 'tnm' => 0,
+ 'gnr' => 'unknown',
+ 'yrr' => 0,
+ 'brt' => 0,
+ 'fmt' => 'wav',
+ 'dur' => info[:milliseconds],
+ 'rmd' => 1,
+ 'enc' => '',
+ }
+ end
+
+ def to_s
+ to_hash['fpt']
+ end
+
+ private
+ def wav_info_for(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]
+ read_bytes =
+ if info[:bytes_in_seconds] > bytes_in_seconds
+ bytes_in_seconds
+ else
+ info[:bytes_in_seconds]
+ end
+
+ data = io.read(read_bytes)
+ info[:fpt] = EarwormLib.ofa_create_print( data,
+ 0,
+ read_bytes/2,
+ info[:in_samplerate], 1)
+ info
+ 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
+ end
+end
View
2 lib/earworm/track.rb
@@ -1,4 +1,4 @@
-class Earworm
+module Earworm
class Track
attr_accessor :title, :artist_name, :puid_list
def initialize
View
4 test/test_earworm.rb
@@ -19,13 +19,13 @@ def test_ofa_version
end
def test_fingerprint
- ew = Earworm.new('123')
+ ew = Earworm::Client.new('123')
key = ew.fingerprint(@mp3)
assert key
end
def test_identify_file
- ew = Earworm.new(@key)
+ ew = Earworm::Client.new(@key)
info = ew.identify(:file => @mp3)
assert info
assert info.artist_name

0 comments on commit 4b2a343

Please sign in to comment.
Something went wrong with that request. Please try again.