|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "Arduino.h" |
| 4 | +#include "CommonHelix.h" |
| 5 | +#include "libhelix-aac/aacdec.h" |
| 6 | + |
| 7 | +#define AAC_MAX_OUTPUT_SIZE 2048 |
| 8 | +#define AAC_MAX_FRAME_SIZE 1600 |
| 9 | + |
| 10 | +namespace libhelix { |
| 11 | + |
| 12 | +typedef void (*AACInfoCallback)(_AACFrameInfo &info); |
| 13 | +typedef void (*AACDataCallback)(_AACFrameInfo &info,short *pwm_buffer, size_t len); |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief A simple Arduino API for the libhelix AAC decoder. The data us provided with the help of write() calls. |
| 17 | + * The decoded result is available either via a callback method or via an output stream. |
| 18 | + */ |
| 19 | +class AACDecoderHelix : public CommonHelix { |
| 20 | + public: |
| 21 | + AACDecoderHelix() { |
| 22 | + } |
| 23 | + |
| 24 | + AACDecoderHelix(Print &output, AACInfoCallback infoCallback=nullptr){ |
| 25 | + this->out = &output; |
| 26 | + this->infoCallback = infoCallback; |
| 27 | + } |
| 28 | + |
| 29 | + AACDecoderHelix(AACDataCallback dataCallback){ |
| 30 | + this->pwmCallback = dataCallback; |
| 31 | + } |
| 32 | + void setInfoCallback(AACInfoCallback cb){ |
| 33 | + this->infoCallback = cb; |
| 34 | + } |
| 35 | + |
| 36 | + void setDataCallback(AACDataCallback cb){ |
| 37 | + this->pwmCallback = cb; |
| 38 | + } |
| 39 | + |
| 40 | + void setOutput(Print &output){ |
| 41 | + this->out = &output; |
| 42 | + } |
| 43 | + |
| 44 | + /// Starts the processing |
| 45 | + void begin(){ |
| 46 | + LOG(Debug, "begin"); |
| 47 | + CommonHelix::begin(); |
| 48 | + decoder = AACInitDecoder(); |
| 49 | + if (decoder==nullptr){ |
| 50 | + LOG(Error, "MP3InitDecoder has failed"); |
| 51 | + active = false; |
| 52 | + return; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Releases the reserved memory |
| 57 | + void end(){ |
| 58 | + LOG(Debug, "end"); |
| 59 | + AACFreeDecoder(decoder); |
| 60 | + CommonHelix::end(); |
| 61 | + } |
| 62 | + |
| 63 | + /// Provides the last available _AACFrameInfo_t |
| 64 | + _AACFrameInfo audioInfo(){ |
| 65 | + return aacFrameInfo; |
| 66 | + } |
| 67 | + |
| 68 | + protected: |
| 69 | + HAACDecoder decoder = nullptr; |
| 70 | + AACDataCallback pwmCallback = nullptr; |
| 71 | + AACInfoCallback infoCallback = nullptr; |
| 72 | + _AACFrameInfo aacFrameInfo; |
| 73 | + |
| 74 | + size_t maxFrameSize(){ |
| 75 | + return max_frame_size == 0 ? AAC_MAX_FRAME_SIZE : max_frame_size; |
| 76 | + } |
| 77 | + |
| 78 | + size_t maxPWMSize() { |
| 79 | + return max_pwm_size == 0 ? AAC_MAX_OUTPUT_SIZE : max_pwm_size; |
| 80 | + } |
| 81 | + |
| 82 | + int findSynchWord(int offset=0) { |
| 83 | + int result = AACFindSyncWord(frame_buffer+offset, buffer_size)+offset; |
| 84 | + return result < 0 ? result : result + offset; |
| 85 | + } |
| 86 | + |
| 87 | + /// decods the data and removes the decoded frame from the buffer |
| 88 | + void decode(Range r) { |
| 89 | + LOG(Debug, "decode %d", r.end); |
| 90 | + int len = r.end; |
| 91 | + int bytesLeft = r.end; //r.end; //r.end; // buffer_size |
| 92 | + uint8_t* ptr = frame_buffer + r.start; |
| 93 | + |
| 94 | + int result = AACDecode(decoder, &ptr, &bytesLeft, pwm_buffer); |
| 95 | + int decoded = len - bytesLeft; |
| 96 | + assert(decoded == ptr-(frame_buffer + r.start)); |
| 97 | + if (result==0){ |
| 98 | + LOG(Debug, "-> bytesLeft %d -> %d = %d ", buffer_size, bytesLeft, decoded); |
| 99 | + LOG(Debug, "-> End of frame (%d) vs end of decoding (%d)", r.end, decoded) |
| 100 | + |
| 101 | + // return the decoded result |
| 102 | + _AACFrameInfo info; |
| 103 | + AACGetLastFrameInfo(decoder, &info); |
| 104 | + provideResult(info); |
| 105 | + |
| 106 | + // remove processed data from buffer |
| 107 | + buffer_size -= decoded; |
| 108 | + assert(buffer_size<=maxFrameSize()); |
| 109 | + memmove(frame_buffer, frame_buffer+r.start+decoded, buffer_size); |
| 110 | + LOG(Debug, " -> decoded %d bytes - remaining buffer_size: %d", decoded, buffer_size); |
| 111 | + } else { |
| 112 | + // in the case of error we remove the frame |
| 113 | + LOG(Debug, " -> decode error: %d - removing frame!", result); |
| 114 | + int ignore = decoded; |
| 115 | + if (ignore <= 0) ignore = r.end; |
| 116 | + // We advance to the next synch world |
| 117 | + buffer_size -= ignore; |
| 118 | + assert(buffer_size<=maxFrameSize()); |
| 119 | + memmove(frame_buffer, frame_buffer+ignore, buffer_size); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // return the result PWM data |
| 124 | + void provideResult(_AACFrameInfo &info){ |
| 125 | + LOG(Debug, "provideResult: %d samples",info.outputSamps); |
| 126 | + if (info.outputSamps>0){ |
| 127 | + // provide result |
| 128 | + if(pwmCallback!=nullptr){ |
| 129 | + // output via callback |
| 130 | + pwmCallback(info, pwm_buffer,info.outputSamps); |
| 131 | + } else { |
| 132 | + // output to stream |
| 133 | + if (info.sampRateOut!=aacFrameInfo.sampRateOut && infoCallback!=nullptr){ |
| 134 | + infoCallback(info); |
| 135 | + } |
| 136 | + out->write((uint8_t*)pwm_buffer, info.outputSamps); |
| 137 | + } |
| 138 | + aacFrameInfo = info; |
| 139 | + } |
| 140 | + } |
| 141 | +}; |
| 142 | +} |
0 commit comments