]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/music_file.py
Add config file option to the command line
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
CommitLineData
be27763f
IB
1import threading
2import pydub
3import pygame
87f211fb 4import math
98ff4305 5import time
60979de4 6from transitions.extensions import HierarchicalMachine as Machine
be27763f 7
60979de4 8class MusicFile(Machine):
87f211fb 9 def __init__(self, filename, lock, channel_id, name = None, gain = 1):
60979de4
IB
10 states = [
11 'initial',
12 'loading',
13 'failed',
14 { 'name': 'loaded', 'children': ['stopped', 'playing', 'paused'] }
15 ]
16 transitions = [
17 { 'trigger': 'load', 'source': 'initial', 'dest': 'loading'},
18 { 'trigger': 'fail', 'source': 'loading', 'dest': 'failed'},
19 { 'trigger': 'success', 'source': 'loading', 'dest': 'loaded_stopped'},
20 #{ 'trigger': 'play', 'source': 'loaded_stopped', 'dest': 'loaded_playing'},
21 #{ 'trigger': 'pause', 'source': 'loaded_playing', 'dest': 'loaded_paused'},
22 #{ 'trigger': 'stop', 'source': ['loaded_playing','loaded_paused'], 'dest': 'loaded_stopped'}
23 ]
24
25 Machine.__init__(self, states=states, transitions=transitions, initial='initial')
26
be27763f 27 self.filename = filename
d479af33 28 self.channel_id = channel_id
9de92b6d 29 self.name = name or filename
be27763f 30 self.raw_data = None
87f211fb 31 self.gain = gain
be27763f 32
9de92b6d 33 self.flag_paused = False
60979de4 34 threading.Thread(name = "MSMusicLoad", target = self.load, kwargs = {'lock': lock}).start()
be27763f 35
60979de4 36 def on_enter_loading(self, lock=None):
be27763f 37 lock.acquire()
60979de4
IB
38 try:
39 print("Loading « {} »".format(self.name))
40 volume_factor = 20 * math.log10(self.gain)
41 audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).apply_gain(volume_factor)
42 self.sound_duration = audio_segment.duration_seconds
43 self.raw_data = audio_segment.raw_data
44 except Exception as e:
45 print("failed to load « {} »: {}".format(self.name, e))
46 self.loading_error = e
47 self.fail()
48 else:
49 self.success()
50 print("Loaded « {} »".format(self.name))
51 finally:
52 lock.release()
53
54 def check_is_loaded(self):
55 return self.state.startswith('loaded_')
be27763f 56
0e5d59f7 57 def is_playing(self):
d479af33 58 return self.channel().get_busy()
0e5d59f7 59
9de92b6d
IB
60 def is_paused(self):
61 return self.flag_paused
62
98ff4305
IB
63 @property
64 def sound_position(self):
65 if self.is_playing() and not self.is_paused():
66 return min(time.time() - self.started_at, self.sound_duration)
67 elif self.is_playing() and self.is_paused():
68 return min(self.paused_at - self.started_at, self.sound_duration)
69 else:
70 return 0
71
0e5d59f7
IB
72 def play(self, fade_in = 0, volume = 100, start_at = 0):
73 self.set_volume(volume)
74
75 if start_at > 0:
0e5d59f7 76 raw_data_length = len(self.raw_data)
b58b8220 77 start_offset = int((raw_data_length / self.sound_duration) * start_at)
0e5d59f7 78 start_offset = start_offset - (start_offset % 2)
b58b8220 79 sound = pygame.mixer.Sound(self.raw_data[start_offset:])
0e5d59f7 80 else:
b58b8220 81 sound = pygame.mixer.Sound(self.raw_data)
0e5d59f7 82
98ff4305 83 self.started_at = time.time()
92cc4ce2 84 self.channel().play(sound, fade_ms = int(fade_in * 1000))
9de92b6d 85 self.flag_paused = False
be27763f
IB
86
87 def pause(self):
98ff4305 88 self.paused_at = time.time()
d479af33 89 self.channel().pause()
9de92b6d
IB
90 self.flag_paused = True
91
92 def unpause(self):
98ff4305 93 self.started_at += (time.time() - self.paused_at)
9de92b6d
IB
94 self.channel().unpause()
95 self.flag_paused = False
be27763f 96
0e5d59f7 97 def stop(self, fade_out = 0):
0e5d59f7 98 if fade_out > 0:
92cc4ce2 99 self.channel().fadeout(int(fade_out * 1000))
0e5d59f7 100 else:
d479af33 101 self.channel().stop()
0e5d59f7
IB
102
103 def set_volume(self, value):
104 if value < 0:
105 value = 0
106 if value > 100:
107 value = 100
b58b8220 108 self.channel().set_volume(value / 100)
be27763f 109
b86db9f1
IB
110 def wait_end(self):
111 pass
d479af33
IB
112
113 def channel(self):
114 return pygame.mixer.Channel(self.channel_id)