CAudioInfo: Refactor getters to return in group
[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 < MIN_SYSTEM_SAMPLERATE ||
40         sampleRate > 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 < EChannel::CHANNEL_MONO ||
45         channel >= 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 < ESampleType::SAMPLE_TYPE_U8 ||
50         sampleType >= 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 < EAudioType::AUDIO_IN_TYPE_MEDIA ||
55         audioType >= 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 std::tuple<CAudioInfo::ESampleType, unsigned int, CAudioInfo::EChannel> CAudioInfo::getSpec() const noexcept {
61     return { __mSampleType, __mSampleRate, __mChannel };
62 }
63
64 CAudioInfo::EAudioType CAudioInfo::getAudioType() const noexcept {
65     return __mAudioType;
66 }
67
68 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
69     __mAudioType = convertInputStreamTypeToAudioType(streamType);
70 }
71
72 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
73     __mAudioType = convertOutputStreamTypeToAudioType(streamType);
74 }
75
76 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
77     __mAudioIndex = audioIndex;
78 }
79
80 int CAudioInfo::getSampleSize() const noexcept {
81     int bytes_in_sample = 0;
82
83     switch (__mSampleType) {
84     case ESampleType::SAMPLE_TYPE_U8:
85         bytes_in_sample = 1;
86         break;
87     case ESampleType::SAMPLE_TYPE_S16_LE:
88         bytes_in_sample = 2;
89         break;
90     case ESampleType::SAMPLE_TYPE_S24_LE:
91         bytes_in_sample = 3;
92         break;
93     case ESampleType::SAMPLE_TYPE_S24_32_LE:
94     case ESampleType::SAMPLE_TYPE_S32_LE:
95         bytes_in_sample = 4;
96         break;
97     default:
98         AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
99         bytes_in_sample = 2;
100         break;
101     }
102
103     return bytes_in_sample * static_cast<int>(__mChannel);
104 }
105
106
107 size_t CAudioInfo::msToBytes(size_t milliseconds) const noexcept {
108     return milliseconds * __mSampleRate * getSampleSize() / 1000;
109 }
110
111 size_t CAudioInfo::bytesToMs(size_t bytes) const noexcept {
112     return bytes * 1000 / getSampleSize() / __mSampleRate;
113 }
114
115 void CAudioInfo::setEffectMethod(int method) noexcept {
116     __mEffectMethod = method;
117 }
118
119 std::string CAudioInfo::getEffectMethod() const noexcept {
120     std::string method;
121
122     if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOIP)
123         method += "ns-rnnoise,";
124
125     if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOICE_RECOGNITION)
126         method += "ns-pse,";
127
128     if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_DOORBELL)
129         method += "ns-srid,";
130
131     if (__mEffectMethod & SOUND_EFFECT_AUTOMATIC_GAIN_CONTROL_CAPTURE)
132         method += "agc-speex,";
133
134     return method;
135 }
136
137 void CAudioInfo::setEffectMethodWithReference(sound_effect_method_with_reference_e method, int id) noexcept {
138     __mEffectMethodWithReference = method;
139     __mEffectMethodReferenceDeviceId = id;
140 }
141
142 std::pair<std::string, int> CAudioInfo::getEffectMethodWithReference() const noexcept {
143     std::string method;
144
145     if (__mEffectMethodWithReference == SOUND_EFFECT_REFERENCE_COPY)
146         method += "reference_copy,";
147     else if (__mEffectMethodWithReference == SOUND_EFFECT_ACOUSTIC_ECHO_CANCEL_SPEEX)
148         method += "aec-speex,";
149     else if (__mEffectMethodWithReference == SOUND_EFFECT_ACOUSTIC_ECHO_CANCEL_WEBRTC)
150         method += "aec-webrtc,";
151
152     return make_pair(method, __mEffectMethodReferenceDeviceId);
153 }
154
155 std::pair<const char*, int> CAudioInfo::getStreamProperties() const {
156     if (__mAudioType < EAudioType::AUDIO_IN_TYPE_MEDIA ||
157         __mAudioType >= EAudioType::AUDIO_TYPE_MAX)
158         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
159                                "The audioType is not supported [audioType:%u]", to_integral(__mAudioType));
160
161     return std::make_pair(__STREAM_TYPE_TABLE[(unsigned int)__mAudioType], __mAudioIndex);
162 }
163
164 CAudioInfo::EAudioType CAudioInfo::convertInputStreamTypeToAudioType(const char *streamType) {
165     for (auto i = (unsigned int)EAudioType::AUDIO_IN_TYPE_MEDIA;
166               i < (unsigned int)EAudioType::AUDIO_OUT_TYPE_MEDIA;
167               i++) {
168         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
169             return (EAudioType)i;
170     }
171     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
172                            "The streamType of input is not supported [streamType:%s]", streamType);
173 }
174
175 CAudioInfo::EAudioType CAudioInfo::convertOutputStreamTypeToAudioType(const char *streamType) {
176     for (auto i = (unsigned int)EAudioType::AUDIO_OUT_TYPE_MEDIA;
177               i < (unsigned int)EAudioType::AUDIO_TYPE_MAX;
178               i++) {
179         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
180             return (EAudioType)i;
181     }
182     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
183                            "The streamType of output is not supported [streamType:%s]", streamType);
184 }