1 /* 2 * Copyright 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.cts.verifier.audio.sources; 17 18 import org.hyphonate.megaaudio.player.sources.WaveTableSource; 19 20 import java.util.Arrays; 21 22 /** 23 * An audio source which plays a short tone when triggered 24 */ 25 public class BlipAudioSource extends WaveTableSource { 26 /** 27 * The number of SAMPLES in the Wave table. 28 * This is plenty of samples for a clear wave. 29 * the + 1 is to avoid special handling of the interpolation on the last sample. 30 */ 31 static final int WAVETABLE_LENGTH = 2049; 32 33 // 1/16 second @48000 Hz 34 private static final int NUM_PULSE_FRAMES = (int) (48000 * (1.0 / 16.0)); 35 36 private int mNumPendingPulseFrames; 37 BlipAudioSource()38 public BlipAudioSource() { 39 super(); 40 float[] waveTbl = new float[WAVETABLE_LENGTH]; 41 WaveTableSource.genSinWave(waveTbl); 42 super.setWaveTable(waveTbl); 43 } 44 45 /** 46 * Triggers a "blip" in the output 47 */ 48 @Override trigger()49 public void trigger() { 50 mNumPendingPulseFrames = NUM_PULSE_FRAMES; 51 } 52 53 @Override pull(float[] buffer, int numFrames, int numChans)54 public int pull(float[] buffer, int numFrames, int numChans) { 55 if (mNumPendingPulseFrames <= 0) { 56 Arrays.fill(buffer, 0.0f); 57 } else { 58 super.pull(buffer, numFrames, numChans); 59 mNumPendingPulseFrames -= numFrames; 60 } 61 return numFrames; 62 } 63 } 64