@pytest.mark.parametrize('waveform_data_format', ['default', 'channels_first', 'channels_last'])
@pytest.mark.parametrize('stft_data_format', ['default', 'channels_first', 'channels_last'])
@pytest.mark.parametrize('hop_ratio', [0.5, 0.25, 0.125])
def test_perfectly_reconstructing_stft_istft(waveform_data_format, stft_data_format, hop_ratio):
    n_ch = 1
    (src_mono, batch_src, input_shape) = get_audio(data_format=waveform_data_format, n_ch=n_ch)
    time_axis = (1 if (waveform_data_format == 'channels_first') else 0)
    len_src = input_shape[time_axis]
    n_fft = 2048
    hop_length = int((2048 * hop_ratio))
    n_added_frames = (int((1 / hop_ratio)) - 1)
    (stft, istft) = get_perfectly_reconstructing_stft_istft(stft_input_shape=input_shape, n_fft=n_fft, hop_length=hop_length, waveform_data_format=waveform_data_format, stft_data_format=stft_data_format)
    model = tf.keras.models.Sequential([stft, istft])
    recon_waveform = model(batch_src)
    len_pad_begin = (n_fft - hop_length)
    if (waveform_data_format == 'channels_first'):
        recon_waveform = recon_waveform[:, :, len_pad_begin:(len_pad_begin + len_src)]
    else:
        recon_waveform = recon_waveform[:, len_pad_begin:(len_pad_begin + len_src), :]
    np.testing.assert_allclose(batch_src, recon_waveform, atol=1e-05)
    S = librosa.stft(src_mono, n_fft=n_fft, hop_length=hop_length).T.astype(np.complex64)
    ch_axis = (1 if (stft_data_format == 'channels_first') else 3)
    S = np.expand_dims(S, (0, ch_axis))
    model = tf.keras.models.Sequential([istft, stft])
    recon_S = model(S)
    n = n_added_frames
    n_added_frames += n
    if (stft_data_format == 'channels_first'):
        if (n != 0):
            S = S[:, :, n:(- n), :]
        recon_S = recon_S[:, :, n_added_frames:(- n_added_frames), :]
    else:
        if (n != 0):
            S = S[:, n:(- n), :, :]
        recon_S = recon_S[:, n_added_frames:(- n_added_frames), :, :]
    np.testing.assert_equal(S.shape, recon_S.shape)
    allclose_complex_numbers(S, recon_S)
@pytest.mark.parametrize('waveform_data_format', ['default', 'channels_first', 'channels_last'])
@pytest.mark.parametrize('stft_data_format', ['default', 'channels_first', 'channels_last'])
@pytest.mark.parametrize('hop_ratio', [0.5, 0.25, 0.125])
def test_perfectly_reconstructing_stft_istft(waveform_data_format, stft_data_format, hop_ratio):
    n_ch = 1
    (src_mono, batch_src, input_shape) = get_audio(data_format=waveform_data_format, n_ch=n_ch)
    time_axis = (1 if (waveform_data_format == 'channels_first') else 0)
    len_src = input_shape[time_axis]
    n_fft = 2048
    hop_length = int((2048 * hop_ratio))
    n_added_frames = (int((1 / hop_ratio)) - 1)
    (stft, istft) = get_perfectly_reconstructing_stft_istft(stft_input_shape=input_shape, n_fft=n_fft, hop_length=hop_length, waveform_data_format=waveform_data_format, stft_data_format=stft_data_format)
    model = tf.keras.models.Sequential([stft, istft])
    recon_waveform = model(batch_src)
    len_pad_begin = (n_fft - hop_length)
    if (waveform_data_format == 'channels_first'):
        recon_waveform = recon_waveform[:, :, len_pad_begin:(len_pad_begin + len_src)]
    else:
        recon_waveform = recon_waveform[:, len_pad_begin:(len_pad_begin + len_src), :]
    np.testing.assert_allclose(batch_src, recon_waveform, atol=1e-05)
    S = librosa.stft(src_mono, n_fft=n_fft, hop_length=hop_length).T.astype(np.complex64)
    ch_axis = (1 if (stft_data_format == 'channels_first') else 3)
    S = np.expand_dims(S, (0, ch_axis))
    model = tf.keras.models.Sequential([istft, stft])
    recon_S = model(S)
    n = n_added_frames
    n_added_frames += n
    if (stft_data_format == 'channels_first'):
        if (n != 0):
            S = S[:, :, n:(- n), :]
        recon_S = recon_S[:, :, n_added_frames:(- n_added_frames), :]
    else:
        if (n != 0):
            S = S[:, n:(- n), :, :]
        recon_S = recon_S[:, n_added_frames:(- n_added_frames), :, :]
    np.testing.assert_equal(S.shape, recon_S.shape)
    allclose_complex_numbers(S, recon_S)