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