comparison tests/test_core.py @ 415:e26dcf224846

implement 'make_silence'
author Amine Sehili <amine.sehili@gmail.com>
date Wed, 16 Oct 2024 20:08:37 +0200
parents 9f83c1ecb03b
children 14efef6f4bae
comparison
equal deleted inserted replaced
414:9f83c1ecb03b 415:e26dcf224846
6 from unittest.mock import Mock, patch 6 from unittest.mock import Mock, patch
7 7
8 import numpy as np 8 import numpy as np
9 import pytest 9 import pytest
10 10
11 from auditok import AudioParameterError, AudioRegion, load, split 11 from auditok import AudioParameterError, AudioRegion, load, make_silence, split
12 from auditok.core import ( 12 from auditok.core import (
13 _duration_to_nb_windows, 13 _duration_to_nb_windows,
14 _make_audio_region, 14 _make_audio_region,
15 _read_chunks_online, 15 _read_chunks_online,
16 _read_offline, 16 _read_offline,
72 to_read = -1 72 to_read = -1
73 else: 73 else:
74 to_read = round(max_read * sampling_rate * sample_width * channels) 74 to_read = round(max_read * sampling_rate * sample_width * channels)
75 expected = fp.read(to_read) 75 expected = fp.read(to_read)
76 assert bytes(region) == expected 76 assert bytes(region) == expected
77
78
79 @pytest.mark.parametrize(
80 "duration, sampling_rate, sample_width, channels",
81 [
82 (1.05, 16000, 1, 1), # mono_16K_1byte
83 (1.5, 16000, 2, 1), # mono_16K_2byte
84 (1.0001, 44100, 2, 2), # stereo_44100_2byte
85 (1.000005, 48000, 2, 3), # 3channel_48K_2byte
86 (1.0001, 48000, 4, 4), # 4channel_48K_4byte
87 (0, 48000, 4, 4), # 4channel_4K_4byte_0sec
88 ],
89 ids=[
90 "mono_16K_1byte",
91 "mono_16K_2byte",
92 "stereo_44100_2byte",
93 "3channel_48000_2byte",
94 "4channel_48K_4byte",
95 "4channel_4K_4byte_0sec",
96 ],
97 )
98 def test_make_silence(duration, sampling_rate, sample_width, channels):
99 silence = make_silence(duration, sampling_rate, sample_width, channels)
100 size = round(duration * sampling_rate) * sample_width * channels
101 expected_data = b"\0" * size
102 expected_duration = size / (sampling_rate * sample_width * channels)
103 assert silence.duration == expected_duration
104 assert silence.data == expected_data
77 105
78 106
79 @pytest.mark.parametrize( 107 @pytest.mark.parametrize(
80 "duration, analysis_window, round_fn, expected, kwargs", 108 "duration, analysis_window, round_fn, expected, kwargs",
81 [ 109 [