BeagleBoard/GSoC/2021 Proposal/ALSA plugin for BELA
- 1 [ALSA plugin for BELA]
- 2 Status
- 2.1 About you
- 2.2 About your project
- 2.3 Misc
[ALSA plugin for BELA]
Student: Jakub Duchniewicz
Mentors: Giulio Moro
Code: not yet created!
Discussing the implementation ideas with Giulio Moro and others on #beagle-gsoc IRC.
School: University of Turku/KTH Royal Institute of Technology
Primary language: Polish
Typical work hours: 8AM-5PM CET
Previous GSoC participation: Participating in GSoC, especially with BeagleBoard would further develop my software and hardware skills and help me apply my current knowledge for the mutual benefit of the open source community. I planned to do the YOLO project, but after spending several days researching and preparing the proposal I found it is impossible to do on current BBAI/X15.
About your project
Project name: ALSA plugin for BELA
BELA is a cape designed for BB Black which features real-time audio processing via usage of Xenomai threads. Apart from being a hardware solution, BELA supplies its own operating system based on Debian Linux distribution and a full-fledged IDE allowing for seamless audio development experience. BELA provides its own library for interfacing with the hardware, however it does not provide any unified interface via ALSA, JACK or PulseAudio. Therefore, it is currently impossible to use BELA like a regular Linux audio device and it has to be done by utilizing its API calls.
The main premise of this project is to enable the unified access by means of ALSA plugin. This plugin will allow for tying user-provided functions for regular system calls alsa-lib API uses for operating on its devices. Since such need may arise for any other real-time ALSA devices, this plugin would be a valuable addition to the ALSA ecosystem and would be mainline'able. This way, users can call regular ALSA API's for interacting with the device and still profit from all the real-time benefits it offers.
The project will also focus on writing all necessary components for interfacing with this plugin, such as an exemplary userspace application and instructions on how to use the ALSA API with BELA. BELA also features analog and digial ins and outs which should be representable in ALSA in the manner of channels or as MIDI interfaces. // TODO: totally not sure about this giulimoro,
The first part - ALSA plugin will be realized basing on already existent ALSA plugins, like the file plugin which allows interaction with arbitrary files as if they were regular ALSA devices. If not for hardcoded syscalls in this plugin, we would be fine by simply using it for our problem, or writing some workaround and shipping custom libasound along with other BELA software. However, this solution is not the best idea, as we would rely on shipping this library and maintaining it in sync with the upstream. Hence, having a completely fresh plugin which could be used in various other applications is a better tailored solution.
In order to understand why we even have create special ALSA plugins and not create regular driver for the BELA devices, we need to look closely at how BELA manages its audio data and delivers it to the user.
The graph above shows how the BELA system operates and utilizes the ARM CPU and the PRU unit. It can be seen that there the PRU has an essential role in communicating with various peripherals and delivering the data to the ARM CPU. Since Linux kernel is running alongside the Xenomai kernel, the real-time guarantees can be met. This is especially important in such systems as this, and pairing it with the sheer power of Linux kernel makes this project especially valuable.
Whenever data is delivered, there is an almost instant (1us) context switch to the Xenomai Cobalt thread and the data is served to the user. Xenomai makes use of the hardware interrupts the BELA cape makes (//TODO: check?).
the job done by the PRU is simple but has tight time constrains: every 11us it has to access the SPI bus, the McASP bus and the GPIOs and shift data to/out of RAM.. So you effectively need a computing core that does just that. It doesn't have to be powerful but whatever runs on it cannot be preempted by anything else ever. So yes in principle you could run it on one of the two RISCV cores but that would seem like a huge waste (though in principle doable!)! By having all the low-level access to the peripherals delegated to an external microcontroller, then the data could be shifted to the RISCV core via a single DMA and the 2 CPU cores would be free all of the time!
// add graphs The system overview is described in the above diagram.
Alternative ideas/Stretch goals
- One could even add a virtual MIDI device for delivering GPIO I/O and sensor/actuator analog I/O at non-audio rate.
- The ALSA plugin itself
- Userspace example app
- Guidelines on using ALSA with BELA
During 25.07.21-08.08.21 I have a summer camp from my study programme and will be probably occupied for a half of the day. The camp will most likely be held online though.
Experience and approach
I have strong programming background in the area of embedded Linux/operating systems as a Junior Software Engineer in Samsung Electronics during December 2017-March 2020. Additionally I have developed a game engine (| PolyEngine) in C++ during this time and gave some talks on modern C++ during my time as a Vice-President of Game Development Student Group "Polygon".
Apart from that, I have completed my Bachelors degree at Warsaw University of Technology successfully defending my thesis titled: | FPGA Based Hardware Accelerator for Musical Synthesis for Linux System. In this system I created a polyphonic musical synthesizer capable of producing various waveforms in Verilog code and deployed it on a De0 Nano SoC FPGA. Additionally I wrote two kernel drivers - one encompassed ALSA sound device and was responsible for proper synchronization of DMA transfers.
The ALSA part proved to be very time consuming and difficult to debug, but after hours of a wild goose chase I understand how ALSA works at a level allowing me for various kernel driver creation and userspace plugin extesion.
I am familiar with Deep Learning concepts and basics of Computer Vision. During my studies at UTU I achieved the maximal grades for my subjects, excelling at Navigation Systems for Robotics and Hardware accelerators for AI.
In my professional work, many times I had to complete various tasks under time pressure and choose the proper task scoping. Basing on this experience I believe that this task is deliverable in the mentioned time-frame.
Since I am used to tackling seemingly insurmountable challenges, I will first of all keep calm and try to come up with alternative approach if I get stuck along the way. The internet is a vast ocean of knowledge and time and again I received help from benevolent strangers from reddit or other forums. Since I believe that humans are species, which solve problems in the best way collaboratively, I will contact #beagle, #beagle-gsoc and relevant subreddits (I received tremendous help on /r/FPGA, /r/embedded and /r/askelectronics in the past).
If all fails I may be able be forced to change my approach and backtrack, but this will not be a big problem, because the knowledge won't be lost and it will only make my future approaches better. Alternatively, I can focus on documenting my progress in a form of blogposts and videos while waiting for my mentor to come back to cyberspace.
In case of this problem, there exists a high risk of problems without obvious solutions and in this case I might need to directly email the creators of ALSA (or join the linux kernel mailing list) - Jaroslav Kysela and Takashi Iwai. Because the plugin development will be in the userspace, regular GDB will be enough for debugging it.
During my previous adventures with ALSA i accumulated a list of sound (no pun intended) materials:
- Writing a kernel driver - explains what is happening inside ALSA from the drivers perspective
- Overview of Linux Sound - just a good reference
- Describing ALSA plugins
- Vital element of understanding the ring buffer in ALSA
- The source code of ALSA lib
- Internal workings of BELA
- Overview of BELA
Having an ALSA plugin for BELA would allow interfacing with the devices in a unified manner and it will remove the onus of knowing what API BELA uses for communication. Moreover, such plugin can be mainline'able as it will allow for substituting various Linux syscalls the ALSA API makes for the device interfacing with this plugin, such as substituting the read() syscall with the Xenomai cobalt_read() making the operation run in real-time.
currently for each different application that a user want to run, the audio backend of the application has to be changed from using ALSA/portaudio/RTAudio/jack to using the Bela API. This could be mitigated by having portaudio/RTAudio/jack wrappers (what I put in the original project idea), but I figured if it could be just an ALSA driver (perhaps an ALSA plugin, which runs all in userspace I think?) then things could be more maintainable
~ Giulio Moro
It just occurred to me recently that the ALSA solution would be more generally applicable
~ Giulio Moro
If there is not an ALSA plugin readily available that accepts arbitrary functions for read and write, we could add one. I think that would stand some chance of getting mainlined as it would be a new, clean feature, backward compatible and without hacks.
~ Giulio Moro
I'd also want to know that it would be consistent over different releases. I'm not sure how often ALSA changes... would be super annoying to create a wrapper that suddenly stops working, I'm not sure how much of an issue this is. There's also a few things on top of ALSA that might be a better bet etc like portaudio. I'm just brainstorming here though haven't investigated
~ Lucas Pillsbury
The qualification PR is available here.