forked from Uberi/speech_recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_audio.py
59 lines (50 loc) · 2.69 KB
/
test_audio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
import os
import wave
import aifc
import io
import subprocess
import unittest
import speech_recognition as sr
class TestAudioFile(unittest.TestCase):
def setUp(self):
self.AUDIO_FILE_WAV = os.path.join(os.path.dirname(os.path.realpath(__file__)), "english.wav")
self.AUDIO_FILE_AIFF = os.path.join(os.path.dirname(os.path.realpath(__file__)), "french.aiff")
self.AUDIO_FILE_FLAC = os.path.join(os.path.dirname(os.path.realpath(__file__)), "chinese.flac")
def test_wav_load(self):
r = sr.Recognizer()
with sr.AudioFile(self.AUDIO_FILE_WAV) as source: audio = r.record(source)
self.assertIsInstance(audio, sr.AudioData)
audio_reader = wave.open(self.AUDIO_FILE_WAV, "rb")
self.assertEqual(audio.sample_rate, audio_reader.getframerate())
self.assertEqual(audio.sample_width, audio_reader.getsampwidth())
self.assertEqual(audio.get_raw_data(), audio_reader.readframes(audio_reader.getnframes()))
audio_reader.close()
def test_aiff_load(self):
r = sr.Recognizer()
with sr.AudioFile(self.AUDIO_FILE_AIFF) as source: audio = r.record(source)
self.assertIsInstance(audio, sr.AudioData)
audio_reader = aifc.open(self.AUDIO_FILE_AIFF, "rb")
self.assertEqual(audio.sample_rate, audio_reader.getframerate())
self.assertEqual(audio.sample_width, audio_reader.getsampwidth())
aiff_data = audio_reader.readframes(audio_reader.getnframes())
aiff_data_little_endian = aiff_data[1::-1] + b"".join(aiff_data[i + 2:i:-1] for i in range(1, len(aiff_data), 2))
self.assertEqual(audio.get_raw_data(), aiff_data_little_endian)
audio_reader.close()
def test_flac_load(self):
r = sr.Recognizer()
with sr.AudioFile(self.AUDIO_FILE_FLAC) as source: audio = r.record(source)
self.assertIsInstance(audio, sr.AudioData)
process = subprocess.Popen([sr.get_flac_converter(), "--stdout", "--totally-silent", "--decode", "--force-aiff-format", self.AUDIO_FILE_FLAC], stdout=subprocess.PIPE)
aiff_data, _ = process.communicate()
aiff_file = io.BytesIO(aiff_data)
audio_reader = aifc.open(aiff_file, "rb")
self.assertEqual(audio.sample_rate, audio_reader.getframerate())
self.assertEqual(audio.sample_width, audio_reader.getsampwidth())
aiff_data = audio_reader.readframes(audio_reader.getnframes())
aiff_data_little_endian = aiff_data[1::-1] + b"".join(aiff_data[i + 2:i:-1] for i in range(1, len(aiff_data), 2))
self.assertEqual(audio.get_raw_data(), aiff_data_little_endian)
audio_reader.close()
aiff_file.close()
if __name__ == "__main__":
unittest.main()