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(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
33 __mSampleRate(sampleRate),
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);
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));
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));
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));
60 unsigned int CAudioInfo::getSampleRate() const noexcept {
64 CAudioInfo::EChannel CAudioInfo::getChannel() const noexcept {
68 CAudioInfo::ESampleType CAudioInfo::getSampleType() const noexcept {
72 CAudioInfo::EAudioType CAudioInfo::getAudioType() const noexcept {
76 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
77 __mAudioType = convertInputStreamTypeToAudioType(streamType);
80 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
81 __mAudioType = convertOutputStreamTypeToAudioType(streamType);
84 int CAudioInfo::getAudioIndex() const noexcept {
88 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
89 __mAudioIndex = audioIndex;
92 int CAudioInfo::getSampleSize() const noexcept {
93 int bytes_in_sample = 0;
95 switch (__mSampleType) {
96 case ESampleType::SAMPLE_TYPE_U8:
99 case ESampleType::SAMPLE_TYPE_S16_LE:
102 case ESampleType::SAMPLE_TYPE_S24_LE:
105 case ESampleType::SAMPLE_TYPE_S24_32_LE:
106 case ESampleType::SAMPLE_TYPE_S32_LE:
110 AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
115 return bytes_in_sample * static_cast<int>(__mChannel);
118 void CAudioInfo::setEffectMethod(int method) noexcept {
119 __mEffectMethod = method;
122 std::string CAudioInfo::getEffectMethod() const noexcept {
125 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOIP)
126 method += "ns-rnnoise,";
128 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOICE_RECOGNITION)
131 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_DOORBELL)
132 method += "ns-srid,";
134 if (__mEffectMethod & SOUND_EFFECT_AUTOMATIC_GAIN_CONTROL_CAPTURE)
135 method += "agc-speex,";
140 void CAudioInfo::setEffectMethodWithReference(sound_effect_method_with_reference_e method, int id) noexcept {
141 __mEffectMethodWithReference = method;
142 __mEffectMethodReferenceDeviceId = id;
145 std::pair<std::string, int> CAudioInfo::getEffectMethodWithReference() const noexcept {
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,";
155 return make_pair(method, __mEffectMethodReferenceDeviceId);
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));
164 return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
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;
171 if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
172 return (CAudioInfo::EAudioType)i;
174 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
175 "The streamType of input is not supported [streamType:%s]", streamType);
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;
182 if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
183 return (CAudioInfo::EAudioType)i;
185 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
186 "The streamType of output is not supported [streamType:%s]", streamType);