Skip to content

Commit

Permalink
#7 seems to be OK. Menu items and will be beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Himura2la committed Jul 23, 2017
1 parent c04a1df commit 1a04fd8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
51 changes: 35 additions & 16 deletions background_music_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import wx
import wx.grid

from constants import Colors


class BackgroundMusicPlayer(object):
def __init__(self, parent):
Expand All @@ -18,7 +20,7 @@ def __init__(self, parent):
self.player.audio_set_mute(False)
self.window = BackgroundMusicFrame(self.parent) # None
self.playlist = None
self._current_track_i = -1
self.current_track_i = -1
self.fade_in_out = True

def window_exists(self):
Expand Down Expand Up @@ -50,27 +52,43 @@ def load_playlist_to_grid(self):
self.window.grid.AutoSize()
self.window.Layout()
self.window.play_btn.Enable(True)
if self.parent.bg_player.player.get_state() in range(5): # If playing
self.window.grid.SetCellBackgroundColour(self.current_track_i, 0, Colors.DUP_ROW)

def select_track(self):
def select_track(self, select_next=False):
if not self.playlist:
return
if self.window_exists():
self._current_track_i = self.window.grid.GetSelectedRows()[0]
else:
self._current_track_i = (self._current_track_i + 1) % len(self.playlist)
return self._current_track_i
self.current_track_i = self.window.grid.GetSelectedRows()[0]
if not self.window_exists() or select_next:
self.current_track_i = (self.current_track_i + 1) % len(self.playlist)
return self.current_track_i

# TODO: Async!!!
def _fade(self, vol_range, delay):
for i in vol_range:
self.player.audio_set_volume(i)
vol_msg = 'Vol: %d' % self.player.audio_get_volume()
self.parent.bg_player_status('Fading in... ' + vol_msg)
time.sleep(delay)

def fade_in(self, delay):
self._fade(range(0, self.volume + 1, 1), delay)

def fade_out(self, delay):
self._fade(range(self.volume, 0, -1), delay)

def play(self):
if not self.playlist:
return

self.player.set_media(self.vlc_instance.media_new(self.playlist[self.select_track()]['path']))
self.player.set_media(self.vlc_instance.media_new(self.playlist[self.current_track_i]['path']))
if self.player.play() != 0: # [Play] button is pushed here!
return

state = self.player.get_state()
start = time.time()
while state != vlc.State.Playing:
while state != vlc.State.Playing: # TODO: Async!!!
state = self.player.get_state()
status = "%s [%fs]" % (self.parent.player_state_parse(state), (time.time() - start))
self.parent.bg_player_status(status)
Expand All @@ -80,22 +98,19 @@ def play(self):

if self.window_exists():
self.window.pause_btn.Enable(True)
self.window.grid.SetCellBackgroundColour(self.current_track_i, 0, Colors.DUP_ROW)

volume = 0 if self.fade_in_out else self.volume
start = time.time()
while self.player.audio_get_volume() != volume:
while self.player.audio_get_volume() != volume: # TODO: Async!!!
self.player.audio_set_mute(False)
self.player.audio_set_volume(volume)
status = "Trying to unmute... [%fs]" % (time.time() - start)
self.parent.bg_player_status(status)
time.sleep(0.002)

if self.fade_in_out:
for i in range(0, self.volume + 1, 1):
self.player.audio_set_volume(i)
vol_msg = 'Vol: %d' % self.player.audio_get_volume()
self.parent.bg_player_status('Fading in... ' + vol_msg)
time.sleep(0.05)
self.fade_in(0.05)

self.parent.bg_player_status("%s Vol:%d" %
(self.parent.player_state_parse(self.player.get_state()),
Expand All @@ -105,11 +120,14 @@ def pause(self, paused):
if not self.playlist:
return

if self.fade_in_out:
pass
if self.fade_in_out and paused:
self.fade_out(0.01)

self.player.set_pause(paused)

if self.fade_in_out and not paused:
self.fade_in(0.01)

if not paused:
self.parent.timer_start(self.timer_update_ms)

Expand Down Expand Up @@ -168,6 +186,7 @@ def __init__(self, parent):
self.grid.SetRowLabelSize(20)
self.grid.SetSelectionMode(wx.grid.Grid.wxGridSelectRows)


def select_row(e):
row = e.Row if hasattr(e, 'Row') else e.TopRow
self.grid.Unbind(wx.grid.EVT_GRID_RANGE_SELECT)
Expand Down
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ def fade_switched(self, e):
self.bg_player.window.fade_in_out_switch.SetValue(value)

def background_play(self, e=None):
self.bg_player.select_track()
self.bg_player.play()

def background_pause(self, e=None, paused=None):
Expand All @@ -655,14 +656,11 @@ def timer_start(self, val):
self.bg_player_timer.Stop()

def on_background_timer(self, e):

player = self.bg_player.player
length, time = player.get_length(), player.get_time()

time_remaining = '-%02d:%02d' % divmod(length / 1000 - time / 1000, 60)

print 'bg_timer', time

if self.bg_player.window_exists():
self.bg_player.window.time_slider.SetRange(0, length)
self.bg_player.window.time_slider.SetValue(time)
Expand All @@ -682,7 +680,9 @@ def on_background_timer(self, e):
if self.bg_player.window_exists():
self.bg_player.window.time_slider.SetValue(0)

# Switch to next
self.bg_player.window.grid.SetCellBackgroundColour(self.bg_player.current_track_i, 0,Colors.FILTERED_GRID)
self.bg_player.select_track(True)
self.background_play()

if __name__ == "__main__":
app = wx.App(False)
Expand Down

0 comments on commit 1a04fd8

Please sign in to comment.