def wav_to_mp3(x, sample_rate):
            """
            Apply MP3 compression to audio input of shape (samples, channel).
            """
            from pydub import AudioSegment
            from scipy.io.wavfile import write

            x_dtype = x.dtype
            normalized = bool(x.min() >= -1.0 and x.max() <= 1.0)
            if x_dtype != np.int16 and not normalized:
                # input is not of type np.int16 and seems to be unnormalized. Therefore, casting to np.int16.
                x = x.astype(np.int16)
            elif x_dtype != np.int16 and normalized:
                # x is not of type np.int16 and seems to be normalized. Therefore, undoing normalization and
                # casting to np.int16.
                x = (x * 2**15).astype(np.int16)

            tmp_wav, tmp_mp3 = BytesIO(), BytesIO()
            write(tmp_wav, sample_rate, x)
            AudioSegment.from_wav(tmp_wav).export(tmp_mp3)
            audio_segment = AudioSegment.from_mp3(tmp_mp3)
            tmp_wav.close()
            tmp_mp3.close()
            x_mp3 = np.array(audio_segment.get_array_of_samples()).reshape((-1, audio_segment.channels))

            # WARNING: Sometimes we *still* need to manually resize x_mp3 to original length.
            # This should not be the case, e.g. see https://github.com/jiaaro/pydub/issues/474
            if x.shape[0] != x_mp3.shape[0]:  # pragma: no cover
                logger.warning(
                    "Lengths original input and compressed output don't match. Truncating compressed result."
                )
                x_mp3 = x_mp3[: x.shape[0]]

            if normalized:
                # x was normalized. Therefore, normalizing x_mp3.
                x_mp3 = x_mp3 * 2**-15
            return x_mp3.astype(x_dtype)

            Apply MP3 compression to audio input of shape (samples, channel).
            
art/defences/preprocessor/mp3_compression.py