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 < 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);
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));
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));
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));
60 std::tuple<CAudioInfo::ESampleType, unsigned int, CAudioInfo::EChannel> CAudioInfo::getSpec() const noexcept {
61 return { __mSampleType, __mSampleRate, __mChannel };
64 CAudioInfo::EAudioType CAudioInfo::getAudioType() const noexcept {
68 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
69 __mAudioType = convertInputStreamTypeToAudioType(streamType);
72 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
73 __mAudioType = convertOutputStreamTypeToAudioType(streamType);
76 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
77 __mAudioIndex = audioIndex;
80 int CAudioInfo::getSampleSize() const noexcept {
81 int bytes_in_sample = 0;
83 switch (__mSampleType) {
84 case ESampleType::SAMPLE_TYPE_U8:
87 case ESampleType::SAMPLE_TYPE_S16_LE:
90 case ESampleType::SAMPLE_TYPE_S24_LE:
93 case ESampleType::SAMPLE_TYPE_S24_32_LE:
94 case ESampleType::SAMPLE_TYPE_S32_LE:
98 AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
103 return bytes_in_sample * static_cast<int>(__mChannel);
107 size_t CAudioInfo::msToBytes(size_t milliseconds) const noexcept {
108 return milliseconds * __mSampleRate * getSampleSize() / 1000;
111 size_t CAudioInfo::bytesToMs(size_t bytes) const noexcept {
112 return bytes * 1000 / getSampleSize() / __mSampleRate;
115 void CAudioInfo::setEffectMethod(int method) noexcept {
116 __mEffectMethod = method;
119 std::string CAudioInfo::getEffectMethod() const noexcept {
122 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOIP)
123 method += "ns-rnnoise,";
125 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_VOICE_RECOGNITION)
128 if (__mEffectMethod & SOUND_EFFECT_NOISE_SUPPRESSION_DOORBELL)
129 method += "ns-srid,";
131 if (__mEffectMethod & SOUND_EFFECT_AUTOMATIC_GAIN_CONTROL_CAPTURE)
132 method += "agc-speex,";
137 void CAudioInfo::setEffectMethodWithReference(sound_effect_method_with_reference_e method, int id) noexcept {
138 __mEffectMethodWithReference = method;
139 __mEffectMethodReferenceDeviceId = id;
142 std::pair<std::string, int> CAudioInfo::getEffectMethodWithReference() const noexcept {
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,";
152 return make_pair(method, __mEffectMethodReferenceDeviceId);
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));
161 return std::make_pair(__STREAM_TYPE_TABLE[(unsigned int)__mAudioType], __mAudioIndex);
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;
168 if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
169 return (EAudioType)i;
171 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
172 "The streamType of input is not supported [streamType:%s]", streamType);
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;
179 if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
180 return (EAudioType)i;
182 THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
183 "The streamType of output is not supported [streamType:%s]", streamType);