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