1 /*
2  * Copyright (C) 2022 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.example.android.voiceinteractor;
18 
19 import android.media.AudioRecord;
20 import android.os.Trace;
21 import android.util.Log;
22 
23 import java.time.Duration;
24 import java.util.Arrays;
25 
26 public class AudioUtils {
27     private static final String TAG = "Hotword-AudioUtils";
28     private static final Duration AUDIO_RECORD_READ_DURATION = Duration.ofSeconds(1);
29 
read(AudioRecord record, int bytesPerSecond, float secondsToRead, byte[] buffer)30     static int read(AudioRecord record, int bytesPerSecond, float secondsToRead, byte[] buffer) {
31         Log.i(TAG, "read(): bytesPerSecond=" + bytesPerSecond
32                 + ", secondsToRead=" + secondsToRead + ", bufferSize=" + buffer.length);
33         int numBytes = 0;
34         int nextSecondToSample = 0;
35         while (true) {
36             Trace.beginAsyncSection("AudioRecord.read", 0);
37             int bytesRead = record.read(buffer, numBytes,
38                     (int) (bytesPerSecond * AUDIO_RECORD_READ_DURATION.getSeconds()));
39             Trace.endAsyncSection("AudioRecord.read", 0);
40             Log.i(TAG, "AudioRecord.read offset=" + numBytes + ", size="
41                     + (bytesPerSecond * AUDIO_RECORD_READ_DURATION.getSeconds()));
42             numBytes += bytesRead;
43 
44             if (bytesRead <= 0) {
45                 Log.i(TAG, "Finished reading, last read()=" + bytesRead);
46                 break;
47             }
48             int curSecond = numBytes / bytesPerSecond;
49             if (curSecond == nextSecondToSample
50                     && numBytes > (bytesPerSecond * curSecond) + 10) {
51                 Log.i(TAG, "sample=" + Arrays.toString(
52                         Arrays.copyOfRange(
53                                 buffer, bytesPerSecond * curSecond,
54                                 (bytesPerSecond * curSecond) + 10)));
55                 nextSecondToSample++;
56             }
57             if ((numBytes * 1.0 / bytesPerSecond) >= secondsToRead) {
58                 Log.i(TAG, "recorded enough. stopping. bytesRead=" + numBytes
59                         + ", secondsRead=" + (numBytes * 1.0 / bytesPerSecond));
60                 break;
61             }
62         }
63         return numBytes;
64     }
65 }
66