* Utility function used to convert a buffer of a arbitrary AudioDecoder::Format to a float 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*sizeof(float) bytes big. * @param[in] amount_of_samples_to_read The number of samples to read.
| 41 | * If the returned value has a negative value an error occured. |
| 42 | */ |
| 43 | inline static int DecodeAndConvertFloat(AudioDecoderBase* wrapped_decoder, |
| 44 | uint8_t * buffer, |
| 45 | int amount_of_samples_to_read, |
| 46 | const int input_samplesize, |
| 47 | const AudioDecoder::Format input_format){ |
| 48 | float* bufferAsFloat = (float*)buffer; |
| 49 | |
| 50 | //Workaround for decoders which don't detect their own end |
| 51 | if (wrapped_decoder->IsFinished()) |
| 52 | return 0; |
| 53 | |
| 54 | int amount_of_samples_read = wrapped_decoder->Decode(buffer, amount_of_samples_to_read*input_samplesize); |
| 55 | if (amount_of_samples_read <= 0) { |
| 56 | return amount_of_samples_read; //error occured - or nothing read |
| 57 | } else { |
| 58 | amount_of_samples_read /= input_samplesize; |
| 59 | } |
| 60 | |
| 61 | //Convert the read data (amount_of_data_read is at least one at this moment) |
| 62 | switch (input_format) { |
| 63 | case AudioDecoder::Format::S8: |
| 64 | //Convert inplace (the last frames are unused if smaller) |
| 65 | for (int i = amount_of_samples_read - 1; i >= 0; i--) { |
| 66 | bufferAsFloat[i] = ((int8_t*)bufferAsFloat)[i] / 128.0; |
| 67 | } |
| 68 | break; |
| 69 | case AudioDecoder::Format::U8: |
| 70 | //Convert inplace (the last frames are unused if smaller) |
| 71 | for (int i = amount_of_samples_read - 1; i >= 0; i--) { |
| 72 | bufferAsFloat[i] = ((uint8_t*)bufferAsFloat)[i] / 128.0 - 1.0; |
| 73 | } |
| 74 | break; |
| 75 | case AudioDecoder::Format::S16: |
| 76 | //Convert inplace (the last frames are unused if smaller) |
| 77 | for (int i = amount_of_samples_read - 1; i >= 0; i--) { |
| 78 | bufferAsFloat[i] = ((int16_t*)bufferAsFloat)[i] / 32768.0; |
| 79 | } |
| 80 | break; |
| 81 | case AudioDecoder::Format::U16: |
| 82 | //Convert inplace (the last frames are unused if smaller) |
| 83 | for (int i = amount_of_samples_read - 1; i >= 0; i--) { |
| 84 | bufferAsFloat[i] = ((uint16_t*)bufferAsFloat)[i] / 32768.0 - 1.0; |
| 85 | } |
| 86 | break; |
| 87 | case AudioDecoder::Format::S32: |
| 88 | //Convert inplace (same size) |
| 89 | for (int i = amount_of_samples_read - 1; i >= 0; i--) { |
| 90 | bufferAsFloat[i] = ((int32_t*)bufferAsFloat)[i] / 2147483648.0; |
| 91 | } |
| 92 | break; |
| 93 | case AudioDecoder::Format::U32: |
| 94 | //Convert inplace (same size) |
| 95 | for (int i = amount_of_samples_read - 1; i >= 0; i--) { |
| 96 | bufferAsFloat[i] = ((uint32_t*)bufferAsFloat)[i] / 2147483648.0 - 1.0; |
| 97 | } |
| 98 | break; |
| 99 | case AudioDecoder::Format::F32: |
| 100 | //Nothing to convert |
no test coverage detected