Skip to content

Commit

Permalink
remove testdata
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileGoat committed Aug 29, 2022
1 parent c4b34a6 commit eaa5ceb
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 9,147 deletions.
1 change: 0 additions & 1 deletion python/paddle/audio/compliance/__init__.py
Expand Up @@ -12,5 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from . import kaldi
from . import librosa
615 changes: 0 additions & 615 deletions python/paddle/audio/compliance/kaldi.py

This file was deleted.

15 changes: 0 additions & 15 deletions python/paddle/tests/audio_base.py
Expand Up @@ -86,16 +86,6 @@ def get_wav_data(
num_frames = 1 << 16

# paddle linspace not support uint8, int8, int16
#if dtype == "uint8":
# base = paddle.linspace(0, 255, num_frames, dtype=dtype_)
#dtype_np = getattr(np, dtype)
#base_np = np.linspace(0, 255, num_frames, dtype_np)
#base = paddle.to_tensor(base_np, dtype=dtype_)
#elif dtype == "int8":
# base = paddle.linspace(-128, 127, num_frames, dtype=dtype_)
#dtype_np = getattr(np, dtype)
#base_np = np.linspace(-128, 127, num_frames, dtype_np)
#base = paddle.to_tensor(base_np, dtype=dtype_)
if dtype == "float32":
base = paddle.linspace(-1.0, 1.0, num_frames, dtype=dtype_)
elif dtype == "float64":
Expand All @@ -105,11 +95,6 @@ def get_wav_data(
2147483647,
num_frames,
dtype=dtype_)
#elif dtype == "int16":
# base = paddle.linspace(-32768, 32767, num_frames, dtype=dtype_)
#dtype_np = getattr(np, dtype)
#base_np = np.linspace(-32768, 32767, num_frames, dtype_np)
#base = paddle.to_tensor(base_np, dtype=dtype_)
else:
raise NotImplementedError(f"Unsupported dtype {dtype}")
data = base.tile([num_channels, 1])
Expand Down
110 changes: 72 additions & 38 deletions python/paddle/tests/test_audio_features.py
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import unittest

import librosa
import numpy as np
import paddle

Expand All @@ -39,13 +40,26 @@ def initParmas(self):
self.sr = 16000
self.dtype = "float32"
self.window_size = 1024
self.waveform = np.loadtxt('testdata/audio.txt')
waveform_tensor = get_wav_data(self.dtype,
self.num_channels,
normalize=False,
num_frames=self.duration * self.sr)
self.waveform = waveform_tensor.numpy() * 0.1

def test_stft(self):
if len(self.waveform.shape) == 2: # (C, T)
self.waveform = self.waveform.squeeze(
0) # 1D input for librosa.feature.melspectrogram
feature_librosa = np.load('testdata/librosa_stft.npy')
feature_librosa = librosa.core.stft(
y=self.waveform,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=None,
window=self.window_str,
center=True,
dtype=None,
pad_mode=self.pad_mode,
)
x = paddle.to_tensor(self.waveform).unsqueeze(0)
window = paddle.audio.functional.get_window(self.window_str,
self.n_fft,
Expand All @@ -71,9 +85,25 @@ def test_istft(self):
self.waveform = self.waveform.squeeze(
0) # 1D input for librosa.feature.melspectrogram
# librosa
stft_matrix = np.load('testdata/librosa_stft_matrix.npy')
feature_librosa = np.loadtxt('testdata/librosa_istft.txt')

# Get stft result from librosa.
stft_matrix = librosa.core.stft(
y=self.waveform,
n_fft=self.n_fft,
hop_length=self.hop_length,
win_length=None,
window=self.window_str,
center=True,
pad_mode=self.pad_mode,
)
feature_librosa = librosa.core.istft(
stft_matrix=stft_matrix,
hop_length=self.hop_length,
win_length=None,
window=self.window_str,
center=True,
dtype=None,
length=None,
)
x = paddle.to_tensor(stft_matrix).unsqueeze(0)
window = paddle.audio.functional.get_window(self.window_str,
self.n_fft,
Expand All @@ -97,7 +127,16 @@ def test_istft(self):
decimal=5)

def test_mel(self):
feature_librosa = np.loadtxt('testdata/librosa_filters_mel.txt')
feature_librosa = librosa.filters.mel(
sr=self.sr,
n_fft=self.n_fft,
n_mels=self.n_mels,
fmin=self.fmin,
fmax=None,
htk=False,
norm='slaney',
dtype=self.waveform.dtype,
)
feature_compliance = paddle.audio.compliance.librosa.compute_fbank_matrix(
sr=self.sr,
n_fft=self.n_fft,
Expand Down Expand Up @@ -131,7 +170,13 @@ def test_melspect(self):
0) # 1D input for librosa.feature.melspectrogram

# librosa:
feature_librosa = np.loadtxt('testdata/librosa_melspectrogram.txt')
feature_librosa = librosa.feature.melspectrogram(
y=self.waveform,
sr=self.sr,
n_fft=self.n_fft,
hop_length=self.hop_length,
n_mels=self.n_mels,
fmin=self.fmin)

# paddle.audio.compliance.librosa:
feature_compliance = paddle.audio.compliance.librosa.melspectrogram(
Expand Down Expand Up @@ -168,7 +213,14 @@ def test_log_melspect(self):
0) # 1D input for librosa.feature.melspectrogram

# librosa:
feature_librosa = np.loadtxt('testdata/librosa_logmelspect.txt')
feature_librosa = librosa.feature.melspectrogram(
y=self.waveform,
sr=self.sr,
n_fft=self.n_fft,
hop_length=self.hop_length,
n_mels=self.n_mels,
fmin=self.fmin)
feature_librosa = librosa.power_to_db(feature_librosa, top_db=None)
# paddle.audio.compliance.librosa:
feature_compliance = paddle.audio.compliance.librosa.melspectrogram(
x=self.waveform,
Expand All @@ -177,28 +229,27 @@ def test_log_melspect(self):
hop_length=self.hop_length,
n_mels=self.n_mels,
fmin=self.fmin)

def test_spectrogram(self):
if len(self.waveform.shape) == 2: # (C, T)
self.waveform = self.waveform.squeeze(
0) # 1D input for librosa.feature.melspectrogram
feature_librosa = np.loadtxt('testdata/librosa_spectrogram.txt')
feature_compliance = paddle.audio.compliance.librosa.spectrogram(
x=self.waveform,
sr=self.sr,
window=self.window_str,
pad_mode=self.pad_mode)
np.testing.assert_array_almost_equal(feature_librosa,
feature_compliance,
decimal=4)
decimal=5)

def test_mfcc(self):
if len(self.waveform.shape) == 2: # (C, T)
self.waveform = self.waveform.squeeze(
0) # 1D input for librosa.feature.melspectrogram

# librosa:
feature_librosa = np.loadtxt('testdata/librosa_mfcc.txt')
feature_librosa = librosa.feature.mfcc(y=self.waveform,
sr=self.sr,
S=None,
n_mfcc=self.n_mfcc,
dct_type=2,
norm='ortho',
lifter=0,
n_fft=self.n_fft,
hop_length=self.hop_length,
n_mels=self.n_mels,
fmin=self.fmin)
# paddle.audio.compliance.librosa:
feature_compliance = paddle.audio.compliance.librosa.mfcc(
x=self.waveform,
Expand Down Expand Up @@ -233,23 +284,6 @@ def test_mfcc(self):
feature_layer,
decimal=4)

def test_kaldi_feature(self):
waveform = np.expand_dims(self.waveform, axis=0)
fbank = paddle.audio.compliance.kaldi.fbank(paddle.to_tensor(waveform))
fbank_bg = np.loadtxt('testdata/kaldi_fbank.txt')
np.testing.assert_array_almost_equal(fbank, fbank_bg, decimal=4)

mfcc = paddle.audio.compliance.kaldi.mfcc(paddle.to_tensor(waveform))
mfcc_bg = np.loadtxt('testdata/kaldi_mfcc.txt')
np.testing.assert_array_almost_equal(mfcc, mfcc_bg, decimal=4)

spectrogram = paddle.audio.compliance.kaldi.spectrogram(
paddle.to_tensor(waveform))
spectrogram_bg = np.loadtxt('testdata/kaldi_spectrogram.txt')
np.testing.assert_array_almost_equal(spectrogram,
spectrogram_bg,
decimal=4)


if __name__ == '__main__':
unittest.main()
1 change: 0 additions & 1 deletion python/paddle/tests/testdata/audio.txt

This file was deleted.

0 comments on commit eaa5ceb

Please sign in to comment.