MCPcopy Create free account
hub / github.com/EasyRPG/Player / DecodeAndConvertInt16

Function DecodeAndConvertInt16

src/audio_resampler.cpp:120–182  ·  view source on GitHub ↗

* Utility function used to convert a buffer of a arbitrary AudioDecoder::Format to a int16 buffer * * @param[in] wrapped_decoder The decoder from which audio samples are read * @param[inout] buffer The buffer which will receive the converted samples, * has to be at least amount_of_samples_to_read*max(sizeof(int16_t),input_samplesize) bytes big. * @param[in] amount_of_samples_to_read The num

Source from the content-addressed store, hash-verified

118 * If the returned value has a negative value an error occured.
119 */
120inline static int DecodeAndConvertInt16(AudioDecoderBase* wrapped_decoder,
121 uint8_t * buffer,
122 int amount_of_samples_to_read,
123 const int input_samplesize,
124 const AudioDecoder::Format input_format){
125 int16_t* bufferAsInt16 = (int16_t*)buffer;
126
127 //Workaround for decoders which don't detect their own end
128 if (wrapped_decoder->IsFinished())
129 return 0;
130
131 int amount_of_samples_read = wrapped_decoder->Decode(buffer, amount_of_samples_to_read*input_samplesize);
132 if (amount_of_samples_read <= 0) {
133 return amount_of_samples_read; //error occured - or nothing read
134 } else {
135 //Convert the number of bytes to the number of samples
136 amount_of_samples_read /= input_samplesize;
137 }
138 //Convert the read data (amount_of_data_read is at least one at this moment)
139 switch (input_format) {
140 case AudioDecoder::Format::S8:
141 //Convert inplace (the last frames are unused if smaller)
142 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
143 bufferAsInt16[i] = ((int8_t*)bufferAsInt16)[i] << 8;
144 }
145 break;
146 case AudioDecoder::Format::U8:
147 //Convert inplace (the last frames are unused if smaller)
148 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
149 bufferAsInt16[i] = (((int16_t)(((uint8_t*)bufferAsInt16)[i])) - 128) << 8;
150 }
151 break;
152 case AudioDecoder::Format::S16:
153 //Nothing to convert
154 break;
155 case AudioDecoder::Format::U16:
156 //Convert unsigned to signed
157 for (int i = amount_of_samples_read - 1; i >= 0; i--) {
158 bufferAsInt16[i] = (int16_t)(((int32_t)(((uint16_t*)bufferAsInt16)[i])) - 32768);
159 }
160 break;
161 case AudioDecoder::Format::S32:
162 //Convert inplace (from front to back to prevent overwriting the buffer)
163 for (int i = 0; i < amount_of_samples_read; i++) {
164 bufferAsInt16[i] = (int16_t)((((int32_t*)bufferAsInt16)[i]) >> 16);
165 }
166 break;
167 case AudioDecoder::Format::U32:
168 //Convert inplace (from front to back to prevent overwriting the buffer)
169 for (int i = 0; i < amount_of_samples_read; i++) {
170 bufferAsInt16[i] = (int16_t)(((int32_t)((((uint32_t*)bufferAsInt16)[i]) >> 16)) - 32768);
171 }
172 break;
173 case AudioDecoder::Format::F32:
174 //Convert inplace (from front to back to prevent overwriting the buffer)
175 for (int i = 0; i < amount_of_samples_read; i++) {
176 float number = ((((float*)bufferAsInt16)[i])*32768.0);
177 bufferAsInt16[i] = (number <= 32767.0) ? ((number >= -32768.0) ? number : -32768) : 32767;

Callers 2

FillBufferSameRateMethod · 0.85

Calls 2

IsFinishedMethod · 0.45
DecodeMethod · 0.45

Tested by

no test coverage detected