f5d2666b993c4b4be21037f2d5c38cfa4691c857
[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 <string.h>
20 #include "CAudioIODef.h"
21
22
23 using namespace std;
24 using namespace tizen_media_audio;
25
26
27 /**
28  * class CAudioInfo
29  */
30 CAudioInfo::CAudioInfo() :
31     __mSampleRate(MAX_SYSTEM_SAMPLERATE),
32     __mChannel(EChannel::CHANNEL_MONO),
33     __mSampleType(ESampleType::SAMPLE_TYPE_U8),
34     __mAudioType(EAudioType::AUDIO_IN_TYPE_MEDIA),
35     __mAudioIndex(-1),
36     __mReferenceDeviceId(0),
37     __mNoiseSuppression(false) {
38 }
39
40 CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
41     __mSampleRate(sampleRate),
42     __mChannel(channel),
43     __mSampleType(sampleType),
44     __mAudioType(audioType),
45     __mAudioIndex(audioIndex),
46     __mReferenceDeviceId(0),
47     __mNoiseSuppression(false) {
48     // Check to invalid AudioInfo
49     if (sampleRate < CAudioInfo::MIN_SYSTEM_SAMPLERATE ||
50         sampleRate > CAudioInfo::MAX_SYSTEM_SAMPLERATE)
51         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
52                                "The sampleRate is invalid [sampleRate:%u]", sampleRate);
53
54     if (channel < CAudioInfo::EChannel::CHANNEL_MONO ||
55         channel >= CAudioInfo::EChannel::CHANNEL_MAX)
56         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
57                                "The channel is invalid [channel:%u]", to_integral(channel));
58
59     if (sampleType < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 ||
60         sampleType >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX)
61         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
62                                "The sampleType is invalid [sampleType:%u]", to_integral(sampleType));
63
64     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
65         audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
66         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
67                                "The audioType is invalid [audioType:%u]", to_integral(audioType));
68 }
69
70 unsigned int CAudioInfo::getSampleRate() noexcept {
71     return __mSampleRate;
72 }
73
74 CAudioInfo::EChannel CAudioInfo::getChannel() noexcept {
75     return __mChannel;
76 }
77
78 CAudioInfo::ESampleType CAudioInfo::getSampleType() noexcept {
79     return __mSampleType;
80 }
81
82 CAudioInfo::EAudioType CAudioInfo::getAudioType() noexcept {
83     return __mAudioType;
84 }
85
86 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
87     __mAudioType = convertInputStreamTypeToAudioType(streamType);
88 }
89
90 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
91     __mAudioType = convertOutputStreamTypeToAudioType(streamType);
92 }
93
94 int CAudioInfo::getAudioIndex() noexcept {
95     return __mAudioIndex;
96 }
97
98 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
99     __mAudioIndex = audioIndex;
100 }
101
102 int CAudioInfo::getSampleSize() noexcept {
103     int bytes_in_sample = 0;
104
105     switch (__mSampleType) {
106     case ESampleType::SAMPLE_TYPE_U8:
107         bytes_in_sample = 1;
108         break;
109     case ESampleType::SAMPLE_TYPE_S16_LE:
110         bytes_in_sample = 2;
111         break;
112     case ESampleType::SAMPLE_TYPE_S24_LE:
113         bytes_in_sample = 3;
114         break;
115     case ESampleType::SAMPLE_TYPE_S24_32_LE:
116     case ESampleType::SAMPLE_TYPE_S32_LE:
117         bytes_in_sample = 4;
118         break;
119     default:
120         AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
121         bytes_in_sample = 2;
122         break;
123     }
124
125     return bytes_in_sample * static_cast<int>(__mChannel);
126 }
127
128 void CAudioInfo::bindEchoCancelReferenceDeviceId(int id, sound_acoustic_echo_cancel_type_e type) {
129     if (type == SOUND_ACOUSTIC_ECHO_CANCEL_VOICE_CALL)
130         __mProcessorProperty += "webrtc,";
131     else if (type == SOUND_ACOUSTIC_ECHO_CANCEL_REFERENCE_COPY)
132         __mProcessorProperty += "reference_copy,";
133     else
134         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
135                                "The echo cancel is not supported [type:%d]", type);
136
137     __mReferenceDeviceId = id;
138 }
139
140 int CAudioInfo::getEchoCancelReferenceDeviceId() noexcept {
141     return __mReferenceDeviceId;
142 }
143
144 void CAudioInfo::setNoiseSuppression(bool enable, sound_noise_suppression_type_e type) {
145     if (type == SOUND_NOISE_SUPPRESSION_VOICE_CALL) {
146         __mProcessorProperty += "rnnoise,";
147     } else if (type == SOUND_NOISE_SUPPRESSION_VOICE_RECOGNITION) {
148         __mProcessorProperty += "pse,";
149     } else {
150         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
151                                "The noise suppression type is not supported [type:%d]", type);
152     }
153
154     __mNoiseSuppression = enable;
155 }
156
157 std::string& CAudioInfo::getProcessorProperty() noexcept {
158     return __mProcessorProperty;
159 }
160
161 const char* CAudioInfo::getConvertedStreamType() {
162     if (__mAudioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
163         __mAudioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
164         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
165                                "The audioType is not supported [audioType:%u]", to_integral(__mAudioType));
166
167     return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
168 }
169
170 CAudioInfo::EAudioType CAudioInfo::convertInputStreamTypeToAudioType(const char *streamType) {
171     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
172               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
173               i++) {
174         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
175             return (CAudioInfo::EAudioType)i;
176     }
177     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
178                            "The streamType of input is not supported [streamType:%s]", streamType);
179 }
180
181 CAudioInfo::EAudioType CAudioInfo::convertOutputStreamTypeToAudioType(const char *streamType) {
182     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
183               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_TYPE_MAX;
184               i++) {
185         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
186             return (CAudioInfo::EAudioType)i;
187     }
188     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
189                            "The streamType of output is not supported [streamType:%s]", streamType);
190 }