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