added explicit trace events for AudioRecord

In the VoiceInteractionService sample app, each call to the AudioRecord
is tracked via trace to observe latency.

Bug: 19833161
Test: manual
Change-Id: I24f4b939c7e2eee77399439bc28a9dbce5594129
This commit is contained in:
Nicholas Ambur
2022-09-08 12:23:46 -07:00
parent 010ca91cc7
commit 188fca534f
3 changed files with 17 additions and 8 deletions

View File

@@ -17,12 +17,15 @@
package com.example.android.voiceinteractor;
import android.media.AudioRecord;
import android.os.Trace;
import android.util.Log;
import java.time.Duration;
import java.util.Arrays;
public class AudioUtils {
private static final String TAG = "Hotword-AudioUtils";
private static final Duration AUDIO_RECORD_READ_DURATION = Duration.ofSeconds(1);
static int read(AudioRecord record, int bytesPerSecond, float secondsToRead, byte[] buffer) {
Log.i(TAG, "read(): bytesPerSecond=" + bytesPerSecond
@@ -30,8 +33,12 @@ public class AudioUtils {
int numBytes = 0;
int nextSecondToSample = 0;
while (true) {
int bytesRead = record.read(buffer, numBytes, Math.round(bytesPerSecond * secondsToRead));
Log.i(TAG, "AudioRecord.read offset=" + numBytes + ", size=" + Math.round(bytesPerSecond * secondsToRead));
Trace.beginAsyncSection("AudioUtils.read", 0);
int bytesRead = record.read(buffer, numBytes,
(int) (bytesPerSecond * AUDIO_RECORD_READ_DURATION.getSeconds()));
Trace.endAsyncSection("AudioUtils.read", 0);
Log.i(TAG, "AudioRecord.read offset=" + numBytes + ", size="
+ (bytesPerSecond * AUDIO_RECORD_READ_DURATION.getSeconds()));
numBytes += bytesRead;
if (bytesRead <= 0) {

View File

@@ -41,8 +41,8 @@ public class SampleHotwordDetectionService extends HotwordDetectionService {
static final String TAG = "SHotwordDetectionSrvc";
// AudioRecord config
private static final Duration AUDIO_RECORD_BUFFER_DURATION = Duration.ofSeconds(1);
private static final Duration DSP_AUDIO_READ_DURATION = Duration.ofSeconds(5);
private static final Duration AUDIO_RECORD_BUFFER_DURATION = Duration.ofSeconds(5);
private static final Duration DSP_AUDIO_READ_DURATION = Duration.ofSeconds(3);
private static final Duration AUDIO_RECORD_RELEASE_TIMEOUT = Duration.ofSeconds(10);
private static AudioRecord createAudioRecord(AlwaysOnHotwordDetector.EventPayload eventPayload,

View File

@@ -49,7 +49,7 @@ public class SampleVoiceInteractionService extends VoiceInteractionService {
// AudioRecord config
private static final Duration AUDIO_RECORD_BUFFER_DURATION = Duration.ofSeconds(5);
private static final Duration AUDIO_READ_DURATION = Duration.ofSeconds(5);
private static final Duration AUDIO_READ_DURATION = Duration.ofSeconds(3);
// DSP model config
private static final Locale DSP_MODEL_LOCALE = Locale.US;
@@ -192,8 +192,8 @@ public class SampleVoiceInteractionService extends VoiceInteractionService {
// AudioRecord record = createAudioRecord(eventPayload, bytesPerSecond, sessionId);
AudioRecord record = createAudioRecord(eventPayload, bytesPerSecond);
Trace.endAsyncSection("SampleVoiceInteractionService.createAudioRecord", 1);
if (record.getState() != AudioRecord.STATE_INITIALIZED) {
Trace.endAsyncSection("SampleVoiceInteractionService.createAudioRecord", 1);
Trace.setCounter("SampleVoiceInteractionService AudioRecord.STATE_INITIALIZED",
record.getState());
Log.e(TAG, "Failed to init first AudioRecord.");
@@ -206,9 +206,13 @@ public class SampleVoiceInteractionService extends VoiceInteractionService {
}
byte[] buffer = new byte[bytesPerSecond * (int) AUDIO_READ_DURATION.getSeconds()];
Trace.beginAsyncSection("SampleVoiceInteractionService.startRecording", 1);
record.startRecording();
Trace.endAsyncSection("SampleVoiceInteractionService.startRecording", 1);
Trace.beginAsyncSection("SampleVoiceInteractionService.read", 1);
int numBytes = AudioUtils.read(record, bytesPerSecond, AUDIO_READ_DURATION.getSeconds(),
buffer);
Trace.endAsyncSection("SampleVoiceInteractionService.read", 1);
// try {
// Thread.sleep(2000);
@@ -218,8 +222,6 @@ public class SampleVoiceInteractionService extends VoiceInteractionService {
// }
Trace.endAsyncSection("SampleVoiceInteractionService.createAudioRecord", 1);
Trace.setCounter("SampleVoiceInteractionService Read Complete", numBytes);
record.stop();
record.release();