src/media/audio/audio_core/test/api/audio_renderer_pipeline_test.cc
// During playback, gain changes should not introduce high-frequency distortion.
During playback, gain changes should not introduce high-frequency distortion.
During playback gain changes should not introduce high - frequency distortion .
TEST_F(AudioRendererPipelineTestFloat, NoDistortionOnGainChanges) {
  fuchsia::media::audio::VolumeControlPtr volume;
  audio_core_->BindUsageVolumeControl(
      fuchsia::media::Usage::WithRenderUsage(AudioRenderUsage::MEDIA), volume.NewRequest());
  volume->SetVolume(0.5);
  size_t num_frames = kPayloadFramesPow2;
  const size_t kFramesPerPeriod = 256;
  const size_t freq = num_frames / kFramesPerPeriod;
  auto input_buffer = GenerateCosineAudio(format_, num_frames, freq);
  auto packets = renderer_->AppendPackets({&input_buffer});
  auto start_time = renderer_->PlaySynchronized(this, output_, 0);
  RunLoopWithTimeout(zx::time(start_time) - zx::clock::get_monotonic());
  for (size_t k = 0; k < num_frames / kPacketFrames; k++) {
    volume->SetVolume((k % 2) == 0 ? 1.0 : 0.5);
    RunLoopWithTimeout(zx::msec(RendererShimImpl::kPacketMs));
  }
  renderer_->WaitForPackets(this, packets);
  auto ring_buffer = output_->SnapshotRingBuffer();
  auto output_buffer = AudioBufferSlice(&ring_buffer, 0, input_buffer.NumFrames()).GetChannel(0);
   The precise amount of noise depends on exactly when the gain toggles are applied,
   which is not deterministic. The noise signature also depends on the length and shape
   of the gain ramp -- any intentional ramping change may break this test.
   As of early Aug 2020, typical noise_ratio values are:
   * 0.05-0.07 without ramping
   * 0.001-0.015 with ramping
  std::unordered_set<size_t> highfreqs;
  for (size_t f = freq << 4; f < output_buffer.NumFrames() / 2; f++) {
    highfreqs.insert(f);
  }
  auto result = MeasureAudioFreqs(AudioBufferSlice(&output_buffer), highfreqs);
  auto noise_ratio = result.total_magn_signal / result.total_magn_other;
  EXPECT_LT(noise_ratio, 0.02) << "\ntotal_magn_highfreq_noise = " << result.total_magn_signal
                               << "\ntotal_magn_other = " << result.total_magn_other;
}
TEST_F ( AudioRendererPipelineTestFloat , NoDistortionOnGainChanges ) { fuchsia :: media :: audio :: VolumeControlPtr volume ; audio_core_ -> BindUsageVolumeControl ( fuchsia :: media :: Usage :: WithRenderUsage ( AudioRenderUsage :: MEDIA ) , volume . NewRequest ( ) ) ; volume -> SetVolume ( 0.5 ) ; size_t num_frames = kPayloadFramesPow2 ; const size_t kFramesPerPeriod = 256 ; const size_t freq = num_frames / kFramesPerPeriod ; auto input_buffer = GenerateCosineAudio ( format_ , num_frames , freq ) ; auto packets = renderer_ -> AppendPackets ( { & input_buffer } ) ; auto start_time = renderer_ -> PlaySynchronized ( this , output_ , 0 ) ; RunLoopWithTimeout ( zx :: time ( start_time ) - zx :: clock :: get_monotonic ( ) ) ; for ( size_t k = 0 ; k < num_frames / kPacketFrames ; k ++ ) { volume -> SetVolume ( ( k % 2 ) == 0 ? 1.0 : 0.5 ) ; RunLoopWithTimeout ( zx :: msec ( RendererShimImpl :: kPacketMs ) ) ; } renderer_ -> WaitForPackets ( this , packets ) ; auto ring_buffer = output_ -> SnapshotRingBuffer ( ) ; auto output_buffer = AudioBufferSlice ( & ring_buffer , 0 , input_buffer . NumFrames ( ) ) . GetChannel ( 0 ) ; std :: unordered_set < size_t > highfreqs ; for ( size_t f = freq << 4 ; f < output_buffer . NumFrames ( ) / 2 ; f ++ ) { highfreqs . insert ( f ) ; } auto result = MeasureAudioFreqs ( AudioBufferSlice ( & output_buffer ) , highfreqs ) ; auto noise_ratio = result . total_magn_signal / result . total_magn_other ; EXPECT_LT ( noise_ratio , 0.02 ) << " \n " << result . total_magn_signal << " \n " << result . total_magn_other ; }
During playback, gain changes should not introduce high-frequency distortion.
During playback gain changes should not introduce high - frequency distortion .
// At 48kHz, this is 5.33ms per sinusoidal period. This is chosen intentionally to // (a) not align with volume updates, which happen every 10ms, and (b) include a // power-of-2 number of frames, to simplify the FFT comparison. // Wait until the first packet will be rendered, then make a few gain toggles. // Now wait for all packets to be rendered. // If we properly ramp gain changes, there should not be very much high-frequency noise. // For the purpose of this test, we'll define "high-frequency" to be anything at least 4 // octaves above the base frequency. // // The precise amount of noise depends on exactly when the gain toggles are applied, // which is not deterministic. The noise signature also depends on the length and shape // of the gain ramp -- any intentional ramping change may break this test. // // As of early Aug 2020, typical noise_ratio values are: // * 0.05-0.07 without ramping // * 0.001-0.015 with ramping