diff --git a/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/AudioUtils.java b/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/AudioUtils.java index d517de2d3..324ab3af9 100644 --- a/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/AudioUtils.java +++ b/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/AudioUtils.java @@ -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) { diff --git a/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleHotwordDetectionService.java b/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleHotwordDetectionService.java index e0b122df8..d31402d29 100644 --- a/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleHotwordDetectionService.java +++ b/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleHotwordDetectionService.java @@ -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, diff --git a/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleVoiceInteractionService.java b/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleVoiceInteractionService.java index c244e7dd4..f1473b1ed 100644 --- a/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleVoiceInteractionService.java +++ b/samples/VoiceInteractionService/src/com/example/android/voiceinteractor/SampleVoiceInteractionService.java @@ -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();