This repository has been archived by the owner on Aug 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
track.rb
172 lines (152 loc) · 4.7 KB
/
track.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# coding: utf-8
module Hallon
# Tracks are an essential part to the Spotify service. They
# are browsable entities that can also be played by streaming.
#
# @see http://developer.spotify.com/en/libspotify/docs/group__track.html
class Track < Base
extend Linkable
from_link :as_track_and_offset
to_link :from_track
# Overriden to use default parameter.
# @see #to_link
alias_method :_to_link, :to_link
# Create a Link to the current track and offset in seconds.
#
# @param [Float] offset offset into track in seconds
# @return [Hallon::Link]
def to_link(offset = offset)
_to_link (offset * 1000).to_i
end
# Offset into track in seconds this track was created with.
#
# @return [Rational]
attr_reader :offset
# Construct a new Track instance.
#
# @param [String, Link, FFI::Pointer] link
def initialize(link)
FFI::MemoryPointer.new(:int) do |ptr|
@pointer = Spotify::Pointer.new from_link(link, ptr), :track, true
@offset = Rational(ptr.read_int, 1000)
end
end
# Create a new local track.
#
# @param [String] title
# @param [String] artist
# @param [String] album
# @param [Integer] length
# @return [Track]
def self.local(title, artist, album = nil, length = nil)
track = Spotify.localtrack_create(artist, title, album || "", length || -1)
new(track)
end
# @note This’ll be an empty string unless the track is loaded.
# @return [String]
def name
Spotify.track_name(@pointer)
end
# Duration of the track in seconds.
#
# @note This’ll be `0` unless the track is loaded.
# @return [Rational]
def duration
Rational(Spotify.track_duration(@pointer), 1000)
end
# Track popularity, between 0 and 1.
#
# @note This’ll be `0` unless the track is loaded.
# @return [Rational]
def popularity
Rational(Spotify.track_popularity(@pointer), 100)
end
# Disc number this track appears in.
#
# @note This function is a bit special. See libspotify docs for details.
def disc
Spotify.track_disc(@pointer)
end
# Position of track on its’ disc.
#
# @note This function is a bit special. See libspotify docs for details.
def index
Spotify.track_index(@pointer)
end
# Retrieve track error status.
#
# @return [Symbol]
def status
Spotify.track_error(@pointer)
end
# True if the track is loaded
#
# @return [Boolean]
def loaded?
Spotify.track_is_loaded(@pointer)
end
# Album this track belongs to.
#
# @note This’ll be `nil` unless the track is loaded.
# @return [Hallon::Album]
def album
album = Spotify.track_album(@pointer)
Hallon::Album.new(album) unless album.null?
end
# Artist who performed this Track.
#
# @see #artists
# @note There may be more than one artist, see {artists} for retrieving them all!
# @return [Hallon::Artist, nil]
def artist
artists.first
end
# All {Artist}s who performed this Track.
#
# @note Track must be loaded, or all you’ll be getting is nil.
# @return [Hallon::Enumerator<Artist>]
def artists
Hallon::Enumerator.new(Spotify::track_num_artists(@pointer)) do |i|
artist = Spotify.track_artist(@pointer, i)
Hallon::Artist.new(artist) unless artist.null?
end
end
# True if the Track is available.
#
# @note This’ll always return false unless the track is loaded.
# @return [Boolean]
def available?(session = Session.instance)
Spotify.track_is_available(session.pointer, @pointer)
end
# True if the Track is a local track.
#
# @note This’ll always return false unless the track is loaded.
# @return [Boolean]
def local?(session = Session.instance)
Spotify.track_is_local(session.pointer, @pointer)
end
# True if the Track is autolinked.
#
# @note This’ll always return false unless the track is loaded.
# @return [Boolean]
def autolinked?(session = Session.instance)
Spotify.track_is_autolinked(session.pointer, @pointer)
end
# True if the track is starred.
#
# @note This’ll always return false unless the track is loaded.
# @return [Boolean]
def starred?(session = Session.instance)
Spotify.track_is_starred(session.pointer, @pointer)
end
# Set {#starred?} status of current track.
#
# @note It’ll set the starred status for the current Session.instance.
# @param [Boolean] starred
# @return [Boolean]
def starred=(starred)
session = Session.instance
starred ? session.star(self) : session.unstar(self)
end
end
end