1 /* 2 * Copyright 2017 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.Manifest; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.media.audiofx.AcousticEchoCanceler; 23 import android.media.audiofx.AutomaticGainControl; 24 import android.net.Uri; 25 import android.os.Bundle; 26 import android.os.Environment; 27 import android.support.annotation.NonNull; 28 import android.support.v4.app.ActivityCompat; 29 import android.support.v4.content.FileProvider; 30 import android.view.View; 31 import android.widget.CheckBox; 32 import android.widget.RadioButton; 33 import android.widget.TextView; 34 import android.widget.Toast; 35 36 import com.google.sample.oboe.manualtest.R; 37 38 import java.io.File; 39 import java.io.IOException; 40 41 /** 42 * Test Oboe Capture 43 */ 44 45 public class TestInputActivity extends TestAudioActivity 46 implements ActivityCompat.OnRequestPermissionsResultCallback { 47 48 private static final int AUDIO_ECHO_REQUEST = 0; 49 protected AudioInputTester mAudioInputTester; 50 private static final int NUM_VOLUME_BARS = 4; 51 private VolumeBarView[] mVolumeBars = new VolumeBarView[NUM_VOLUME_BARS]; 52 private InputMarginView mInputMarginView; 53 private int mInputMarginBursts = 0; 54 private WorkloadView mWorkloadView; 55 setMinimumFramesBeforeRead(int frames)56 public native void setMinimumFramesBeforeRead(int frames); saveWaveFile(String absolutePath)57 public native int saveWaveFile(String absolutePath); 58 isOutput()59 @Override boolean isOutput() { return false; } 60 61 @Override inflateActivity()62 protected void inflateActivity() { 63 setContentView(R.layout.activity_test_input); 64 65 BufferSizeView bufferSizeView = findViewById(R.id.buffer_size_view); 66 bufferSizeView.setVisibility(View.GONE); 67 } 68 69 @Override onCreate(Bundle savedInstanceState)70 protected void onCreate(Bundle savedInstanceState) { 71 super.onCreate(savedInstanceState); 72 73 mVolumeBars[0] = (VolumeBarView) findViewById(R.id.volumeBar0); 74 mVolumeBars[1] = (VolumeBarView) findViewById(R.id.volumeBar1); 75 mVolumeBars[2] = (VolumeBarView) findViewById(R.id.volumeBar2); 76 mVolumeBars[3] = (VolumeBarView) findViewById(R.id.volumeBar3); 77 78 mInputMarginView = (InputMarginView) findViewById(R.id.input_margin_view); 79 80 updateEnabledWidgets(); 81 82 mAudioInputTester = addAudioInputTester(); 83 84 mWorkloadView = (WorkloadView) findViewById(R.id.workload_view); 85 if (mWorkloadView != null) { 86 mWorkloadView.setAudioStreamTester(mAudioInputTester); 87 } 88 } 89 90 @Override onStart()91 protected void onStart() { 92 super.onStart(); 93 setActivityType(ACTIVITY_TEST_INPUT); 94 } 95 96 @Override resetConfiguration()97 protected void resetConfiguration() { 98 super.resetConfiguration(); 99 mAudioInputTester.reset(); 100 } 101 102 @Override updateStreamDisplay()103 void updateStreamDisplay() { 104 int numChannels = mAudioInputTester.getCurrentAudioStream().getChannelCount(); 105 if (numChannels > NUM_VOLUME_BARS) { 106 numChannels = NUM_VOLUME_BARS; 107 } 108 for (int i = 0; i < numChannels; i++) { 109 if (mVolumeBars[i] == null) break; 110 double level = mAudioInputTester.getPeakLevel(i); 111 mVolumeBars[i].setVolume((float) level); 112 } 113 } 114 resetVolumeBars()115 void resetVolumeBars() { 116 for (int i = 0; i < mVolumeBars.length; i++) { 117 if (mVolumeBars[i] == null) break; 118 mVolumeBars[i].setVolume((float) 0.0); 119 } 120 } 121 setMinimumBurstsBeforeRead(int numBursts)122 void setMinimumBurstsBeforeRead(int numBursts) { 123 int framesPerBurst = mAudioInputTester.getCurrentAudioStream().getFramesPerBurst(); 124 if (framesPerBurst > 0) { 125 setMinimumFramesBeforeRead(numBursts * framesPerBurst); 126 } 127 } 128 129 @Override openAudio()130 public void openAudio() throws IOException { 131 if (!isRecordPermissionGranted()){ 132 requestRecordPermission(); 133 return; 134 } 135 super.openAudio(); 136 setMinimumBurstsBeforeRead(mInputMarginBursts); 137 resetVolumeBars(); 138 } 139 140 @Override stopAudio()141 public void stopAudio() { 142 super.stopAudio(); 143 resetVolumeBars(); 144 } 145 146 @Override toastPauseError(int result)147 protected void toastPauseError(int result) { 148 showToast("Pause not implemented. Returned " + result); 149 } 150 isRecordPermissionGranted()151 private boolean isRecordPermissionGranted() { 152 return (ActivityCompat.checkSelfPermission(this, 153 Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED); 154 } 155 requestRecordPermission()156 private void requestRecordPermission(){ 157 ActivityCompat.requestPermissions( 158 this, 159 new String[]{Manifest.permission.RECORD_AUDIO}, 160 AUDIO_ECHO_REQUEST); 161 } 162 163 @Override onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)164 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 165 @NonNull int[] grantResults) { 166 167 if (AUDIO_ECHO_REQUEST != requestCode) { 168 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 169 return; 170 } 171 172 if (grantResults.length != 1 || 173 grantResults[0] != PackageManager.PERMISSION_GRANTED) { 174 175 Toast.makeText(getApplicationContext(), 176 getString(R.string.need_record_audio_permission), 177 Toast.LENGTH_SHORT) 178 .show(); 179 } else { 180 // Permission was granted 181 try { 182 super.openAudio(); 183 } catch (IOException e) { 184 showErrorToast(e.getMessage()); 185 } 186 } 187 } 188 setupAGC(int sessionId)189 public void setupAGC(int sessionId) { 190 AutomaticGainControl effect = AutomaticGainControl.create(sessionId); 191 } 192 setupAEC(int sessionId)193 public void setupAEC(int sessionId) { 194 AcousticEchoCanceler effect = AcousticEchoCanceler.create(sessionId); 195 } 196 197 @Override setupEffects(int sessionId)198 public void setupEffects(int sessionId) { 199 setupAEC(sessionId); 200 } 201 saveWaveFile(File file)202 protected int saveWaveFile(File file) { 203 // Pass filename to native to write WAV file 204 int result = saveWaveFile(file.getAbsolutePath()); 205 if (result < 0) { 206 showErrorToast("Save returned " + result); 207 } else { 208 showToast("Saved " + result + " bytes."); 209 } 210 return result; 211 } 212 getWaveTag()213 String getWaveTag() { 214 return "input"; 215 } 216 217 @NonNull createFileName()218 private File createFileName() { 219 // Get directory and filename 220 File dir = getExternalFilesDir(Environment.DIRECTORY_MUSIC); 221 return new File(dir, "oboe_" + getWaveTag() + "_" + getTimestampString() + ".wav"); 222 } 223 shareWaveFile()224 public void shareWaveFile() { 225 // Share WAVE file via GMail, Drive or other method. 226 File file = createFileName(); 227 int result = saveWaveFile(file); 228 if (result > 0) { 229 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 230 sharingIntent.setType("audio/wav"); 231 String subjectText = file.getName(); 232 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subjectText); 233 Uri uri = FileProvider.getUriForFile(this, 234 BuildConfig.APPLICATION_ID + ".provider", 235 file); 236 sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); 237 sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 238 startActivity(Intent.createChooser(sharingIntent, "Share WAV using:")); 239 } 240 } 241 onShareFile(View view)242 public void onShareFile(View view) { 243 shareWaveFile(); 244 } 245 onMarginBoxClicked(View view)246 public void onMarginBoxClicked(View view) { 247 RadioButton radioButton = (RadioButton) view; 248 String text = (String) radioButton.getText(); 249 mInputMarginBursts = Integer.parseInt(text); 250 setMinimumBurstsBeforeRead(mInputMarginBursts); 251 } 252 } 253