2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "CAudioIODef.h"
22 #include <sound_manager.h>
23 #include <sound_manager_internal.h>
26 using namespace tizen_media_audio;
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),
38 __mEffectMethodWithReference((sound_effect_method_with_reference_e)0),
39 __mEffectMethodReferenceDeviceId(0),
43 CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
44 __mSampleRate(sampleRate),
46 __mSampleType(sampleType),
47 __mAudioType(audioType),
48 __mAudioIndex(audioIndex),
49 __mEffectMethodWithReference((sound_effect_method_with_reference_e)0),
50 __mEffectMethodReferenceDeviceId(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);
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));
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));
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));
74 unsigned int CAudioInfo::getSampleRate() noexcept {
78 CAudioInfo::EChannel CAudioInfo::getChannel() noexcept {
82 CAudioInfo::ESampleType CAudioInfo::getSampleType() noexcept {
86 CAudioInfo::EAudioType CAudioInfo::getAudioType() noexcept {
90 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
91 __mAudioType = convertInputStreamTypeToAudioType(streamType);
94 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
95 __mAudioType = convertOutputStreamTypeToAudioType(streamType);
98 int CAudioInfo::getAudioIndex() noexcept {
102 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
103 __mAudioIndex = audioIndex;
106 int CAudioInfo::getSampleSize() noexcept {
107 int bytes_in_sample = 0;
109 switch (__mSampleType) {
110 case ESampleType::SAMPLE_TYPE_U8:
113 case ESampleType::SAMPLE_TYPE_S16_LE:
116 case ESampleType::SAMPLE_TYPE_S24_LE:
119 case ESampleType::SAMPLE_TYPE_S24_32_LE:
120 case ESampleType::SAMPLE_TYPE_S32_LE:
124 AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
129 return bytes_in_sample * static_cast<int>(__mChannel);
132 void CAudioInfo::setEffectMethod(int method) noexcept {
133 __mEffectMethod = method;
136 std::string CAudioInfo::getEffectMethod() noexcept {
139 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOIP)
140 method += "ns-rnnoise,";
142 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOICE_RECOGNITION)
145 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_DOORBELL)
146 method += "ns-srid,";
148 if (__mEffectMethod & SOUND_EFFECT_AUTOMATIC_GAIN_CONTROL_CAPTURE)
149 method += "agc-speex,";
154 void CAudioInfo::setEffectMethodWithReference(sound_effect_method_with_reference_e method, int id) noexcept {
155 __mEffectMethodWithReference = method;
156 __mEffectMethodReferenceDeviceId = id;
159 std::pair<std::string, int> CAudioInfo::getEffectMethodWithReference() noexcept {
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,";
169 return make_pair(method, __mEffectMethodReferenceDeviceId);
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));
178 return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
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;
185 if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
186 return (CAudioInfo::EAudioType)i;
188 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
189 "The streamType of input is not supported [streamType:%s]", streamType);
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;
196 if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
197 return (CAudioInfo::EAudioType)i;
199 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
200 "The streamType of output is not supported [streamType:%s]", streamType);