arduino-audio-tools
Loading...
Searching...
No Matches
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
WAVDecoder Class Reference

A simple WAVDecoder: We parse the header data on the first record to determine the format. If no AudioDecoderExt is specified we just write the PCM data to the output that is defined by calling setOutput(). You can define a ADPCM decoder to decode WAV files that contain ADPCM data. Since WAV files can use different ADPCM (or other) encodings, you can register more than one decoder with addDecoder(): the decoder matching the format tag found in the WAV header is selected automatically when a file is parsed. More...

#include <CodecWAV.h>

Inheritance diagram for WAVDecoder:
AudioDecoder AudioWriter AudioInfoSource AudioInfoSupport

Classes

struct  DecoderEntry
 Associates a WAV format tag with the decoder responsible for it. More...
 

Public Member Functions

 WAVDecoder ()=default
 Construct a new WAVDecoder object for PCM data.
 
 WAVDecoder (AudioDecoderExt &dec, AudioFormat fmt=AudioFormat::UNKNOWN)
 Construct a new WAVDecoder object for ADPCM data. If fmt is not provided, it is derived from dec.wavFormat() (see setDecoder()).
 
void addDecoder (AudioDecoderExt &dec, AudioFormat fmt=AudioFormat::UNKNOWN)
 
virtual void addNotifyAudioChange (AudioInfoSupport &bi)
 Adds target to be notified about audio changes.
 
AudioInfo audioInfo () override
 Exposed AudioInfo (may reflect conversion flags)
 
WAVAudioInfoaudioInfoEx ()
 Extended WAV specific info (original header values)
 
virtual AudioInfo audioInfoOut ()
 
bool begin () override
 Prepare decoder for a new WAV stream.
 
virtual bool begin (AudioInfo info) override
 
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
 
void end () override
 Finish decoding and release temporary buffers.
 
WAVHeadergetHeader ()
 Access to the internal header parser and info.
 
PrintgetOutput ()
 
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
 
virtual bool isResultPCM ()
 Returns true to indicate that the decoding result is PCM data.
 
const char * mime ()
 Provides MIME type "audio/wav".
 
virtual operator bool () override
 Check if the decoder is active.
 
virtual bool removeNotifyAudioChange (AudioInfoSupport &bi)
 Removes a target in order not to be notified about audio changes.
 
void setAudioInfo (AudioInfo from) override
 for most decoders this is not needed
 
virtual bool setCodecConfig (const uint8_t *data, size_t len)
 Some decoders need e.g. a magic cookie to provide the relevant info for decoding.
 
void setConvert24Bit (bool enable)
 Convert 24 bit (3 byte) to 32 bit (4 byte) PCM data (default: enabled)
 
void setConvert8Bit (bool enable)
 Convert 8 bit to 16 bit PCM data (default: enabled)
 
void setConvertALawMuLaw (bool enable)
 
void setConvertFloatToInt16 (bool enable)
 
void setDecoder (AudioDecoderExt &dec, AudioFormat fmt=AudioFormat::UNKNOWN)
 
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active)
 
virtual void setOutput (AudioOutput &out_stream)
 Defines where the decoded result is written to.
 
virtual void setOutput (AudioStream &out_stream)
 Defines where the decoded result is written to.
 
void setOutput (Print &out_stream) override
 Defines the output Stream.
 
virtual size_t write (const uint8_t *data, size_t len) override
 Write incoming WAV data (header + PCM) into output.
 

Public Attributes

int id
 custom id to be used by application
 

Protected Member Functions

int decodeHeader (uint8_t *in_ptr, size_t in_size)
 Decodes the header data: Returns the start pos of the data.
 
void endCurrentDecoder ()
 
AudioDecoderExtfindDecoder (AudioFormat format)
 Finds the decoder registered for the given format tag, if any.
 
void logSupportedFormats ()
 
void notifyAudioChange (AudioInfo info)
 
Printout ()
 
void setupEncodedAudio ()
 
virtual size_t write_out (const uint8_t *in_ptr, size_t in_size)
 
size_t write_out_24 (const uint8_t *in_ptr, size_t in_size)
 convert 3 byte int24 to 4 byte int32
 
size_t write_out_8to16 (const uint8_t *in_ptr, size_t in_size)
 Convert 8-bit PCM to 16-bit PCM and write out.
 
size_t write_out_alaw (const uint8_t *in_ptr, size_t in_size)
 Expand G.711 A-law 8-bit samples to 16-bit linear PCM and write out.
 
size_t write_out_float_to_int16 (const uint8_t *in_ptr, size_t in_size)
 
size_t write_out_mulaw (const uint8_t *in_ptr, size_t in_size)
 Expand G.711 mu-law 8-bit samples to 16-bit linear PCM and write out.
 
void writeBlocking (Print *out, uint8_t *data, size_t len)
 

Static Protected Member Functions

static int16_t alaw2linear (uint8_t a_val)
 Converts a G.711 A-law encoded byte to a 16-bit linear PCM sample.
 
static bool isNativelySupported (AudioFormat format)
 Formats that are handled internally without an external decoder.
 
static int16_t ulaw2linear (uint8_t u_val)
 Converts a G.711 mu-law encoded byte to a 16-bit linear PCM sample.
 

Protected Attributes

bool active = false
 
const size_t batch_size = 256
 
SingleBuffer< int32_t > buffer24 {0}
 
SingleBuffer< int16_t > buffer_float16 {0}
 
SingleBuffer< uint8_t > byte_buffer {0}
 
bool convert24 = true
 
bool convert8to16 = true
 
bool convertALawMuLaw = true
 
bool convertFloatToInt16 = true
 
EncodedAudioOutput dec_out
 
Vector< DecoderEntrydecoders
 decoders registered via setDecoder()/addDecoder(), keyed by format tag
 
WAVHeader header
 
AudioInfo info
 
bool is_notify_active = true
 
bool isFirst = true
 
bool isValid = true
 
Vector< AudioInfoSupport * > notify_vector
 
AudioDecoderExtp_decoder = nullptr
 
Printp_print = nullptr
 

Detailed Description

A simple WAVDecoder: We parse the header data on the first record to determine the format. If no AudioDecoderExt is specified we just write the PCM data to the output that is defined by calling setOutput(). You can define a ADPCM decoder to decode WAV files that contain ADPCM data. Since WAV files can use different ADPCM (or other) encodings, you can register more than one decoder with addDecoder(): the decoder matching the format tag found in the WAV header is selected automatically when a file is parsed.

Besides plain PCM, the following formats are natively supported without the need to provide an external decoder:

Optionally, if the input WAV file contains 8-bit PCM data, you can enable automatic conversion to 16-bit PCM output by calling setConvert8to16(true). This will convert unsigned 8-bit samples to signed 16-bit samples before writing to the output stream, and the reported bits_per_sample in audioInfo() will be 16 when conversion is active. The same is valid for the 24 bit conversion which converts 24 bit (3 byte) to 32 bit (4 byte).

Please note that you need to call begin() everytime you process a new file to let the decoder know that we start with a new header.

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ WAVDecoder() [1/2]

WAVDecoder ( )
default

Construct a new WAVDecoder object for PCM data.

◆ WAVDecoder() [2/2]

Construct a new WAVDecoder object for ADPCM data. If fmt is not provided, it is derived from dec.wavFormat() (see setDecoder()).

Member Function Documentation

◆ addDecoder()

void addDecoder ( AudioDecoderExt dec,
AudioFormat  fmt = AudioFormat::UNKNOWN 
)
inline

Registers an additional decoder for a specific WAV format tag, on top of any decoder(s) already defined. The decoder used to decode the stream is selected automatically based on the format tag found in the WAV header, so a single WAVDecoder instance can transparently handle files using any of the registered formats (e.g. multiple ADPCM variants). If a decoder is already registered for the same format tag, it is replaced. See setDecoder() for details on the optional fmt parameter.

◆ addNotifyAudioChange()

virtual void addNotifyAudioChange ( AudioInfoSupport bi)
inlinevirtualinherited

◆ alaw2linear()

static int16_t alaw2linear ( uint8_t  a_val)
inlinestaticprotected

Converts a G.711 A-law encoded byte to a 16-bit linear PCM sample.

◆ audioInfo()

AudioInfo audioInfo ( )
inlineoverridevirtual

Exposed AudioInfo (may reflect conversion flags)

Reimplemented from AudioDecoder.

◆ audioInfoEx()

WAVAudioInfo & audioInfoEx ( )
inline

Extended WAV specific info (original header values)

◆ audioInfoOut()

virtual AudioInfo audioInfoOut ( )
inlinevirtualinherited

◆ begin() [1/2]

bool begin ( )
inlineoverridevirtual

Prepare decoder for a new WAV stream.

Reimplemented from AudioDecoder.

◆ begin() [2/2]

virtual bool begin ( AudioInfo  info)
inlineoverridevirtualinherited

Reimplemented from AudioWriter.

Reimplemented in DecoderBasic, and OggContainerDecoder.

◆ clearNotifyAudioChange()

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

Deletes all change notify subscriptions.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ decodeHeader()

int decodeHeader ( uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotected

Decodes the header data: Returns the start pos of the data.

◆ end()

void end ( )
inlineoverridevirtual

Finish decoding and release temporary buffers.

Reimplemented from AudioDecoder.

◆ endCurrentDecoder()

void endCurrentDecoder ( )
inlineprotected

Ends the decoder currently selected for the stream just finished (if any), so it doesn't retain state (e.g. sample_rate/channels) across unrelated streams the next time this format is matched

◆ findDecoder()

AudioDecoderExt * findDecoder ( AudioFormat  format)
inlineprotected

Finds the decoder registered for the given format tag, if any.

◆ getHeader()

WAVHeader & getHeader ( )
inline

Access to the internal header parser and info.

◆ getOutput()

Print * getOutput ( )
inlineinherited

◆ isNativelySupported()

static bool isNativelySupported ( AudioFormat  format)
inlinestaticprotected

Formats that are handled internally without an external decoder.

◆ isNotifyActive()

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ isResultPCM()

virtual bool isResultPCM ( )
inlinevirtualinherited

Returns true to indicate that the decoding result is PCM data.

Reimplemented in CopyDecoder, DecoderNetworkFormat, and ContainerM4A.

◆ logSupportedFormats()

void logSupportedFormats ( )
inlineprotected

Logs (at error level) the WAV format tags this instance can currently decode: the natively handled ones, plus any registered via setDecoder()/addDecoder()

◆ mime()

const char * mime ( )
inlinevirtual

Provides MIME type "audio/wav".

Implements AudioDecoder.

◆ notifyAudioChange()

void notifyAudioChange ( AudioInfo  info)
inlineprotectedinherited

◆ operator bool()

virtual operator bool ( )
inlineoverridevirtual

Check if the decoder is active.

Implements AudioWriter.

◆ out()

Print & out ( )
inlineprotected

◆ removeNotifyAudioChange()

virtual bool removeNotifyAudioChange ( AudioInfoSupport bi)
inlinevirtualinherited

Removes a target in order not to be notified about audio changes.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ setAudioInfo()

void setAudioInfo ( AudioInfo  from)
inlineoverridevirtualinherited

for most decoders this is not needed

Implements AudioWriter.

Reimplemented in DecoderALAC, CodecChain, DSFDecoder, DecoderL8, OpusAudioDecoder, OpusMultiStreamAudioDecoder, CodecNOP, G729Decoder, and G7xxDecoder.

◆ setCodecConfig()

virtual bool setCodecConfig ( const uint8_t *  data,
size_t  len 
)
inlinevirtualinherited

Some decoders need e.g. a magic cookie to provide the relevant info for decoding.

Reimplemented in DecoderALAC, and MultiDecoder.

◆ setConvert24Bit()

void setConvert24Bit ( bool  enable)
inline

Convert 24 bit (3 byte) to 32 bit (4 byte) PCM data (default: enabled)

◆ setConvert8Bit()

void setConvert8Bit ( bool  enable)
inline

Convert 8 bit to 16 bit PCM data (default: enabled)

◆ setConvertALawMuLaw()

void setConvertALawMuLaw ( bool  enable)
inline

Expand ALAW/MULAW 8-bit logarithmic samples to 16-bit linear PCM (default: enabled). If disabled, the raw encoded bytes are passed through unchanged.

◆ setConvertFloatToInt16()

void setConvertFloatToInt16 ( bool  enable)
inline

Convert 32-bit IEEE_FLOAT samples to 16-bit linear PCM (default: enabled). Samples are clamped to [-1.0, 1.0] before scaling to the int16 range. If disabled, the raw 32-bit float bytes are passed through unchanged.

◆ setDecoder()

void setDecoder ( AudioDecoderExt dec,
AudioFormat  fmt = AudioFormat::UNKNOWN 
)
inline

Defines an optional decoder if the format is not PCM. This replaces any previously registered decoders. To support WAV files that may use different ADPCM (or other) formats, register multiple decoders with addDecoder() instead.

fmt is the WAV format tag (see AudioFormat) that dec decodes. It can be omitted if dec can identify its own format via AudioDecoderExt::wavFormat() - e.g. ADPCMDecoder does this for the codec ids that correspond to a well defined WAV format tag (currently MS ADPCM, IMA/DVI ADPCM and Yamaha ADPCM). For codecs that don't map to a single WAV format tag, fmt must be provided explicitly.

◆ setNotifyActive()

void setNotifyActive ( bool  flag)
inlineinherited

Deactivate/Reactivate automatic AudioInfo updates: (default is active)

◆ setOutput() [1/3]

virtual void setOutput ( AudioOutput out_stream)
inlinevirtualinherited

Defines where the decoded result is written to.

Reimplemented in ADTSDecoder, CodecChain, MTSDecoder, MTSDecoderTSDemux, and MetaDataFilterDecoder.

◆ setOutput() [2/3]

virtual void setOutput ( AudioStream out_stream)
inlinevirtualinherited

Defines where the decoded result is written to.

Reimplemented in ADTSDecoder, CodecChain, MTSDecoder, MTSDecoderTSDemux, and MetaDataFilterDecoder.

◆ setOutput() [3/3]

void setOutput ( Print out_stream)
inlineoverridevirtual

Defines the output Stream.

Reimplemented from AudioDecoder.

◆ setupEncodedAudio()

void setupEncodedAudio ( )
inlineprotected

◆ ulaw2linear()

static int16_t ulaw2linear ( uint8_t  u_val)
inlinestaticprotected

Converts a G.711 mu-law encoded byte to a 16-bit linear PCM sample.

◆ write()

virtual size_t write ( const uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Write incoming WAV data (header + PCM) into output.

Implements AudioWriter.

◆ write_out()

virtual size_t write_out ( const uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotectedvirtual

◆ write_out_24()

size_t write_out_24 ( const uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotected

convert 3 byte int24 to 4 byte int32

◆ write_out_8to16()

size_t write_out_8to16 ( const uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotected

Convert 8-bit PCM to 16-bit PCM and write out.

◆ write_out_alaw()

size_t write_out_alaw ( const uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotected

Expand G.711 A-law 8-bit samples to 16-bit linear PCM and write out.

◆ write_out_float_to_int16()

size_t write_out_float_to_int16 ( const uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotected

Convert 32-bit IEEE_FLOAT samples to 16-bit linear PCM and write out. Samples are clamped to [-1.0, 1.0] before scaling to the int16 range.

◆ write_out_mulaw()

size_t write_out_mulaw ( const uint8_t *  in_ptr,
size_t  in_size 
)
inlineprotected

Expand G.711 mu-law 8-bit samples to 16-bit linear PCM and write out.

◆ writeBlocking()

void writeBlocking ( Print out,
uint8_t *  data,
size_t  len 
)
inlineprotectedinherited

Member Data Documentation

◆ active

bool active = false
protected

◆ batch_size

const size_t batch_size = 256
protected

◆ buffer24

SingleBuffer<int32_t> buffer24 {0}
protected

◆ buffer_float16

SingleBuffer<int16_t> buffer_float16 {0}
protected

◆ byte_buffer

SingleBuffer<uint8_t> byte_buffer {0}
protected

◆ convert24

bool convert24 = true
protected

◆ convert8to16

bool convert8to16 = true
protected

◆ convertALawMuLaw

bool convertALawMuLaw = true
protected

◆ convertFloatToInt16

bool convertFloatToInt16 = true
protected

◆ dec_out

EncodedAudioOutput dec_out
protected

◆ decoders

Vector<DecoderEntry> decoders
protected

decoders registered via setDecoder()/addDecoder(), keyed by format tag

◆ header

WAVHeader header
protected

◆ id

int id
inherited

custom id to be used by application

◆ info

AudioInfo info
protectedinherited

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ isFirst

bool isFirst = true
protected

◆ isValid

bool isValid = true
protected

◆ notify_vector

Vector<AudioInfoSupport*> notify_vector
protectedinherited

◆ p_decoder

AudioDecoderExt* p_decoder = nullptr
protected

decoder matching the format of the WAV file currently being processed (selected in decodeHeader() from the registered decoders)

◆ p_print

Print* p_print = nullptr
protectedinherited

The documentation for this class was generated from the following file: