@@ -13,32 +13,28 @@ def assertSimilar(self, bytes_1, bytes_2):
1313 raise AssertionError ("{} is really different from {} at index {}" .format (bytes_1 , bytes_2 , i ))
1414
1515 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" ))
1817 self .assertEqual (audio .get_raw_data (), audio .get_segment ().get_raw_data ())
1918 self .assertEqual (audio .get_raw_data ()[8 :], audio .get_segment (0.022675738 * 2 ).get_raw_data ())
2019 self .assertEqual (audio .get_raw_data ()[:16 ], audio .get_segment (None , 0.022675738 * 4 ).get_raw_data ())
2120 self .assertEqual (audio .get_raw_data ()[8 :16 ], audio .get_segment (0.022675738 * 2 , 0.022675738 * 4 ).get_raw_data ())
2221
2322 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" ))
2624 self .assertIsInstance (audio , sr .AudioData )
2725 self .assertEqual (audio .sample_rate , 44100 )
2826 self .assertEqual (audio .sample_width , 1 )
2927 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 " )
3028
3129 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" ))
3431 self .assertIsInstance (audio , sr .AudioData )
3532 self .assertEqual (audio .sample_rate , 44100 )
3633 self .assertEqual (audio .sample_width , 2 )
3734 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 " )
3835
3936 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" ))
4238 self .assertIsInstance (audio , sr .AudioData )
4339 self .assertEqual (audio .sample_rate , 44100 )
4440 if audio .sample_width == 3 :
@@ -47,33 +43,28 @@ def test_wav_mono_24_bit(self):
4743 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 " )
4844
4945 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" ))
5347 self .assertIsInstance (audio , sr .AudioData )
5448 self .assertEqual (audio .sample_rate , 44100 )
5549 self .assertEqual (audio .sample_width , 4 )
5650 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 " )
5751
5852 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" ))
6154 self .assertIsInstance (audio , sr .AudioData )
6255 self .assertEqual (audio .sample_rate , 44100 )
6356 self .assertEqual (audio .sample_width , 1 )
6457 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 " )
6558
6659 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" ))
6961 self .assertIsInstance (audio , sr .AudioData )
7062 self .assertEqual (audio .sample_rate , 44100 )
7163 self .assertEqual (audio .sample_width , 2 )
7264 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 " )
7365
7466 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" ))
7768 self .assertIsInstance (audio , sr .AudioData )
7869 self .assertEqual (audio .sample_rate , 44100 )
7970 if audio .sample_width == 3 :
@@ -82,40 +73,35 @@ def test_wav_stereo_24_bit(self):
8273 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 " )
8374
8475 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" ))
8777 self .assertIsInstance (audio , sr .AudioData )
8878 self .assertEqual (audio .sample_rate , 44100 )
8979 self .assertEqual (audio .sample_width , 4 )
9080 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 " )
9181
9282 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" ))
9584 self .assertIsInstance (audio , sr .AudioData )
9685 self .assertEqual (audio .sample_rate , 44100 )
9786 self .assertEqual (audio .sample_width , 2 )
9887 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 " )
9988
10089 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" ))
10391 self .assertIsInstance (audio , sr .AudioData )
10492 self .assertEqual (audio .sample_rate , 44100 )
10593 self .assertEqual (audio .sample_width , 2 )
10694 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 " )
10795
10896 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" ))
11198 self .assertIsInstance (audio , sr .AudioData )
11299 self .assertEqual (audio .sample_rate , 44100 )
113100 self .assertEqual (audio .sample_width , 2 )
114101 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 " )
115102
116103 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" ))
119105 self .assertIsInstance (audio , sr .AudioData )
120106 self .assertEqual (audio .sample_rate , 44100 )
121107 if audio .sample_width == 3 :
@@ -124,16 +110,14 @@ def test_flac_mono_24_bit(self):
124110 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 " )
125111
126112 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" ))
129114 self .assertIsInstance (audio , sr .AudioData )
130115 self .assertEqual (audio .sample_rate , 44100 )
131116 self .assertEqual (audio .sample_width , 2 )
132117 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 " )
133118
134119 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" ))
137121 self .assertIsInstance (audio , sr .AudioData )
138122 self .assertEqual (audio .sample_rate , 44100 )
139123 if audio .sample_width == 3 :
0 commit comments