PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/mp3info/extension_modules.rb

http://github.com/moumar/ruby-mp3info
Ruby | 66 lines | 49 code | 7 blank | 10 comment | 8 complexity | 7fd65cbf1b377a7ecf5333ad4e138767 MD5 | raw file
  1. # coding:utf-8
  2. # License:: Ruby
  3. # Author:: Guillaume Pierronnet (mailto:guillaume.pierronnet@gmail.com)
  4. class Mp3Info
  5. module HashKeys #:nodoc:
  6. ### lets you specify hash["key"] as hash.key
  7. ### this came from CodingInRuby on RubyGarden
  8. ### http://www.rubygarden.org/ruby?CodingInRuby
  9. def method_missing(meth,*args)
  10. m = meth.id2name
  11. if /=$/ =~ m
  12. self[m.chop] = (args.length<2 ? args[0] : args)
  13. else
  14. self[m]
  15. end
  16. end
  17. end
  18. module Mp3FileMethods #:nodoc:
  19. def get32bits
  20. (getbyte << 24) + (getbyte << 16) + (getbyte << 8) + getbyte
  21. end
  22. def get_syncsafe
  23. (getbyte << 21) + (getbyte << 14) + (getbyte << 7) + getbyte
  24. end
  25. end
  26. class EncodingHelper #:nodoc:
  27. def self.convert_to(value, from, to)
  28. if to == "utf-16"
  29. ("\uFEFF" + value).encode("UTF-16LE") # Chab 01.apr.2012 : moved from big to little endian for more compatibility (Windows Media Player, older Quicktime..)
  30. else
  31. value.encode(to)
  32. end
  33. end
  34. def self.convert_from_iso_8859_1(value)
  35. value.force_encoding("iso-8859-1").encode("utf-8")
  36. end
  37. def self.ruby_18_encode(from, to, value)
  38. Iconv.iconv(to, from, value).first
  39. end
  40. def self.decode_utf16(out)
  41. # String#bytes is not an array in Ruby 1.9
  42. bytes = out.bytes.to_a
  43. if out.length >= 2 and bytes[0] == 0xff and bytes[1] == 0xfe
  44. tag_encoding = "UTF-16LE"
  45. first_valid = 1
  46. elsif out.length >= 2 and bytes[0] == 0xfe and bytes[1] == 0xff
  47. tag_encoding = "UTF-16BE"
  48. first_valid = 1
  49. else
  50. # ID3v2.3.0 section 3.3 mandates a BOM but some software
  51. # erroneously omits it so we have to guess. Since most of
  52. # the world is little endian we might as well go with that.
  53. tag_encoding = "UTF-16LE"
  54. first_valid = 0
  55. end
  56. out = out.dup.force_encoding(tag_encoding)[first_valid..-1]
  57. end
  58. end
  59. end