SlideShare a Scribd company logo
Real-time Audio on Embedded Linux


                                              April, 2011

                                           Remi Lorriaux
                                       Adeneo Embedded

                               Embedded Linux Conference 2011




Remi Lorriaux - ELC 2011   1
Agenda

●   Introduction to audio latency
●   Choosing a board for our project
●   Audio using a vanilla kernel
●   Audio using a real-time kernel
●   JACK on our embedded device
●   Conclusion

Remi Lorriaux - ELC 2011   2
Who am I?

●   Software engineer at Adeneo Embedded
    (Bellevue, WA)
     ●   Linux, Android
     ●   Main activities:
          –   Firmware development (BSP adaptation, driver
              development, system integration)
          –   Training delivery


●   Guitar player (at home)

Remi Lorriaux - ELC 2011            3
Context and objectives
●   Case study
     ●   Using a generic embedded device, see how we can
         create a Linux system
     ●   Does not necessarily apply to all types of devices
     ●   Not an in-depth study of real-time systems


●   Focus on software integration
     ●   Not an exhaustive review of all software solutions
     ●   We want to use open source components


Remi Lorriaux - ELC 2011        4
A typical audio chain


                           ADC




                                             DAC
                                 CPU / DSP




●   Digital processing made with a DSP or a
    general-purpose CPU
●   CPUs used in embedded devices generally
    have sufficient power to do the audio
    processing in software

Remi Lorriaux - ELC 2011            5
Typical audio chain on Linux
                 Application



     Server (e.g. JACK, PulseAudio)                                    Application



                                       alsa-lib
                                                read(), write(), poll(), select(), mmap()...


                                 ALSA kernel driver
              Capture buffer                                          Output buffer
                                      INT   DMA

                                      Hardware



Remi Lorriaux - ELC 2011                    6
Audio Latency

●   Delay between an action and its effect, e.g.
     ●   Pressing a key → Hearing a note
     ●   Record input → Processed output
●   Causes
     ●   Physical: 3ft away from loudspeakers ~ 3ms latency
     ●   Hardware: conversion can cause delays
         (magnitude: 40/Fs)
     ●   Software: various levels of buffering


Remi Lorriaux - ELC 2011        7
Audio Latency


●   Consequences on applications:
     ●   Music: critical issue
     ●   Communications: larger latencies can be tolerated
         but still have to be limited (Android specifies
         continuous output latency < 45 ms, continuous
         input latency < 50 ms)
     ●   Audio/Video playback: larger latencies can be
         tolerated as long as synchronization is maintained



Remi Lorriaux - ELC 2011         8
Measuring audio latency
●   Measuring total latency (including hardware):
     ●   Use an oscilloscope or a soundcard
     ●   Can measure the difference between 2
         measurements to assess software changes

●   Measuring software latency
     ●   ALSA: use latency test in alsa-lib (can automatically
         figure out the optimal latency through trial and error)
     ●   JACK: jdelay, qjacklam


Remi Lorriaux - ELC 2011          9
Buffering
 ●   ALSA uses periods: blocks of audio samples
 ●   Size and number is configurable on runtime by applications
 ●   At the end of each period, an interrupt is generated.
     → Shorter periods: more interrupts
     → Shorter periods + less periods: lower latency


          p1                p2              p3               p4

                                                                   INT




Application fills output buffer                     Samples sent to device


 Remi Lorriaux - ELC 2011            10
Reducing audio latency
●   Buffering creates latency
    → Solution: Reduce buffering

●   Reducing buffer size
    → Interrupts are generated more frequently
    → The system has less time to process the
    buffers: risk of over/underrun
    → The system must be designed to schedule
    the different tasks on time

Remi Lorriaux - ELC 2011   11
Tuning the latency


●   alsa-lib provides “latency”
     ●   Test tool used for measuring latency between
         capture and playback
     ●   Audio latency is measured from driver (difference
         when playback and capture was started)
     ●   Can choose a real-time priority (we changed the
         code to make it configurable at runtime)




Remi Lorriaux - ELC 2011       12
Tuning the latency
●   Trial and error:
     ●   Start with a low latency value
     ●   Load the system (CPU, IO stress, your
         application...)
     ●   If XRUN or bad sound quality → Increase the
         latency (periods number and size)
●   “latency” can do it for you automatically:
         > latency ­m [minimum latency in 
         frames] ­M [maximum latency in frames] 
         ­r [sample rate] ­S [priority] ­p


Remi Lorriaux - ELC 2011        13
Loading the system
●   Ideally:
     ●   Realistic load (i.e. your end applications)
     ●   Worst-case scenario

●   Our experiment:
     ●   Stress tests from the Linux Test Projet (LTP)
          –   CPU stress
          –   Memory allocation
     ●   Generate IO interrupts copying from block devices
     ●   cyclictest


Remi Lorriaux - ELC 2011          14
Choosing a board for our project
●   Deciding factors:
     ●   Pick a board that is supported in the latest mainline
         kernel with real-time patches (2.6.33)
     ●   Tested real-time features (Open Source Automation
         Development Lab)
     ●   Pick a board that was available at the time
     ●   Power and features did not matter (except for audio)


●   Other boards could have been chosen (e.g.
    BeagleBoard)


Remi Lorriaux - ELC 2011         15
Our project: Hardware
●   Zoom™ AM3517 EVM Development Kit (AM3517 EVM)
●   ARM™ Cortex™-A8 600 MHz
    → Powerful, but real-time issues still have to be taken into account
●   Ethernet, USB, SD, NAND, Wireless, CAN, LCD, Video capture...
    → Many ways to generate software interrupts
●   Audio in/out (obviously!)




Remi Lorriaux - ELC 2011                16
Our project: Software
●   Bootloader
     ●   U-Boot (Built by Arago)
●   Kernel
     ●   2.6.33
     ●   Patched to 2.6.33.7
     ●   Patched to 2.6.33.7.2-rt30 (RT version)
●   Root filesystem
     ●   Built by Arago
     ●   Customized recipe (arago-console-image.bb + JACK)
     ●   Test applications: alsa-lib tests, LTP, rt-tests

Remi Lorriaux - ELC 2011           17
Our project: Software challenges
●   Support for the AM3517 EVM in mainline 2.6.33
    is somewhat minimal (this has changed a lot)

●   Ported some of the board code/drivers from TI's
    PSP (2.6.32): audio, SD/MMC

●   Still, not many drivers are supported on our test
    system (not exactly a real-life example)


Remi Lorriaux - ELC 2011   18
Experiment #1: Vanilla kernel


●   Vanilla kernel: no RT patches
●   Kernel configuration (see files/kernel/am3517_std_defconfig)
         CONFIG_PREEMPT_DESKTOP=y
         CONFIG_PREEMPT=y
    (other preemption schemes not investigated)




Remi Lorriaux - ELC 2011         19
Experiment #1: First observation
●   The priority of latency has to be set to real-time
    otherwise, it over/underruns for low latency values
    (under 1024 samples @ 44.1 kHz)
●   Even using a non-RT kernel, once the application
    priority is increased, it runs fine when competing with
    other “stress” processes (can be different in the real
    world)
●   cyclictest also exhibits this behavior
    → Use sched_setscheduler() or chrt and set your
    audio thread priority to SCHED_RR or
    SCHED_FIFO


Remi Lorriaux - ELC 2011      20
Experiment #1: CPU usage vs latency
●   Measured with no load:
         > ./latency -m 64 -M 64 -r 44100 -p -S 60
    → Low-latencies can generate a large CPU load!
    Latency (samples)      Latency (@ 44.1kHz)   CPU Load (%)

    64                     1.5 ms                65%

    128                    2.90 ms               3%

    1024                   23 ms                 < 1%

    8192                   185 ms                < 1%




Remi Lorriaux - ELC 2011             21
Experiment #1: Loading the system
●   Creating CPU load:
     ●   LTP CPU stress test
         → Competition between processes (preemptible)
●   Creating IRQ pressure:
     ●   Transfers from SD Card

●   Result: Works fine with 1.5ms latencies
    → Vanilla + SCHED_FIFO did the trick


Remi Lorriaux - ELC 2011       22
Why use a real-time kernel?
●   Tuning the application priority and audio buffering
    requirements can be sufficient for “soft real-time”
    audio systems – where occasional errors do not
    cause unacceptable audio quality consequences
    BUT:
●   There is still a lot of non-preemptible code in the
    vanilla kernel
    ●   Interrupt handlers, tasklets...
    ●   Regions protected by spinlocks (depending on the number
        of processors on your system)



Remi Lorriaux - ELC 2011            23
CONFIG PREEMPT RT Patch

●   Using the CONFIG PREEMPT RT Patch gives
    you:
     ●   Almost full-preemption
     ●   Threaded interrupts: ability to prioritize interrupts
         like processes → your application can have a
         higher priority than hardware interrupts
         → Lower risk of over/underruns




Remi Lorriaux - ELC 2011          24
Making the system real-time
●   Apply the CONFIG PREEMPT RT Patch on
    vanilla kernels.
     ●   Latest available version: patch-2.6.33.7.2-rt30
     ●   Also set the configuration
●   Difficult task on non-mainline/recent kernels
     ●   Making non-mainline boards compatible can be
         difficult (check locks and interrupt management in
         drivers and platform code, make sure that the
         timers are appropriate)
     ●   Cannot apply the patch on proprietary drivers


Remi Lorriaux - ELC 2011        25
Checking the real-time behavior

●   Check that the patch has been correctly applied:
    ●   Running “ps” on the device will show that IRQs are
        threaded
    ●   Use cyclictest to check the scheduling latency under load
        → The non-RT kernel fails right away under high IRQ
        pressure
        → Latencies remain bounded with RT
    ●   Use ftrace to see advanced information (see kernel
        documentation for usage) – changes the timing behavior of
        the system!




Remi Lorriaux - ELC 2011          26
Tuning the real-time system
●   Set the priority of IRQs and processes
    (decreasing priority):
     ●   (High-resolution) Timers (especially since we are
         using a tickless system!)
     ●   Audio (e.g. DMA on our platform)
     ●   Your audio process
     ●   Other interrupts
     ●   Other processes

     ●   More information (FFADO project)


Remi Lorriaux - ELC 2011        27
Tuning audio parameters (RT edition)
●   Same process as the non-RT experiment: trial and error
●   Use the results provided by cyclictest to adjust the latency, e.g.
●   Does not solve the problem of using shared hardware resources
    (e.g. DMA, busses...)
    → Requires careful platform and driver design

root@am3517-evm:~# cyclictest -t5 -n -p 60
policy: fifo: loadavg: 10.89 9.99 6.83 12/76 1342
T: 0 ( 1338) P:60 I:1000 C: 42699 Min:      21 Act:   36 Avg:   37 Max:     96
T: 1 ( 1339) P:59 I:1500 C: 28463 Min:      25 Act:   41 Avg:   37 Max:    135
T: 2 ( 1340) P:58 I:2000 C: 21344 Min:      25 Act:   37 Avg:   39 Max:    111
T: 3 ( 1341) 41 Avg:   39 Max:     111    22 Act:   54 Avg:   38 Max:     91
T: 3 ( 1341) P:57 I:2500 C: 17140 Min:      22 Act:   41 Avg:   38 Max:     91
T: 4 ( 1342) P:56 I:3000 C: 14283 Min:      25 Act:   27 Avg:   38 Max:     86




Remi Lorriaux - ELC 2011                   28
Experiment #2: RT Audio

●   Dirty trick: Added udelay(1000) in the USB
    interrupt handler!
    → Simulate IRQ pressure since the kernel for
    our board did not support ethernet, display...
    → Not a perfect example

●   Result: Works flawlessly with 1ms latency



Remi Lorriaux - ELC 2011   29
Improving the experiment


●   Run typical applications (UI, gstreamer,
    LADSPA...)

●   More stress on the interrupts: Network, Display,
    Video Capture...




Remi Lorriaux - ELC 2011   30
High-end audio applications: JACK
●   System for handling real-time, low latency audio (and MIDI)
●   Cross-platform: GNU/Linux, Solaris, FreeBSD, OS X and
    Windows
●   Server/client model
●   Connectivity:
    ●   Different applications can access an audio device
    ●   Audio applications can share data between each other
    ●   Support for distributing audio processing across a network, both
        fast & reliable LANs as well as slower, less reliable WANs.
●   Designed for professional audio work



Remi Lorriaux - ELC 2011              31
JACK: latency and real-time
●   JACK does not add latency

●   An RT kernel is needed only if:
    ●   You want to run JACK with very low latency settings that
        require real-time performance that can only be achieved
        with an RT kernel
    ●   Your hardware configuration triggers poor latency
        behaviour which might be improved with an RT kernel
●   Most users do not need an RT kernel in order to use
    JACK, and most will be happy using settings that are
    effective without an RT kernel


Remi Lorriaux - ELC 2011         32
JACK on our platform

●   Built using OpenEmbedded (added the recipe
    to our image)
●   Used straight out of the box

●   Set priorities:
     ●   Make jackd have SCHED_FIFO priority (like your
         audio application seen before)
     ●   More information: FFADO wiki



Remi Lorriaux - ELC 2011      33
Experiment #3: JACK on our platform
root@am3517­evm:~#jackd ­R ­P 60 ­d alsa ­i 2 ­r 44100 ­o 2 ­ ­p 64 ­n 6 &
root@am3517­evm:~# jackd 0.118.0                                                
Copyright 2001­2009 Paul Davis, Stephane Letz, Jack O'Quinn, Torben Hohn and ot.
jackd comes with ABSOLUTELY NO WARRANTY                                         
This is free software, and you are welcome to redistribute it                   
under certain conditions; see the file COPYING for details                      
                                                                                
JACK compiled with System V SHM support.                                        
loading driver ..                                                               
apparent rate = 44100                                                           
creating alsa driver ... hw:0|hw:0|64|3|44100|2|2|nomon|swmeter|­|32bit         
control device hw:0                                                             
configuring for 44100Hz, period = 64 frames (1.5 ms), buffer = 6 periods        
ALSA: final selected sample format for capture: 16bit little­endian             
ALSA: use 3 periods for capture                                                 
ALSA: final selected sample format for playback: 16bit little­endian            
ALSA: use 3 periods for playback                                                
                                                                                
root@am3517­evm:~# jack_simple_client                                           
engine sample rate: 44100  




Remi Lorriaux - ELC 2011                       34
Experiment #3: JACK on our platform


●   JACK works fine on our platform with
    < 10 ms latency


    (Note: simplest possible test, so this is a best-
    case value)




Remi Lorriaux - ELC 2011   35
Using multi-core systems
●   Embedded systems can have multi-core architectures:
    ●   Several CPUs
    ●   Mixed CPU/DSP
●   The audio processing can be assigned to a particular
    core
●   Example: TI Audio SoC example (Mixed DSP and ARM
    core)

●   Shared hardware resources is still important (bus
    contention, DMA access...)



Remi Lorriaux - ELC 2011      36
Conclusion
●   Trade-offs:
     ●   CPU/power consumption
     ●   Latency
     ●   Design complexity


●   Tune your priorities/audio parameters first and
    load your system
     ●   Procedure similar to desktop environments (well
         documented)


Remi Lorriaux - ELC 2011       37
Conclusion


●   Adding real-time support:
     ●   Not necessarily trivial or required
     ●   Depends on the implementation
     ●   Non-RT kernel can be surprisingly adequate for
         “soft real-time” audio




Remi Lorriaux - ELC 2011         38
Questions?




Remi Lorriaux - ELC 2011   39
Appendix: Files
●   The files used for this experiment should be
    attached with the presentation
●   Just run or have a look at the different scripts
●   Run (in order):
     ●   oe_prepare.sh: installs prerequisites for
         Arago/OpenEmbedded (some other changes might
         be needed)
     ●   oe_download.sh: downloads and installs custom
         recipes for OE
     ●   oe_build.sh: builds OE


Remi Lorriaux - ELC 2011          40
Appendix: Files
●   Run (continued):
     ●   uboot_build.sh: builds U-Boot (sources pulled from
         OE)
     ●   kernel_download.sh: downloads the kernel sources
         and applies relevant patches for our experiments
     ●   kernel_build.sh: builds the RT and non-RT kernel
     ●   apps_build.sh: builds extra test applications out of
         the OE tree
     ●   sd_flash.sh: flashes the bootloader + kernel + rootfs
         on an SD Card


Remi Lorriaux - ELC 2011        41
Appendix: References
●   Real-Time Linux wiki: Lots of information about
    the RT patch and testing procedures
●   The JACK Audio Connection Kit: General
    presentation. Also covers audio topics on Linux
●   FFADO wiki: How to tune audio parameters
●   ALSA wiki: General documentation and ALSA
    samples
●   JACK: Developer documentation, tuning...
●   AM3517 EVM: Board specification and tools


Remi Lorriaux - ELC 2011   42
Audio in linux embedded

More Related Content

What's hot (20)

PPTX
Rtos by shibu
Shibu Krishnan
 
PDF
Intro to Embedded OS, RTOS and Communication Protocols
Emertxe Information Technologies Pvt Ltd
 
PPTX
Linux device drivers
Abhishek Sagar
 
PPTX
QEMU - Binary Translation
Jiann-Fuh Liaw
 
PDF
The Linux Kernel Scheduler (For Beginners) - SFO17-421
Linaro
 
PDF
ARM Trusted FirmwareのBL31を単体で使う!
Mr. Vengineer
 
PDF
Making Linux do Hard Real-time
National Cheng Kung University
 
PDF
LAS16-TR06: Remoteproc & rpmsg development
Linaro
 
PDF
DPDKによる高速コンテナネットワーキング
Tomoya Hibi
 
PPT
RTOS
Ramasubbu .P
 
PDF
Trusted firmware deep_dive_v1.0_
Linaro
 
PDF
Intel dpdk Tutorial
Saifuddin Kaijar
 
PDF
HKG18-402 - Build secure key management services in OP-TEE
Linaro
 
PDF
Video Drivers
Anil Kumar Pugalia
 
PDF
Linux Internals - Interview essentials - 1.0
Emertxe Information Technologies Pvt Ltd
 
PPTX
Introduction to DPDK
Kernel TLV
 
PPTX
What is Bootloader???
Dinesh Damodar
 
ODP
4. linux file systems
Marian Marinov
 
PDF
The overview of lazypull with containerd Remote Snapshotter & Stargz Snapshotter
Kohei Tokunaga
 
DOC
Computer hardware and simulation lab manual
Vivek Kumar Sinha
 
Rtos by shibu
Shibu Krishnan
 
Intro to Embedded OS, RTOS and Communication Protocols
Emertxe Information Technologies Pvt Ltd
 
Linux device drivers
Abhishek Sagar
 
QEMU - Binary Translation
Jiann-Fuh Liaw
 
The Linux Kernel Scheduler (For Beginners) - SFO17-421
Linaro
 
ARM Trusted FirmwareのBL31を単体で使う!
Mr. Vengineer
 
Making Linux do Hard Real-time
National Cheng Kung University
 
LAS16-TR06: Remoteproc & rpmsg development
Linaro
 
DPDKによる高速コンテナネットワーキング
Tomoya Hibi
 
Trusted firmware deep_dive_v1.0_
Linaro
 
Intel dpdk Tutorial
Saifuddin Kaijar
 
HKG18-402 - Build secure key management services in OP-TEE
Linaro
 
Video Drivers
Anil Kumar Pugalia
 
Linux Internals - Interview essentials - 1.0
Emertxe Information Technologies Pvt Ltd
 
Introduction to DPDK
Kernel TLV
 
What is Bootloader???
Dinesh Damodar
 
4. linux file systems
Marian Marinov
 
The overview of lazypull with containerd Remote Snapshotter & Stargz Snapshotter
Kohei Tokunaga
 
Computer hardware and simulation lab manual
Vivek Kumar Sinha
 

Viewers also liked (20)

ODP
Embedded Linux Multimedia
Caglar Dursun
 
PPTX
오픈세미나 플러그인만들기(한번더)
승훈 오
 
PPT
學生學習歷程簡報
Fang-fang
 
DOCX
Kpi publikovani 2
Eva Gildainová
 
PDF
r3-4-2009f_xts5000_new
Bob Imbrigiotta, PMP
 
PPT
Tg discussion guide90
lschmidt1170
 
PDF
CV_Jeeshan CPE
Zeeshan UMer
 
DOC
CV_SANJAY SOHIL
Sanjay Sohil
 
PDF
Geog 5 fa 2012 schmidt fri
lschmidt1170
 
PPT
Grouping objects
Ludmila Kalinichenko
 
PPT
Brain maturation
Ludmila Kalinichenko
 
PPT
Pi3 Ef 7 Pe Revision Class
Ana Menezes
 
PDF
Tutorial para crear cuenta en slideshare
Annie Nitzschke Peña
 
PPTX
Rreflexion cuento
Jesus Birriel
 
PPTX
Heap and Partners
Hardide Coatings
 
PDF
Isabelle: Not Only a Proof Assistant
Achim D. Brucker
 
PPT
Device driver
Yongjae Choi
 
PPTX
UDPSRC GStreamer Plugin Session VIII
NEEVEE Technologies
 
PDF
2014 April 17- Fox Valley Program
Qbera (A Creditexchange Product)
 
PDF
Java Thread
艾鍗科技
 
Embedded Linux Multimedia
Caglar Dursun
 
오픈세미나 플러그인만들기(한번더)
승훈 오
 
學生學習歷程簡報
Fang-fang
 
Kpi publikovani 2
Eva Gildainová
 
r3-4-2009f_xts5000_new
Bob Imbrigiotta, PMP
 
Tg discussion guide90
lschmidt1170
 
CV_Jeeshan CPE
Zeeshan UMer
 
CV_SANJAY SOHIL
Sanjay Sohil
 
Geog 5 fa 2012 schmidt fri
lschmidt1170
 
Grouping objects
Ludmila Kalinichenko
 
Brain maturation
Ludmila Kalinichenko
 
Pi3 Ef 7 Pe Revision Class
Ana Menezes
 
Tutorial para crear cuenta en slideshare
Annie Nitzschke Peña
 
Rreflexion cuento
Jesus Birriel
 
Heap and Partners
Hardide Coatings
 
Isabelle: Not Only a Proof Assistant
Achim D. Brucker
 
Device driver
Yongjae Choi
 
UDPSRC GStreamer Plugin Session VIII
NEEVEE Technologies
 
2014 April 17- Fox Valley Program
Qbera (A Creditexchange Product)
 
Java Thread
艾鍗科技
 
Ad

Similar to Audio in linux embedded (20)

PPTX
Ec8791 unit 5 processes and operating systems
RajalakshmiSermadurai
 
PDF
Tommaso Cucinotta - Low-latency and power-efficient audio applications on Linux
linuxlab_conf
 
PPTX
EC8791-U5-PPT.pptx
Pavithra525349
 
PPTX
Developing Real-Time Systems on Application Processors
Toradex
 
PDF
Lac2006 Lee Revell
rlrevell
 
PPTX
Embedded os
chian417
 
PPT
Rtos
Ruchi Sharma
 
PPTX
Real time Linux
navid ashrafi
 
PDF
Mastering Real-time Linux
Jean-François Deverge
 
PPTX
Fixed-point Multi-Core DSP Platform
Sundance Multiprocessor Technology Ltd.
 
PPT
Rtos
seenakumari
 
PPT
Rtos 2
SahibZada Ibm
 
PPT
ghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjjjjjjjjjjjjjjj
adugnanegero
 
PDF
Enea Enabling Real-Time in Linux Whitepaper
Enea Software AB
 
PPT
Rtos
Naveen N
 
PPTX
EC8791-Embedded and Real Time Systems UNITS NOTES (1).pptx
SyedZ6
 
PPTX
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
realme6igamerr
 
PPTX
Seminario utovrm
Dario Pennisi
 
PPT
DSP/Bios
Weifu Yu
 
PPTX
EC 308 Embedded Systems Module 1 Notes APJKTU
Agi George
 
Ec8791 unit 5 processes and operating systems
RajalakshmiSermadurai
 
Tommaso Cucinotta - Low-latency and power-efficient audio applications on Linux
linuxlab_conf
 
EC8791-U5-PPT.pptx
Pavithra525349
 
Developing Real-Time Systems on Application Processors
Toradex
 
Lac2006 Lee Revell
rlrevell
 
Embedded os
chian417
 
Real time Linux
navid ashrafi
 
Mastering Real-time Linux
Jean-François Deverge
 
Fixed-point Multi-Core DSP Platform
Sundance Multiprocessor Technology Ltd.
 
ghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhjjjjjjjjjjjjjjj
adugnanegero
 
Enea Enabling Real-Time in Linux Whitepaper
Enea Software AB
 
Rtos
Naveen N
 
EC8791-Embedded and Real Time Systems UNITS NOTES (1).pptx
SyedZ6
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
realme6igamerr
 
Seminario utovrm
Dario Pennisi
 
DSP/Bios
Weifu Yu
 
EC 308 Embedded Systems Module 1 Notes APJKTU
Agi George
 
Ad

Recently uploaded (20)

PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Biography of Daniel Podor.pdf
Daniel Podor
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 

Audio in linux embedded

  • 1. Real-time Audio on Embedded Linux April, 2011 Remi Lorriaux Adeneo Embedded Embedded Linux Conference 2011 Remi Lorriaux - ELC 2011 1
  • 2. Agenda ● Introduction to audio latency ● Choosing a board for our project ● Audio using a vanilla kernel ● Audio using a real-time kernel ● JACK on our embedded device ● Conclusion Remi Lorriaux - ELC 2011 2
  • 3. Who am I? ● Software engineer at Adeneo Embedded (Bellevue, WA) ● Linux, Android ● Main activities: – Firmware development (BSP adaptation, driver development, system integration) – Training delivery ● Guitar player (at home) Remi Lorriaux - ELC 2011 3
  • 4. Context and objectives ● Case study ● Using a generic embedded device, see how we can create a Linux system ● Does not necessarily apply to all types of devices ● Not an in-depth study of real-time systems ● Focus on software integration ● Not an exhaustive review of all software solutions ● We want to use open source components Remi Lorriaux - ELC 2011 4
  • 5. A typical audio chain ADC DAC CPU / DSP ● Digital processing made with a DSP or a general-purpose CPU ● CPUs used in embedded devices generally have sufficient power to do the audio processing in software Remi Lorriaux - ELC 2011 5
  • 6. Typical audio chain on Linux Application Server (e.g. JACK, PulseAudio) Application alsa-lib read(), write(), poll(), select(), mmap()... ALSA kernel driver Capture buffer Output buffer INT DMA Hardware Remi Lorriaux - ELC 2011 6
  • 7. Audio Latency ● Delay between an action and its effect, e.g. ● Pressing a key → Hearing a note ● Record input → Processed output ● Causes ● Physical: 3ft away from loudspeakers ~ 3ms latency ● Hardware: conversion can cause delays (magnitude: 40/Fs) ● Software: various levels of buffering Remi Lorriaux - ELC 2011 7
  • 8. Audio Latency ● Consequences on applications: ● Music: critical issue ● Communications: larger latencies can be tolerated but still have to be limited (Android specifies continuous output latency < 45 ms, continuous input latency < 50 ms) ● Audio/Video playback: larger latencies can be tolerated as long as synchronization is maintained Remi Lorriaux - ELC 2011 8
  • 9. Measuring audio latency ● Measuring total latency (including hardware): ● Use an oscilloscope or a soundcard ● Can measure the difference between 2 measurements to assess software changes ● Measuring software latency ● ALSA: use latency test in alsa-lib (can automatically figure out the optimal latency through trial and error) ● JACK: jdelay, qjacklam Remi Lorriaux - ELC 2011 9
  • 10. Buffering ● ALSA uses periods: blocks of audio samples ● Size and number is configurable on runtime by applications ● At the end of each period, an interrupt is generated. → Shorter periods: more interrupts → Shorter periods + less periods: lower latency p1 p2 p3 p4 INT Application fills output buffer Samples sent to device Remi Lorriaux - ELC 2011 10
  • 11. Reducing audio latency ● Buffering creates latency → Solution: Reduce buffering ● Reducing buffer size → Interrupts are generated more frequently → The system has less time to process the buffers: risk of over/underrun → The system must be designed to schedule the different tasks on time Remi Lorriaux - ELC 2011 11
  • 12. Tuning the latency ● alsa-lib provides “latency” ● Test tool used for measuring latency between capture and playback ● Audio latency is measured from driver (difference when playback and capture was started) ● Can choose a real-time priority (we changed the code to make it configurable at runtime) Remi Lorriaux - ELC 2011 12
  • 13. Tuning the latency ● Trial and error: ● Start with a low latency value ● Load the system (CPU, IO stress, your application...) ● If XRUN or bad sound quality → Increase the latency (periods number and size) ● “latency” can do it for you automatically: > latency ­m [minimum latency in  frames] ­M [maximum latency in frames]  ­r [sample rate] ­S [priority] ­p Remi Lorriaux - ELC 2011 13
  • 14. Loading the system ● Ideally: ● Realistic load (i.e. your end applications) ● Worst-case scenario ● Our experiment: ● Stress tests from the Linux Test Projet (LTP) – CPU stress – Memory allocation ● Generate IO interrupts copying from block devices ● cyclictest Remi Lorriaux - ELC 2011 14
  • 15. Choosing a board for our project ● Deciding factors: ● Pick a board that is supported in the latest mainline kernel with real-time patches (2.6.33) ● Tested real-time features (Open Source Automation Development Lab) ● Pick a board that was available at the time ● Power and features did not matter (except for audio) ● Other boards could have been chosen (e.g. BeagleBoard) Remi Lorriaux - ELC 2011 15
  • 16. Our project: Hardware ● Zoom™ AM3517 EVM Development Kit (AM3517 EVM) ● ARM™ Cortex™-A8 600 MHz → Powerful, but real-time issues still have to be taken into account ● Ethernet, USB, SD, NAND, Wireless, CAN, LCD, Video capture... → Many ways to generate software interrupts ● Audio in/out (obviously!) Remi Lorriaux - ELC 2011 16
  • 17. Our project: Software ● Bootloader ● U-Boot (Built by Arago) ● Kernel ● 2.6.33 ● Patched to 2.6.33.7 ● Patched to 2.6.33.7.2-rt30 (RT version) ● Root filesystem ● Built by Arago ● Customized recipe (arago-console-image.bb + JACK) ● Test applications: alsa-lib tests, LTP, rt-tests Remi Lorriaux - ELC 2011 17
  • 18. Our project: Software challenges ● Support for the AM3517 EVM in mainline 2.6.33 is somewhat minimal (this has changed a lot) ● Ported some of the board code/drivers from TI's PSP (2.6.32): audio, SD/MMC ● Still, not many drivers are supported on our test system (not exactly a real-life example) Remi Lorriaux - ELC 2011 18
  • 19. Experiment #1: Vanilla kernel ● Vanilla kernel: no RT patches ● Kernel configuration (see files/kernel/am3517_std_defconfig) CONFIG_PREEMPT_DESKTOP=y CONFIG_PREEMPT=y (other preemption schemes not investigated) Remi Lorriaux - ELC 2011 19
  • 20. Experiment #1: First observation ● The priority of latency has to be set to real-time otherwise, it over/underruns for low latency values (under 1024 samples @ 44.1 kHz) ● Even using a non-RT kernel, once the application priority is increased, it runs fine when competing with other “stress” processes (can be different in the real world) ● cyclictest also exhibits this behavior → Use sched_setscheduler() or chrt and set your audio thread priority to SCHED_RR or SCHED_FIFO Remi Lorriaux - ELC 2011 20
  • 21. Experiment #1: CPU usage vs latency ● Measured with no load: > ./latency -m 64 -M 64 -r 44100 -p -S 60 → Low-latencies can generate a large CPU load! Latency (samples) Latency (@ 44.1kHz) CPU Load (%) 64 1.5 ms 65% 128 2.90 ms 3% 1024 23 ms < 1% 8192 185 ms < 1% Remi Lorriaux - ELC 2011 21
  • 22. Experiment #1: Loading the system ● Creating CPU load: ● LTP CPU stress test → Competition between processes (preemptible) ● Creating IRQ pressure: ● Transfers from SD Card ● Result: Works fine with 1.5ms latencies → Vanilla + SCHED_FIFO did the trick Remi Lorriaux - ELC 2011 22
  • 23. Why use a real-time kernel? ● Tuning the application priority and audio buffering requirements can be sufficient for “soft real-time” audio systems – where occasional errors do not cause unacceptable audio quality consequences BUT: ● There is still a lot of non-preemptible code in the vanilla kernel ● Interrupt handlers, tasklets... ● Regions protected by spinlocks (depending on the number of processors on your system) Remi Lorriaux - ELC 2011 23
  • 24. CONFIG PREEMPT RT Patch ● Using the CONFIG PREEMPT RT Patch gives you: ● Almost full-preemption ● Threaded interrupts: ability to prioritize interrupts like processes → your application can have a higher priority than hardware interrupts → Lower risk of over/underruns Remi Lorriaux - ELC 2011 24
  • 25. Making the system real-time ● Apply the CONFIG PREEMPT RT Patch on vanilla kernels. ● Latest available version: patch-2.6.33.7.2-rt30 ● Also set the configuration ● Difficult task on non-mainline/recent kernels ● Making non-mainline boards compatible can be difficult (check locks and interrupt management in drivers and platform code, make sure that the timers are appropriate) ● Cannot apply the patch on proprietary drivers Remi Lorriaux - ELC 2011 25
  • 26. Checking the real-time behavior ● Check that the patch has been correctly applied: ● Running “ps” on the device will show that IRQs are threaded ● Use cyclictest to check the scheduling latency under load → The non-RT kernel fails right away under high IRQ pressure → Latencies remain bounded with RT ● Use ftrace to see advanced information (see kernel documentation for usage) – changes the timing behavior of the system! Remi Lorriaux - ELC 2011 26
  • 27. Tuning the real-time system ● Set the priority of IRQs and processes (decreasing priority): ● (High-resolution) Timers (especially since we are using a tickless system!) ● Audio (e.g. DMA on our platform) ● Your audio process ● Other interrupts ● Other processes ● More information (FFADO project) Remi Lorriaux - ELC 2011 27
  • 28. Tuning audio parameters (RT edition) ● Same process as the non-RT experiment: trial and error ● Use the results provided by cyclictest to adjust the latency, e.g. ● Does not solve the problem of using shared hardware resources (e.g. DMA, busses...) → Requires careful platform and driver design root@am3517-evm:~# cyclictest -t5 -n -p 60 policy: fifo: loadavg: 10.89 9.99 6.83 12/76 1342 T: 0 ( 1338) P:60 I:1000 C: 42699 Min: 21 Act: 36 Avg: 37 Max: 96 T: 1 ( 1339) P:59 I:1500 C: 28463 Min: 25 Act: 41 Avg: 37 Max: 135 T: 2 ( 1340) P:58 I:2000 C: 21344 Min: 25 Act: 37 Avg: 39 Max: 111 T: 3 ( 1341) 41 Avg: 39 Max: 111 22 Act: 54 Avg: 38 Max: 91 T: 3 ( 1341) P:57 I:2500 C: 17140 Min: 22 Act: 41 Avg: 38 Max: 91 T: 4 ( 1342) P:56 I:3000 C: 14283 Min: 25 Act: 27 Avg: 38 Max: 86 Remi Lorriaux - ELC 2011 28
  • 29. Experiment #2: RT Audio ● Dirty trick: Added udelay(1000) in the USB interrupt handler! → Simulate IRQ pressure since the kernel for our board did not support ethernet, display... → Not a perfect example ● Result: Works flawlessly with 1ms latency Remi Lorriaux - ELC 2011 29
  • 30. Improving the experiment ● Run typical applications (UI, gstreamer, LADSPA...) ● More stress on the interrupts: Network, Display, Video Capture... Remi Lorriaux - ELC 2011 30
  • 31. High-end audio applications: JACK ● System for handling real-time, low latency audio (and MIDI) ● Cross-platform: GNU/Linux, Solaris, FreeBSD, OS X and Windows ● Server/client model ● Connectivity: ● Different applications can access an audio device ● Audio applications can share data between each other ● Support for distributing audio processing across a network, both fast & reliable LANs as well as slower, less reliable WANs. ● Designed for professional audio work Remi Lorriaux - ELC 2011 31
  • 32. JACK: latency and real-time ● JACK does not add latency ● An RT kernel is needed only if: ● You want to run JACK with very low latency settings that require real-time performance that can only be achieved with an RT kernel ● Your hardware configuration triggers poor latency behaviour which might be improved with an RT kernel ● Most users do not need an RT kernel in order to use JACK, and most will be happy using settings that are effective without an RT kernel Remi Lorriaux - ELC 2011 32
  • 33. JACK on our platform ● Built using OpenEmbedded (added the recipe to our image) ● Used straight out of the box ● Set priorities: ● Make jackd have SCHED_FIFO priority (like your audio application seen before) ● More information: FFADO wiki Remi Lorriaux - ELC 2011 33
  • 34. Experiment #3: JACK on our platform root@am3517­evm:~#jackd ­R ­P 60 ­d alsa ­i 2 ­r 44100 ­o 2 ­ ­p 64 ­n 6 & root@am3517­evm:~# jackd 0.118.0                                                 Copyright 2001­2009 Paul Davis, Stephane Letz, Jack O'Quinn, Torben Hohn and ot. jackd comes with ABSOLUTELY NO WARRANTY                                          This is free software, and you are welcome to redistribute it                    under certain conditions; see the file COPYING for details                                                                                                        JACK compiled with System V SHM support.                                         loading driver ..                                                                apparent rate = 44100                                                            creating alsa driver ... hw:0|hw:0|64|3|44100|2|2|nomon|swmeter|­|32bit          control device hw:0                                                              configuring for 44100Hz, period = 64 frames (1.5 ms), buffer = 6 periods         ALSA: final selected sample format for capture: 16bit little­endian              ALSA: use 3 periods for capture                                                  ALSA: final selected sample format for playback: 16bit little­endian             ALSA: use 3 periods for playback                                                                                                                                  root@am3517­evm:~# jack_simple_client                                            engine sample rate: 44100   Remi Lorriaux - ELC 2011 34
  • 35. Experiment #3: JACK on our platform ● JACK works fine on our platform with < 10 ms latency (Note: simplest possible test, so this is a best- case value) Remi Lorriaux - ELC 2011 35
  • 36. Using multi-core systems ● Embedded systems can have multi-core architectures: ● Several CPUs ● Mixed CPU/DSP ● The audio processing can be assigned to a particular core ● Example: TI Audio SoC example (Mixed DSP and ARM core) ● Shared hardware resources is still important (bus contention, DMA access...) Remi Lorriaux - ELC 2011 36
  • 37. Conclusion ● Trade-offs: ● CPU/power consumption ● Latency ● Design complexity ● Tune your priorities/audio parameters first and load your system ● Procedure similar to desktop environments (well documented) Remi Lorriaux - ELC 2011 37
  • 38. Conclusion ● Adding real-time support: ● Not necessarily trivial or required ● Depends on the implementation ● Non-RT kernel can be surprisingly adequate for “soft real-time” audio Remi Lorriaux - ELC 2011 38
  • 40. Appendix: Files ● The files used for this experiment should be attached with the presentation ● Just run or have a look at the different scripts ● Run (in order): ● oe_prepare.sh: installs prerequisites for Arago/OpenEmbedded (some other changes might be needed) ● oe_download.sh: downloads and installs custom recipes for OE ● oe_build.sh: builds OE Remi Lorriaux - ELC 2011 40
  • 41. Appendix: Files ● Run (continued): ● uboot_build.sh: builds U-Boot (sources pulled from OE) ● kernel_download.sh: downloads the kernel sources and applies relevant patches for our experiments ● kernel_build.sh: builds the RT and non-RT kernel ● apps_build.sh: builds extra test applications out of the OE tree ● sd_flash.sh: flashes the bootloader + kernel + rootfs on an SD Card Remi Lorriaux - ELC 2011 41
  • 42. Appendix: References ● Real-Time Linux wiki: Lots of information about the RT patch and testing procedures ● The JACK Audio Connection Kit: General presentation. Also covers audio topics on Linux ● FFADO wiki: How to tune audio parameters ● ALSA wiki: General documentation and ALSA samples ● JACK: Developer documentation, tuning... ● AM3517 EVM: Board specification and tools Remi Lorriaux - ELC 2011 42