Mercurial > hg > auditok
comparison tests/test_core.py @ 411:0e938065a2db
AudioRegion as a dataclass
author | Amine Sehili <amine.sehili@gmail.com> |
---|---|
date | Thu, 20 Jun 2024 21:45:08 +0200 |
parents | f56b4d8adfb8 |
children | 9f83c1ecb03b |
comparison
equal
deleted
inserted
replaced
410:9c9112e23c1c | 411:0e938065a2db |
---|---|
1 import math | 1 import math |
2 import os | 2 import os |
3 from pathlib import Path | |
3 from random import random | 4 from random import random |
4 from tempfile import TemporaryDirectory | 5 from tempfile import TemporaryDirectory |
5 from unittest.mock import Mock, patch | 6 from unittest.mock import Mock, patch |
6 | 7 |
7 import numpy as np | 8 import numpy as np |
1360 channels, | 1361 channels, |
1361 expected_end, | 1362 expected_end, |
1362 expected_duration_s, | 1363 expected_duration_s, |
1363 expected_duration_ms, | 1364 expected_duration_ms, |
1364 ): | 1365 ): |
1365 meta = {"start": start, "end": expected_end} | 1366 region = AudioRegion(data, sampling_rate, sample_width, channels, start) |
1366 region = AudioRegion(data, sampling_rate, sample_width, channels, meta) | |
1367 assert region.sampling_rate == sampling_rate | 1367 assert region.sampling_rate == sampling_rate |
1368 assert region.sr == sampling_rate | 1368 assert region.sr == sampling_rate |
1369 assert region.sample_width == sample_width | 1369 assert region.sample_width == sample_width |
1370 assert region.sw == sample_width | 1370 assert region.sw == sample_width |
1371 assert region.channels == channels | 1371 assert region.channels == channels |
1516 "start_end_duration_2", | 1516 "start_end_duration_2", |
1517 ], | 1517 ], |
1518 ) | 1518 ) |
1519 def test_save(format, start, expected): | 1519 def test_save(format, start, expected): |
1520 with TemporaryDirectory() as tmpdir: | 1520 with TemporaryDirectory() as tmpdir: |
1521 region = AudioRegion(b"0" * 160, 160, 1, 1) | 1521 region = AudioRegion(b"0" * 160, 160, 1, 1, start) |
1522 meta = {"start": start, "end": start + region.duration} | |
1523 region.meta = meta | |
1524 format = os.path.join(tmpdir, format) | 1522 format = os.path.join(tmpdir, format) |
1525 filename = region.save(format)[len(tmpdir) + 1 :] | 1523 filename = region.save(format)[len(tmpdir) + 1 :] |
1526 assert filename == expected | 1524 assert filename == expected |
1527 | 1525 |
1528 | 1526 |
1531 filename = os.path.join(tmpdir, "output.wav") | 1529 filename = os.path.join(tmpdir, "output.wav") |
1532 open(filename, "w").close() | 1530 open(filename, "w").close() |
1533 region = AudioRegion(b"0" * 160, 160, 1, 1) | 1531 region = AudioRegion(b"0" * 160, 160, 1, 1) |
1534 with pytest.raises(FileExistsError): | 1532 with pytest.raises(FileExistsError): |
1535 region.save(filename, exists_ok=False) | 1533 region.save(filename, exists_ok=False) |
1534 | |
1535 with pytest.raises(FileExistsError): | |
1536 region.save(Path(filename), exists_ok=False) | |
1536 | 1537 |
1537 | 1538 |
1538 @pytest.mark.parametrize( | 1539 @pytest.mark.parametrize( |
1539 "region, slice_, expected_data", | 1540 "region, slice_, expected_data", |
1540 [ | 1541 [ |