annotate tests/test_audio_source.py @ 157:682bf4477fae

Move helper test functions to a new file
author Amine Sehili <amine.sehili@gmail.com>
date Sun, 24 Feb 2019 17:16:53 +0100
parents 71181bbe312a
children 017994445d87
rev   line source
amine@93 1 """
amine@2 2 @author: Amine Sehili <amine.sehili@gmail.com>
amine@93 3 """
amine@2 4 import unittest
amine@91 5 from auditok import BufferAudioSource, AudioParameterError
amine@2 6
amine@2 7
amine@2 8 class TestBufferAudioSource_SR10_SW1_CH1(unittest.TestCase):
amine@2 9 def setUp(self):
amine@92 10 self.data = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
amine@93 11 self.audio_source = BufferAudioSource(
amine@93 12 data_buffer=self.data, sampling_rate=10, sample_width=1, channels=1
amine@93 13 )
amine@2 14 self.audio_source.open()
amine@71 15
amine@2 16 def tearDown(self):
amine@2 17 self.audio_source.close()
amine@2 18
amine@2 19 def test_sr10_sw1_ch1_read_1(self):
amine@2 20 block = self.audio_source.read(1)
amine@92 21 exp = b"A"
amine@93 22 self.assertEqual(
amine@93 23 block,
amine@93 24 exp,
amine@93 25 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 26 )
amine@71 27
amine@2 28 def test_sr10_sw1_ch1_read_6(self):
amine@2 29 block = self.audio_source.read(6)
amine@92 30 exp = b"ABCDEF"
amine@93 31 self.assertEqual(
amine@93 32 block,
amine@93 33 exp,
amine@93 34 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 35 )
amine@71 36
amine@2 37 def test_sr10_sw1_ch1_read_multiple(self):
amine@2 38 block = self.audio_source.read(1)
amine@92 39 exp = b"A"
amine@93 40 self.assertEqual(
amine@93 41 block,
amine@93 42 exp,
amine@93 43 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 44 )
amine@71 45
amine@2 46 block = self.audio_source.read(6)
amine@92 47 exp = b"BCDEFG"
amine@93 48 self.assertEqual(
amine@93 49 block,
amine@93 50 exp,
amine@93 51 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 52 )
amine@71 53
amine@2 54 block = self.audio_source.read(13)
amine@92 55 exp = b"HIJKLMNOPQRST"
amine@93 56 self.assertEqual(
amine@93 57 block,
amine@93 58 exp,
amine@93 59 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 60 )
amine@71 61
amine@2 62 block = self.audio_source.read(9999)
amine@92 63 exp = b"UVWXYZ012345"
amine@93 64 self.assertEqual(
amine@93 65 block,
amine@93 66 exp,
amine@93 67 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 68 )
amine@71 69
amine@2 70 def test_sr10_sw1_ch1_read_all(self):
amine@2 71 block = self.audio_source.read(9999)
amine@93 72 self.assertEqual(
amine@93 73 block,
amine@93 74 self.data,
amine@93 75 msg="wrong block, expected: {}, found: {} ".format(
amine@93 76 self.data, block
amine@93 77 ),
amine@93 78 )
amine@71 79
amine@2 80 block = self.audio_source.read(1)
amine@93 81 self.assertEqual(
amine@93 82 block,
amine@93 83 None,
amine@93 84 msg="wrong block, expected: {}, found: {} ".format(None, block),
amine@93 85 )
amine@71 86
amine@2 87 def test_sr10_sw1_ch1_get_sampling_rate(self):
amine@2 88 srate = self.audio_source.get_sampling_rate()
amine@93 89 self.assertEqual(
amine@93 90 srate,
amine@93 91 10,
amine@93 92 msg="wrong sampling rate, expected: 10, found: {0} ".format(srate),
amine@93 93 )
amine@71 94
amine@2 95 def test_sr10_sw1_ch1_get_sample_width(self):
amine@2 96 swidth = self.audio_source.get_sample_width()
amine@93 97 self.assertEqual(
amine@93 98 swidth,
amine@93 99 1,
amine@93 100 msg="wrong sample width, expected: 1, found: {0} ".format(swidth),
amine@93 101 )
amine@71 102
amine@2 103 def test_sr10_sw1_ch1_get_channels(self):
amine@2 104 channels = self.audio_source.get_channels()
amine@93 105 self.assertEqual(
amine@93 106 channels,
amine@93 107 1,
amine@93 108 msg="wrong number of channels, expected: 1, found: {0} ".format(
amine@93 109 channels
amine@93 110 ),
amine@93 111 )
amine@71 112
amine@2 113 def test_sr10_sw1_ch1_get_position_0(self):
amine@2 114 pos = self.audio_source.get_position()
amine@93 115 self.assertEqual(
amine@93 116 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
amine@93 117 )
amine@71 118
amine@2 119 def test_sr10_sw1_ch1_get_position_5(self):
amine@2 120 self.audio_source.read(5)
amine@2 121 pos = self.audio_source.get_position()
amine@93 122 self.assertEqual(
amine@93 123 pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)
amine@93 124 )
amine@71 125
amine@2 126 def test_sr10_sw1_ch1_get_position_25(self):
amine@2 127 self.audio_source.read(5)
amine@2 128 self.audio_source.read(20)
amine@71 129
amine@2 130 pos = self.audio_source.get_position()
amine@93 131 self.assertEqual(
amine@93 132 pos, 25, msg="wrong position, expected: 5, found: {0} ".format(pos)
amine@93 133 )
amine@71 134
amine@2 135 def test_sr10_sw1_ch1_set_position_0(self):
amine@2 136 self.audio_source.read(10)
amine@2 137 self.audio_source.set_position(0)
amine@2 138 pos = self.audio_source.get_position()
amine@93 139 self.assertEqual(
amine@93 140 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
amine@93 141 )
amine@71 142
amine@2 143 def test_sr10_sw1_ch1_set_position_10(self):
amine@2 144 self.audio_source.set_position(10)
amine@2 145 pos = self.audio_source.get_position()
amine@93 146 self.assertEqual(
amine@93 147 pos,
amine@93 148 10,
amine@93 149 msg="wrong position, expected: 10, found: {0} ".format(pos),
amine@93 150 )
amine@71 151
amine@2 152 def test_sr10_sw1_ch1_get_time_position_0(self):
amine@2 153 tp = self.audio_source.get_time_position()
amine@93 154 self.assertEqual(
amine@93 155 tp,
amine@93 156 0.0,
amine@93 157 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
amine@93 158 )
amine@71 159
amine@2 160 def test_sr10_sw1_ch1_get_time_position_1(self):
amine@2 161 srate = self.audio_source.get_sampling_rate()
amine@2 162 # read one second
amine@2 163 self.audio_source.read(srate)
amine@2 164 tp = self.audio_source.get_time_position()
amine@93 165 self.assertEqual(
amine@93 166 tp,
amine@93 167 1.0,
amine@93 168 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 169 )
amine@71 170
amine@2 171 def test_sr10_sw1_ch1_get_time_position_2_5(self):
amine@2 172 # read 2.5 seconds
amine@2 173 self.audio_source.read(25)
amine@2 174 tp = self.audio_source.get_time_position()
amine@93 175 self.assertEqual(
amine@93 176 tp,
amine@93 177 2.5,
amine@93 178 msg="wrong time position, expected: 2.5, found: {0} ".format(tp),
amine@93 179 )
amine@71 180
amine@2 181 def test_sr10_sw1_ch1_set_time_position_0(self):
amine@2 182 self.audio_source.read(10)
amine@2 183 self.audio_source.set_time_position(0)
amine@2 184 tp = self.audio_source.get_time_position()
amine@93 185 self.assertEqual(
amine@93 186 tp,
amine@93 187 0.0,
amine@93 188 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
amine@93 189 )
amine@71 190
amine@2 191 def test_sr10_sw1_ch1_set_time_position_1(self):
amine@2 192 self.audio_source.set_time_position(1)
amine@2 193 tp = self.audio_source.get_time_position()
amine@93 194 self.assertEqual(
amine@93 195 tp,
amine@93 196 1.0,
amine@93 197 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 198 )
amine@71 199
amine@2 200 def test_sr10_sw1_ch1_set_time_position_end(self):
amine@2 201 self.audio_source.set_time_position(100)
amine@2 202 tp = self.audio_source.get_time_position()
amine@93 203 self.assertEqual(
amine@93 204 tp,
amine@93 205 3.2,
amine@93 206 msg="wrong time position, expected: 3.2, found: {0} ".format(tp),
amine@93 207 )
amine@71 208
amine@2 209 def test_sr10_sw1_ch1_rewind(self):
amine@2 210 self.audio_source.read(10)
amine@2 211 self.audio_source.rewind()
amine@2 212 tp = self.audio_source.get_position()
amine@93 213 self.assertEqual(
amine@93 214 tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)
amine@93 215 )
amine@71 216
amine@2 217 def test_sr10_sw1_ch1_set_data(self):
amine@92 218 self.audio_source.set_data(b"12345")
amine@2 219 block = self.audio_source.read(9999)
amine@93 220 self.assertEqual(
amine@93 221 block,
amine@93 222 b"12345",
amine@93 223 msg="wrong block, expected: '12345', found: {0} ".format(block),
amine@93 224 )
amine@71 225
amine@2 226 def test_sr10_sw1_ch1_read_closed(self):
amine@2 227 self.audio_source.close()
amine@2 228 with self.assertRaises(Exception):
amine@2 229 self.audio_source.read(1)
amine@71 230
amine@2 231
amine@2 232 class TestBufferAudioSource_SR16_SW2_CH1(unittest.TestCase):
amine@2 233 def setUp(self):
amine@92 234 self.data = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ012345"
amine@93 235 self.audio_source = BufferAudioSource(
amine@93 236 data_buffer=self.data, sampling_rate=16, sample_width=2, channels=1
amine@93 237 )
amine@2 238 self.audio_source.open()
amine@71 239
amine@2 240 def tearDown(self):
amine@2 241 self.audio_source.close()
amine@2 242
amine@2 243 def test_sr16_sw2_ch1_read_1(self):
amine@2 244 block = self.audio_source.read(1)
amine@92 245 exp = b"AB"
amine@93 246 self.assertEqual(
amine@93 247 block,
amine@93 248 exp,
amine@93 249 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 250 )
amine@71 251
amine@2 252 def test_sr16_sw2_ch1_read_6(self):
amine@2 253 block = self.audio_source.read(6)
amine@92 254 exp = b"ABCDEFGHIJKL"
amine@93 255 self.assertEqual(
amine@93 256 block,
amine@93 257 exp,
amine@93 258 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 259 )
amine@71 260
amine@2 261 def test_sr16_sw2_ch1_read_multiple(self):
amine@2 262 block = self.audio_source.read(1)
amine@92 263 exp = b"AB"
amine@93 264 self.assertEqual(
amine@93 265 block,
amine@93 266 exp,
amine@93 267 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 268 )
amine@71 269
amine@2 270 block = self.audio_source.read(6)
amine@92 271 exp = b"CDEFGHIJKLMN"
amine@93 272 self.assertEqual(
amine@93 273 block,
amine@93 274 exp,
amine@93 275 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 276 )
amine@71 277
amine@2 278 block = self.audio_source.read(5)
amine@92 279 exp = b"OPQRSTUVWX"
amine@93 280 self.assertEqual(
amine@93 281 block,
amine@93 282 exp,
amine@93 283 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 284 )
amine@71 285
amine@2 286 block = self.audio_source.read(9999)
amine@92 287 exp = b"YZ012345"
amine@93 288 self.assertEqual(
amine@93 289 block,
amine@93 290 exp,
amine@93 291 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 292 )
amine@71 293
amine@2 294 def test_sr16_sw2_ch1_read_all(self):
amine@2 295 block = self.audio_source.read(9999)
amine@93 296 self.assertEqual(
amine@93 297 block,
amine@93 298 self.data,
amine@93 299 msg="wrong block, expected: {0}, found: {1} ".format(
amine@93 300 self.data, block
amine@93 301 ),
amine@93 302 )
amine@71 303
amine@2 304 block = self.audio_source.read(1)
amine@93 305 self.assertEqual(
amine@93 306 block,
amine@93 307 None,
amine@93 308 msg="wrong block, expected: {0}, found: {1} ".format(None, block),
amine@93 309 )
amine@71 310
amine@2 311 def test_sr16_sw2_ch1_get_sampling_rate(self):
amine@2 312 srate = self.audio_source.get_sampling_rate()
amine@93 313 self.assertEqual(
amine@93 314 srate,
amine@93 315 16,
amine@93 316 msg="wrong sampling rate, expected: 10, found: {0} ".format(srate),
amine@93 317 )
amine@71 318
amine@2 319 def test_sr16_sw2_ch1_get_sample_width(self):
amine@2 320 swidth = self.audio_source.get_sample_width()
amine@93 321 self.assertEqual(
amine@93 322 swidth,
amine@93 323 2,
amine@93 324 msg="wrong sample width, expected: 1, found: {0} ".format(swidth),
amine@93 325 )
amine@71 326
amine@2 327 def test_sr16_sw2_ch1_get_channels(self):
amine@71 328
amine@2 329 channels = self.audio_source.get_channels()
amine@93 330 self.assertEqual(
amine@93 331 channels,
amine@93 332 1,
amine@93 333 msg="wrong number of channels, expected: 1, found: {0} ".format(
amine@93 334 channels
amine@93 335 ),
amine@93 336 )
amine@71 337
amine@2 338 def test_sr16_sw2_ch1_get_position_0(self):
amine@2 339 pos = self.audio_source.get_position()
amine@93 340 self.assertEqual(
amine@93 341 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
amine@93 342 )
amine@71 343
amine@2 344 def test_sr16_sw2_ch1_get_position_5(self):
amine@2 345 self.audio_source.read(5)
amine@2 346 pos = self.audio_source.get_position()
amine@93 347 self.assertEqual(
amine@93 348 pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)
amine@93 349 )
amine@71 350
amine@2 351 def test_sr16_sw2_ch1_get_position_15(self):
amine@2 352 self.audio_source.read(5)
amine@2 353 self.audio_source.read(10)
amine@2 354 pos = self.audio_source.get_position()
amine@93 355 self.assertEqual(
amine@93 356 pos, 15, msg="wrong position, expected: 5, found: {0} ".format(pos)
amine@93 357 )
amine@71 358
amine@2 359 def test_sr16_sw2_ch1_set_position_0(self):
amine@2 360 self.audio_source.read(10)
amine@2 361 self.audio_source.set_position(0)
amine@2 362 pos = self.audio_source.get_position()
amine@93 363 self.assertEqual(
amine@93 364 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
amine@93 365 )
amine@71 366
amine@2 367 def test_sr16_sw2_ch1_set_position_10(self):
amine@2 368 self.audio_source.set_position(10)
amine@2 369 pos = self.audio_source.get_position()
amine@93 370 self.assertEqual(
amine@93 371 pos,
amine@93 372 10,
amine@93 373 msg="wrong position, expected: 10, found: {0} ".format(pos),
amine@93 374 )
amine@71 375
amine@2 376 def test_sr16_sw2_ch1_get_time_position_0(self):
amine@2 377 tp = self.audio_source.get_time_position()
amine@93 378 self.assertEqual(
amine@93 379 tp,
amine@93 380 0.0,
amine@93 381 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
amine@93 382 )
amine@71 383
amine@2 384 def test_sr16_sw2_ch1_get_time_position_1(self):
amine@2 385 srate = self.audio_source.get_sampling_rate()
amine@2 386 # read one second
amine@2 387 self.audio_source.read(srate)
amine@2 388 tp = self.audio_source.get_time_position()
amine@93 389 self.assertEqual(
amine@93 390 tp,
amine@93 391 1.0,
amine@93 392 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 393 )
amine@71 394
amine@2 395 def test_sr16_sw2_ch1_get_time_position_0_75(self):
amine@2 396 # read 2.5 seconds
amine@2 397 self.audio_source.read(12)
amine@2 398 tp = self.audio_source.get_time_position()
amine@93 399 self.assertEqual(
amine@93 400 tp,
amine@93 401 0.75,
amine@93 402 msg="wrong time position, expected: 0.75, found: {0} ".format(tp),
amine@93 403 )
amine@71 404
amine@2 405 def test_sr16_sw2_ch1_set_time_position_0(self):
amine@2 406 self.audio_source.read(10)
amine@2 407 self.audio_source.set_time_position(0)
amine@2 408 tp = self.audio_source.get_time_position()
amine@93 409 self.assertEqual(
amine@93 410 tp,
amine@93 411 0.0,
amine@93 412 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
amine@93 413 )
amine@71 414
amine@2 415 def test_sr16_sw2_ch1_set_time_position_1(self):
amine@2 416 self.audio_source.set_time_position(1)
amine@2 417 tp = self.audio_source.get_time_position()
amine@93 418 self.assertEqual(
amine@93 419 tp,
amine@93 420 1.0,
amine@93 421 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 422 )
amine@71 423
amine@2 424 def test_sr16_sw2_ch1_set_time_position_end(self):
amine@2 425 self.audio_source.set_time_position(100)
amine@2 426 tp = self.audio_source.get_time_position()
amine@93 427 self.assertEqual(
amine@93 428 tp,
amine@93 429 1.0,
amine@93 430 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 431 )
amine@71 432
amine@2 433 def test_sr16_sw2_ch1_rewind(self):
amine@2 434 self.audio_source.read(10)
amine@2 435 self.audio_source.rewind()
amine@2 436 tp = self.audio_source.get_position()
amine@93 437 self.assertEqual(
amine@93 438 tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)
amine@93 439 )
amine@71 440
amine@2 441 def test_sr16_sw2_ch1_set_data(self):
amine@92 442 self.audio_source.set_data(b"abcdef")
amine@2 443 block = self.audio_source.read(9999)
amine@93 444 self.assertEqual(
amine@93 445 block,
amine@93 446 b"abcdef",
amine@93 447 msg="wrong block, expected: 'abcdef', found: {0} ".format(block),
amine@93 448 )
amine@71 449
amine@2 450 def test_sr16_sw2_ch1_set_data_exception(self):
amine@91 451 with self.assertRaises(AudioParameterError) as audio_param_err:
amine@91 452 self.audio_source.set_data("abcde")
amine@91 453 self.assertEqual(
amine@91 454 "The length of audio data must be an integer "
amine@91 455 "multiple of `sample_width * channels`",
amine@91 456 str(audio_param_err.exception),
amine@91 457 )
amine@93 458
amine@91 459 def test_sr16_sw2_ch1_append_data_exception(self):
amine@91 460 with self.assertRaises(AudioParameterError) as audio_param_err:
amine@91 461 self.audio_source.append_data("abcde")
amine@91 462 self.assertEqual(
amine@91 463 "The length of audio data must be an integer "
amine@91 464 "multiple of `sample_width * channels`",
amine@91 465 str(audio_param_err.exception),
amine@91 466 )
amine@71 467
amine@71 468
amine@2 469 class TestBufferAudioSource_SR11_SW4_CH1(unittest.TestCase):
amine@2 470 def setUp(self):
amine@92 471 self.data = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefgh"
amine@93 472 self.audio_source = BufferAudioSource(
amine@93 473 data_buffer=self.data, sampling_rate=11, sample_width=4, channels=1
amine@93 474 )
amine@2 475 self.audio_source.open()
amine@71 476
amine@2 477 def tearDown(self):
amine@2 478 self.audio_source.close()
amine@2 479
amine@2 480 def test_sr11_sw4_ch1_read_1(self):
amine@2 481 block = self.audio_source.read(1)
amine@92 482 exp = b"ABCD"
amine@93 483 self.assertEqual(
amine@93 484 block,
amine@93 485 exp,
amine@93 486 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 487 )
amine@71 488
amine@2 489 def test_sr11_sw4_ch1_read_6(self):
amine@2 490 block = self.audio_source.read(6)
amine@92 491 exp = b"ABCDEFGHIJKLMNOPQRSTUVWX"
amine@93 492 self.assertEqual(
amine@93 493 block,
amine@93 494 exp,
amine@93 495 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 496 )
amine@71 497
amine@2 498 def test_sr11_sw4_ch1_read_multiple(self):
amine@2 499 block = self.audio_source.read(1)
amine@92 500 exp = b"ABCD"
amine@93 501 self.assertEqual(
amine@93 502 block,
amine@93 503 exp,
amine@93 504 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 505 )
amine@71 506
amine@2 507 block = self.audio_source.read(6)
amine@92 508 exp = b"EFGHIJKLMNOPQRSTUVWXYZ01"
amine@93 509 self.assertEqual(
amine@93 510 block,
amine@93 511 exp,
amine@93 512 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 513 )
amine@71 514
amine@2 515 block = self.audio_source.read(3)
amine@92 516 exp = b"23456789abcd"
amine@93 517 self.assertEqual(
amine@93 518 block,
amine@93 519 exp,
amine@93 520 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 521 )
amine@71 522
amine@2 523 block = self.audio_source.read(9999)
amine@92 524 exp = b"efgh"
amine@93 525 self.assertEqual(
amine@93 526 block,
amine@93 527 exp,
amine@93 528 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 529 )
amine@71 530
amine@2 531 def test_sr11_sw4_ch1_read_all(self):
amine@2 532 block = self.audio_source.read(9999)
amine@93 533 self.assertEqual(
amine@93 534 block,
amine@93 535 self.data,
amine@93 536 msg="wrong block, expected: {0}, found: {1} ".format(
amine@93 537 self.data, block
amine@93 538 ),
amine@93 539 )
amine@71 540
amine@2 541 block = self.audio_source.read(1)
amine@93 542 self.assertEqual(
amine@93 543 block,
amine@93 544 None,
amine@93 545 msg="wrong block, expected: {0}, found: {1} ".format(None, block),
amine@93 546 )
amine@71 547
amine@2 548 def test_sr11_sw4_ch1_get_sampling_rate(self):
amine@2 549 srate = self.audio_source.get_sampling_rate()
amine@93 550 self.assertEqual(
amine@93 551 srate,
amine@93 552 11,
amine@93 553 msg="wrong sampling rate, expected: 10, found: {0} ".format(srate),
amine@93 554 )
amine@71 555
amine@2 556 def test_sr11_sw4_ch1_get_sample_width(self):
amine@2 557 swidth = self.audio_source.get_sample_width()
amine@93 558 self.assertEqual(
amine@93 559 swidth,
amine@93 560 4,
amine@93 561 msg="wrong sample width, expected: 1, found: {0} ".format(swidth),
amine@93 562 )
amine@71 563
amine@2 564 def test_sr11_sw4_ch1_get_channels(self):
amine@2 565 channels = self.audio_source.get_channels()
amine@93 566 self.assertEqual(
amine@93 567 channels,
amine@93 568 1,
amine@93 569 msg="wrong number of channels, expected: 1, found: {0} ".format(
amine@93 570 channels
amine@93 571 ),
amine@93 572 )
amine@71 573
amine@2 574 def test_sr11_sw4_ch1_get_position_0(self):
amine@2 575 pos = self.audio_source.get_position()
amine@93 576 self.assertEqual(
amine@93 577 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
amine@93 578 )
amine@71 579
amine@2 580 def test_sr11_sw4_ch1_get_position_5(self):
amine@2 581 self.audio_source.read(5)
amine@2 582 pos = self.audio_source.get_position()
amine@93 583 self.assertEqual(
amine@93 584 pos, 5, msg="wrong position, expected: 5, found: {0} ".format(pos)
amine@93 585 )
amine@71 586
amine@2 587 def test_sr11_sw4_ch1_get_position_9(self):
amine@2 588 self.audio_source.read(5)
amine@2 589 self.audio_source.read(4)
amine@2 590 pos = self.audio_source.get_position()
amine@93 591 self.assertEqual(
amine@93 592 pos, 9, msg="wrong position, expected: 5, found: {0} ".format(pos)
amine@93 593 )
amine@71 594
amine@2 595 def test_sr11_sw4_ch1_set_position_0(self):
amine@2 596 self.audio_source.read(10)
amine@2 597 self.audio_source.set_position(0)
amine@2 598 pos = self.audio_source.get_position()
amine@93 599 self.assertEqual(
amine@93 600 pos, 0, msg="wrong position, expected: 0, found: {0} ".format(pos)
amine@93 601 )
amine@71 602
amine@2 603 def test_sr11_sw4_ch1_set_position_10(self):
amine@2 604 self.audio_source.set_position(10)
amine@2 605 pos = self.audio_source.get_position()
amine@93 606 self.assertEqual(
amine@93 607 pos,
amine@93 608 10,
amine@93 609 msg="wrong position, expected: 10, found: {0} ".format(pos),
amine@93 610 )
amine@71 611
amine@2 612 def test_sr11_sw4_ch1_get_time_position_0(self):
amine@2 613 tp = self.audio_source.get_time_position()
amine@93 614 self.assertEqual(
amine@93 615 tp,
amine@93 616 0.0,
amine@93 617 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
amine@93 618 )
amine@71 619
amine@2 620 def test_sr11_sw4_ch1_get_time_position_1(self):
amine@2 621 srate = self.audio_source.get_sampling_rate()
amine@2 622 # read one second
amine@2 623 self.audio_source.read(srate)
amine@2 624 tp = self.audio_source.get_time_position()
amine@93 625 self.assertEqual(
amine@93 626 tp,
amine@93 627 1.0,
amine@93 628 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 629 )
amine@71 630
amine@2 631 def test_sr11_sw4_ch1_get_time_position_0_63(self):
amine@2 632 # read 2.5 seconds
amine@2 633 self.audio_source.read(7)
amine@2 634 tp = self.audio_source.get_time_position()
amine@93 635 self.assertAlmostEqual(
amine@93 636 tp,
amine@93 637 0.636363636364,
amine@93 638 msg="wrong time position, expected: 0.636363636364, "
amine@93 639 "found: {0} ".format(tp),
amine@93 640 )
amine@71 641
amine@2 642 def test_sr11_sw4_ch1_set_time_position_0(self):
amine@2 643 self.audio_source.read(10)
amine@2 644 self.audio_source.set_time_position(0)
amine@2 645 tp = self.audio_source.get_time_position()
amine@93 646 self.assertEqual(
amine@93 647 tp,
amine@93 648 0.0,
amine@93 649 msg="wrong time position, expected: 0.0, found: {0} ".format(tp),
amine@93 650 )
amine@71 651
amine@2 652 def test_sr11_sw4_ch1_set_time_position_1(self):
amine@2 653
amine@2 654 self.audio_source.set_time_position(1)
amine@2 655 tp = self.audio_source.get_time_position()
amine@93 656 self.assertEqual(
amine@93 657 tp,
amine@93 658 1.0,
amine@93 659 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 660 )
amine@71 661
amine@2 662 def test_sr11_sw4_ch1_set_time_position_end(self):
amine@2 663 self.audio_source.set_time_position(100)
amine@2 664 tp = self.audio_source.get_time_position()
amine@93 665 self.assertEqual(
amine@93 666 tp,
amine@93 667 1.0,
amine@93 668 msg="wrong time position, expected: 1.0, found: {0} ".format(tp),
amine@93 669 )
amine@71 670
amine@2 671 def test_sr11_sw4_ch1_rewind(self):
amine@2 672 self.audio_source.read(10)
amine@2 673 self.audio_source.rewind()
amine@2 674 tp = self.audio_source.get_position()
amine@93 675 self.assertEqual(
amine@93 676 tp, 0, msg="wrong position, expected: 0.0, found: {0} ".format(tp)
amine@93 677 )
amine@71 678
amine@2 679 def test_sr11_sw4_ch1_set_data(self):
amine@92 680 self.audio_source.set_data(b"abcdefgh")
amine@2 681 block = self.audio_source.read(9999)
amine@92 682 exp = b"abcdefgh"
amine@93 683 self.assertEqual(
amine@93 684 block,
amine@93 685 exp,
amine@93 686 msg="wrong block, expected: {}, found: {} ".format(exp, block),
amine@93 687 )
amine@71 688
amine@2 689 def test_sr11_sw4_ch1_set_data_exception(self):
amine@91 690 with self.assertRaises(AudioParameterError) as audio_param_err:
amine@92 691 self.audio_source.set_data(b"abcdef")
amine@91 692 self.assertEqual(
amine@91 693 "The length of audio data must be an integer "
amine@91 694 "multiple of `sample_width * channels`",
amine@91 695 str(audio_param_err.exception),
amine@91 696 )
amine@93 697
amine@91 698 def test_sr11_sw4_ch1_append_data_exception(self):
amine@91 699 with self.assertRaises(AudioParameterError) as audio_param_err:
amine@92 700 self.audio_source.append_data(b"abcdef")
amine@91 701 self.assertEqual(
amine@91 702 "The length of audio data must be an integer "
amine@91 703 "multiple of `sample_width * channels`",
amine@91 704 str(audio_param_err.exception),
amine@91 705 )
amine@71 706
amine@91 707
amine@91 708 class TestBufferAudioSourceCreationException(unittest.TestCase):
amine@91 709 def test_wrong_sample_width_value(self):
amine@91 710 with self.assertRaises(AudioParameterError) as audio_param_err:
amine@93 711 _ = BufferAudioSource(
amine@93 712 data_buffer=b"ABCDEFGHI",
amine@93 713 sampling_rate=9,
amine@93 714 sample_width=3,
amine@93 715 channels=1,
amine@93 716 )
amine@91 717 self.assertEqual(
amine@91 718 "Sample width must be one of: 1, 2 or 4 (bytes)",
amine@91 719 str(audio_param_err.exception),
amine@91 720 )
amine@91 721
amine@91 722 def test_wrong_data_buffer_size(self):
amine@91 723 with self.assertRaises(AudioParameterError) as audio_param_err:
amine@93 724 _ = BufferAudioSource(
amine@93 725 data_buffer=b"ABCDEFGHI",
amine@93 726 sampling_rate=8,
amine@93 727 sample_width=2,
amine@94 728 channels=1,
amine@93 729 )
amine@91 730 self.assertEqual(
amine@91 731 "The length of audio data must be an integer "
amine@91 732 "multiple of `sample_width * channels`",
amine@91 733 str(audio_param_err.exception),
amine@91 734 )
amine@71 735
amine@71 736
amine@71 737 class TestAudioSourceProperties(unittest.TestCase):
amine@71 738 def test_read_properties(self):
amine@92 739 data = b""
amine@71 740 sampling_rate = 8000
amine@71 741 sample_width = 2
amine@71 742 channels = 1
amine@93 743 a_source = BufferAudioSource(
amine@93 744 data, sampling_rate, sample_width, channels
amine@93 745 )
amine@71 746
amine@71 747 self.assertEqual(a_source.sampling_rate, sampling_rate)
amine@71 748 self.assertEqual(a_source.sample_width, sample_width)
amine@71 749 self.assertEqual(a_source.channels, channels)
amine@71 750
amine@71 751 def test_set_readonly_properties_exception(self):
amine@92 752 data = b""
amine@71 753 sampling_rate = 8000
amine@71 754 sample_width = 2
amine@71 755 channels = 1
amine@93 756 a_source = BufferAudioSource(
amine@93 757 data, sampling_rate, sample_width, channels
amine@93 758 )
amine@71 759
amine@71 760 with self.assertRaises(AttributeError):
amine@71 761 a_source.sampling_rate = 16000
amine@71 762 a_source.sample_width = 1
amine@71 763 a_source.channels = 2
amine@2 764
amine@2 765
amine@73 766 class TestAudioSourceShortProperties(unittest.TestCase):
amine@73 767 def test_read_short_properties(self):
amine@92 768 data = b""
amine@73 769 sampling_rate = 8000
amine@73 770 sample_width = 2
amine@73 771 channels = 1
amine@93 772 a_source = BufferAudioSource(
amine@93 773 data, sampling_rate, sample_width, channels
amine@93 774 )
amine@73 775
amine@73 776 self.assertEqual(a_source.sr, sampling_rate)
amine@73 777 self.assertEqual(a_source.sw, sample_width)
amine@73 778 self.assertEqual(a_source.ch, channels)
amine@73 779
amine@73 780 def test_set_readonly_short_properties_exception(self):
amine@92 781 data = b""
amine@73 782 sampling_rate = 8000
amine@73 783 sample_width = 2
amine@73 784 channels = 1
amine@93 785 a_source = BufferAudioSource(
amine@93 786 data, sampling_rate, sample_width, channels
amine@93 787 )
amine@73 788
amine@73 789 with self.assertRaises(AttributeError):
amine@73 790 a_source.sr = 16000
amine@73 791 a_source.sw = 1
amine@73 792 a_source.ch = 2
amine@73 793
amine@73 794
amine@2 795 if __name__ == "__main__":
amine@2 796 unittest.main()