]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/music_file.py
6 def __init__(self
, filename
, lock
, channel_id
, name
= None):
7 self
.filename
= filename
8 self
.channel_id
= channel_id
9 self
.name
= name
or filename
14 self
.flag_paused
= False
15 threading
.Thread(name
= "MSMusicLoad", target
= self
.load_sound
, args
= [lock
]).start()
17 def load_sound(self
, lock
):
19 print("Loading « {} »".format(self
.name
))
20 self
.raw_data
= pydub
.AudioSegment
.from_file(self
.filename
).raw_data
21 self
.sound
= pygame
.mixer
.Sound(self
.raw_data
)
22 print("Loaded « {} »".format(self
.name
))
27 return self
.channel().get_busy()
30 return self
.flag_paused
32 def play(self
, fade_in
= 0, volume
= 100, start_at
= 0):
33 self
.channel().set_endevent()
34 self
.channel().set_endevent(pygame
.USEREVENT
)
35 self
.set_volume(volume
)
38 song_duration
= self
.sound
.get_length()
39 raw_data_length
= len(self
.raw_data
)
40 start_offset
= int((raw_data_length
/ song_duration
) * start_at
)
41 start_offset
= start_offset
- (start_offset
% 2)
42 self
.sound
= pygame
.mixer
.Sound(self
.raw_data
[start_offset
:])
44 self
.sound
= pygame
.mixer
.Sound(self
.raw_data
)
46 self
.channel().play(self
.sound
, fade_ms
= fade_in
* 1000)
47 self
.flag_paused
= False
50 self
.channel().pause()
51 self
.flag_paused
= True
54 self
.channel().unpause()
55 self
.flag_paused
= False
57 def stop(self
, fade_out
= 0):
59 self
.channel().fadeout(fade_out
* 1000)
63 def set_volume(self
, value
):
68 self
.sound
.set_volume(value
/ 100)
74 return pygame
.mixer
.Channel(self
.channel_id
)