src/media/audio/audio_core/test/api/audio_renderer_pipeline_test.cc
// During playback, gain changes should be ramped.
During playback, gain changes should be ramped.
During playback gain changes should be ramped .
TEST_F(AudioRendererPipelineTestInt16, RampOnGainChanges) {
  fuchsia::media::audio::VolumeControlPtr volume;
  audio_core_->BindUsageVolumeControl(
      fuchsia::media::Usage::WithRenderUsage(AudioRenderUsage::MEDIA), volume.NewRequest());
  volume->SetVolume(0.5);
  auto num_packets = kNumPacketsInPayload;
  auto num_frames = num_packets * kPacketFrames;
  const int16_t kSampleFullVolume = 0x0200;
  const int16_t kSampleHalfVolume = 0x0010;
  auto input_buffer = GenerateConstantAudio<ASF::SIGNED_16>(format_, num_frames, kSampleFullVolume);
  auto packets = renderer_->AppendPackets({&input_buffer});
  auto start_time = renderer_->PlaySynchronized(this, output_, 0);
  auto start_delay = zx::time(start_time) - zx::clock::get_monotonic();
  RunLoopWithTimeout(start_delay + zx::msec((num_packets / 2) * RendererShimImpl::kPacketMs));
  volume->SetVolume(1.0);
  renderer_->WaitForPackets(this, packets);
  auto ring_buffer = output_->SnapshotRingBuffer();
  size_t start = ring_buffer.NumFrames() - 1;
  for (;; start--) {
    if (ring_buffer.SampleAt(start, 0) == kSampleHalfVolume) {
      break;
    }
    if (start == 0) {
      ADD_FAILURE() << "could not find half volume sample 0x" << std::hex << kSampleHalfVolume;
      ring_buffer.Display(0, 3 * kPacketFrames);
      return;
    }
  }
  size_t end = start + 1;
  for (;; end++) {
    if (ring_buffer.SampleAt(end, 0) == kSampleFullVolume) {
      break;
    }
    if (end == ring_buffer.NumFrames() - 1) {
      ADD_FAILURE() << "could not find full volume sample 0x" << std::hex << kSampleFullVolume
                    << " after frame " << std::dec << start;
      ring_buffer.Display(start, kPacketFrames);
      return;
    }
  }
  const auto ns_per_frame = format_.frames_per_ns().Inverse();
  const auto dt = zx::nsec(ns_per_frame.Scale(end - start));
  const auto tol = zx::nsec(ns_per_frame.Scale(1));
  EXPECT_NEAR(kVolumeRampDuration.get(), dt.get(), tol.get())
      << "ramp has length " << (end - start) << " frames, from frame " << start << " to " << end;
}
TEST_F ( AudioRendererPipelineTestInt16 , RampOnGainChanges ) { fuchsia :: media :: audio :: VolumeControlPtr volume ; audio_core_ -> BindUsageVolumeControl ( fuchsia :: media :: Usage :: WithRenderUsage ( AudioRenderUsage :: MEDIA ) , volume . NewRequest ( ) ) ; volume -> SetVolume ( 0.5 ) ; auto num_packets = kNumPacketsInPayload ; auto num_frames = num_packets * kPacketFrames ; const int16_t kSampleFullVolume = 0x0200 ; const int16_t kSampleHalfVolume = 0x0010 ; auto input_buffer = GenerateConstantAudio < ASF :: SIGNED_16 > ( format_ , num_frames , kSampleFullVolume ) ; auto packets = renderer_ -> AppendPackets ( { & input_buffer } ) ; auto start_time = renderer_ -> PlaySynchronized ( this , output_ , 0 ) ; auto start_delay = zx :: time ( start_time ) - zx :: clock :: get_monotonic ( ) ; RunLoopWithTimeout ( start_delay + zx :: msec ( ( num_packets / 2 ) * RendererShimImpl :: kPacketMs ) ) ; volume -> SetVolume ( 1.0 ) ; renderer_ -> WaitForPackets ( this , packets ) ; auto ring_buffer = output_ -> SnapshotRingBuffer ( ) ; size_t start = ring_buffer . NumFrames ( ) - 1 ; for ( ; ; start -- ) { if ( ring_buffer . SampleAt ( start , 0 ) == kSampleHalfVolume ) { break ; } if ( start == 0 ) { ADD_FAILURE ( ) << " " << std :: hex << kSampleHalfVolume ; ring_buffer . Display ( 0 , 3 * kPacketFrames ) ; return ; } } size_t end = start + 1 ; for ( ; ; end ++ ) { if ( ring_buffer . SampleAt ( end , 0 ) == kSampleFullVolume ) { break ; } if ( end == ring_buffer . NumFrames ( ) - 1 ) { ADD_FAILURE ( ) << " " << std :: hex << kSampleFullVolume << " " << std :: dec << start ; ring_buffer . Display ( start , kPacketFrames ) ; return ; } } const auto ns_per_frame = format_ . frames_per_ns ( ) . Inverse ( ) ; const auto dt = zx :: nsec ( ns_per_frame . Scale ( end - start ) ) ; const auto tol = zx :: nsec ( ns_per_frame . Scale ( 1 ) ) ; EXPECT_NEAR ( kVolumeRampDuration . get ( ) , dt . get ( ) , tol . get ( ) ) << " " << ( end - start ) << " " << start << " " << end ; }
During playback, gain changes should be ramped.
During playback gain changes should be ramped .
// Wait until a few packets are rendered, then raise the volume to 1.0. // Now wait for all packets to be rendered. // The output should contain a sequence at half volume, followed by a ramp, // followed by a sequence at full volume. Verify that the length of the ramp // matches the expected ramp duration. // The exact length can be off by a fractional frame due to rounding.