Skip to content

Commit

Permalink
Optimized volume normalization (CorentinJ#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidalei authored and CorentinJ committed Aug 31, 2019
1 parent 6a8bf3a commit a5b82ee
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions encoder/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ def moving_average(array, width):
def normalize_volume(wav, target_dBFS, increase_only=False, decrease_only=False):
if increase_only and decrease_only:
raise ValueError("Both increase only and decrease only are set")
rms = np.sqrt(np.mean((wav * int16_max) ** 2))
wave_dBFS = 20 * np.log10(rms / int16_max)
dBFS_change = target_dBFS - wave_dBFS
if dBFS_change < 0 and increase_only or dBFS_change > 0 and decrease_only:
dBFS_change = target_dBFS - 10 * np.log10(np.mean(wav ** 2))
if (dBFS_change < 0 and increase_only) or (dBFS_change > 0 and decrease_only):
return wav
return wav * (10 ** (dBFS_change / 20))

0 comments on commit a5b82ee

Please sign in to comment.