Skip to content

Commit

Permalink
add clipping protection for seanwood#2
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwood committed Aug 5, 2017
1 parent dae61a7 commit 2d7a45c
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions gccNMF/wavfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@
import numpy as np
import contextlib
from scipy.io import wavfile
import logging

CLIP_PROTECTION_MAX_SAMPLE_VALUE = 0.99

def wavread(filePath):
sampleRate, samples_pcm = wavfile.read(filePath)
samples_float32 = pcm2float(samples_pcm)
return samples_float32.T, sampleRate

def wavwrite(samples_float32, filePath, sampleRate):
if np.max(np.abs(samples_float32)) > 1:
raise ValueError('wavwrite: max abs signal value exceeds 1')
def wavwrite(samples_float32, filePath, sampleRate, clipProtection=True):
maxAbsValue = np.max(np.abs(samples_float32))
if maxAbsValue >= 1:
if clipProtection:
logging.warning('wavwrite: max abs signal value exceeds 1, rescaling to %2f' % CLIP_PROTECTION_MAX_SAMPLE_VALUE)
samples_float32 = samples_float32 / maxAbsValue * CLIP_PROTECTION_MAX_SAMPLE_VALUE
else:
raise ValueError('wavwrite: max abs signal value exceeds 1')
samples_pcm = float2pcm( samples_float32.astype(np.float32) )
wavfile.write( filePath, sampleRate, samples_pcm.T )

Expand Down

0 comments on commit 2d7a45c

Please sign in to comment.