@@ -13,32 +13,28 @@ def assertSimilar(self, bytes_1, bytes_2):
13
13
raise AssertionError ("{} is really different from {} at index {}" .format (bytes_1 , bytes_2 , i ))
14
14
15
15
def test_get_segment (self ):
16
- r = sr .Recognizer ()
17
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-32-bit-44100Hz.wav" )) as source : audio = r .record (source )
16
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-32-bit-44100Hz.wav" ))
18
17
self .assertEqual (audio .get_raw_data (), audio .get_segment ().get_raw_data ())
19
18
self .assertEqual (audio .get_raw_data ()[8 :], audio .get_segment (0.022675738 * 2 ).get_raw_data ())
20
19
self .assertEqual (audio .get_raw_data ()[:16 ], audio .get_segment (None , 0.022675738 * 4 ).get_raw_data ())
21
20
self .assertEqual (audio .get_raw_data ()[8 :16 ], audio .get_segment (0.022675738 * 2 , 0.022675738 * 4 ).get_raw_data ())
22
21
23
22
def test_wav_mono_8_bit (self ):
24
- r = sr .Recognizer ()
25
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-8-bit-44100Hz.wav" )) as source : audio = r .record (source )
23
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-8-bit-44100Hz.wav" ))
26
24
self .assertIsInstance (audio , sr .AudioData )
27
25
self .assertEqual (audio .sample_rate , 44100 )
28
26
self .assertEqual (audio .sample_width , 1 )
29
27
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \xff \x00 \xff \x00 \xff \xff \x00 \xff \x00 \xff \x00 \xff \x00 \x00 \xff \x00 \x00 \xff \x00 \xff \x00 \xff \x00 \xff \x00 \xff \x00 \xff \x00 \xff \xff " )
30
28
31
29
def test_wav_mono_16_bit (self ):
32
- r = sr .Recognizer ()
33
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-16-bit-44100Hz.wav" )) as source : audio = r .record (source )
30
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-16-bit-44100Hz.wav" ))
34
31
self .assertIsInstance (audio , sr .AudioData )
35
32
self .assertEqual (audio .sample_rate , 44100 )
36
33
self .assertEqual (audio .sample_width , 2 )
37
34
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \xff \xff \x01 \x00 \xff \xff \x00 \x00 \x01 \x00 \xfe \xff \x01 \x00 \xfe \xff \x04 \x00 \xfc \xff \x04 \x00 \xfe \xff \xff \xff \x03 \x00 \xfe \xff " )
38
35
39
36
def test_wav_mono_24_bit (self ):
40
- r = sr .Recognizer ()
41
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-24-bit-44100Hz.wav" )) as source : audio = r .record (source )
37
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-24-bit-44100Hz.wav" ))
42
38
self .assertIsInstance (audio , sr .AudioData )
43
39
self .assertEqual (audio .sample_rate , 44100 )
44
40
if audio .sample_width == 3 :
@@ -47,33 +43,28 @@ def test_wav_mono_24_bit(self):
47
43
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \x00 \x00 \x00 \x00 \xff \xff \x00 \x00 \x01 \x00 \x00 \x00 \xff \xff \x00 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \xfe \xff \x00 \x00 \x01 \x00 " )
48
44
49
45
def test_wav_mono_32_bit (self ):
50
- r = sr .Recognizer ()
51
- audio_file_path = path .join (path .dirname (path .realpath (__file__ )), "audio-mono-32-bit-44100Hz.wav" )
52
- with sr .AudioFile (audio_file_path ) as source : audio = r .record (source )
46
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-32-bit-44100Hz.wav" ))
53
47
self .assertIsInstance (audio , sr .AudioData )
54
48
self .assertEqual (audio .sample_rate , 44100 )
55
49
self .assertEqual (audio .sample_width , 4 )
56
50
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \x00 \x00 \x00 \x00 \xff \xff \x00 \x00 \x01 \x00 \x00 \x00 \xff \xff \x00 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \xfe \xff \x00 \x00 \x01 \x00 " )
57
51
58
52
def test_wav_stereo_8_bit (self ):
59
- r = sr .Recognizer ()
60
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-8-bit-44100Hz.wav" )) as source : audio = r .record (source )
53
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-8-bit-44100Hz.wav" ))
61
54
self .assertIsInstance (audio , sr .AudioData )
62
55
self .assertEqual (audio .sample_rate , 44100 )
63
56
self .assertEqual (audio .sample_width , 1 )
64
57
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \xff \x00 \xff \x00 \x00 \xff \x7f \x7f \x00 \xff \x00 \xff \x00 \x00 \xff \x00 \x7f \x7f \x7f \x00 \x00 \xff \x00 \xff \x00 \xff \x00 \x7f \x7f \x7f \x7f " )
65
58
66
59
def test_wav_stereo_16_bit (self ):
67
- r = sr .Recognizer ()
68
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-16-bit-44100Hz.wav" )) as source : audio = r .record (source )
60
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-16-bit-44100Hz.wav" ))
69
61
self .assertIsInstance (audio , sr .AudioData )
70
62
self .assertEqual (audio .sample_rate , 44100 )
71
63
self .assertEqual (audio .sample_width , 2 )
72
64
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x02 \x00 \xfb \xff \x04 \x00 \xfe \xff \xfe \xff \x07 \x00 \xf6 \xff \x07 \x00 \xf9 \xff \t \x00 \xf5 \xff \x0c \x00 \xf8 \xff \x02 \x00 \x04 \x00 \xfa \xff " )
73
65
74
66
def test_wav_stereo_24_bit (self ):
75
- r = sr .Recognizer ()
76
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-24-bit-44100Hz.wav" )) as source : audio = r .record (source )
67
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-24-bit-44100Hz.wav" ))
77
68
self .assertIsInstance (audio , sr .AudioData )
78
69
self .assertEqual (audio .sample_rate , 44100 )
79
70
if audio .sample_width == 3 :
@@ -82,40 +73,35 @@ def test_wav_stereo_24_bit(self):
82
73
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \x00 \x00 \x00 \x00 \xfe \xff \x00 \x00 \x02 \x00 \x00 \x00 \xfe \xff \x00 \x00 \x00 \x00 \x00 \x00 \x02 \x00 \x00 \x00 \xfc \xff \x00 \x00 \x02 \x00 " )
83
74
84
75
def test_wav_stereo_32_bit (self ):
85
- r = sr .Recognizer ()
86
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-32-bit-44100Hz.wav" )) as source : audio = r .record (source )
76
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-32-bit-44100Hz.wav" ))
87
77
self .assertIsInstance (audio , sr .AudioData )
88
78
self .assertEqual (audio .sample_rate , 44100 )
89
79
self .assertEqual (audio .sample_width , 4 )
90
80
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \x00 \x00 \x00 \x00 \xfe \xff \x00 \x00 \x02 \x00 \x00 \x00 \xfe \xff \x00 \x00 \x00 \x00 \x00 \x00 \x02 \x00 \x00 \x00 \xfc \xff \x00 \x00 \x02 \x00 " )
91
81
92
82
def test_aiff_mono_16_bit (self ):
93
- r = sr .Recognizer ()
94
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-16-bit-44100Hz.aiff" )) as source : audio = r .record (source )
83
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-16-bit-44100Hz.aiff" ))
95
84
self .assertIsInstance (audio , sr .AudioData )
96
85
self .assertEqual (audio .sample_rate , 44100 )
97
86
self .assertEqual (audio .sample_width , 2 )
98
87
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \x00 \x00 \xff \xff \x01 \x00 \xff \xff \x01 \x00 \xfe \xff \x02 \x00 \xfd \xff \x04 \x00 \xfc \xff \x03 \x00 \x00 \x00 \xfe \xff \x03 \x00 \xfd \xff " )
99
88
100
89
def test_aiff_stereo_16_bit (self ):
101
- r = sr .Recognizer ()
102
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-16-bit-44100Hz.aiff" )) as source : audio = r .record (source )
90
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-16-bit-44100Hz.aiff" ))
103
91
self .assertIsInstance (audio , sr .AudioData )
104
92
self .assertEqual (audio .sample_rate , 44100 )
105
93
self .assertEqual (audio .sample_width , 2 )
106
94
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \xfe \xff \x02 \x00 \xfe \xff \xff \xff \x04 \x00 \xfa \xff \x04 \x00 \xfa \xff \t \x00 \xf6 \xff \n \x00 \xfa \xff \xff \xff \x08 \x00 \xf5 \xff " )
107
95
108
96
def test_flac_mono_16_bit (self ):
109
- r = sr .Recognizer ()
110
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-16-bit-44100Hz.flac" )) as source : audio = r .record (source )
97
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-16-bit-44100Hz.flac" ))
111
98
self .assertIsInstance (audio , sr .AudioData )
112
99
self .assertEqual (audio .sample_rate , 44100 )
113
100
self .assertEqual (audio .sample_width , 2 )
114
101
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \xff \xff \x01 \x00 \xff \xff \x00 \x00 \x01 \x00 \xfe \xff \x02 \x00 \xfc \xff \x06 \x00 \xf9 \xff \x06 \x00 \xfe \xff \xfe \xff \x05 \x00 \xfa \xff " )
115
102
116
103
def test_flac_mono_24_bit (self ):
117
- r = sr .Recognizer ()
118
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-24-bit-44100Hz.flac" )) as source : audio = r .record (source )
104
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-mono-24-bit-44100Hz.flac" ))
119
105
self .assertIsInstance (audio , sr .AudioData )
120
106
self .assertEqual (audio .sample_rate , 44100 )
121
107
if audio .sample_width == 3 :
@@ -124,16 +110,14 @@ def test_flac_mono_24_bit(self):
124
110
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\x00 \x00 \x00 \x00 \x00 \xff \xfe \xff \x00 \x02 \x01 \x00 \x00 \xfd \xfe \xff \x00 \x04 \x00 \x00 \x00 \xfc \x00 \x00 \x00 \x04 \xfe \xff \x00 \xfb \x00 \x00 " )
125
111
126
112
def test_flac_stereo_16_bit (self ):
127
- r = sr .Recognizer ()
128
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-16-bit-44100Hz.flac" )) as source : audio = r .record (source )
113
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-16-bit-44100Hz.flac" ))
129
114
self .assertIsInstance (audio , sr .AudioData )
130
115
self .assertEqual (audio .sample_rate , 44100 )
131
116
self .assertEqual (audio .sample_width , 2 )
132
117
self .assertSimilar (audio .get_raw_data ()[:32 ], b"\xff \xff \xff \xff \x02 \x00 \xfe \xff \x00 \x00 \x01 \x00 \xfd \xff \x01 \x00 \xff \xff \x04 \x00 \xfa \xff \x05 \x00 \xff \xff \xfd \xff \x08 \x00 \xf6 \xff " )
133
118
134
119
def test_flac_stereo_24_bit (self ):
135
- r = sr .Recognizer ()
136
- with sr .AudioFile (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-24-bit-44100Hz.flac" )) as source : audio = r .record (source )
120
+ audio = sr .AudioData .from_file (path .join (path .dirname (path .realpath (__file__ )), "audio-stereo-24-bit-44100Hz.flac" ))
137
121
self .assertIsInstance (audio , sr .AudioData )
138
122
self .assertEqual (audio .sample_rate , 44100 )
139
123
if audio .sample_width == 3 :
0 commit comments