1 /* 2 * Copyright 2019 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 17 package com.google.sample.oboe.manualtest; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.LayoutInflater; 22 23 24 import android.widget.SeekBar; 25 import android.widget.TextView; 26 import android.widget.LinearLayout; 27 28 public class BufferSizeView extends LinearLayout { 29 30 AudioOutputTester mAudioOutTester; 31 32 protected static final int FADER_THRESHOLD_MAX = 1000; // must match layout 33 protected TextView mTextThreshold; 34 protected SeekBar mFaderThreshold; 35 protected ExponentialTaper mTaperThreshold; 36 private int mCachedCapacity; 37 38 private SeekBar.OnSeekBarChangeListener mThresholdListener = new SeekBar.OnSeekBarChangeListener() { 39 @Override 40 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 41 setBufferSizeByPosition(progress); 42 } 43 44 @Override 45 public void onStartTrackingTouch(SeekBar seekBar) { 46 } 47 48 @Override 49 public void onStopTrackingTouch(SeekBar seekBar) { 50 } 51 }; 52 BufferSizeView(Context context)53 public BufferSizeView(Context context) { 54 super(context); 55 initializeViews(context); 56 } 57 BufferSizeView(Context context, AttributeSet attrs)58 public BufferSizeView(Context context, AttributeSet attrs) { 59 super(context, attrs); 60 initializeViews(context); 61 } 62 BufferSizeView(Context context, AttributeSet attrs, int defStyle)63 public BufferSizeView(Context context, 64 AttributeSet attrs, 65 int defStyle) { 66 super(context, attrs, defStyle); 67 initializeViews(context); 68 } 69 getAudioOutTester()70 public AudioOutputTester getAudioOutTester() { 71 return mAudioOutTester; 72 } 73 setAudioOutTester(AudioOutputTester audioOutTester)74 public void setAudioOutTester(AudioOutputTester audioOutTester) { 75 mAudioOutTester = audioOutTester; 76 } 77 setFaderNormalizedProgress(double fraction)78 void setFaderNormalizedProgress(double fraction) { 79 mFaderThreshold.setProgress((int)(fraction * FADER_THRESHOLD_MAX)); 80 } 81 82 /** 83 * Inflates the views in the layout. 84 * 85 * @param context 86 * the current context for the view. 87 */ initializeViews(Context context)88 private void initializeViews(Context context) { 89 LayoutInflater inflater = (LayoutInflater) context 90 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 91 inflater.inflate(R.layout.buffer_size_view, this); 92 93 mTextThreshold = (TextView) findViewById(R.id.textThreshold); 94 mFaderThreshold = (SeekBar) findViewById(R.id.faderThreshold); 95 mFaderThreshold.setOnSeekBarChangeListener(mThresholdListener); 96 mTaperThreshold = new ExponentialTaper(0.0, 1.0, 10.0); 97 mFaderThreshold.setProgress(0); 98 } 99 setBufferSizeByPosition(int progress)100 private void setBufferSizeByPosition(int progress) { 101 StringBuffer message = new StringBuffer(); 102 double normalizedThreshold = mTaperThreshold.linearToExponential( 103 ((double)progress)/FADER_THRESHOLD_MAX); 104 if (normalizedThreshold < 0.0) normalizedThreshold = 0.0; 105 else if (normalizedThreshold > 1.0) normalizedThreshold = 1.0; 106 int percent = (int) (normalizedThreshold * 100); 107 message.append("bufferSize = " + percent + "%"); 108 109 OboeAudioStream stream = null; 110 int sizeFrames = 0; 111 if (getAudioOutTester() != null) { 112 stream = (OboeAudioStream) getAudioOutTester().getCurrentAudioStream(); 113 if (stream != null) { 114 int capacity = stream.getBufferCapacityInFrames(); 115 if (capacity > 0) mCachedCapacity = capacity; 116 } 117 } 118 if (mCachedCapacity > 0) { 119 sizeFrames = (int) (normalizedThreshold * mCachedCapacity); 120 message.append(" = " + sizeFrames); 121 if (stream != null) { 122 stream.setBufferSizeInFrames(sizeFrames); 123 } 124 int bufferSize = getAudioOutTester().getCurrentAudioStream().getBufferSizeInFrames(); 125 if (bufferSize >= 0) { 126 message.append(" / " + bufferSize); 127 } 128 message.append(" / " + mCachedCapacity); 129 } 130 mTextThreshold.setText(message.toString()); 131 } 132 updateBufferSize()133 public void updateBufferSize() { 134 int progress = mFaderThreshold.getProgress(); 135 setBufferSizeByPosition(progress); 136 } 137 138 @Override setEnabled(boolean enabled)139 public void setEnabled(boolean enabled) { 140 super.setEnabled(enabled); 141 mFaderThreshold.setEnabled(enabled); 142 } 143 } 144