Esempio n. 1
0
    def test_read_float_wav_with_dtype(self):
        """Test reading a float WAV with conversion to specified dtype."""
        samples = np.expand_dims(
            [0.0, 1e-6, -1e-4, 0.1, -0.5, 1.0, -1.0, np.inf, -np.inf, np.nan],
            axis=1).astype(np.float32)
        wav_bytes = make_float_wav(samples, 8000)

        # float32 -> int16.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.int16)
        self.assert_equal_same_dtype(
            out,
            np.expand_dims(
                [0, 0, -3, 3277, -16384, 32767, -32768, 32767, -32768, 0],
                axis=1).astype(np.int16))
        # float32 -> int32.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.int32)
        self.assert_equal_same_dtype(
            out,
            np.expand_dims([
                0, 2147, -214748, 214748368, -1073741824, 2147483647,
                -2147483648, 2147483647, -2147483648, 0
            ],
                           axis=1).astype(np.int32))
        # float32 -> float32.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.float32)
        self.assert_equal_same_dtype(out, samples)
Esempio n. 2
0
    def test_read_16_bit_wav_with_dtype(self):
        """Test reading a 16-bit WAV with conversion to specified dtype."""
        samples = np.expand_dims([0, 1, 2, -5, 25000, 32767, -32768],
                                 axis=1).astype(np.int16)
        wav_bytes = wav_io.to_bytes(samples, 8000)

        # int16 -> int16.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.int16)
        self.assert_equal_same_dtype(out, samples)
        # int16 -> int32.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.int32)
        self.assert_equal_same_dtype(out, samples.astype(np.int32) * 2**16)
        # int16 -> float32.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.float32)
        self.assert_equal_same_dtype(out, samples.astype(np.float32) / 2.0**15)
Esempio n. 3
0
    def test_from_bytes(self):
        """Read WAV from a byte string with from_bytes()."""
        samples, sample_rate_hz = wav_io.from_bytes(self.wav_bytes)

        self.assertEqual(samples.dtype, np.int16)
        np.testing.assert_array_equal(samples, self.wav_samples)
        self.assertEqual(sample_rate_hz, 48000)
Esempio n. 4
0
    def test_read_24_bit_wav(self):
        """Read a 48kHz mono WAV file with 24-bit samples."""
        np.random.seed(0)
        expected = np.random.randint(-2**23, 2**23 - 1, size=(20, 3)) * 256
        wav_bytes = make_24_bit_wav(expected, 44100)

        samples, sample_rate_hz = wav_io.from_bytes(wav_bytes)

        self.assertEqual(samples.dtype, np.int32)
        np.testing.assert_array_equal(samples, expected)
        self.assertEqual(sample_rate_hz, 44100)

        # Read with conversion to float32.
        samples, _ = wav_io.from_bytes(wav_bytes, dtype=np.float32)
        self.assert_equal_same_dtype(samples,
                                     expected.astype(np.float32) / 2.0**31)
Esempio n. 5
0
    def test_to_bytes(self):
        """Write WAV to byte string with to_bytes()."""
        wav_bytes = wav_io.to_bytes(self.wav_samples, 44100)

        samples, sample_rate_hz = wav_io.from_bytes(wav_bytes)
        np.testing.assert_array_equal(samples, self.wav_samples)
        self.assertEqual(sample_rate_hz, 44100)
Esempio n. 6
0
    def test_write_wav_1d_array(self):
        """Test writing a 1D array as a mono WAV file."""
        samples = np.arange(20, dtype=np.int16)
        recovered, sample_rate_hz = wav_io.from_bytes(
            wav_io.to_bytes(samples, 8000))

        np.testing.assert_array_equal(recovered, samples.reshape(-1, 1))
        self.assertEqual(sample_rate_hz, 8000)
Esempio n. 7
0
    def test_read_24_bit_wav_with_dtype(self):
        """Test reading a 24-bit WAV with conversion to specified dtype."""
        samples = 256 * np.expand_dims([1, -1500000, 2**23 - 1, -2**23],
                                       axis=1).astype(np.int32)
        wav_bytes = make_24_bit_wav(samples, 8000)

        # int32 -> int16.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.int16)
        self.assert_equal_same_dtype(
            out,
            np.expand_dims([0, -5859, 32767, -32768], axis=1).astype(np.int16))
        # int32 -> int32.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.int32)
        self.assert_equal_same_dtype(out, samples)
        # int32 -> float32.
        out, _ = wav_io.from_bytes(wav_bytes, dtype=np.float32)
        self.assert_equal_same_dtype(out, samples.astype(np.float32) / 2.0**31)
Esempio n. 8
0
    def test_read_float_wav(self):
        """Read a 48kHz mono WAV file with 32-bit float samples."""
        np.random.seed(0)
        expected = np.random.randn(15, 4).astype(np.float32)
        wav_bytes = make_float_wav(expected, 48000)

        samples, sample_rate_hz = wav_io.from_bytes(wav_bytes)

        self.assertEqual(samples.dtype, np.float32)
        np.testing.assert_array_equal(samples, expected)
        self.assertEqual(sample_rate_hz, 48000)
Esempio n. 9
0
 def test_read_wav_bad_fact_chunk(self):
     """Test where WAV fact chunk is corrupt."""
     with six.assertRaisesRegex(self, OSError, 'error reading WAV header'):
         wav_io.from_bytes(b'RIFF_\000\000\000WAVEfactbusted')
Esempio n. 10
0
 def test_read_wav_bad_dtype(self):
     """Test where WAV fact chunk is corrupt."""
     with six.assertRaisesRegex(self, ValueError, 'dtype must be one of'):
         wav_io.from_bytes(b'RIFF', dtype=np.uint8)