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
Download dsp_template v0.1 Project Sources as zip
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).
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).
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.
Example AudioProcessor implementations (from simple to more complex examples):
audioprocessor_io_copy.cpp
The simplest example that copies the sound card input to the output
audioprocessor_noise.cpp
White noise is generated using the rand() function and is played back.
audioprocessor_sine.cpp
A 1kHz sine wave is generate and played back
audioprocessor_fft.cpp
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.