Mercurial > hg > auditok
comparison tests/test_core.py @ 414:9f83c1ecb03b
implement 'join' for AudioRegion
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Tue, 15 Oct 2024 21:56:12 +0200 |
parents | 0e938065a2db |
children | e26dcf224846 |
comparison
equal
deleted
inserted
replaced
413:0a6bc66562d3 | 414:9f83c1ecb03b |
---|---|
1535 with pytest.raises(FileExistsError): | 1535 with pytest.raises(FileExistsError): |
1536 region.save(Path(filename), exists_ok=False) | 1536 region.save(Path(filename), exists_ok=False) |
1537 | 1537 |
1538 | 1538 |
1539 @pytest.mark.parametrize( | 1539 @pytest.mark.parametrize( |
1540 "sampling_rate, sample_width, channels", | |
1541 [ | |
1542 (16000, 1, 1), # mono_16K_1byte | |
1543 (16000, 2, 1), # mono_16K_2byte | |
1544 (44100, 2, 2), # stereo_44100_2byte | |
1545 (44100, 2, 3), # 3channel_44100_2byte | |
1546 ], | |
1547 ids=[ | |
1548 "mono_16K_1byte", | |
1549 "mono_16K_2byte", | |
1550 "stereo_44100_2byte", | |
1551 "3channel_44100_2byte", | |
1552 ], | |
1553 ) | |
1554 def test_join(sampling_rate, sample_width, channels): | |
1555 duration = 1 | |
1556 size = int(duration * sampling_rate * sample_width * channels) | |
1557 glue_data = b"\0" * size | |
1558 regions_data = [ | |
1559 b"\1" * int(size * 1.5), | |
1560 b"\2" * int(size * 0.5), | |
1561 b"\3" * int(size * 0.75), | |
1562 ] | |
1563 | |
1564 glue_region = AudioRegion(glue_data, sampling_rate, sample_width, channels) | |
1565 regions = [ | |
1566 AudioRegion(data, sampling_rate, sample_width, channels) | |
1567 for data in regions_data | |
1568 ] | |
1569 joined = glue_region.join(regions) | |
1570 assert joined.data == glue_data.join(regions_data) | |
1571 assert joined.duration == duration * 2 + 1.5 + 0.5 + 0.75 | |
1572 | |
1573 | |
1574 @pytest.mark.parametrize( | |
1575 "sampling_rate, sample_width, channels", | |
1576 [ | |
1577 (32000, 1, 1), # different_sampling_rate | |
1578 (16000, 2, 1), # different_sample_width | |
1579 (16000, 1, 2), # different_channels | |
1580 ], | |
1581 ids=[ | |
1582 "different_sampling_rate", | |
1583 "different_sample_width", | |
1584 "different_channels", | |
1585 ], | |
1586 ) | |
1587 def test_join_exception(sampling_rate, sample_width, channels): | |
1588 | |
1589 glue_sampling_rate = 16000 | |
1590 glue_sample_width = 1 | |
1591 glue_channels = 1 | |
1592 | |
1593 duration = 1 | |
1594 size = int( | |
1595 duration * glue_sampling_rate * glue_sample_width * glue_channels | |
1596 ) | |
1597 glue_data = b"\0" * size | |
1598 glue_region = AudioRegion( | |
1599 glue_data, glue_sampling_rate, glue_sample_width, glue_channels | |
1600 ) | |
1601 | |
1602 size = int(duration * sampling_rate * sample_width * channels) | |
1603 regions_data = [ | |
1604 b"\1" * int(size * 1.5), | |
1605 b"\2" * int(size * 0.5), | |
1606 b"\3" * int(size * 0.75), | |
1607 ] | |
1608 regions = [ | |
1609 AudioRegion(data, sampling_rate, sample_width, channels) | |
1610 for data in regions_data | |
1611 ] | |
1612 | |
1613 with pytest.raises(AudioParameterError): | |
1614 glue_region.join(regions) | |
1615 | |
1616 | |
1617 @pytest.mark.parametrize( | |
1540 "region, slice_, expected_data", | 1618 "region, slice_, expected_data", |
1541 [ | 1619 [ |
1542 ( | 1620 ( |
1543 AudioRegion(b"a" * 80 + b"b" * 80, 160, 1, 1), | 1621 AudioRegion(b"a" * 80 + b"b" * 80, 160, 1, 1), |
1544 slice(0, 500), | 1622 slice(0, 500), |
1884 | 1962 |
1885 def test_concatenation_different_sampling_rate_error(): | 1963 def test_concatenation_different_sampling_rate_error(): |
1886 region_1 = AudioRegion(b"a" * 100, 8000, 1, 1) | 1964 region_1 = AudioRegion(b"a" * 100, 8000, 1, 1) |
1887 region_2 = AudioRegion(b"b" * 100, 3000, 1, 1) | 1965 region_2 = AudioRegion(b"b" * 100, 3000, 1, 1) |
1888 | 1966 |
1889 with pytest.raises(ValueError) as val_err: | 1967 with pytest.raises(AudioParameterError) as val_err: |
1890 region_1 + region_2 | 1968 region_1 + region_2 |
1891 assert str(val_err.value) == ( | 1969 assert str(val_err.value) == ( |
1892 "Can only concatenate AudioRegions of the same " | 1970 "Can only concatenate AudioRegions of the same " |
1893 "sampling rate (8000 != 3000)" # different_sampling_rate | 1971 "sampling rate (8000 != 3000)" # different_sampling_rate |
1894 ) | 1972 ) |
1896 | 1974 |
1897 def test_concatenation_different_sample_width_error(): | 1975 def test_concatenation_different_sample_width_error(): |
1898 region_1 = AudioRegion(b"a" * 100, 8000, 2, 1) | 1976 region_1 = AudioRegion(b"a" * 100, 8000, 2, 1) |
1899 region_2 = AudioRegion(b"b" * 100, 8000, 4, 1) | 1977 region_2 = AudioRegion(b"b" * 100, 8000, 4, 1) |
1900 | 1978 |
1901 with pytest.raises(ValueError) as val_err: | 1979 with pytest.raises(AudioParameterError) as val_err: |
1902 region_1 + region_2 | 1980 region_1 + region_2 |
1903 assert str(val_err.value) == ( | 1981 assert str(val_err.value) == ( |
1904 "Can only concatenate AudioRegions of the same sample width (2 != 4)" | 1982 "Can only concatenate AudioRegions of the same sample width (2 != 4)" |
1905 ) | 1983 ) |
1906 | 1984 |
1907 | 1985 |
1908 def test_concatenation_different_number_of_channels_error(): | 1986 def test_concatenation_different_number_of_channels_error(): |
1909 region_1 = AudioRegion(b"a" * 100, 8000, 1, 1) | 1987 region_1 = AudioRegion(b"a" * 100, 8000, 1, 1) |
1910 region_2 = AudioRegion(b"b" * 100, 8000, 1, 2) | 1988 region_2 = AudioRegion(b"b" * 100, 8000, 1, 2) |
1911 | 1989 |
1912 with pytest.raises(ValueError) as val_err: | 1990 with pytest.raises(AudioParameterError) as val_err: |
1913 region_1 + region_2 | 1991 region_1 + region_2 |
1914 assert str(val_err.value) == ( | 1992 assert str(val_err.value) == ( |
1915 "Can only concatenate AudioRegions of the same " | 1993 "Can only concatenate AudioRegions of the same " |
1916 "number of channels (1 != 2)" # different_number_of_channels | 1994 "number of channels (1 != 2)" # different_number_of_channels |
1917 ) | 1995 ) |