def run_once(self): """Entry point of this test.""" # Sine wav file lasts 5 seconds wav_path = os.path.join(self.bindir, '5SEC.wav') data_format = dict(file_type='wav', sample_format='S16_LE', channel=2, rate=48000) wav_file = audio_test_data.GenerateAudioTestData( path=wav_path, data_format=data_format, duration_secs=5, frequencies=[440, 440], volume_scale=0.9) recorded_file = os.path.join(self.resultsdir, 'hw_recorded.wav') # Get selected input and output devices. cras_input = cras_utils.get_selected_input_device_name() cras_output = cras_utils.get_selected_output_device_name() logging.debug("Selected input=%s, output=%s", cras_input, cras_output) if cras_input is None: raise error.TestFail("Fail to get selected input device.") if cras_output is None: raise error.TestFail("Fail to get selected output device.") alsa_input = alsa_utils.convert_device_name(cras_input) alsa_output = alsa_utils.convert_device_name(cras_output) (output_type, input_type) = cras_utils.get_selected_node_types() if not any(t in input_type for t in ['MIC', 'USB']): raise error.TestFail("Wrong input type=%s", input_type) if not any(t in output_type for t in ['HEADPHONE', 'USB']): raise error.TestFail("Wrong output type=%s", output_type) # Stop CRAS to make sure the audio device won't be occupied. utils.stop_service('cras', ignore_status=True) p = cmd_utils.popen( alsa_utils.playback_cmd(wav_file.path, device=alsa_output)) try: # Wait one second to make sure the playback has been started. time.sleep(1) alsa_utils.record(recorded_file, duration=TEST_DURATION, device=alsa_input) # Make sure the audio is still playing. if p.poll() != None: raise error.TestError('playback stopped') finally: cmd_utils.kill_or_log_returncode(p) wav_file.delete() # Restart CRAS. utils.start_service('cras', ignore_status=True) rms_value = audio_helper.get_rms(recorded_file)[0] self.write_perf_keyval({'rms_value': rms_value})
def run_once(self): """Entry point of this test.""" # Multitone wav file lasts 10 seconds wav_path = os.path.join(self.bindir, '10SEC.wav') noise_file = os.path.join(self.resultsdir, 'cras_noise.wav') recorded_file = os.path.join(self.resultsdir, 'cras_recorded.wav') # Record a sample of "silence" to use as a noise profile. cras_utils.capture(noise_file, duration=1) self.wait_for_active_stream_count(0) p = cmd_utils.popen(cras_utils.playback_cmd(wav_path)) try: self.wait_for_active_stream_count(1) cras_utils.capture(recorded_file, duration=TEST_DURATION) # Make sure the audio is still playing. if p.poll() != None: raise error.TestError('playback stopped') finally: cmd_utils.kill_or_log_returncode(p) rms_value = audio_helper.reduce_noise_and_get_rms( recorded_file, noise_file)[0] self.write_perf_keyval({'rms_value': rms_value})
def run_once(self): """Entry point of this test.""" # Generate sine raw file that lasts 5 seconds. raw_path = os.path.join(self.bindir, '5SEC.raw') data_format = dict(file_type='raw', sample_format='S16_LE', channel=2, rate=48000) raw_file = audio_test_data.GenerateAudioTestData( path=raw_path, data_format=data_format, duration_secs=5, frequencies=[440, 440], volume_scale=0.9) recorded_file = os.path.join(self.resultsdir, 'cras_recorded.raw') self.wait_for_active_stream_count(0) p = cmd_utils.popen(cras_utils.playback_cmd(raw_file.path)) try: self.wait_for_active_stream_count(1) cras_utils.capture(recorded_file, duration=TEST_DURATION) # Make sure the audio is still playing. if p.poll() != None: raise error.TestError('playback stopped') finally: cmd_utils.kill_or_log_returncode(p) raw_file.delete() rms_value = audio_helper.get_rms(recorded_file)[0] self.write_perf_keyval({'rms_value': rms_value})
def _test_audio_disabled(self, policy_value): """ Verify the AudioOutputAllowed policy behaves as expected. Generate and play a sample audio file. When enabled, the difference between the muted and unmuted RMS should be greater than 0.75. When disabled, the RMS difference should be less than 0.05. @param policy_value: policy value for this case. @raises error.TestFail: In the case where the audio behavior does not match the policy value. """ audio_allowed = policy_value or policy_value is None RAW_FILE = os.path.join(self.enterprise_dir, 'test_audio.raw') noise_file = os.path.join(self.resultsdir, 'noise.wav') recorded_file = os.path.join(self.resultsdir, 'recorded-cras.raw') recorded_rms = [] # Record a sample of silence to use as a noise profile. cras_utils.capture(noise_file, duration=2) logging.info('NOISE: %s', audio_helper.get_rms(noise_file)) # Get two RMS samples: one when muted and one when not for muted in [False, True]: cras_utils.set_system_mute(muted) # Play the audio file and capture the output self.wait_for_active_stream_count(0) p = cmd_utils.popen(cras_utils.playback_cmd(RAW_FILE)) try: self.wait_for_active_stream_count(1) cras_utils.capture(recorded_file, duration=self.SAMPLE_DURATION) if p.poll() is not None: raise error.TestError('Audio playback stopped prematurely') finally: cmd_utils.kill_or_log_returncode(p) rms_value = audio_helper.reduce_noise_and_get_rms( recorded_file, noise_file)[0] logging.info('muted (%s): %s' % (muted, rms_value)) recorded_rms.append(rms_value) rms_diff = recorded_rms[0] - recorded_rms[1] self.write_perf_keyval({'rms_diff': rms_diff}) if audio_allowed: if rms_diff < 0.4: raise error.TestFail('RMS difference not large enough between ' 'mute and ummute: %s' % rms_diff) else: if abs(rms_diff) > 0.05: raise error.TestFail('RMS difference too wide while audio ' 'disabled: %s' % rms_diff)
def loopback(self, noise_profile, primary, secondary): """Gets the rms value of the recorded audio of playing two different tones (the 440 and 523 Hz sine wave) at the specified sampling rate. @param noise_profile: The noise profile which is used to reduce the noise of the recored audio. @param primary: The first sample rate, HW will be set to this. @param secondary: The second sample rate, will be SRC'd to the first. """ popens = [] record_file = os.path.join(self.resultsdir, 'record-%s-%s.wav' % (primary, secondary)) # There should be no other active streams. self.wait_for_active_stream_count(0) # Start with the primary sample rate, then add the secondary. This # causes the secondary to be SRC'd to the primary rate. try: # Play the first audio stream and make sure it has been played popens += self.play_sine_tone(_TEST_TONE_ONE, primary) self.wait_for_active_stream_count(1) # Play the second audio stream and make sure it has been played popens += self.play_sine_tone(_TEST_TONE_TWO, secondary) self.wait_for_active_stream_count(2) cras_utils.capture(record_file, duration=1, rate=44100) # Make sure the playback is still in good shape if any(p.poll() is not None for p in popens): # We will log more details later in finally. raise error.TestFail('process unexpectly stopped') reduced_file = tempfile.NamedTemporaryFile() sox_utils.noise_reduce(record_file, reduced_file.name, noise_profile, rate=44100) sox_stat = sox_utils.get_stat(reduced_file.name, rate=44100) logging.info('The sox stat of (%d, %d) is %s', primary, secondary, str(sox_stat)) return sox_stat.rms finally: cmd_utils.kill_or_log_returncode(*popens)
def simulate_hangout(self, decode_videos, encode_videos, measurer): popens = self.run_in_parallel( self.get_vda_unittest_cmd_line(decode_videos), self.get_vea_unittest_cmd_line(encode_videos)) try: time.sleep(STABILIZATION_DURATION) measurer.start() time.sleep(MEASUREMENT_DURATION) measurement = measurer.stop() # Ensure both encoding and decoding are still alive if any(p.poll() is not None for p in popens): raise error.TestError('vea/vda_unittest failed') return measurement finally: cmd_utils.kill_or_log_returncode(*popens)
def stop(self): """Stops playback subprocess.""" cmd_utils.kill_or_log_returncode(self._playback_subprocess)