]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/music_file.py
7 def __init__(self
, filename
, lock
, channel_id
, name
= None, gain
= 1):
8 self
.filename
= filename
9 self
.channel_id
= channel_id
10 self
.name
= name
or filename
15 self
.flag_paused
= False
16 threading
.Thread(name
= "MSMusicLoad", target
= self
.load_sound
, args
= [lock
]).start()
18 def load_sound(self
, lock
):
20 print("Loading « {} »".format(self
.name
))
21 volume_factor
= 20 * math
.log10(self
.gain
)
22 audio_segment
= pydub
.AudioSegment
.from_file(self
.filename
).set_frame_rate(44100).apply_gain(volume_factor
)
23 self
.sound_duration
= audio_segment
.duration_seconds
24 self
.raw_data
= audio_segment
.raw_data
25 print("Loaded « {} »".format(self
.name
))
30 return self
.channel().get_busy()
33 return self
.flag_paused
35 def play(self
, fade_in
= 0, volume
= 100, start_at
= 0):
36 self
.channel().set_endevent()
37 self
.channel().set_endevent(pygame
.USEREVENT
)
38 self
.set_volume(volume
)
41 raw_data_length
= len(self
.raw_data
)
42 start_offset
= int((raw_data_length
/ self
.sound_duration
) * start_at
)
43 start_offset
= start_offset
- (start_offset
% 2)
44 sound
= pygame
.mixer
.Sound(self
.raw_data
[start_offset
:])
46 sound
= pygame
.mixer
.Sound(self
.raw_data
)
48 self
.channel().play(sound
, fade_ms
= int(fade_in
* 1000))
49 self
.flag_paused
= False
52 self
.channel().pause()
53 self
.flag_paused
= True
56 self
.channel().unpause()
57 self
.flag_paused
= False
59 def stop(self
, fade_out
= 0):
61 self
.channel().fadeout(int(fade_out
* 1000))
65 def set_volume(self
, value
):
70 self
.channel().set_volume(value
/ 100)
76 return pygame
.mixer
.Channel(self
.channel_id
)