]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/mixer.py
1 import sounddevice
as sd
7 sample_width
= Config
.sample_width
9 def sample_width_to_dtype(sample_width
):
10 if sample_width
== 1 or sample_width
== 2 or sample_width
== 4:
11 return 'int' + str(8*sample_width
)
13 raise "Unknown sample width"
15 def _latency(latency
):
16 if latency
== "high" or latency
== "low":
23 self
.stream
= sd
.RawOutputStream(
24 samplerate
=Config
.frame_rate
,
25 channels
=Config
.channels
,
26 dtype
=sample_width_to_dtype(Config
.sample_width
),
27 latency
=_latency(Config
.latency
),
28 blocksize
=Config
.blocksize
,
29 callback
=self
.play_callback
)
32 def add_file(self
, music_file
):
33 if music_file
not in self
.open_files
:
34 self
.open_files
.append(music_file
)
37 def remove_file(self
, music_file
):
38 self
.open_files
.remove(music_file
)
39 if len(self
.open_files
) == 0:
48 def play_callback(self
, out_data
, frame_count
, time_info
, status_flags
):
49 out_data_length
= len(out_data
)
50 empty_data
= b
"\0" * out_data_length
51 data
= b
"\0" * out_data_length
53 for open_file
in self
.open_files
:
54 file_data
= open_file
.play_callback(out_data_length
, frame_count
)
56 if data
== empty_data
:
58 elif file_data
!= empty_data
:
59 data
= audioop
.add(data
, file_data
, sample_width
)