A Template for writing Digital Signal Processing Applications with a Sound Card under Linux and Windows


dsp_template is a project that can be used as starting point for writing applications for digital signal processing (DSP) with an ordinary sound card. The project is written in C++ and uses the Qt library from Trolltech, which is a portable library for writing graphical user interfaces. Furthermore, the libraries Qwt, fftw and portaudio are included. Qwt is used for the plotting of data, fftw can be used for the fast processing of FFTs and portaudio is used for the unique access to the sound card interface.

All these libraries are portable to different platforms. The application has been developed under Linux and tested for Windows, too. In principle, Mac Os should work too but I simply don't have a Mac so I can't test it ;-) The portaudio is included as source code as the used version (v19_trunk) is not an official release. All other libraries are linked at runtime. Under linux, the libraries libqt4-core, libqt4-gui, libqwt5-qt4, libfftw3 and libasound2 must be installed (which can be done with "apt-get install " for Debian based distributions). Under Windows precompiled DLLs can be downloaded here. dsp_template is licensed under the GNU public license GPL.

Download dsp_template v0.1 Project Sources as zip

Getting Started

  1. The project file (dsp_template.pro) can be opened with Qt Creator, which is an integrated development tool from Trolltech. Alternatively, the qmake command can be used to parse the project file and to create a make file for the used platform (no cross-compiling is supported, I used sun's VirtaulBox to compile for Windows).
  2. You can try different examples by uncommenting appropriated lines in the project file with name "audioprocessor_xxx.cpp/.h" *and* the corresponding header in "mainwindow.h". A description of the different examples can be found in the text below.
  3. To change the name of the project just rename the project file (dsp_template.pro) to and put the same name as TARGET in the project file
  4. Have fun!

Notes for Linux

Although the ALSA is supported by portaudio, the playback with ALSA was not possible. Hopefully this will be fixed in the next release. Nevertheless, OSS is working fine. The support of the different audio APIs can be defined in the project file (uncomment PA_USE_xxxx).

Notes for Windows

For using Direct-X the Microsoft Direct-X SDK has to be installed (DXSDK_Nov08 was tested) and the header directory must be specified using "win32:INCLUDEPATH" in the project file (see comment).

Short Description of the Files and Classes

In the following, the implemented files and classes are explained briefly:

main.cpp - the starting point.
This is the file where the main() method is implemented. As it is usual for Qt applications, the main window class is created here - in this case the class MainWindow.

wainwindow.cpp - Implementation of the main window (class MainWindow)
This is the implementation file of the main window. The GUI is mainly defined in mainwindow.ui (the Qt-UI Format) which can be edited by the Qt Assistant or the integrated IDE Qt Creator. A menu is created with one entry "Settings->Sound Settings" which activates a dialog where the basic sound settings, like selecting the sound device and the frame length can be done. This dialog is defined in soundsettingsdlg.ui and soundsettingsdlg.cpp. An additional QwtPlot widget is created and initialized (outside mainwindow.ui). Furthermore, an instance of the class AudioProcessor is created, which is the starting point for the audio processing routines.

soundsettingsdlg.cpp - Implementation of the sound settings dialog
Based on soundsettingsdlg.ui a simple dialog is created for sound card settings.

pawrapper.cpp - A simple c++ wrapper for portaudio
In this class (PaWrapper) the important methods are encapsulated for configuring the sound device and to start and stop the audio processing. PaWrapper is not used directly, the application specific audio processing algorithm is implemented in a class derived from PaWrapper (the class AudioProcessor was used in the examples) which must override the virtual method ProcessingCallback(). Here the audio processing takes place.

fftprocessor.cpp - A simple wrapper class for fftw
By creating an fftProcessor object, the memory for input and output data is reserved and a fft plan for real input data is created. The buffers can be directly accessed via the fftIn attribute. The calculation is started with the execute() method and the result can be read from fftOut in a complex data format.

Short Description of the Examples

Example AudioProcessor implementations (from simple to more complex examples):
The simplest example that copies the sound card input to the output

White noise is generated using the rand() function and is played back.

A 1kHz sine wave is generate and played back

The fftProcessor is used to calculate the spectrum of the input data in realtime. A hamming window is applied to the input data to sharpen the frequency resolution and the fft calculation is started for each frame. Every "noOfFramesPerPlot" frames, the Qt signal "frameProcessed" is emitted which is connected to the "onFrameProcessed" slot in the main window which replots the QwtPlot.

05Misc/A_Template_for_Audio_DSP_Applications.txt · Last modified: 2017/09/25 15:34 (external edit)  (login)