Support 24bit sample format and up-to 192kHz sample rate
[platform/core/api/audio-io.git] / src / cpp / CAudioInfo.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #include <stdio.h>
19 #include "CAudioIODef.h"
20
21
22 using namespace std;
23 using namespace tizen_media_audio;
24
25
26 /**
27  * class CAudioInfo
28  */
29 CAudioInfo::CAudioInfo() :
30     __mSampleRate(MAX_SYSTEM_SAMPLERATE),
31     __mChannel(EChannel::CHANNEL_MONO),
32     __mSampleType(ESampleType::SAMPLE_TYPE_U8),
33     __mAudioType(EAudioType::AUDIO_IN_TYPE_MEDIA),
34     __mAudioIndex(-1) {
35 }
36
37 CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
38     __mSampleRate(sampleRate),
39     __mChannel(channel),
40     __mSampleType(sampleType),
41     __mAudioType(audioType),
42     __mAudioIndex(audioIndex) {
43     // Check to invalid AudioInfo
44     if (sampleRate < CAudioInfo::MIN_SYSTEM_SAMPLERATE || sampleRate > CAudioInfo::MAX_SYSTEM_SAMPLERATE) {
45         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The sampleRate is invalid [sampleRate:%u]", sampleRate);
46     }
47
48     if (channel < CAudioInfo::EChannel::CHANNEL_MONO || channel >= CAudioInfo::EChannel::CHANNEL_MAX) {
49         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The channel is invalid [channel:%u]", to_integral(channel));
50     }
51
52     if (sampleType < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 || sampleType >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX) {
53         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The sampleType is invalid [sampleType:%u]", to_integral(sampleType));
54     }
55
56     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX) {
57         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT, "The audioType is invalid [audioType:%u]", to_integral(audioType));
58     }
59 }
60
61 unsigned int CAudioInfo::getSampleRate() {
62     return __mSampleRate;
63 }
64
65 CAudioInfo::EChannel CAudioInfo::getChannel() {
66     return __mChannel;
67 }
68
69 CAudioInfo::ESampleType CAudioInfo::getSampleType() {
70     return __mSampleType;
71 }
72
73 CAudioInfo::EAudioType CAudioInfo::getAudioType() {
74     return __mAudioType;
75 }
76
77 void CAudioInfo::setAudioType(CAudioInfo::EAudioType audioType) {
78     __mAudioType = audioType;
79     return;
80 }
81
82 int CAudioInfo::getAudioIndex() {
83     return __mAudioIndex;
84 }
85
86 void CAudioInfo::setAudioIndex(int audioIndex) {
87     __mAudioIndex = audioIndex;
88     return;
89 }
90
91 int CAudioInfo::getSampleSize() {
92     int bytes_in_sample = 0;
93     int number_of_channel = 0;
94
95     switch (__mSampleType) {
96     case ESampleType::SAMPLE_TYPE_U8:
97         bytes_in_sample = 1;
98         break;
99     case ESampleType::SAMPLE_TYPE_S16_LE:
100         bytes_in_sample = 2;
101         break;
102     case ESampleType::SAMPLE_TYPE_S24_LE:
103         bytes_in_sample = 3;
104         break;
105     case ESampleType::SAMPLE_TYPE_S24_32_LE:
106         bytes_in_sample = 4;
107         break;
108     default:
109         AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
110         bytes_in_sample = 2;
111         break;
112     }
113
114     switch (__mChannel) {
115     case EChannel::CHANNEL_MONO:
116         number_of_channel = 1;
117         break;
118     case EChannel::CHANNEL_STEREO:
119         number_of_channel = 2;
120         break;
121     default:
122         AUDIO_IO_LOGW("As unrecognized channel %d, let's assume STEREO", static_cast<int>(__mChannel));
123         number_of_channel = 2;
124         break;
125     }
126
127     return bytes_in_sample * number_of_channel;
128 }
129
130 void CAudioInfo::convertAudioType2StreamType(CAudioInfo::EAudioType audioType, char **streamType) {
131     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA || audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
132         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
133                                "The audioType is not supported [audioType:%u]", to_integral(audioType));
134
135     *streamType = (char *)__STREAM_TYPE_TABLE[(unsigned int)audioType];
136     return;
137 }
138
139 void CAudioInfo::convertInputStreamType2AudioType(char *streamType, CAudioInfo::EAudioType *audioType) {
140     for (unsigned int i = (unsigned int)CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ; i < (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA ; i++) {
141         if (!strcmp((char *)__STREAM_TYPE_TABLE[i], streamType)) {
142             *audioType = (CAudioInfo::EAudioType)i;
143             return;
144         }
145     }
146     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "The streamType of input is not supported [streamType:%s]", streamType);
147     return;
148 }
149
150 void CAudioInfo::convertOutputStreamType2AudioType(char *streamType, CAudioInfo::EAudioType *audioType) {
151     for (unsigned int i = (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA ; i < (unsigned int)CAudioInfo::EAudioType::AUDIO_TYPE_MAX ; i++) {
152         if (!strcmp((char *)__STREAM_TYPE_TABLE[i], streamType)) {
153             *audioType = (CAudioInfo::EAudioType)i;
154             return;
155         }
156     }
157     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE, "The streamType of output is not supported [streamType:%s]", streamType);
158     return;
159 }
160