test_sox.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import math
  2. import os
  3. import infra.basetest
  4. class TestSox(infra.basetest.BRTest):
  5. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  6. """
  7. BR2_PACKAGE_AUBIO=y
  8. BR2_PACKAGE_SOX=y
  9. BR2_TARGET_ROOTFS_CPIO=y
  10. # BR2_TARGET_ROOTFS_TAR is not set
  11. """
  12. def note_from_freq(self, freq):
  13. """Return a note number from the input frequency in Hertz."""
  14. return round((12 * math.log(freq / 440) / math.log(2)) + 69)
  15. def check_audio_note(self, input_file, expected_note):
  16. """Check the input_file include the expected_note."""
  17. out, ret = self.emulator.run(f"aubionotes {input_file}", timeout=20)
  18. self.assertEqual(ret, 0)
  19. note_found = False
  20. for line in out:
  21. values = line.split()
  22. if len(values) == 3:
  23. note = round(float(values[0]))
  24. if note == expected_note:
  25. note_found = True
  26. self.assertTrue(note_found, "The expected note was not found")
  27. def test_run(self):
  28. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  29. self.emulator.boot(arch="armv5",
  30. kernel="builtin",
  31. options=["-initrd", cpio_file])
  32. self.emulator.login()
  33. # Check the program can execute.
  34. self.assertRunOk("sox --version")
  35. freq = 440 # General Midi note A3
  36. expected_note = self.note_from_freq(freq)
  37. wav_file = "ref.wav"
  38. tmpwav_file = "tmp.wav"
  39. # Generate a sinusoidal tone.
  40. cmd = "sox -V -r 48000 -n -b 16 -c 1"
  41. cmd += f" {wav_file} synth 3 sin {freq} vol -10dB"
  42. self.assertRunOk(cmd)
  43. # Compute statistics on the generated file.
  44. self.assertRunOk(f"sox {wav_file} -n stat")
  45. # We check the generated wave file includes the expected note.
  46. self.check_audio_note(wav_file, expected_note)
  47. # We resample the reference file.
  48. cmd = f"sox -V {wav_file} -r 22050 {tmpwav_file}"
  49. self.assertRunOk(cmd)
  50. # We should still detect our expected note.
  51. self.check_audio_note(tmpwav_file, expected_note)
  52. # We convert the file by changing the speed by a factor.
  53. speed_factor = 2
  54. cmd = f"sox -V {wav_file} {tmpwav_file} speed {speed_factor}"
  55. self.assertRunOk(cmd)
  56. # We compute the new expected note from this test controller
  57. # side, and check we detect this new note in the audio file.
  58. new_expected_note = self.note_from_freq(freq * speed_factor)
  59. self.check_audio_note(tmpwav_file, new_expected_note)