• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
20  * Container for the properties of a Stream.
21  *
22  * This can be used to build a stream, or as a base class for a Stream,
23  * or as a way to report the properties of a Stream.
24  */
25 
26 public class StreamConfiguration {
27     public static final int UNSPECIFIED = 0;
28 
29     // These must match order in Spinner and in native code and in AAudio.h
30     public static final int NATIVE_API_UNSPECIFIED = 0;
31     public static final int NATIVE_API_OPENSLES = 1;
32     public static final int NATIVE_API_AAUDIO = 2;
33 
34     public static final int SHARING_MODE_EXCLUSIVE = 0; // must match AAUDIO
35     public static final int SHARING_MODE_SHARED = 1; // must match AAUDIO
36 
37     public static final int AUDIO_FORMAT_PCM_16 = 1; // must match AAUDIO
38     public static final int AUDIO_FORMAT_PCM_FLOAT = 2; // must match AAUDIO
39 
40     public static final int DIRECTION_OUTPUT = 0; // must match AAUDIO
41     public static final int DIRECTION_INPUT = 1; // must match AAUDIO
42 
43     public static final int SESSION_ID_NONE = -1; // must match AAUDIO
44     public static final int SESSION_ID_ALLOCATE = 0; // must match AAUDIO
45 
46     public static final int PERFORMANCE_MODE_NONE = 10; // must match AAUDIO
47     public static final int PERFORMANCE_MODE_POWER_SAVING = 11; // must match AAUDIO
48     public static final int PERFORMANCE_MODE_LOW_LATENCY = 12; // must match AAUDIO
49 
50     public static final int RATE_CONVERSION_QUALITY_NONE = 0; // must match Oboe
51     public static final int RATE_CONVERSION_QUALITY_FASTEST = 1; // must match Oboe
52     public static final int RATE_CONVERSION_QUALITY_LOW = 2; // must match Oboe
53     public static final int RATE_CONVERSION_QUALITY_MEDIUM = 3; // must match Oboe
54     public static final int RATE_CONVERSION_QUALITY_HIGH = 4; // must match Oboe
55     public static final int RATE_CONVERSION_QUALITY_BEST = 5; // must match Oboe
56 
57     public static final int STREAM_STATE_STARTING = 3; // must match Oboe
58     public static final int STREAM_STATE_STARTED = 4; // must match Oboe
59 
60     public static final int INPUT_PRESET_GENERIC = 1; // must match Oboe
61     public static final int INPUT_PRESET_CAMCORDER = 5; // must match Oboe
62     public static final int INPUT_PRESET_VOICE_RECOGNITION = 6; // must match Oboe
63     public static final int INPUT_PRESET_VOICE_COMMUNICATION = 7; // must match Oboe
64     public static final int INPUT_PRESET_UNPROCESSED = 9; // must match Oboe
65     public static final int INPUT_PRESET_VOICE_PERFORMANCE = 10; // must match Oboe
66 
67     public static final int ERROR_DISCONNECTED = -899; // must match Oboe
68 
69     private int mNativeApi;
70     private int mBufferCapacityInFrames;
71     private int mChannelCount;
72     private int mDeviceId;
73     private int mSessionId;
74     private int mDirection; // does not get reset
75     private int mFormat;
76     private int mSampleRate;
77     private int mSharingMode;
78     private int mPerformanceMode;
79     private boolean mFormatConversionAllowed;
80     private boolean mChannelConversionAllowed;
81     private int mRateConversionQuality;
82     private int mInputPreset;
83 
84     private int mFramesPerBurst = 0;
85 
86     private boolean mMMap = false;
87 
StreamConfiguration()88     public StreamConfiguration() {
89         reset();
90     }
91 
reset()92     public void reset() {
93         mNativeApi = NATIVE_API_UNSPECIFIED;
94         mBufferCapacityInFrames = UNSPECIFIED;
95         mChannelCount = UNSPECIFIED;
96         mDeviceId = UNSPECIFIED;
97         mSessionId = -1;
98         mFormat = AUDIO_FORMAT_PCM_FLOAT;
99         mSampleRate = UNSPECIFIED;
100         mSharingMode = SHARING_MODE_EXCLUSIVE;
101         mPerformanceMode = PERFORMANCE_MODE_LOW_LATENCY;
102         mInputPreset = INPUT_PRESET_VOICE_RECOGNITION;
103         mFormatConversionAllowed = false;
104         mChannelConversionAllowed = false;
105         mRateConversionQuality = RATE_CONVERSION_QUALITY_NONE;
106         mMMap = NativeEngine.isMMapSupported();
107     }
108 
getFramesPerBurst()109     public int getFramesPerBurst() {
110         return mFramesPerBurst;
111     }
112 
setFramesPerBurst(int framesPerBurst)113     public void setFramesPerBurst(int framesPerBurst) {
114         this.mFramesPerBurst = framesPerBurst;
115     }
116 
getBufferCapacityInFrames()117     public int getBufferCapacityInFrames() {
118         return mBufferCapacityInFrames;
119     }
120 
setBufferCapacityInFrames(int bufferCapacityInFrames)121     public void setBufferCapacityInFrames(int bufferCapacityInFrames) {
122         this.mBufferCapacityInFrames = bufferCapacityInFrames;
123     }
124 
getFormat()125     public int getFormat() {
126         return mFormat;
127     }
128 
setFormat(int format)129     public void setFormat(int format) {
130         this.mFormat = format;
131     }
132 
getDirection()133     public int getDirection() {
134         return mDirection;
135     }
136 
setDirection(int direction)137     public void setDirection(int direction) {
138         this.mDirection = direction;
139     }
140 
getPerformanceMode()141     public int getPerformanceMode() {
142         return mPerformanceMode;
143     }
144 
setPerformanceMode(int performanceMode)145     public void setPerformanceMode(int performanceMode) {
146         this.mPerformanceMode = performanceMode;
147     }
148 
convertPerformanceModeToText(int performanceMode)149     static String convertPerformanceModeToText(int performanceMode) {
150         switch(performanceMode) {
151             case PERFORMANCE_MODE_NONE:
152                 return "NO";
153             case PERFORMANCE_MODE_POWER_SAVING:
154                 return "PS";
155             case PERFORMANCE_MODE_LOW_LATENCY:
156                 return "LL";
157             default:
158                 return "??";
159         }
160     }
161 
getInputPreset()162     public int getInputPreset() {
163         return mInputPreset;
164     }
165 
setInputPreset(int inputPreset)166     public void setInputPreset(int inputPreset) {
167         this.mInputPreset = inputPreset;
168     }
169 
getSharingMode()170     public int getSharingMode() {
171         return mSharingMode;
172     }
173 
setSharingMode(int sharingMode)174     public void setSharingMode(int sharingMode) {
175         this.mSharingMode = sharingMode;
176     }
177 
convertSharingModeToText(int sharingMode)178     static String convertSharingModeToText(int sharingMode) {
179         switch(sharingMode) {
180             case SHARING_MODE_SHARED:
181                 return "SH";
182             case SHARING_MODE_EXCLUSIVE:
183                 return "EX";
184             default:
185                 return "??";
186         }
187     }
188 
convertFormatToText(int format)189     public static String convertFormatToText(int format) {
190         switch(format) {
191             case UNSPECIFIED:
192                 return "Unspecified";
193             case AUDIO_FORMAT_PCM_16:
194                 return "I16";
195             case AUDIO_FORMAT_PCM_FLOAT:
196                 return "Float";
197             default:
198                 return "Invalid";
199         }
200     }
201 
convertNativeApiToText(int api)202     public static String convertNativeApiToText(int api) {
203         switch(api) {
204             case NATIVE_API_UNSPECIFIED:
205                 return "Unspec";
206             case NATIVE_API_AAUDIO:
207                 return "AAudio";
208             case NATIVE_API_OPENSLES:
209                 return "OpenSL";
210             default:
211                 return "Invalid";
212         }
213     }
214 
215 
dump()216     public String dump() {
217         String prefix = (getDirection() == DIRECTION_INPUT) ? "in" : "out";
218         StringBuffer message = new StringBuffer();
219         message.append(String.format("%s.channels = %d\n", prefix, mChannelCount));
220         message.append(String.format("%s.perf = %s\n", prefix,
221                 convertPerformanceModeToText(mPerformanceMode).toLowerCase()));
222         if (getDirection() == DIRECTION_INPUT) {
223             message.append(String.format("%s.preset = %s\n", prefix,
224                     convertInputPresetToText(mInputPreset).toLowerCase()));
225         }
226         message.append(String.format("%s.sharing = %s\n", prefix,
227                 convertSharingModeToText(mSharingMode).toLowerCase()));
228         message.append(String.format("%s.api = %s\n", prefix,
229                 convertNativeApiToText(getNativeApi()).toLowerCase()));
230         message.append(String.format("%s.rate = %d\n", prefix, mSampleRate));
231         message.append(String.format("%s.device = %d\n", prefix, mDeviceId));
232         message.append(String.format("%s.mmap = %s\n", prefix, isMMap() ? "yes" : "no"));
233         message.append(String.format("%s.rate.conversion.quality = %d\n", prefix, mRateConversionQuality));
234         return message.toString();
235     }
236 
237     // text must match menu values
238     public static final String NAME_INPUT_PRESET_GENERIC = "Generic";
239     public static final String NAME_INPUT_PRESET_CAMCORDER = "Camcorder";
240     public static final String NAME_INPUT_PRESET_VOICE_RECOGNITION = "VoiceRec";
241     public static final String NAME_INPUT_PRESET_VOICE_COMMUNICATION = "VoiceComm";
242     public static final String NAME_INPUT_PRESET_UNPROCESSED = "Unprocessed";
243     public static final String NAME_INPUT_PRESET_VOICE_PERFORMANCE = "Performance";
244 
convertInputPresetToText(int inputPreset)245     public static String convertInputPresetToText(int inputPreset) {
246         switch(inputPreset) {
247             case INPUT_PRESET_GENERIC:
248                 return NAME_INPUT_PRESET_GENERIC;
249             case INPUT_PRESET_CAMCORDER:
250                 return NAME_INPUT_PRESET_CAMCORDER;
251             case INPUT_PRESET_VOICE_RECOGNITION:
252                 return NAME_INPUT_PRESET_VOICE_RECOGNITION;
253             case INPUT_PRESET_VOICE_COMMUNICATION:
254                 return NAME_INPUT_PRESET_VOICE_COMMUNICATION;
255             case INPUT_PRESET_UNPROCESSED:
256                 return NAME_INPUT_PRESET_UNPROCESSED;
257             case INPUT_PRESET_VOICE_PERFORMANCE:
258                 return NAME_INPUT_PRESET_VOICE_PERFORMANCE;
259             default:
260                 return "Invalid";
261         }
262     }
263 
matchInputPreset(String text, int preset)264     private static boolean matchInputPreset(String text, int preset) {
265         return convertInputPresetToText(preset).toLowerCase().equals(text);
266     }
267 
268     /**
269      * Case insensitive.
270      * @param text
271      * @return inputPreset, eg. INPUT_PRESET_CAMCORDER
272      */
convertTextToInputPreset(String text)273     public static int convertTextToInputPreset(String text) {
274         text = text.toLowerCase();
275         if (matchInputPreset(text, INPUT_PRESET_GENERIC)) {
276             return INPUT_PRESET_GENERIC;
277         } else if (matchInputPreset(text, INPUT_PRESET_CAMCORDER)) {
278             return INPUT_PRESET_CAMCORDER;
279         } else if (matchInputPreset(text, INPUT_PRESET_VOICE_RECOGNITION)) {
280             return INPUT_PRESET_VOICE_RECOGNITION;
281         } else if (matchInputPreset(text, INPUT_PRESET_VOICE_COMMUNICATION)) {
282             return INPUT_PRESET_VOICE_COMMUNICATION;
283         } else if (matchInputPreset(text, INPUT_PRESET_UNPROCESSED)) {
284             return INPUT_PRESET_UNPROCESSED;
285         } else if (matchInputPreset(text, INPUT_PRESET_VOICE_PERFORMANCE)) {
286             return INPUT_PRESET_VOICE_PERFORMANCE;
287         }
288         return -1;
289     }
290 
getChannelCount()291     public int getChannelCount() {
292         return mChannelCount;
293     }
294 
setChannelCount(int channelCount)295     public void setChannelCount(int channelCount) {
296         this.mChannelCount = channelCount;
297     }
298 
getSampleRate()299     public int getSampleRate() {
300         return mSampleRate;
301     }
302 
setSampleRate(int sampleRate)303     public void setSampleRate(int sampleRate) {
304         this.mSampleRate = sampleRate;
305     }
306 
getDeviceId()307     public int getDeviceId() {
308         return mDeviceId;
309     }
310 
setDeviceId(int deviceId)311     public void setDeviceId(int deviceId) {
312         this.mDeviceId = deviceId;
313     }
314 
getSessionId()315     public int getSessionId() {
316         return mSessionId;
317     }
318 
setSessionId(int sessionId)319     public void setSessionId(int sessionId) {
320         mSessionId = sessionId;
321     }
322 
isMMap()323     public boolean isMMap() {
324         return mMMap;
325     }
326 
setMMap(boolean b)327     public void setMMap(boolean b) { mMMap = b; }
328 
getNativeApi()329     public int getNativeApi() {
330         return mNativeApi;
331     }
332 
setNativeApi(int nativeApi)333     public void setNativeApi(int nativeApi) {
334         mNativeApi = nativeApi;
335     }
336 
setChannelConversionAllowed(boolean b)337     public void setChannelConversionAllowed(boolean b) { mChannelConversionAllowed = b; }
338 
getChannelConversionAllowed()339     public boolean getChannelConversionAllowed() {
340         return mChannelConversionAllowed;
341     }
342 
setFormatConversionAllowed(boolean b)343     public void setFormatConversionAllowed(boolean b) {
344         mFormatConversionAllowed = b;
345     }
346 
getFormatConversionAllowed()347     public boolean getFormatConversionAllowed() {
348         return mFormatConversionAllowed;
349     }
350 
setRateConversionQuality(int quality)351     public void setRateConversionQuality(int quality) { mRateConversionQuality = quality; }
352 
getRateConversionQuality()353     public int getRateConversionQuality() {
354         return mRateConversionQuality;
355     }
356 
357 }
358