Class: Soundcloud9000::Models::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/soundcloud9000/models/player.rb

Overview

responsible for drawing and updating the player above tracklist

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



10
11
12
13
14
15
16
17
18
19
# File 'lib/soundcloud9000/models/player.rb', line 10

def initialize
  @track = nil
  @events = Events.new
  @folder = File.expand_path('~/.soundcloud9000')
  @seek_speed = {}
  @seek_time = {}
  create_player

  Dir.mkdir(@folder) unless File.exist?(@folder)
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



8
9
10
# File 'lib/soundcloud9000/models/player.rb', line 8

def events
  @events
end

#trackObject (readonly)

Returns the value of attribute track.



8
9
10
# File 'lib/soundcloud9000/models/player.rb', line 8

def track
  @track
end

Instance Method Details

#create_playerObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/soundcloud9000/models/player.rb', line 21

def create_player
  @player = Audite.new
  @player.events.on(:position_change) do
    events.trigger(:progress)
  end

  @player.events.on(:complete) do
    events.trigger(:complete)
  end
end

#download_progressObject



85
86
87
# File 'lib/soundcloud9000/models/player.rb', line 85

def download_progress
  @download ? @download.progress / @download.total.to_f : 1
end

#durationObject



43
44
45
# File 'lib/soundcloud9000/models/player.rb', line 43

def duration
  @track.duration.to_f / 1000
end

#forwardObject



122
123
124
125
126
127
# File 'lib/soundcloud9000/models/player.rb', line 122

def forward
  seconds = seek_speed(:forward)

  seek_percentage = (seconds + seconds_played) / duration
  @player.forward(seconds) if seek_percentage < download_progress
end

#length_in_secondsObject



51
52
53
54
# File 'lib/soundcloud9000/models/player.rb', line 51

def length_in_seconds
  mpg = Mpg123.new(@file)
  mpg.length * mpg.tpf / mpg.spf
end

#levelObject



77
78
79
# File 'lib/soundcloud9000/models/player.rb', line 77

def level
  @player.level
end

#load(track, location) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/soundcloud9000/models/player.rb', line 56

def load(track, location)
  @file = "#{@folder}/#{track.id}.mp3"

  if !File.exist?(@file) || track.duration / 1000 > length_in_seconds * 0.95
    begin
      File.unlink(@file)
    rescue StandardError
      nil
    end
    @download = DownloadThread.new(location, @file)
  else
    @download = nil
  end

  @player.load(@file)
end

#log(*args) ⇒ Object



73
74
75
# File 'lib/soundcloud9000/models/player.rb', line 73

def log(*args)
  Soundcloud9000::Application.logger.debug 'Player: ' + args.join(' ')
end

#play(track, location) ⇒ Object



32
33
34
35
36
37
# File 'lib/soundcloud9000/models/player.rb', line 32

def play(track, location)
  log :play, track.id
  @track = track
  load(track, location)
  start
end

#play_progressObject



39
40
41
# File 'lib/soundcloud9000/models/player.rb', line 39

def play_progress
  seconds_played / duration
end

#playing?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/soundcloud9000/models/player.rb', line 89

def playing?
  @player.active
end

#rewindObject



118
119
120
# File 'lib/soundcloud9000/models/player.rb', line 118

def rewind
  @player.rewind(seek_speed(:rewind))
end

#seconds_playedObject



81
82
83
# File 'lib/soundcloud9000/models/player.rb', line 81

def seconds_played
  @player.position
end

#seek_position(position) ⇒ Object

change song position



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/soundcloud9000/models/player.rb', line 105

def seek_position(position)
  position *= 0.1
  relative_position = position * duration
  if relative_position < seconds_played
    difference = seconds_played - relative_position
    @player.rewind(difference)
  elsif download_progress > (relative_position / duration) && relative_position > seconds_played
    log download_progress
    difference = relative_position - seconds_played
    @player.forward(difference)
  end
end

#seek_speed(direction) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/soundcloud9000/models/player.rb', line 93

def seek_speed(direction)
  if @seek_time[direction] && Time.now - @seek_time[direction] < 0.5
    @seek_speed[direction] *= 1.05
  else
    @seek_speed[direction] = 1
  end

  @seek_time[direction] = Time.now
  @seek_speed[direction]
end

#startObject



133
134
135
# File 'lib/soundcloud9000/models/player.rb', line 133

def start
  @player.start_stream
end

#stopObject



129
130
131
# File 'lib/soundcloud9000/models/player.rb', line 129

def stop
  @player.stop_stream
end

#titleObject



47
48
49
# File 'lib/soundcloud9000/models/player.rb', line 47

def title
  [@track.title, @track.user.username].join(' - ')
end

#toggleObject



137
138
139
# File 'lib/soundcloud9000/models/player.rb', line 137

def toggle
  @player.toggle
end