blob: d9d2e8883a33dbedb88954127f09c333511bc691 [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) {
33 case OBOE_AUDIO_FORMAT_PCM16:
Phil Burke1ce4912016-11-21 10:40:25 -080034 size = sizeof(int16_t);
35 break;
Phil Burkd8bdcab2017-01-03 17:20:30 -080036 case OBOE_AUDIO_FORMAT_PCM32:
37 case OBOE_AUDIO_FORMAT_PCM824:
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
70oboe_result_t OboeConvert_androidToOboeError(status_t error) {
71 if (error >= 0) {
72 return error;
73 }
74 oboe_result_t result;
75 switch (error) {
76 case OK:
77 result = OBOE_OK;
78 break;
79 case INVALID_OPERATION:
80 result = OBOE_ERROR_INVALID_STATE;
81 break;
82 case BAD_VALUE:
83 result = OBOE_ERROR_UNEXPECTED_VALUE;
84 break;
85 case WOULD_BLOCK:
86 result = OBOE_ERROR_WOULD_BLOCK;
87 break;
88 // TODO add more error codes
89 default:
90 result = OBOE_ERROR_INTERNAL;
91 break;
92 }
93 return result;
94}
95
96audio_format_t OboeConvert_oboeToAndroidDataFormat(oboe_audio_format_t oboeFormat) {
97 audio_format_t androidFormat;
98 switch (oboeFormat) {
99 case OBOE_AUDIO_FORMAT_PCM16:
100 androidFormat = AUDIO_FORMAT_PCM_16_BIT;
101 break;
102 case OBOE_AUDIO_FORMAT_PCM_FLOAT:
103 androidFormat = AUDIO_FORMAT_PCM_FLOAT;
104 break;
105 case OBOE_AUDIO_FORMAT_PCM824:
106 androidFormat = AUDIO_FORMAT_PCM_8_24_BIT;
107 break;
108 case OBOE_AUDIO_FORMAT_PCM32:
109 androidFormat = AUDIO_FORMAT_PCM_32_BIT;
110 break;
111 default:
112 androidFormat = AUDIO_FORMAT_DEFAULT;
113 ALOGE("OboeConvert_oboeToAndroidDataFormat 0x%08X unrecognized", oboeFormat);
114 break;
115 }
116 return androidFormat;
117}
118
119oboe_audio_format_t OboeConvert_androidToOboeDataFormat(audio_format_t androidFormat) {
120 oboe_audio_format_t oboeFormat = OBOE_AUDIO_FORMAT_INVALID;
121 switch (androidFormat) {
122 case AUDIO_FORMAT_PCM_16_BIT:
123 oboeFormat = OBOE_AUDIO_FORMAT_PCM16;
124 break;
125 case AUDIO_FORMAT_PCM_FLOAT:
126 oboeFormat = OBOE_AUDIO_FORMAT_PCM_FLOAT;
127 break;
128 case AUDIO_FORMAT_PCM_32_BIT:
129 oboeFormat = OBOE_AUDIO_FORMAT_PCM32;
130 break;
131 case AUDIO_FORMAT_PCM_8_24_BIT:
132 oboeFormat = OBOE_AUDIO_FORMAT_PCM824;
133 break;
134 default:
135 oboeFormat = OBOE_AUDIO_FORMAT_INVALID;
136 ALOGE("OboeConvert_androidToOboeDataFormat 0x%08X unrecognized", androidFormat);
137 break;
138 }
139 return oboeFormat;
140}
141
142oboe_size_bytes_t OboeConvert_framesToBytes(oboe_size_frames_t numFrames,
143 oboe_size_bytes_t bytesPerFrame,
144 oboe_size_bytes_t *sizeInBytes) {
145 // TODO implement more elegantly
146 const int32_t maxChannels = 256; // ridiculously large
147 const oboe_size_frames_t maxBytesPerFrame = maxChannels * sizeof(float);
148 // Prevent overflow by limiting multiplicands.
149 if (bytesPerFrame > maxBytesPerFrame || numFrames > (0x3FFFFFFF / maxBytesPerFrame)) {
150 ALOGE("size overflow, numFrames = %d, frameSize = %zd", numFrames, bytesPerFrame);
151 return OBOE_ERROR_OUT_OF_RANGE;
152 }
153 *sizeInBytes = numFrames * bytesPerFrame;
154 return OBOE_OK;
155}