use constexpr whenever possible / remove unused member function
[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
23 using namespace std;
24 using namespace tizen_media_audio;
25
26
27 /**
28  * class CAudioInfo
29  */
30 CAudioInfo::CAudioInfo() :
31     __mSampleRate(MAX_SYSTEM_SAMPLERATE),
32     __mChannel(EChannel::CHANNEL_MONO),
33     __mSampleType(ESampleType::SAMPLE_TYPE_U8),
34     __mAudioType(EAudioType::AUDIO_IN_TYPE_MEDIA),
35     __mAudioIndex(-1) {
36 }
37
38 CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
39     __mSampleRate(sampleRate),
40     __mChannel(channel),
41     __mSampleType(sampleType),
42     __mAudioType(audioType),
43     __mAudioIndex(audioIndex) {
44     // Check to invalid AudioInfo
45     if (sampleRate < CAudioInfo::MIN_SYSTEM_SAMPLERATE ||
46         sampleRate > CAudioInfo::MAX_SYSTEM_SAMPLERATE)
47         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
48                                "The sampleRate is invalid [sampleRate:%u]", sampleRate);
49
50     if (channel < CAudioInfo::EChannel::CHANNEL_MONO ||
51         channel >= CAudioInfo::EChannel::CHANNEL_MAX)
52         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
53                                "The channel is invalid [channel:%u]", to_integral(channel));
54
55     if (sampleType < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 ||
56         sampleType >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX)
57         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
58                                "The sampleType is invalid [sampleType:%u]", to_integral(sampleType));
59
60     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
61         audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
62         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
63                                "The audioType is invalid [audioType:%u]", to_integral(audioType));
64 }
65
66 unsigned int CAudioInfo::getSampleRate() noexcept {
67     return __mSampleRate;
68 }
69
70 CAudioInfo::EChannel CAudioInfo::getChannel() noexcept {
71     return __mChannel;
72 }
73
74 CAudioInfo::ESampleType CAudioInfo::getSampleType() noexcept {
75     return __mSampleType;
76 }
77
78 CAudioInfo::EAudioType CAudioInfo::getAudioType() noexcept {
79     return __mAudioType;
80 }
81
82 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
83     __mAudioType = convertInputStreamTypeToAudioType(streamType);
84 }
85
86 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
87     __mAudioType = convertOutputStreamTypeToAudioType(streamType);
88 }
89
90 int CAudioInfo::getAudioIndex() noexcept {
91     return __mAudioIndex;
92 }
93
94 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
95     __mAudioIndex = audioIndex;
96 }
97
98 int CAudioInfo::getSampleSize() noexcept {
99     int bytes_in_sample = 0;
100
101     switch (__mSampleType) {
102     case ESampleType::SAMPLE_TYPE_U8:
103         bytes_in_sample = 1;
104         break;
105     case ESampleType::SAMPLE_TYPE_S16_LE:
106         bytes_in_sample = 2;
107         break;
108     case ESampleType::SAMPLE_TYPE_S24_LE:
109         bytes_in_sample = 3;
110         break;
111     case ESampleType::SAMPLE_TYPE_S24_32_LE:
112     case ESampleType::SAMPLE_TYPE_S32_LE:
113         bytes_in_sample = 4;
114         break;
115     default:
116         AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
117         bytes_in_sample = 2;
118         break;
119     }
120
121     return bytes_in_sample * static_cast<int>(__mChannel);
122 }
123
124 const char* CAudioInfo::getConvertedStreamType() {
125     if (__mAudioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
126         __mAudioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
127         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
128                                "The audioType is not supported [audioType:%u]", to_integral(__mAudioType));
129
130     return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
131 }
132
133 CAudioInfo::EAudioType CAudioInfo::convertInputStreamTypeToAudioType(const char *streamType) {
134     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
135               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
136               i++) {
137         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
138             return (CAudioInfo::EAudioType)i;
139     }
140     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
141                            "The streamType of input is not supported [streamType:%s]", streamType);
142 }
143
144 CAudioInfo::EAudioType CAudioInfo::convertOutputStreamTypeToAudioType(const char *streamType) {
145     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
146               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_TYPE_MAX;
147               i++) {
148         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
149             return (CAudioInfo::EAudioType)i;
150     }
151     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
152                            "The streamType of output is not supported [streamType:%s]", streamType);
153 }