Module: MrEko

Defined in:
lib/mr_eko.rb,
lib/mr_eko/core.rb,
lib/mr_eko/exceptions.rb

Defined Under Namespace

Modules: Core, Presets Classes: EnmfpError, InvalidAttributes, NoSongsError, Playlist, PlaylistEntry, Song, TagParser, TimedPlaylist

Constant Summary collapse

VERSION =
'0.6.1'
USER_DIR =
File.join(ENV['HOME'], ".mreko")
FINGERPRINTS_DIR =
File.join(USER_DIR, 'fingerprints')
LOG_DIR =
File.join(USER_DIR, 'logs')
HOME_DIR =
File.join(File.dirname(__FILE__), '..')
MODES =
%w(minor major).freeze
CHROMATIC_SCALE =
%w(C C# D D# E F F# G G# A A# B).freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

Returns the value of attribute logger.



35
36
37
# File 'lib/mr_eko.rb', line 35

def logger
  @logger
end

Class Method Details

.api_keyObject



89
90
91
92
93
94
# File 'lib/mr_eko.rb', line 89

def api_key
  [File.join(USER_DIR, 'echonest_api.key'), File.join(HOME_DIR, 'echonest_api.key')].each do |file|
    return file if File.exists?(file)
  end
  raise "You need to create an echonest_api.key file in #{USER_DIR}"
end

.connectionObject



41
42
43
# File 'lib/mr_eko.rb', line 41

def connection
  @connection
end

.db_nameObject



85
86
87
# File 'lib/mr_eko.rb', line 85

def db_name
  env == 'test' ? File.join('db', 'eko_test.db') : File.join(USER_DIR, 'eko.db')
end

.enmfp_binaryObject

Use the platform-specific binary.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mr_eko.rb', line 112

def enmfp_binary
  bin = case ruby_platform
  when /darwin/
    'codegen.Darwin'
  when /686/
    'codegen.Linux-i686'
  when /x86/
    'codegen.Linux-x86_64'
  else
    'codegen.windows.exe'
  end

  File.join(HOME_DIR, 'ext', 'enmfp', bin)
end

.envObject



37
38
39
# File 'lib/mr_eko.rb', line 37

def env
  EKO_ENV
end

.key_letter(key) ⇒ Object

Takes an integer and returns its standard (chromatic) representation.



107
108
109
# File 'lib/mr_eko.rb', line 107

def key_letter(key)
  CHROMATIC_SCALE[key]
end

.key_lookup(key_letter) ⇒ Object

Takes a chromatic key (eg: G#) and returns its integer representation.



102
103
104
# File 'lib/mr_eko.rb', line 102

def key_lookup(key_letter)
  CHROMATIC_SCALE.index(key_letter.upcase)
end

.md5(filename) ⇒ Object



49
50
51
# File 'lib/mr_eko.rb', line 49

def md5(filename)
  Digest::MD5.hexdigest(open(filename).read)
end

.mode_lookup(mode) ⇒ Object

Takes ‘minor’ or ‘major’ and returns its integer representation.



97
98
99
# File 'lib/mr_eko.rb', line 97

def mode_lookup(mode)
  MODES.index(mode.to_s.downcase)
end

.nestObject



45
46
47
# File 'lib/mr_eko.rb', line 45

def nest
  @nest
end

.ruby_platformObject



127
128
129
# File 'lib/mr_eko.rb', line 127

def ruby_platform
  RUBY_PLATFORM
end

.setup!Object



53
54
55
56
57
58
# File 'lib/mr_eko.rb', line 53

def setup!
  setup_directories!
  setup_logger!
  setup_db!
  setup_echonest!
end

.setup_db!Object



72
73
74
75
76
77
78
79
# File 'lib/mr_eko.rb', line 72

def setup_db!
  return @connection if @connection
  @connection = Sequel.sqlite(db_name)
  @connection.loggers << @logger

  File.unlink(MrEko.db_name) if File.exist?(MrEko.db_name) && env == 'test'
  Sequel::Migrator.apply(MrEko.connection, File.join(File.dirname(__FILE__), "..", "db", "migrate"))
end

.setup_directories!Object



66
67
68
69
70
# File 'lib/mr_eko.rb', line 66

def setup_directories!
  Dir.mkdir(USER_DIR) unless File.directory?(USER_DIR)
  Dir.mkdir(FINGERPRINTS_DIR) unless File.directory?(FINGERPRINTS_DIR)
  Dir.mkdir(LOG_DIR) unless File.directory?(LOG_DIR)
end

.setup_echonest!Object



81
82
83
# File 'lib/mr_eko.rb', line 81

def setup_echonest!
  @nest ||= Echonest(File.read(api_key))
end

.setup_logger!Object

Output to STDOUT in debug, otherwise, save to logfile



61
62
63
64
# File 'lib/mr_eko.rb', line 61

def setup_logger!
  out = ENV['DEBUG'] ? STDOUT : File.join(LOG_DIR, "#{env}.log")
  @logger ||= Logger.new(out)
end