1 /* 2 * Copyright (C) 2024 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.android.cts.verifier.audio.audiolib; 18 19 /** 20 * Implements a facility for capturing (buffering) audio data from a native stream and writing 21 * it out to a WAV format file. 22 */ 23 public class WavFileCapture { 24 private static final String TAG = "WavFileCapture"; 25 private static final boolean LOG = false; 26 27 private long mNativeWavFileCapture; 28 29 private String mWavFilePath; 30 WavFileCapture()31 public WavFileCapture() { 32 getCapture(); 33 } 34 35 /** 36 * @return a pointer to the native WavFileCapture cast to a long 37 */ getCapture()38 public long getCapture() { 39 return mNativeWavFileCapture = getCapture_n(); 40 } 41 42 /** 43 * Specifies the location and filename for any capture files. 44 * @param wavFileName - The full path name of the captured file. 45 */ setCaptureFile(String wavFileName)46 public void setCaptureFile(String wavFileName) { 47 mWavFilePath = wavFileName; 48 49 setCaptureFile_n(mNativeWavFileCapture, mWavFilePath); 50 } 51 52 /** 53 * @return The name of the captured file (if any) 54 */ getWavFilePath()55 public String getWavFilePath() { 56 return mWavFilePath; 57 } 58 59 /** 60 * Specifies the format of the captured WAV file name. 61 * @param numChannels The number of channels 62 * @param sampleRate The sample rate 63 */ setWavSpec(int numChannels, int sampleRate)64 public void setWavSpec(int numChannels, int sampleRate) { 65 setWavSpec_n(mNativeWavFileCapture, numChannels, sampleRate); 66 } 67 68 /** 69 * Starts the capture process 70 */ startCapture()71 public void startCapture() { 72 startCapture_n(mNativeWavFileCapture); 73 } 74 75 /** 76 * completeCapture() status codes. 77 * Note: These need to be kept in sync with the equivalent constants 78 * in WavFileCapture.h. 79 */ 80 public static final int CAPTURE_NOTDONE = 1; 81 public static final int CAPTURE_SUCCESS = 0; 82 public static final int CAPTURE_BADOPEN = -1; 83 public static final int CAPTURE_BADWRITE = -2; 84 /** 85 * Finishes the capture process and saves the captured WAV data 86 * @return a return code (from above) indicating the result of the file save. 87 */ completeCapture()88 public int completeCapture() { 89 return completeCapture_n(mNativeWavFileCapture); 90 } 91 92 /** 93 * Abandons the capture process. Discards any captured WAV data. 94 */ abandonCaptureData()95 public void abandonCaptureData() { 96 abandonCaptureData_n(mNativeWavFileCapture); 97 } 98 99 /* 100 * JNI Interface 101 */ getCapture_n()102 private native long getCapture_n(); setCaptureFile_n(long wavCaptureObj, String wavFilePath)103 private native void setCaptureFile_n(long wavCaptureObj, String wavFilePath); setWavSpec_n(long wavCaptureObj, int numChannels, int sampleRate)104 private native void setWavSpec_n(long wavCaptureObj, int numChannels, int sampleRate); 105 startCapture_n(long wavCaptureObj)106 private native void startCapture_n(long wavCaptureObj); completeCapture_n(long wavCaptureObj)107 private native int completeCapture_n(long wavCaptureObj); abandonCaptureData_n(long wavCaptureObj)108 private native void abandonCaptureData_n(long wavCaptureObj); 109 } 110