Skip to content

Commit

Permalink
Black code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sergree committed Nov 10, 2020
1 parent 474d220 commit f03b97b
Show file tree
Hide file tree
Showing 24 changed files with 586 additions and 533 deletions.
13 changes: 6 additions & 7 deletions README.md
Expand Up @@ -6,6 +6,7 @@
[![PyPI Version](https://badge.fury.io/py/matchering.svg)](https://badge.fury.io/py/matchering)
[![PyPI Python Versions](https://img.shields.io/pypi/pyversions/matchering.svg)](https://pypi.python.org/pypi/matchering/)
[![Mentioned in Awesome Python](https://awesome.re/mentioned-badge.svg)](https://github.com/vinta/awesome-python)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

## Matching + Mastering = ❤️

Expand Down Expand Up @@ -100,16 +101,14 @@ mg.log(print)

mg.process(
# The track you want to master
target='my_song.wav',

target="my_song.wav",
# Some "wet" reference track
reference='some_popular_song.wav',

reference="some_popular_song.wav",
# Where and how to save your results
results=[
mg.pcm16('my_song_master_16bit.wav'),
mg.pcm24('my_song_master_24bit.wav'),
]
mg.pcm16("my_song_master_16bit.wav"),
mg.pcm24("my_song_master_24bit.wav"),
],
)

```
Expand Down
21 changes: 8 additions & 13 deletions examples/advanced_results.py
Expand Up @@ -4,34 +4,29 @@
mg.log(info_handler=print, warning_handler=print)

mg.process(
target='my_song.wav',
reference='some_popular_song.wav',
target="my_song.wav",
reference="some_popular_song.wav",
# pcm16 and pcm24 are just basic shortcuts
# You can also use the Result class to make some advanced results
results=[
# Basic WAV 16-bit, match + master
mg.pcm16('my_song_master_16bit.wav'),

mg.pcm16("my_song_master_16bit.wav"),
# FLAC 24-bit, match only (no limiter), normalized to -0.01 dB
# Recommendations for adjusting the amplitude will be displayed in the debug print if it is enabled
mg.Result(
'custom_result_24bit_no_limiter.flac',
subtype='PCM_24',
use_limiter=False
"custom_result_24bit_no_limiter.flac", subtype="PCM_24", use_limiter=False
),

# AIFF 32-bit float, match only (no limiter), non-normalized
# Can exceed 0 dB without clipping
# So you can directly feed it to some VST limiter in your DAW
mg.Result(
'custom_result_32bit_no_limiter_non-normalized.aiff',
subtype='FLOAT',
"custom_result_32bit_no_limiter_non-normalized.aiff",
subtype="FLOAT",
use_limiter=False,
normalize=False
normalize=False,
)

# More available formats and subtypes:
# https://pysoundfile.readthedocs.io/en/latest/#soundfile.available_formats
# https://pysoundfile.readthedocs.io/en/latest/#soundfile.available_subtypes
]
],
)
26 changes: 11 additions & 15 deletions examples/advanced_text_output.py
Expand Up @@ -5,35 +5,31 @@

# Let's define a basic text output function that will also output the current datetime
def my_print(text):
print(f'{datetime.now()}: {text}')
print(f"{datetime.now()}: {text}")


# The information output will be marked with a prefix
def info(text):
my_print(f'INFO: {text}')
my_print(f"INFO: {text}")


# The warning output will be highlighted with exclamation marks on both sides
def warning(text):
my_print('!' * 20)
my_print(f'! WARNING: {text}')
my_print('!' * 20)
my_print("!" * 20)
my_print(f"! WARNING: {text}")
my_print("!" * 20)


# Set new handlers
mg.log(
warning_handler=warning,
info_handler=info,
debug_handler=my_print
)
mg.log(warning_handler=warning, info_handler=info, debug_handler=my_print)

mg.process(
target='my_song.wav',
reference='some_popular_song.wav',
target="my_song.wav",
reference="some_popular_song.wav",
results=[
mg.pcm16('my_song_master_16bit.wav'),
mg.pcm24('my_song_master_24bit.wav'),
]
mg.pcm16("my_song_master_16bit.wav"),
mg.pcm24("my_song_master_24bit.wav"),
],
)

# These settings will result in the following text output:
Expand Down
10 changes: 5 additions & 5 deletions examples/basic.py
Expand Up @@ -6,12 +6,12 @@

mg.process(
# The track you want to master
target='my_song.wav',
target="my_song.wav",
# Some "wet" reference track
reference='some_popular_song.wav',
reference="some_popular_song.wav",
# Where and how to save your results
results=[
mg.pcm16('my_song_master_16bit.wav'),
mg.pcm24('my_song_master_24bit.wav'),
]
mg.pcm16("my_song_master_16bit.wav"),
mg.pcm24("my_song_master_24bit.wav"),
],
)
12 changes: 6 additions & 6 deletions examples/edited_config.py
Expand Up @@ -4,11 +4,11 @@
# mg.log(print)

mg.process(
target='my_song.wav',
reference='some_popular_song.wav',
target="my_song.wav",
reference="some_popular_song.wav",
results=[
mg.pcm16('my_song_master_16bit.wav'),
mg.pcm24('my_song_master_24bit.wav'),
mg.pcm16("my_song_master_16bit.wav"),
mg.pcm24("my_song_master_24bit.wav"),
],
# Create a custom Config instance to edit matchering configuration
# Think twice before you change something here
Expand All @@ -20,13 +20,13 @@
# Change the threshold value (float, not dB) from the default value of 0.9981 (-0.01 dB)
threshold=0.7079, # -3 dB
# Change the temp folder to work with ffmpeg
temp_folder='/tmp',
temp_folder="/tmp",
# Lower the preview length to 15 seconds from the default value of 30
preview_size=15,
# Allow matchering to accept the same files (useless in fact)
allow_equality=True
# Etc...
# The remaining parameters will be filled with default values
# Examine defaults.py to find other parameters
)
),
)
12 changes: 6 additions & 6 deletions examples/with_preview.py
Expand Up @@ -4,15 +4,15 @@
mg.log(warning_handler=print)

mg.process(
target='my_song.wav',
reference='some_popular_song.wav',
target="my_song.wav",
reference="some_popular_song.wav",
results=[
mg.pcm16('my_song_master_16bit.wav'),
mg.pcm24('my_song_master_24bit.wav'),
mg.pcm16("my_song_master_16bit.wav"),
mg.pcm24("my_song_master_24bit.wav"),
],
# These two lines will allow you to create two 30-second FLAC files with the loudest parts of:
# 'my_song.wav' and 'my_song_master_16bit.wav'
# Use them to quickly compare the target audio with the resulting audio
preview_target=mg.pcm16('preview_my_song.flac'),
preview_result=mg.pcm16('preview_my_song_master.flac')
preview_target=mg.pcm16("preview_my_song.flac"),
preview_result=mg.pcm16("preview_my_song_master.flac"),
)
16 changes: 8 additions & 8 deletions matchering/__init__.py
Expand Up @@ -11,16 +11,16 @@
"""

__title__ = 'matchering'
__title__ = "matchering"

__author__ = 'Sergree'
__credits__ = ['Sergey Grishakov', 'Igor Isaev', 'Chin Yun Yu', 'Elizaveta Grishakova']
__maintainer__ = 'Sergree'
__email__ = 'wokashi.rg@gmail.com'
__license__ = 'GPLv3'
__copyright__ = 'Copyright (C) 2016-2021 Sergree'
__author__ = "Sergree"
__credits__ = ["Sergey Grishakov", "Igor Isaev", "Chin Yun Yu", "Elizaveta Grishakova"]
__maintainer__ = "Sergree"
__email__ = "wokashi.rg@gmail.com"
__license__ = "GPLv3"
__copyright__ = "Copyright (C) 2016-2021 Sergree"

__version__ = '2.0.3'
__version__ = "2.0.3"

from .log.handlers import set_handlers as log
from .results import Result, pcm16, pcm24
Expand Down
78 changes: 42 additions & 36 deletions matchering/checker.py
Expand Up @@ -28,41 +28,41 @@


def __check_sample_rate(
array: np.ndarray,
sample_rate: int,
required_sample_rate: int,
name: str,
log_handler,
log_code: Code
array: np.ndarray,
sample_rate: int,
required_sample_rate: int,
name: str,
log_handler,
log_code: Code,
) -> (np.ndarray, int):
if sample_rate != required_sample_rate:
debug(f'Resampling {name} audio from {sample_rate} Hz to {required_sample_rate} Hz...')
debug(
f"Resampling {name} audio from {sample_rate} Hz to {required_sample_rate} Hz..."
)
array = resample(array, sample_rate, required_sample_rate, axis=0)
log_handler(log_code)
return array, required_sample_rate


def __check_length(
array: np.ndarray,
sample_rate: int,
max_length: int,
min_length: int,
name: str,
error_code_max: Code,
error_code_min: Code
array: np.ndarray,
sample_rate: int,
max_length: int,
min_length: int,
name: str,
error_code_max: Code,
error_code_min: Code,
) -> None:
length = size(array)
debug(f'{name} audio length: {length} samples ({time_str(length, sample_rate)})')
debug(f"{name} audio length: {length} samples ({time_str(length, sample_rate)})")
if length > max_length:
raise ModuleError(error_code_max)
elif length < min_length:
raise ModuleError(error_code_min)


def __check_channels(
array: np.ndarray,
info_code_mono: Code,
error_code_not_stereo: Code
array: np.ndarray, info_code_mono: Code, error_code_not_stereo: Code
) -> np.ndarray:
if is_mono(array):
info(info_code_mono)
Expand All @@ -73,21 +73,23 @@ def __check_channels(


def __check_clipping_limiting(
array: np.ndarray,
clipping_samples_threshold: int,
limited_samples_threshold: int,
warning_code_clipping: Code,
warning_code_limiting: Code
array: np.ndarray,
clipping_samples_threshold: int,
limited_samples_threshold: int,
warning_code_clipping: Code,
warning_code_limiting: Code,
) -> None:
max_value, max_count = count_max_peaks(array)
if max_count > clipping_samples_threshold:
if np.isclose(max_value, 1.):
if np.isclose(max_value, 1.0):
warning(warning_code_clipping)
elif max_count > limited_samples_threshold:
warning(warning_code_limiting)


def check(array: np.ndarray, sample_rate: int, config: Config, name: str) -> (np.ndarray, int):
def check(
array: np.ndarray, sample_rate: int, config: Config, name: str
) -> (np.ndarray, int):
name = name.upper()

__check_length(
Expand All @@ -96,36 +98,40 @@ def check(array: np.ndarray, sample_rate: int, config: Config, name: str) -> (np
config.max_length * sample_rate,
config.fft_size * sample_rate // config.internal_sample_rate,
name,
Code.ERROR_TARGET_LENGTH_IS_EXCEEDED if name == 'TARGET'
Code.ERROR_TARGET_LENGTH_IS_EXCEEDED
if name == "TARGET"
else Code.ERROR_REFERENCE_LENGTH_LENGTH_IS_EXCEEDED,
Code.ERROR_TARGET_LENGTH_IS_TOO_SMALL if name == 'TARGET'
else Code.ERROR_REFERENCE_LENGTH_LENGTH_TOO_SMALL
Code.ERROR_TARGET_LENGTH_IS_TOO_SMALL
if name == "TARGET"
else Code.ERROR_REFERENCE_LENGTH_LENGTH_TOO_SMALL,
)

array = __check_channels(
array,
Code.INFO_TARGET_IS_MONO if name == 'TARGET' else Code.INFO_REFERENCE_IS_MONO,
Code.ERROR_TARGET_NUM_OF_CHANNELS_IS_EXCEEDED if name == 'TARGET'
else Code.ERROR_REFERENCE_NUM_OF_CHANNELS_IS_EXCEEDED
Code.INFO_TARGET_IS_MONO if name == "TARGET" else Code.INFO_REFERENCE_IS_MONO,
Code.ERROR_TARGET_NUM_OF_CHANNELS_IS_EXCEEDED
if name == "TARGET"
else Code.ERROR_REFERENCE_NUM_OF_CHANNELS_IS_EXCEEDED,
)

array, sample_rate = __check_sample_rate(
array,
sample_rate,
config.internal_sample_rate,
name,
warning if name == 'TARGET' else info,
Code.WARNING_TARGET_IS_RESAMPLED if name == 'TARGET'
else Code.INFO_REFERENCE_IS_RESAMPLED
warning if name == "TARGET" else info,
Code.WARNING_TARGET_IS_RESAMPLED
if name == "TARGET"
else Code.INFO_REFERENCE_IS_RESAMPLED,
)

if name == 'TARGET':
if name == "TARGET":
__check_clipping_limiting(
array,
config.clipping_samples_threshold,
config.limited_samples_threshold,
Code.WARNING_TARGET_IS_CLIPPING,
Code.WARNING_TARGET_LIMITER_IS_APPLIED
Code.WARNING_TARGET_LIMITER_IS_APPLIED,
)

return array, sample_rate
Expand Down

0 comments on commit f03b97b

Please sign in to comment.