File size: 9,402 Bytes
c7e47b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | """Functions for creating new mixes from medleydb multitracks.
"""
import shutil
import sox
VOCALS = ["male singer", "female singer", "male speaker", "female speaker",
"male rapper", "female rapper", "beatboxing", "vocalists"]
def mix_multitrack(mtrack, output_path, stem_indices=None,
alternate_weights=None, alternate_files=None,
additional_files=None):
"""Mix the stems of a multitrack to create a new mix.
Can optionally adjust the volume of stems and replace, remove, or add
stems.
Parameters
----------
mtrack : Multitrack
Multitrack object
output_path : str
Path to save output file.
stem_indices : list or None, default=None
stem indices to include in mix.
If None, mixes all stems
alternate_weights : dict or None, default=None
Dictionary with stem indices as keys and mixing coefficients as values.
Stem indices present that are not in this dictionary will use the
default estimated mixing coefficient.
alternate_files : dict or None, default=None
Dictionary with stem indices as keys and filepaths as values.
Audio file to use in place of original stem. Stem indices present that
are not in this dictionary will use the original stems.
additional_files : list of tuple or None, default=None
List of tuples of (filepath, mixing_coefficient) pairs to additionally
add to final mix.
Returns
-------
filepaths : list
List of filepaths used in the mix
weights : list
List of weights used to mix filepaths
"""
filepaths, weights = _build_mix_args(
mtrack, stem_indices, alternate_weights, alternate_files,
additional_files
)
if len(filepaths) == 1:
shutil.copyfile(filepaths[0], output_path)
else:
cbn = sox.Combiner()
cbn.build(
filepaths, output_path, 'mix', input_volumes=weights
)
return filepaths, weights
def _build_mix_args(mtrack, stem_indices, alternate_weights, alternate_files,
additional_files):
"""Create lists of filepaths and weights to use in final mix.
Parameters
----------
mtrack : Multitrack
Multitrack object
stem_indices : list
stem indices to include in mix.
If None, mixes all stems
alternate_weights : dict
Dictionary with stem indices as keys and mixing coefficients as values.
Stem indices present that are not in this dictionary will use the
default estimated mixing coefficient.
alternate_files : dict
Dictionary with stem indices as keys and filepaths as values.
Audio file to use in place of original stem. Stem indices present that
are not in this dictionary will use the original stems.
additional_files : list of tuples
List of tuples of (filepath, mixing_coefficient) pairs to additionally
add to final mix.
Returns
-------
filepaths : list
List of filepaths that were included in mix.
weights : list
List of weights that were used in mix
"""
if stem_indices is None:
stem_indices = list(mtrack.stems.keys())
if alternate_files is None:
alternate_files = {}
if alternate_weights is None:
alternate_weights = {}
weights = []
filepaths = []
for index in stem_indices:
if index in alternate_files.keys():
filepaths.append(alternate_files[index])
else:
filepaths.append(mtrack.stems[index].audio_path)
if index in alternate_weights.keys():
weights.append(alternate_weights[index])
else:
if mtrack.stems[index].mixing_coefficient is not None:
mix_coeff = mtrack.stems[index].mixing_coefficient
else:
print(
'[Warning] Multitrack does not have mixing coefficients. '
'Using uniform stem weights.'
)
mix_coeff = 1
weights.append(mix_coeff)
if additional_files is not None:
for fpath, weight in additional_files:
filepaths.append(fpath)
weights.append(weight)
return filepaths, weights
def mix_melody_stems(mtrack, output_path, max_melody_stems=None,
include_percussion=False, require_mono=False):
"""Creates a mix using only the stems labeled as melody.
Parameters
----------
mtrack : Multitrack
Multitrack object
output_path : str
Path to save output wav file.
max_melody_stems : int or None, default=None
The maximum number of melody stems to mix. If None, uses the number of
melody stems in the mix.
include_percussion : bool, default=False
If true, adds percussion stems to the mix.
require_mono : bool, default=False
If true, only includes melody stems that are monophonic instruments.
Returns
-------
melody_indices : list
List of selected melody indices.
stem_indices : list
List of stem indices used in mix.
"""
if max_melody_stems is None:
max_melody_stems = 100
melody_rankings = mtrack.melody_rankings
inverse_ranking = {v: k for k, v in melody_rankings.items()}
n_melody_stems = len(list(melody_rankings.keys()))
stem_indices = []
melody_indices = []
n_chosen = 0
for i in range(1, n_melody_stems + 1):
if n_chosen >= max_melody_stems:
break
this_stem_index = inverse_ranking[i]
if require_mono:
mono = all([
f0_type == 'm' for f0_type
in mtrack.stems[this_stem_index].f0_type
])
if mono:
stem_indices.append(this_stem_index)
melody_indices.append(this_stem_index)
n_chosen += 1
else:
stem_indices.append(this_stem_index)
melody_indices.append(this_stem_index)
n_chosen += 1
if include_percussion:
percussive_indices = [
i for i, s in mtrack.stems.items() if s.f0_type == 'u'
]
for i in percussive_indices:
stem_indices.append(i)
mix_multitrack(mtrack, output_path, stem_indices=stem_indices)
return melody_indices, stem_indices
def mix_mono_stems(mtrack, output_path, include_percussion=False):
"""Creates a mix using only the stems that are monophonic. For example, in
mix with piano, voice, and clarinet, the resulting mix would include
only voice and clarinet.
Parameters
----------
mtrack : Multitrack
Multitrack object
output_path : str
Path to save output wav file.
include_percussion : bool, default=False
If true, percussive instruments are included in the mix. If false, they
are excluded.
Returns
-------
mono_indices : list
List of stem indices containing monophonic instruments.
stem_indices : list
List of stem indices used in mix.
"""
stems = mtrack.stems
stem_indices = []
mono_indices = []
for i in stems.keys():
mono = all([
f0_type == 'm' for f0_type in mtrack.stems[i].f0_type
])
unvoiced = all([
f0_type == 'u' for f0_type in mtrack.stems[i].f0_type
])
if mono:
stem_indices.append(i)
mono_indices.append(i)
elif include_percussion and unvoiced:
stem_indices.append(i)
mix_multitrack(mtrack, output_path, stem_indices=stem_indices)
return mono_indices, stem_indices
def mix_no_vocals(mtrack, output_path):
"""Remixes a multitrack with anything type of vocals removed.
If no vocals are present, the mix will be a simple weighted linear remix.
Parameters
----------
mtrack : Multitrack
Multitrack object
output_path : str
Path to save output file.
Returns
-------
stem_indices : list
List of stem indices used in mix.
"""
stems = mtrack.stems
stem_indices = []
for i in stems.keys():
not_vocal = all([inst not in VOCALS for inst in stems[i].instrument])
if not_vocal:
stem_indices.append(i)
mix_multitrack(mtrack, output_path, stem_indices=stem_indices)
return stem_indices
def remix_vocals(mtrack, output_path, vocals_scale):
"""Remixes a multitrack, changing the volume of the vocals.
Parameters
----------
mtrack : Multitrack
Multitrack object
output_path : str
Path to save output wav file.
vocals_scale : float
The target scale factor for vocals. A value of 1 keeps the volume the
same. Values above 1 increase the volume and below 1 decrease it.
Returns
-------
alternate_weights : dict
Dictionary of vocal weights keyed by vocal stem index.
"""
stems = mtrack.stems
alternate_weights = {}
for i in stems.keys():
vocal = any([inst in VOCALS for inst in stems[i].instrument])
if vocal:
vocal_weight = stems[i].mixing_coefficient * vocals_scale
alternate_weights[i] = vocal_weight
mix_multitrack(
mtrack, output_path, alternate_weights=alternate_weights
)
return alternate_weights
|