blob: fcf42529d573808e702cdb9cb83b38ccc32e85f8 [file] [log] [blame]
Phil Burke1ce4912016-11-21 10:40:25 -08001/*
2 * Copyright 2016 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#define LOG_TAG "OboeAudio"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <sys/types.h>
23#include <utils/Errors.h>
24
25#include "oboe/OboeDefinitions.h"
26#include "OboeUtilities.h"
27
28using namespace android;
29
30oboe_size_bytes_t OboeConvert_formatToSizeInBytes(oboe_audio_format_t format) {
Phil Burkd8bdcab2017-01-03 17:20:30 -080031 oboe_size_bytes_t size = OBOE_ERROR_ILLEGAL_ARGUMENT;
32 switch (format) {
Phil Burkdec33ab2017-01-17 14:48:16 -080033 case OBOE_AUDIO_FORMAT_PCM_I16:
Phil Burke1ce4912016-11-21 10:40:25 -080034 size = sizeof(int16_t);
35 break;
Phil Burkdec33ab2017-01-17 14:48:16 -080036 case OBOE_AUDIO_FORMAT_PCM_I32:
37 case OBOE_AUDIO_FORMAT_PCM_I8_24:
Phil Burke1ce4912016-11-21 10:40:25 -080038 size = sizeof(int32_t);
39 break;
Phil Burkd8bdcab2017-01-03 17:20:30 -080040 case OBOE_AUDIO_FORMAT_PCM_FLOAT:
Phil Burke1ce4912016-11-21 10:40:25 -080041 size = sizeof(float);
42 break;
43 default:
Phil Burke1ce4912016-11-21 10:40:25 -080044 break;
45 }
46 return size;
47}
48
49// TODO This similar to a function in audio_utils. Consider using that instead.
50void OboeConvert_floatToPcm16(const float *source, int32_t numSamples, int16_t *destination) {
51 for (int i = 0; i < numSamples; i++) {
52 float fval = source[i];
53 fval += 1.0; // to avoid discontinuity at 0.0 caused by truncation
54 fval *= 32768.0f;
55 int32_t sample = (int32_t) fval;
56 // clip to 16-bit range
57 if (sample < 0) sample = 0;
58 else if (sample > 0x0FFFF) sample = 0x0FFFF;
59 sample -= 32768; // center at zero
60 destination[i] = (int16_t) sample;
61 }
62}
63
64void OboeConvert_pcm16ToFloat(const float *source, int32_t numSamples, int16_t *destination) {
65 for (int i = 0; i < numSamples; i++) {
66 destination[i] = source[i] * (1.0f / 32768.0f);
67 }
68}
69
Phil Burkdec33ab2017-01-17 14:48:16 -080070status_t OboeConvert_oboeToAndroidStatus(oboe_result_t result) {
71 // This covers the case for OBOE_OK and for positive results.
72 if (result >= 0) {
73 return result;
74 }
75 status_t status;
76 switch (result) {
77 case OBOE_ERROR_DISCONNECTED:
78 case OBOE_ERROR_INVALID_HANDLE:
79 status = DEAD_OBJECT;
80 break;
81 case OBOE_ERROR_INVALID_STATE:
82 status = INVALID_OPERATION;
83 break;
84 case OBOE_ERROR_UNEXPECTED_VALUE: // TODO redundant?
85 case OBOE_ERROR_ILLEGAL_ARGUMENT:
86 status = BAD_VALUE;
87 break;
88 case OBOE_ERROR_WOULD_BLOCK:
89 status = WOULD_BLOCK;
90 break;
91 // TODO add more result codes
92 default:
93 status = UNKNOWN_ERROR;
94 break;
95 }
96 return status;
97}
98
99oboe_result_t OboeConvert_androidToOboeResult(status_t status) {
100 // This covers the case for OK and for positive result.
101 if (status >= 0) {
102 return status;
Phil Burke1ce4912016-11-21 10:40:25 -0800103 }
104 oboe_result_t result;
Phil Burkdec33ab2017-01-17 14:48:16 -0800105 switch (status) {
106 case BAD_TYPE:
107 result = OBOE_ERROR_INVALID_HANDLE;
108 break;
109 case DEAD_OBJECT:
110 result = OBOE_ERROR_DISCONNECTED;
Phil Burke1ce4912016-11-21 10:40:25 -0800111 break;
112 case INVALID_OPERATION:
113 result = OBOE_ERROR_INVALID_STATE;
114 break;
115 case BAD_VALUE:
116 result = OBOE_ERROR_UNEXPECTED_VALUE;
117 break;
118 case WOULD_BLOCK:
119 result = OBOE_ERROR_WOULD_BLOCK;
120 break;
Phil Burkdec33ab2017-01-17 14:48:16 -0800121 // TODO add more status codes
Phil Burke1ce4912016-11-21 10:40:25 -0800122 default:
123 result = OBOE_ERROR_INTERNAL;
124 break;
125 }
126 return result;
127}
128
129audio_format_t OboeConvert_oboeToAndroidDataFormat(oboe_audio_format_t oboeFormat) {
130 audio_format_t androidFormat;
131 switch (oboeFormat) {
Phil Burkdec33ab2017-01-17 14:48:16 -0800132 case OBOE_AUDIO_FORMAT_PCM_I16:
Phil Burke1ce4912016-11-21 10:40:25 -0800133 androidFormat = AUDIO_FORMAT_PCM_16_BIT;
134 break;
135 case OBOE_AUDIO_FORMAT_PCM_FLOAT:
136 androidFormat = AUDIO_FORMAT_PCM_FLOAT;
137 break;
Phil Burkdec33ab2017-01-17 14:48:16 -0800138 case OBOE_AUDIO_FORMAT_PCM_I8_24:
Phil Burke1ce4912016-11-21 10:40:25 -0800139 androidFormat = AUDIO_FORMAT_PCM_8_24_BIT;
140 break;
Phil Burkdec33ab2017-01-17 14:48:16 -0800141 case OBOE_AUDIO_FORMAT_PCM_I32:
Phil Burke1ce4912016-11-21 10:40:25 -0800142 androidFormat = AUDIO_FORMAT_PCM_32_BIT;
143 break;
144 default:
145 androidFormat = AUDIO_FORMAT_DEFAULT;
146 ALOGE("OboeConvert_oboeToAndroidDataFormat 0x%08X unrecognized", oboeFormat);
147 break;
148 }
149 return androidFormat;
150}
151
152oboe_audio_format_t OboeConvert_androidToOboeDataFormat(audio_format_t androidFormat) {
153 oboe_audio_format_t oboeFormat = OBOE_AUDIO_FORMAT_INVALID;
154 switch (androidFormat) {
155 case AUDIO_FORMAT_PCM_16_BIT:
Phil Burkdec33ab2017-01-17 14:48:16 -0800156 oboeFormat = OBOE_AUDIO_FORMAT_PCM_I16;
Phil Burke1ce4912016-11-21 10:40:25 -0800157 break;
158 case AUDIO_FORMAT_PCM_FLOAT:
159 oboeFormat = OBOE_AUDIO_FORMAT_PCM_FLOAT;
160 break;
161 case AUDIO_FORMAT_PCM_32_BIT:
Phil Burkdec33ab2017-01-17 14:48:16 -0800162 oboeFormat = OBOE_AUDIO_FORMAT_PCM_I32;
Phil Burke1ce4912016-11-21 10:40:25 -0800163 break;
164 case AUDIO_FORMAT_PCM_8_24_BIT:
Phil Burkdec33ab2017-01-17 14:48:16 -0800165 oboeFormat = OBOE_AUDIO_FORMAT_PCM_I8_24;
Phil Burke1ce4912016-11-21 10:40:25 -0800166 break;
167 default:
168 oboeFormat = OBOE_AUDIO_FORMAT_INVALID;
169 ALOGE("OboeConvert_androidToOboeDataFormat 0x%08X unrecognized", androidFormat);
170 break;
171 }
172 return oboeFormat;
173}
174
175oboe_size_bytes_t OboeConvert_framesToBytes(oboe_size_frames_t numFrames,
176 oboe_size_bytes_t bytesPerFrame,
177 oboe_size_bytes_t *sizeInBytes) {
178 // TODO implement more elegantly
179 const int32_t maxChannels = 256; // ridiculously large
180 const oboe_size_frames_t maxBytesPerFrame = maxChannels * sizeof(float);
181 // Prevent overflow by limiting multiplicands.
182 if (bytesPerFrame > maxBytesPerFrame || numFrames > (0x3FFFFFFF / maxBytesPerFrame)) {
183 ALOGE("size overflow, numFrames = %d, frameSize = %zd", numFrames, bytesPerFrame);
184 return OBOE_ERROR_OUT_OF_RANGE;
185 }
186 *sizeInBytes = numFrames * bytesPerFrame;
187 return OBOE_OK;
188}