Skip to content

Tips_AudioFiles

Wiki converter edited this page Sep 24, 2020 · 2 revisions

Tips - Audio Files

Reading and writing audio from a file is one of the first things many people want to do with PortAudio. There are generally two considerations to keep in mind:

  • PortAudio does not provide file I/O: In order to read and write audio data from or to a file, you must either write the code to parse the file yourself, or use an existing library such as http://www.mega-nerd.com/libsndfile/ libsndfile. Note that at this time, we do not know of any audio file I/O libraries with licenses that are as unrestrictive as the PortAudio license (most are LGPL).

  • Don't do I/O in the callback: Because the callback is a sensitive place in most operating systems, it is a good idea to pass the data to another thread for file I/O rather than doing file I/O in the callback. Even if the OS docs say it's safe, doing I/O in the callback can cause unbounded delays which may cause [wiki:tips/ClickingNoises dropouts] in playback. If passing audio data between threads sounds hard to you, you're right; it's not easy. However, the task is made much easier by PortAudio's [wiki:TutorialDir/BlockingReadWrite Blocking IO] interface. (Users of the older version of PortAudio, V18, may be able to take advantage of the PABLIO interface). Because the blocking interface takes care of transferring audio between threads for you, it is safe to do file I/O in the same thread as your blocking I/O calls.

Clone this wiki locally