Skip to content
Browse files

adding a track object

  • Loading branch information...
1 parent 36de4bc commit 8c1d08a778373513cce31498e2ca02c4dd89da79 @tenderlove committed
Showing with 35 additions and 5 deletions.
  1. +25 −5 lib/earworm.rb
  2. +10 −0 lib/earworm/track.rb
View
30 lib/earworm.rb
@@ -1,6 +1,7 @@
require 'icanhasaudio'
require 'tempfile'
require 'earworm_lib'
+require 'earworm/track'
require 'rexml/document'
require 'rexml/parsers/pullparser'
@@ -48,12 +49,22 @@ def identify(options = {})
}
end
xml = Net::HTTP.post_form(URI.parse(URL), post_opts).body
- puts xml
- require 'pp'
parser = REXML::Parsers::PullParser.new(xml)
+ track = Track.new
while parser.has_next?
- pp parser.pull
+ 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)
@@ -68,6 +79,8 @@ def fingerprint_to_hash(filename)
tmpfile = case filename
when /mp3$/
decode_mp3(filename)
+ when /wav$/
+ filename
end
File.open(tmpfile, 'rb') { |f|
return fingerprint_io(f)
@@ -91,10 +104,17 @@ def fingerprint_io(io)
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)
+ 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,
- bytes_in_seconds/2,
+ read_bytes/2,
info[:in_samplerate], 1)
info
end
View
10 lib/earworm/track.rb
@@ -0,0 +1,10 @@
+class Earworm
+ class Track
+ attr_accessor :title, :artist_name, :puid_list
+ def initialize
+ @title = nil
+ @artist_name = nil
+ @puid_list = []
+ end
+ end
+end

0 comments on commit 8c1d08a

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