2

I thought of the following project that I want to pursue:

I want to create a C++ program, that can play audiofiles and visualize the amplitudes of individual frequency bands in real time (e.g. with bars).

I did some search on google, and I found that SDL provides libraries that might do the trick. First I read about the format in which an audio file is saved (more specific uncompressed .wav files, as they seem to be the easiest to read). However, even though I now know how I can get the data of the file, I don't know what it tells me. I mean, how many bytes do I have to read to get one sample, how is the sample saved (I think it's not a direct representation of the frequency space), and so I really have no idea how to work with it. As a direct consequence, I neither know how to play the files. Then I found SDL, and figured it should be able to extract the information that I need. Looking through the SDL wiki I was not able to find out how I have to proceed, though.

I have done some basic C++ programming in the past, I think I'm on a high enough level to do this on the programming side. I have, however, now worked with binary files so far. Also I have a strong maths background, the maths involved are no problem at all.

The ultimate goal would probably be to do the frequency analysis of all audio output or input, but I read that this depends heavily on the operating system used. Also, applying filters in real time would be a nice thing to do, but this should be rather advanced. Hence just playing an audio file and visualizing would be the first goal.

So to sum up, I think my question is the following:

What does the data in an audio file tell me? How can I get it to a state such that I can do a frequency analysis? How can this be done in real time, while playing the audio file? Is SDL a good choice for this, or is there a better way without taking away all the work (I wouldn't like to just use a program that does everything as I need it, I want to do something of my own!)?

Any ideas and inputs are much appreciated!

PS: I'm not sure I'm on the right stackexchange here, but I really couldn't find out where else this would fit.

  • It probably is operating system specific. libSDL, SFML, ALSA, Qt might be relevant – Basile Starynkevitch Jun 16 '15 at 8:48
  • Also, on Linux, you'll probably find several free software doing related things, and you should study their source code. – Basile Starynkevitch Jun 16 '15 at 8:59
  • I should probably add that I am currently working on Arch, but I'd also like to have it working on OSX (at some later stage). – Pascal Engeler Jun 16 '15 at 9:00
  • have a look at audacity, and the portaudio library for a start. I think audacity can do what you want already, alternatively look at ffmpeg, which does audio too (obviously). All these will give you a very good start point for your work, but I wouldn't reinvent something new when you can build on top of the great work already done by others. – gbjbaanb Jun 16 '15 at 10:27
  • Last I looked, the audio data in a raw WAV file is simply measured amplitudes. For example a mono, 44.1KHz 16-bit uncompressed WAV is literally 44100 unsigned 16-bit integers for each second of audio once you unwrap the chunks. Very direct. At that point you have your raw samples and can apply FFT etc. If you strictly control your input, raw parsing may not be too hard but if you don't, probably find a library. Now as for how to get input from say a microphone or to output to a speaker, that is all very OS-dependent and you will definitely want a library. – J Trana Jun 22 '15 at 5:09
1

What does the data in an audio file tell me?

The header tells you the sampling rate, the data format (compressed or raw, integer or float, how many bits per sample), number of channels (mono, stereo or more channels). The actual data describes the amplitude of the signal as a function of time.

How can I get it to a state such that I can do a frequency analysis? How can this be done in real time, while playing the audio file?

Usually you launch an audio i/o background thread which repeatedly calls back your processing function when a new buffer of input audio is available (a few millisecs). In that function you process the audio and may call another function of the audio library to add a buffer to the audio output queue.

You can also update your own data structures, like the FFT of the last x millisecs. You can then mark the FFT graphics window 'dirty' to trigger a redraw on the UI thread.

Is SDL a good choice for this, or is there a better way without taking away all the work

I would recommend JUCE since it contains all what you need, from platform-independent audio input to UI with graphics. There's a demo application which demonstrates all the features and also serves as a code sample collection.

0

Here's a good library for opening and reading a sound file. It supports most open codecs, and it is not nearly as complex as ffmpeg. However it does not support any closed codecs like mp3. It is written in pure C.

http://www.mega-nerd.com/libsndfile/

For analyzing the audio you could use fftw3 to do a dft to analyze the audio.

Most of the audio libraries, I have found are almost pure C.

Beyond that you can use whatever you want for handling the gui.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.