This application note describes a simple and easy-to-use solution to help design and
develop products with an audio output, for example, toys, white goods, washing machines,
and alarms.
Audio data generally require the storage of a lot of memory. However, the memory space
needed to preserve such data can be reduced by compressing the audio data via a
specified compression method. Processing these data is a complex task requiring powerful
microcontrollers such as DSP or audio chip. Usually, 8-bit microcontrolles do not offer either
a large memory or a high computing performance.
The software solution offered in this application note reconstructs audio signals from
compressed samples. A simple audio codec based on an adaptive differential pulse coded
modulation (ADPCM) algorithm is used and advantage is taken of the advanced STM8S
core with its 3-staged pipeline and 16 or 24 MIPS peak performance.
The digital samples are usually converted to an analog signal by some external or onchip
digital-to-analog converter (DAC). There are a lot of applications where the high fidelity
audio output is not a priority and/or the use of an external DAC is not a suitable costeffective solution for 8-bit microcontrollers. To reduce costs and to offer a good quality audio,
an ideal solution is to generate an audio signal using pulse width modulation (PWM).
The example firmware and application hardware design are provided with this application
note to enable easy porting of the offered solution to the final application.
ADPCM is a fixed length codeword audio codec which reduces redundant information from
audio waveforms. The information core is separated from the correlated waveform samples
by encoding differences between the current samples and the predicted ones. As the
correlation between consecutive audio samples is generally high, this method is reasonably
effective and preserves good audio quality. The ADPCM codec, which is based on coding
waveforms in time domains, is much less complex than codecs based on voice perception
(vocoders) that operate in frequency domains. For these reasons, the ADPCM codec is the
best choice for the audio output on 8-bit microcontrollers.
The ADPCM algorithm has been developed for speech coding. It is implemented in several
steps and is used in telephone systems like ITU-T G.726 (covering CCITT G.721 and G.723
standards). G.726 and similar telecommunication codecs, offer several advanced features
which are necessary for successful transmission and streaming. Such features include
synchronous coding adjustment, tone detection to carry data modem signals, adaptated
speed control, and recovery from transmission error conditions. These features lead to
additional algorithm complexity which either requires the use of a digital signal processor
(DSP) or lowering the encoding/decoding sample rate. Fortunately, such features do not
bring many additional benefits to the microcontroller for coding/decoding the audio or
speech signals. Less complex alternatives can be found.
1.2 Interactive multimedia association (IMA) ADPCM
The IMA
different computers platforms, for example in Microsoft® Sound Recorder or Apple®
QuickTime®. It was originally offered by Intel/DVI® as an open standard for use by the IMA.
The reference algorithms and recommended formats were initially developed by the digital
audio technical working group (DATWG) and refined by the digital audio focus group (DAFG)
of the IMA. These groups are no longer active.
The IMA DATWG reference algorithm is less complex than the G.726 algorithm. The number
of encoding/decoding CPU cycles needed is reduced by using fixed prediction and by
replacing complex floating point mathematical operations by look-up tables. The
implementation of the ADPCM codec presented in this application note is compatible with
the IMA reference algorithm published by the IMA DATWG/DAFG in the “Recommended
Practices for Enhancing Digital Audio Compatibility in Multimedia Systems”, revision 3.00
(see References).
(a)
ADPCM codec is one such alternative solution. This codec is widely used across
a. IMA is a computer/audio/video industry trade association that has worked to promote multimedia application
development.
Doc ID 17011 Rev 13/13
ADPCM codecAN3143
3
Q
3
D
3
,
3
P
Q
)NVERSEQUANTIZER
0REDICTOR3TEPSIZEADAPTOR
AI
1.3 IMA ADPCM decompression
The IMA ADPCM decompression algorithm compresses samples which are stored as 4-bit
signed two’s complement values. The result is decompressed samples in linear 16-bit two’s
complement format.
The input sample (S
obtain a difference (S
S
. The resulting decompressed linear sample is the sum of Sd and the predicted sample
d
(S
). As Sp is a simple zero order hold function, Sd is simply added to the previous output
p
value. The step size is adapted according to S
) is dequantized using an inverse quantizer with an adapted step (q) to
q
). To reduce quantization errors, one half of the step size is added to
d
. The decoder preconditions are that Sp is
q
cleared and that the quantization step is set to smallest one.
Figure 1.ADPCM decoder
4/13Doc ID 17011 Rev 1
AN3143ADPCM codec
)NVERSEQUANTIZER3TEPSIZEADAPTOR
0REDICTOR
1UANTIZER
qq
3
P
3
IN
3
,
D
3
D
3
Q
AI
1.4 IMA ADPCM compression
The IMA ADPCM compression algorithm encoder calculates a difference (d) between a 16bit input linear sample (S
to a 4-bit compressed output sample (S
step is the same one as that used by the decoder. Figure 2 provides a flowchart of the
ADPCM encoder. It also shows the decoder building blocks with the functionality described
in Section 1.3 (see filled light green components). As the encoder covers identical blocks to
the decoder and produces identical results, there is no need to store prediction information.
The quantized difference is the only core information about the coded samples which needs
to be stored or transmitted.
Figure 2.ADPCM encoder
) and a predicted linear sample (SL). The difference is quantized
in
) by using an adapted quantization step (q). This
q
Doc ID 17011 Rev 15/13
Audio data encoding and storingAN3143
Memorysampleratelength2⁄×=
2 Audio data encoding and storing
The IMA ADPCM codec is quite popular on PC platforms and a wide range of audio
software with different capabilities to process and encode audio data is available. The
problematic part of encoded file storage is the file format of the ADPCM bitstream as it is not
standardized in [1]. On 8-bit microcontrollers, raw data without any formatting is preferred to
obtain the easiest manipulation with the coded audio file.
On the Microsoft Windows® platform, a WAVEform (WAV) audio data container is often
used to store linear PCM data. The WAV can also be used to store IMA ADPCM audio data.
To use the WAV for storing audio bitstream, a WAV parser has to be implemented in the
microcontroller decoding firmware. This is to unpack raw data so that they can be
decompressed. The WAV parser increases the complexity and size of the application and
does not bring many additional benefits to an 8-bit system.
Fortunately, software is available which can store coded audio bitstream directly as raw
data. An example includes the Sound eXchange (SOX) command-line application for audio
manipulation which is distributed under a GNU general purpose license.
SOX is able to:
●Resample input audio data to any target frequency
●Encode such data in IMA ADPCM format
●Save output bitstream as unformatted raw data
The example command below shows the input parameters used to resample data to:
●Target frequency 15625 Hz
●Reduce volume by -12 dB
●Compress data by using the IMA ADPCM codec
sox inputfilename -r 15625 outputfilename.ima gain -12
Various input file formats can be used, including PCM WAV, MP3, MP4, OGG, FLAC, and
many others. See the SOX documentation for further details.
2.1 Internal Flash memory used for audio data storing
The internal memory of STM8S can be used to store short audio waveforms. One 16-bit
PCM sample takes half a byte when compressed by IMA ADPCM codec. The memory
requirement can be evaluated using Equation 1.
Equation 1
For example,
memory.
The audio data can be saved to the internal memory by using “in-application programming”
(IAP) or the bootloader. See the AN2659 (STM8S in-application programming (IAP) using a
customized bootloader) and UM0560 (STM8L/S bootloader) for more details.
a 5-second ADPCM bitstream with an 8-kHz sample rate uses 20 Kbytes of
6/13Doc ID 17011 Rev 1
AN3143Audio data encoding and storing
Another way to store audio data to the internal memory is to link audio data arrays with the
compiled code. To include raw data in a project, a binary representation of the data have to
be converted to ASCII format. Any advanced HEX editor is able to export data in this format
which is then readable by ANSI C compilers. The TxD hexa editor (see References) is an
easy-to-use software, with a free license, no restrictions for commercial use, and which can
be freely distributed.
The processing of audio data, which have to be included in a project, consists of loading
binary data into the editor and exporting them as a C source code.
Figure 3.Hexa editor export to
C source code
The output of this operation is a C source file with one array:
unsigned char rawData[10340] = { ...
This declaration has to be modified, using compiler directives, into an array of constants in
the program memory. This is done for the COSMIC compiler as follows:
const @far unsigned char rawData[10340] = { ...
For the Raisonance compiler, the following directive has to be used:
fcode unsigned char rawData[10340] = { ...
For more details, see the firmware project associated with this application note on
www.st.com/mcu.
Doc ID 17011 Rev 17/13
Audio data encoding and storingAN3143
2.2 External memory used for audio data storing
The micro SD card is used for demonstration purposes in this application note for storing
audio bitstreams. Any other external memory with an interface that is supported by STM8S
can be used instead.
The SD card can be accessed immediately by the operating system if it is formatted by a
supported file system i.e. File Allocation Table (FAT) filing system. If the SD card is FAT
formatted, the microcontroller firmware must also support the FAT file system. Unfortunately,
FAT implementation on an 8-bit microcontroller uses too many resources and the footprint is
too large for a low-cost oriented microcontroller application. Moreover, it is usually not
needed as the audio content of the card is often changed only once. A more convenient way
of putting audio data on the SD card is to use specific software which has an embedded
driver that bypasses the operation and file system drivers, therby allowing direct SD card
access.
A hex editor with such direct access feature can be used for modification of the unformatted
SD card content. One of the available and tested hex editors can be found in Maël Hörz (see
Section 5).
When the compressed ADPCM data is written to the SD card the MCU firmware can directly
read the compressed data samples from the SD card sector by sector. The standard SD
card has fixed sector size of 512 Bytes.
8/13Doc ID 17011 Rev 1
AN3143STM8S audio output hardware overview
STM8S
PWM1
STM8S
PWM1
LP
R
C
2
N
f
HSI
4f
sampling
-----------------------=
3 STM8S audio output hardware overview
The hardware solution benefits from the STM8S 16-bit timer with pulse width modulation
(PWM) output which can operate in center aligned mode. Only a few external components
are needed to build an application with audio output, for example, a reconstruction filter is
needed to reduce the high frequency noise in the PWM output frequency spectrum (see
Figure 4).
Figure 4.Single PWM output
1. LP = Lowpass filter
The reconstruction filter should be set up as a lowpass filter with a cutoff frequency close to half
that of the sampling frequency (f
sampling
). In the current example, the PWM frequency is four
times higher than the sampling frequency. This is to keep an adequate margin in the
frequency band for elimination of high frequency noise commponents by the reconstruction
filter. The higher the band stop attenuation (steepest frequency characteristic transition), the
lower the output noise, and consequently, the higher the resolution.
The achievable resolution (N) of the PWM signal is given by the frequency of the PWM
signal (f
internal oscillator (f
) and the system clock frequency of 16 MHz which is given by the high-speed
PWM
). This relation is expressed by Equation 2.
HSI
Equation 2
By evaluatingEquation 2, where we have a sampling frequency of 15625 Hz and a 16-MHz
system clock, we can only get an 8-bit resolution. Due to the 16-bit output samples, the
lower byte could be lost. This can be audible with higher quality speakers and a good
reconstruction filter.
To avoid such reduction, a second PWM2 output is used to generate a signal corresponding
to the lower byte. This signal is multiplied by an adequate weighting factor (1/2
N
) and
subsequently added to the signal corresponding to the higher byte PWM1. Theoreticaly, this
allows the 16-bit resolution to be reached. This principle is described in Figure 5.
Doc ID 17011 Rev 19/13
STM8S audio output hardware overviewAN3143
STM8S
PWM1
LP
PWM2
LP
2
N
STM8S
PWM1
PWM2
R
1
R
2
C
Figure 5.Dual PWM output
1. LP = Lowpass filter.
The simplest form of the reconstruction filter is the RC lowpass filter. It is present on the
STM8/128-EVAL evaluation board. Details including schematics can be found in the
UM0482.
10/13Doc ID 17011 Rev 1
AN3143Conclusion
4 Conclusion
This application note offers an easy-to-use application solution for the STM8S with good
quality audio/speech output. To adequately reduce memory space which allows an audio
bitstream to be stored, an IMA ADPCM codec is implemented. Short audio clips can be
stored directly in the internal program memory. To store long length audio clips, an external
memory is needed. The SD card was used as an example in this application note.
The STM8S audio output solution was developed with the aim to limit external components.
This solution benefits from the STM8S timer with dual PWM output to reach 16-bit
resolution.
5 References
IMA Digital Audio Focus and Technical Working Group, Recommended Practices for
Enhancing Digital Audio Compatibility in Multimedia Systems, revision 3.00, October 21,
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOT
RECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAINING
APPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,
DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVE
GRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.