Fluidsynth Real-Time and Thread Safety Challenges

Total Page:16

File Type:pdf, Size:1020Kb

Fluidsynth Real-Time and Thread Safety Challenges FluidSynth real-time and thread safety challenges David Henningsson FluidSynth Developer Team [email protected] Abstract 1.1 FluidSynth's use cases FluidSynth takes soundfonts and MIDI data as FluidSynth is not only a command-line input, and gives rendered audio samples as output. application, but also a library used by more than On the surface, this might sound simple, but doing 15 other applications [1], all putting their it with hard real-time guarantees, perfect timing, requirements on the FluidSynth engine. and in a thread safe way, is difficult. Requirements include: This paper discusses the different approaches that • low-latency guarantees, e.g. when have been used in FluidSynth to solve that playing live on a keyboard. problem both in the past, present, as well as • fast rendering1, e.g. when rendering a suggestions for the future. MIDI file to disk. • configurability, such as loading and Keywords changing soundfonts on the fly. • FluidSynth, real-time, thread safety, soundfont, monitoring current state and what©s MIDI. currently happening inside the engine, needed by GUI front-ends and 1 Introduction to FluidSynth soundfont editors. FluidSynth is one of the more common software 1.2 Introduction to SoundFonts synthesizers in Linux today. It features a high level SoundFont (SF2) files contains samples and of compliance to the SoundFont (SF2) standard, as instructions for how to play them, just like similar well as good performance. The design is modular formats such as DLS, Gigastudio and Akai. A enough to suit a variety of use cases. soundfont renderer must implement features such FluidSynth does not come bundled with a GUI, as cut-off and resonance filters, ADSR envelopes, but several front-ends exist. It does however come LFOs (Low-Frequency Oscillators), reverb and with several drivers for MIDI and audio, e.g. chorus, and a flexible system for how MIDI JACK, ALSA, PulseAudio, OSS, CoreAudio/ CoreMidi (MacOSX), and DirectSound 1 Sometimes known as ªbatch processingº, a mode of (Windows). operation where throughput matters more than latency. SF2 metadata SF2 sample data MIDI MIDI processing: Audio processing: Presets, tuning, Voice(s) Interpolation, Rendered audio gain, etc filters, etc Overview of FluidSynth core messages affect the different parameters of these 2 Architecture before 1.1.0 features. FluidSynth has always had a multi-threaded 1.3 More background information architecture: One or more MIDI threads produce MIDI input to the synthesizer, and the audio driver 1.3.1 Buffer management thread is asking for more samples. Other threads FluidSynth internally processes data in blocks of would set and get the current gain, or load new 64 samples2. It is between these blocks the soundfonts. rendering engine can recalculate parameters, such 2.1 Thread safety versus low latency as e.g. current LFO values and how they affect pitch, volume, etc. When the author got involved with the There is also the concept of the audio buffer FluidSynth project, a few years ago, thread safety size, which controls the latency: the audio driver was not being actively maintained, or at least not uses this size parameter to determine how often the documented properly. There weren©t any clear system should wake up, executing one or more directions for users of FluidSynth©s API on what internal block rendering cycles, and write the result could be done in parallel. to the sound card©s buffer. Yet there seems to have been some kind of balance: Unless you stress tested it, it wouldn©t 1.3.2 MIDI processing latency crash that often ± even though several race To understand some of the problems faced conditions could be found by looking at the source below, it is also important to understand the code. At the same time, latency performance was difficulty of handling all MIDI messages in a acceptable ± again, unless you stress tested it, it timely fashion: wouldn©t underrun that often. • Loading soundfonts or MIDI files from This ªbalanceº was likely caused by carefully disk are worst, and are not guaranteed to selecting places for locking a mutex ± the more execute within an acceptable amount of MIDI messages and API calls protected by this time due to disk accesses. mutex, the better thread safety, but worse latency • MIDI Program change messages are performance. In several places in the code, one troublesome, somewhat depending on could see this mutex locking code commented out. the current API allowing custom 2.2 The ªdrunk drummerº problem soundfont and preset loaders. • Other MIDI messages, while they are An additional problem was the timing source: not calling into other libraries (and thus The only timing source was the system timer, i.e. unknown code latency-wise), still take timing based on the computer©s internal clock. This some time to process, compared to just had two consequences. rendering a block. The first: All rendering, even rendering to disk, took as long time as the playing time of the song, so if a MIDI file was three minutes long, rendering 2 It is known as the FLUID_BUFSIZE constant in the code, and I have never seen anyone change it. Audio driver thread: Render blocks Shell thread: load new SF2 file MIDI thread: input from keyboard FluidSynth core GUI thread: Set reverb width Different threads calling into FluidSynth that song would take three minutes, with the implemented so that on every 64th sample, a computer idling most of the time. callback was made to the MIDI player so that it The second: With larger audio buffer/block could process new MIDI messages. sizes3, timing got increasingly worse. Since audio 3.3 Problems with the overhaul was rendered one audio buffer at a time, MIDI messages could only be inserted between these 3.3.1 Worse latency buffer blocks. All notes and other MIDI events therefore became quantized to the audio block As the audio thread was now expected to size. (Note that this quantization is not at all process all MIDI messages, this meant more related to the intended timing of the music!) pressure on the MIDI messages to return timely, This problem was labelled ªthe drunk drummer and audio latency now had to take MIDI problemº, since listeners were especially sensitive processing into account as well. The sample timer to the drum track having bad timing (even though made this even worse, as all MIDI file loading and the same bad timing was applied to all channels). parsing now also happened in the audio thread. 3.3.2 Reordering issues To aid the now tougher task of the audio thread, 3 Architecture in 1.1.0 and 1.1.1 program change messages were still processed in 3.1 Queuing input the MIDI thread, queueing the loaded preset instead of the MIDI message. However, this also To make FluidSynth thread safe, it was decided meant that bank messages had to be processed to queue MIDI messages as well as those API calls immediately, or the program change would load setting parameters in the engine. This was the wrong preset. In combination with API calls implemented as lock-free queues ± the MIDI for loading soundfonts, this became tricky and thread would insert the message into the queue, there always seemed to be some combination order and the audio thread would be responsible for not being handled correctly. processing all pending MIDI messages before 3.3.3 Not getting what you're putting in rendering the next block. out Since API calls were now being queued until the 3.2 The sample timer next rendering, this broke API users expecting to To make the drunk drummer sober again, the be able to read back what they just wrote. E g if a ªsample timerº was added ± that uses the number GUI front-end set the gain, and then read it back, it of rendered samples as a timing source instead of would not read the previous set value as that value the system timer. This also allowed features such had not yet been processed by the audio thread. as fast MIDI-file rendering to be added. This was This was somewhat worked around by providing a separate set of variables that were updated 3 In high-latency scenarios, such as a MIDI file immediately, but since these variables could be player, you would typically want as large buffer as simultaneously written by several threads, writes possible, both to avoid underruns and to improve overall and reads had to be atomic, which became difficult performance. Shell thread: load new SF2 file Audio driver thread: MIDI thread: input from keyboard Render blocks GUI thread: Set reverb width FluidSynth core: MIDI processing, Audio rendering MIDI/API queues Threading architecture in 1.1.0 when writes and reads spanned several variables 4.2 Return information internally. A queue with return information also had to be added, with information flowing from the Audio rendering thread to the MIDI threads. This is used 4 Architecture in 1.1.2 and later to notify the MIDI processing when a voice has To overcome the problems introduced with finished, so that the voice can be reallocated at the 1.1.0, the thread safety architecture was once again next MIDI note-on event. This return information rewritten in 1.1.2. This time, it was decided to split queue is processed right after the mutex is locked. the engine into two parts: One for handling MIDI and one for handling audio. Hard real-time is guaranteed for the audio thread only, in order not 5 Conclusion and suggestions for the future to miss a deadline and cause underruns as a result. While the architecture in 1.1.2 seems to have For MIDI, the synth no longer has an input been more successful than the previous attempts in queue, but is instead mutex protected4.
Recommended publications
  • Scummvm Documentation
    ScummVM Documentation CadiH May 10, 2021 The basics 1 Understanding the interface4 1.1 The Launcher........................................4 1.2 The Global Main Menu..................................7 2 Handling game files 10 2.1 Multi-disc games...................................... 11 2.2 CD audio.......................................... 11 2.3 Macintosh games...................................... 11 3 Adding and playing a game 13 3.1 Where to get the games.................................. 13 3.2 Adding games to the Launcher.............................. 13 3.3 A note about copyright.................................. 21 4 Saving and loading a game 22 4.1 Saving a game....................................... 22 4.2 Location of saved game files............................... 27 4.3 Loading a game...................................... 27 5 Keyboard shortcuts 30 6 Changing settings 31 6.1 From the Launcher..................................... 31 6.2 In the configuration file.................................. 31 7 Connecting a cloud service 32 8 Using the local web server 37 9 AmigaOS 4 42 9.1 What you’ll need...................................... 42 9.2 Installing ScummVM.................................... 42 9.3 Transferring game files.................................. 42 9.4 Controls........................................... 44 9.5 Paths............................................ 44 9.6 Settings........................................... 44 9.7 Known issues........................................ 44 10 Android 45 i 10.1 What you’ll need.....................................
    [Show full text]
  • How to Create Music with GNU/Linux
    How to create music with GNU/Linux Emmanuel Saracco [email protected] How to create music with GNU/Linux by Emmanuel Saracco Copyright © 2005-2009 Emmanuel Saracco How to create music with GNU/Linux Warning WORK IN PROGRESS Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is available on the World Wide Web at http://www.gnu.org/licenses/fdl.html. Revision History Revision 0.0 2009-01-30 Revised by: es Not yet versioned: It is still a work in progress. Dedication This howto is dedicated to all GNU/Linux users that refuse to use proprietary software to work with audio. Many thanks to all Free developers and Free composers that help us day-by-day to make this possible. Table of Contents Forword................................................................................................................................................... vii 1. System settings and tuning....................................................................................................................1 1.1. My Studio....................................................................................................................................1 1.2. File system..................................................................................................................................1 1.3. Linux Kernel...............................................................................................................................2
    [Show full text]
  • Midi Player Download Free Free Audio and Video Player Software – Media Player Lite
    midi player download free Free Audio and Video Player Software – Media Player Lite. MediaPlayerLite is a free open source audio and video player on Windows. You can play DVD, AVI, mpeg, FLV, MP4, WMV, MOV, DivX, XviD & more! Play your video and audio now completely free! Features – what can MediaPlayerLite do? Video, Image & Audio Player MPEG- 1, MPEG-2 and MPEG-4 playback. Free MIDI Player. Clicking the download button begins installation of InstallIQ™, which manages your MediaPlayerLite installation. Learn More. You may be offered to install the File Association Manager. For more information click here. You may be offered to install the Yahoo Toolbar. More Information. MediaPlayerLite – Best Software to Open Audio, Music & Sound Files. MediaPlayerLite is a extremely light-weight media player for Windows. It looks just like Windows Media Player v6.4, but has additional features for playing your media. Fast and efficient file playback and without any codecs. Advanced settings for bittrate and resolutions Batch conversion for many files needing to be converted. MediaPlayerLite Features. MediaPlayerLite is based on MPC-HT and supports the following audio, video and image formats: WAV, WMA, MP3, OGG, SND, AU, AIF, AIFC, AIFF, MIDI, MPEG, MPG, MP2, VOB, AC3, DTS, ASX, M3U, PLS, WAX, ASF, WM, WMA, WMV, AVI, CDA, JPEG, JPG, GIF, PNG, BMP, D2V, MP4, SWF, MOV, QT, FLV. Play VCD, SVCD and DVDs Option to remove Tearing Support for EVR (Enhanced Video Renderer) Subtitle Support Playback and recording of television if a supported TV tuner is installed H.264 and VC-1 with DXVA support DivX, Xvid, and Flash Video formats is available MediaPlayerLite can also use the QuickTime and the RealPlayer architectures Supports native playing of OGM and Matroska container formats Use as a Audio player.
    [Show full text]
  • Argomanuv23.Pdf (All Rights Reserved for All Countries) Modules Générateurs Audio Audio Generator Modules
    ARGO est constitué de plus de 100 modules ARGO is made of more than 100 de synthèse et de traitement sonore real-time sound synthesis "modules" fonctionnant en temps réel sous Max/MSP. A module is a Max/MSP patch ARGO est conçu pour des utilisateurs qui ARGO is conceived for users who n'ont jamais programmé avec Max et MSP have never programmed with Max/MSP Pour Macintosh (OS9 & OSX) For Macintosh (OS9 & OSX) et PC (Windows XP) and PC (Windows XP) Logiciel libre Freeware http://perso.orange.fr/Paresys/ARGO/ Auteur Gérard Parésys Author [email protected] ARGOManuv23.pdf (All rights reserved for all countries) Modules générateurs audio Audio generator modules Modules filtres audio Audio filter modules Modules modulateurs audio Audio Modulator modules Modules transformateurs audio Audio transformer modules Modules lecteurs/enregisteurs audio Audio Play/record modules Modules liens audio Audio link modules Modules contrôleurs Control modules Modules visu Visu modules Modules MIDI MIDI modules Les modules ARGO sont des fichiers ARGO Modules are files ("patches") (des "patches") exécutés par executed by Max/MSP application . l'application Max/MSP. Sous Windows, les patches ont l'extension For Windows, the patches have the .pat ou .mxf extension .pat or .mxf Rapidement: Quickly: (Si Max/MSP est déjà installé) (If Max/MSP is installed) 1 Ouvrir un ou plusieurs modules 1 Open one or several modules 2 Relier les modules avec : 2 Link the modules with: les 12 "ARGO Bus" 12 "ARGO Bus" les 16 "ARGO MIDI Bus" 16 "ARGO MIDI Bus" (les entrées sont en haut,
    [Show full text]
  • Release 0.23~Git Max Kellermann
    Music Player Daemon Release 0.23~git Max Kellermann Sep 24, 2021 CONTENTS: 1 User’s Manual 1 1.1 Introduction...............................................1 1.2 Installation................................................1 1.3 Configuration...............................................4 1.4 Advanced configuration......................................... 12 1.5 Using MPD................................................ 14 1.6 Advanced usage............................................. 16 1.7 Client Hacks............................................... 18 1.8 Troubleshooting............................................. 18 2 Plugin reference 23 2.1 Database plugins............................................. 23 2.2 Storage plugins.............................................. 24 2.3 Neighbor plugins............................................. 25 2.4 Input plugins............................................... 25 2.5 Decoder plugins............................................. 27 2.6 Encoder plugins............................................. 32 2.7 Resampler plugins............................................ 33 2.8 Output plugins.............................................. 35 2.9 Filter plugins............................................... 42 2.10 Playlist plugins.............................................. 43 2.11 Archive plugins.............................................. 44 3 Developer’s Manual 45 3.1 Introduction............................................... 45 3.2 Code Style...............................................
    [Show full text]
  • Squishbox User Guide Models 0001-0099 Software Version 3.0
    Squishbox User Guide models 0001-0099 software version 3.0 Contents Overview....................................................................................................................................................1 Quick Start.................................................................................................................................................2 Hardware....................................................................................................................................................3 Squishbox Diagram...............................................................................................................................3 Adjusting Contrast.................................................................................................................................3 Interface.....................................................................................................................................................4 Left Button Menu..................................................................................................................................4 Right Button Menu................................................................................................................................4 Wifi Access............................................................................................................................................5 Settings Files..............................................................................................................................................6
    [Show full text]
  • Sounds on Time: Auditory Feedback in Motor Learning, Re-Learning and Over-Learning of Timing Regularity
    Sounds on time: auditory feedback in motor learning, re-learning and over-learning of timing regularity. Floris van Vugt To cite this version: Floris van Vugt. Sounds on time: auditory feedback in motor learning, re-learning and over-learning of timing regularity.. Neuroscience. Université Claude Bernard - Lyon I, 2013. English. tel-00915893 HAL Id: tel-00915893 https://tel.archives-ouvertes.fr/tel-00915893 Submitted on 9 Dec 2013 HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements d’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. Sounds on time - N° d’ordre 232 - 2013 THESE DE L‘UNIVERSITE DE LYON Délivrée par L’UNIVERSITE CLAUDE BERNARD LYON 1 ECOLE DOCTORALE NEUROSCIENCES ET COGNITION (NSCo) DIPLOME DE DOCTORAT EN NEUROSCIENCES (arrêté du 7 août 2006) Soutenue publiquement le 27 novembre 2013 à Lyon par Floris Tijmen VAN VUGT Directeurs de hèse: Dr. Barbara TILLMANN et Prof. Eckart ALTENMÜLLER Composition du Jury Prof. Eckart ALTENMÜLLER (directeur de thèse) Dr. Barbara TILLMANN (directeur de thèse) Prof. Peter KELLER (rapporteur) Prof. Virginia PENHUNE (rapporteur) Dr. Fabien PERRIN (président du jury) | 3 | Sounds on time - Sounds on time: auditory feedback in motor learning, re-learning and over-learning of timing regularity.
    [Show full text]
  • Automated Composition of Picture-Synched Music Soundtracks for Movies
    Automated Composition of Picture-Synched Music Soundtracks for Movies Vansh Dassani Jon Bird Dave Cli e University of Bristol e University of Bristol e University of Bristol Bristol BS8 1UB, UK Bristol BS8 1UB, UK Bristol BS8 1UB, UK [email protected] [email protected] [email protected] ABSTRACT 15-minute music track to your liking, the chances are that you’d We describe the implementation of and early results from a system need to go back into edit to adjust the cut, so that key events in the that automatically composes picture-synched musical soundtracks movie t well to the timeline imposed by the choice of music: but for videos and movies. We use the phrase picture-synched to mean what if you don’t want to do that (remember, your movie is already that the structure of the automatically composed music is deter- at picture-lock)? If you’re a gied auteur, you could compose your mined by visual events in the input movie, i.e. the nal music is own soundtrack, ing the music to the video, but that will likely synchronised to visual events and features such as cut transitions take some time; and maybe you just don’t have the music skills. or within-shot key-frame events. Our system combines automated If instead you have money, you could pay a composer to write a video analysis and computer-generated music-composition tech- specically commissioned soundtrack, synchronised to the cut of niques to create unique soundtracks in response to the video input, the video.
    [Show full text]
  • Pytuning Documentation Release 0.7.1
    PyTuning Documentation Release 0.7.1 Mark Conway Wirt Jul 21, 2017 Contents 1 Installation 3 2 Documentation 5 3 Roadmap 7 3.1 Basic Concepts..............................................7 3.2 Scale Creation.............................................. 12 3.3 Metric Functions............................................. 18 3.4 Scale Analysis and Creation (Redux).................................. 20 3.5 Tuning Tables.............................................. 26 3.6 Number Theory............................................. 32 3.7 Utilities.................................................. 34 3.8 Visualizations.............................................. 38 3.9 Using PyTuning Interactively...................................... 42 4 Index 55 i ii PyTuning Documentation, Release 0.7.1 PyTuning is a Python library intended for the exploration of musical scales and microtonalities. It can be used by developers who need ways of calculating, analyzing, and manipulating musical scales, but it can also be used interac- tively. It makes heavy use of the SymPy package, a pure-Python computer algebra system, which allows scales and scale degrees to be manipulated symbolically, with no loss of precision. There is also an optional dependency on Matplotlib (and Seaborn) for some visualizations that have been included in the package. Some of the package’s features include: • Creation of scales in a variety of ways (EDO, Euler-Fokker, Diatonic, Harmonic, from generator intervals, etc.) • Ability to represent created scales in ways that are understood
    [Show full text]
  • 1. Why POCS.Key
    Symptoms of Complexity Prof. George Candea School of Computer & Communication Sciences Building Bridges A RTlClES A COMPUTER SCIENCE PERSPECTIVE OF BRIDGE DESIGN What kinds of lessonsdoes a classical engineering discipline like bridge design have for an emerging engineering discipline like computer systems Observation design?Case-study editors Alfred Spector and David Gifford consider the • insight and experienceof bridge designer Gerard Fox to find out how strong the parallels are. • bridges are normally on-time, on-budget, and don’t fall ALFRED SPECTORand DAVID GIFFORD • software projects rarely ship on-time, are often over- AS Gerry, let’s begin with an overview of THE DESIGN PROCESS bridges. AS What is the procedure for designing and con- GF In the United States, most highway bridges are budget, and rarely work exactly as specified structing a bridge? mandated by a government agency. The great major- GF It breaks down into three phases: the prelimi- ity are small bridges (with spans of less than 150 nay design phase, the main design phase, and the feet) and are part of the public highway system. construction phase. For larger bridges, several alter- There are fewer large bridges, having spans of 600 native designs are usually considered during the Blueprints for bridges must be approved... feet or more, that carry roads over bodies of water, preliminary design phase, whereas simple calcula- • gorges, or other large obstacles. There are also a tions or experience usually suffices in determining small number of superlarge bridges with spans ap- the appropriate design for small bridges. There are a proaching a mile, like the Verrazzano Narrows lot more factors to take into account with a large Bridge in New Yor:k.
    [Show full text]
  • Hands-On Engineering Experience, a Liberal Arts Case
    Paper ID #29699 Hands-on Engineering Experience, a Liberal Arts Case Dr. Niloofar Kamran, Cornell College Mr. Qingbao Wang, Cornell College Mr. Andy Grove William Nitschke Dragon II, Cornell College c American Society for Engineering Education, 2020 Hands-on Engineering Experience, a Liberal Arts Case Will Dragon, Qingbao Wang, Andy Grove, Niloofar Kamran Abstract Our project was a part of the 2019 Cornell College Summer Research Institute (CSRI), where Cornell College students and faculty work in close collaboration on a research project for eight weeks during summer. The program includes one faculty member and one or two students per each research topic. As the educational goals of the program, we expect our students to commit to the eight-week timeline, and we hope to get them ready for their future workplace and give them some ideas of the graduate school experience. They practice to learn how a research project is done from the literature review to the built, and we hope to encourage them to be independent thinkers. To satisfy the educational goals of the program, the students were given the flexibility to lead the project in the direction they wished, and the supervisor provided a guiding hand, emulating a graduate-level research work. In addition, the students presented their work for their peers and public audience multiple times during the project that helped them with their presentation skills. We chose the project based on the students' background and passion and with having their degree, general engineering, in mind. Students at Cornell College are heavily involved in artistic and athletic activities.
    [Show full text]
  • Linux Audio Conference 2019
    Proceedings of the Linux Audio Conference 2019 March 23rd – 26th, 2019 Center for Computer Research in Music and Acoustics (CCRMA) Stanford University, USA “In Ping(uins) e trust! Published by CCRMA, Stanford University, California, US March 2019 All copyrights remain with the authors http://lac.linuxaudio.org/2019 ISBN 978-0-359-46387-9 Credits Layout: Frank Neumann and Romain Michon Typesetting: LATEX and pdfLaTeX Logo Design: The Linuxaudio.org logo and its variations copyright Thorsten Wilms c 2006, imported into "LAC 2014" logo by Robin Gareus Thanks to: Martin Monperrus for his webpage "Creating proceedings from PDF files" ii Partners and Sponsors Linuxaudio.org iii iv Foreword Welcome everyone to LAC 2019 at CCRMA! For the second time in its seventeen year history, the Linux Audio Conference (LAC) is hosted in the United Stated of America by the Center for Computer Research in Mu- sic and Acoustics (CCRMA) at Stanford University. With its informal workshop-like at- mosphere, LAC is a blend of scientific and technical papers, tutorials, sound installations, and concerts centered on the free GNU/Linux operating system and open-source free soft- ware for audio, multimedia, and musical applications. LAC is a unique platform during which members of this community gather to exchange ideas, draft new projects, and see old friends. In these times of increasing political tensions and of rising extremism throughout the world, we believe that emphasizing and promoting the universality of this type of event is of the utmost importance. The Linux audio community exists worldwide; we believe it should remain a priority to diversify LAC’s geographical location from year to year for the benefit of those who can’t afford to travel to the other side of the world.
    [Show full text]