Analysing audio data for attributes at time intervals - c++

I've been wanting to play around with audio parsing for a while now but I haven't really been able to find the correct library for what I want to do.
I basically just want to parse through a sound file and get amplitudes/frequencies and other relevant information at certain times during the song (like every 10 ms or so) so I can graph the data for example where the song speeds up a lot and where it gets really loud.
I've looked at OpenAL quite a bit but it doesn't look like it provides this ability, other than that I have not had much luck with finding out where to start. If anyone has done this or used a library which can do this a point in the right direction would be greatly appreciated. Thanks!

For parsing and decoding audio files I had good results with libsndfile, which runs on Windows/OSX/Linux and is open source (LGPL license). This library does not support mp3 (the author wants to avoid licensing issues), but it does support FLAC and Ogg/Vorbis.
If working with closed source libraries is not a problem for you, then an interesting option could be the Quicktime SDK from Apple. This SDK is available for OSX and Windows and is free for registered developers (you can register as an Apple developer for free as well). With the QT SDK you can parse all the file formats that the Quicktime Player supports, and that includes .mp3. The SDK gives you access to all the codecs installed by QuickTime, so you can read .mp3 files and have them decoded to PCM on the fly. Note that to use this SDK you have to have the free QuickTime Player installed.
As far as signal processing libraries I honestly can't recommend any, as I have written my own functions (for speech recognition, in case you are curious). There are a few open source projects that seem interesting listed in this page.
I recommend that you start simple, for example working on analyzing amplitude data, which is readily available from the PCM samples without having to do any processing. Being able to visualize the data is very useful, I have found Audacity to be an excellent visualization tool, and since it is open source you can build your own tests inside it.
Good luck!

Related

C++: Writing images to video file independent of installed codecs

I'm trying to save a series of images (16 bit grayscale pgm) as video. The video has to be compressed. My program has to be independent of the codecs installed in the system.
My initial idea was to use OpenCV for this, unfortunately it depends on codecs installed in the system (unless I'm missing something).
I feel like there should be a way to compile an encoder (H264 or similar would be perfect) into the program or redistribute it as a dll with my program. I just can't find any good up to date guidance/examples.
I've been swimming in the deep vast ocean of AV encoding for a couple of days and would really appreciate it if someone could point me to a right direction.
Thanks.
As Ben suggests, it would be a good idea to use an established library in your code.
FFMPEG is probably the most used at the moment - it can be used on the command line, with a 'wrapper' program or the libraries it is built with can be used directly.
I think the last case sounds like the one you want - you can find documentation here:
https://trac.ffmpeg.org/wiki/Using%20libav*
Note the comment about disambiguation at the start - this is important to understand as the project lib and the library (which is what you want) are different things.
and there is some notes in this answer on how to build it into a program:
FFMpeg sample program

reading mp3 file for game development

I am currently creating a game. My game will use music from an mp3 file that the user sends in in order to make decisions on where to place things, how fast the level moves, etc. I am fairly new at this, I have been reading information about mp3. Currently I have found all the frames in the mp3 file that I am using. I don't really know where to go from here. What I want to do is measure the frequencies of the sound wave of the music at certain times (like every sec) and then based on that frequency, do what I need to for the game. I don't know whether I should decode the mp3, that looks like a lot of work and I don't want to do that if I don't have 2 or if I can just read the bytes in the frame and convert them without decoding anything. I am developing this in c#, using the game engine FlatRedBall. I am not using any libraries. I am also planning on selling this game so I would like to avoid using other people's code if I can avoid it. Please someone help me, I just need a direction to go from here. I know how to parse the header and calculate the framelength, I just don't know the next step in what I want to do...
Convert your music to .ogg format which is free and use free library to play it.
Note: I was going to post this as a comment but it quickly grew too big. :)
Writing your own MP3 enconder/decoder is probably going to take a good ammount of effort; effort which would probably be better spent on your game itself. Therefore, is possible, I would be all means try to use an open source library.
That said, most good MP3 libraries are LGPL/GPL licensed. This means you can use it in a commercial setting, as long as you dynamically link to it. Also the SDL Mixer library, as of version 1.2.12, supports MP3s and is under a more permissive zlib license, but since you mention C# I don't know if stable and up-to-date bindings are available. Also since your project isn't written in SDL to begin with, it might be hard to integrate it.
Also, as #pro_metedor hinted, perhaps using a more open format could help in licensing issues. In general, OGG achieves better compression than MP3, which is a plus for things like download size, bandwidth/resource usage, etc.
Just shop around for a while, and try to be a little flexible. I'm sure you'll find something nice! :)

What MPEG4 encoder library to use?

So I have a WIN32 app that records videos using DirectShow. Now I want to convert the .AVI files to .MP4.
I rather not use a custom filter in the source, since I don't want to have to register filters(admin needed). I also dont want to use a standalone application since it should be automatised. Preferbly I just want a library with a well documented API since im rather new to this. Then I can use it from my app to convert the .AVI files when they are done being recorded. Anyone can point me in a direction? Or have comments on my method of choice?
I'd be outmost grateful for any help and thanks in advance!
Because MPEG-4 codecs are not royalty free, finding suitable encoder might be not as easy as you could think of it. Microsoft does not provide Windows with the encoder, except H.264 (MPEG-4 Part 10) encoder in some editions of Windows 7, and only within Media Foundation (as opposed to DirectShow). If you are OK to be limited to those Windows 7 versions, Media Foundation might be a good option, MSDN offers samples to transcode file into file and it is reasonably easy and well documented.
There are third party solutions, there half made libraries you can leverage to encoder, there is FFmpeg which offers MPEG-4 Part 2 video encoder under LGPL, and MPEG-4 Part 10 through libx264 under GPL. And my understanding you might still be expected to pay royalties to MPEG-LA. FFmpeg might still be a good option to convert file to file because its command lnie interface is well documented (as opposed to libavformat/libavcodec API which are not so well documented on the contrary).
Another option is to use Windows Media codecs and compress into ASF/WMV files.
Libavcodec and libffmpeg -- an everything-to-everything media converter (includes library and command-line application): http://ffmpeg.org/

Absolute beginners guide to working with audio in C/C++?

I've always been curious about audio conversion software, but I have never seen a proper explanation from a beginners point of view as to how to write a simple program that converts for example, a mp3 file to a wav. I'm not asking about any of the complex algorithms involved, just a small example using a simple library. Searching on SO, I came up with several names including:
Lame
The Synthesis Toolkit
OpenAL
DirectSound
But I'm unable to find a straightforward example of any of these libraries. Usually I don't mind wading through tons of code, but here I have absolutely no knowledge about the subject and so I always feel like I'm shooting in the dark.
Anyone here have a simple example / tutorial on converting a sound file using any of these libraries? My question is specifically directed towards C/C++ because those are the two languages I'm currently learning and so I'd like to continue to focus on them.
Edit: One thing I forgot to mention: I'm on a *NIX system.
Thanks everyone for the responses! I sort of cobbled them together to successfully make a small utility that converts a AIFF/WAV/etc file to an mp3 file. There seems to be some interest in this question, so here it what I did, step by step:
Step 1:
Download and install the libsndfile library as suggested by James Morris. This library is very easy to use – its only shortcoming is it won't work with mp3 files.
Step 2:
Look inside the 'examples' folder that comes with libsndfile and find generate.c. This gives a nice working example of converting any non-mp3 file to various file formats. It also gives a glimpse of the power behind libsndfile.
Step 3:
Borrowing code from generate.c, I created a c file that just converts an audio file to a .wav file. Here is my code: http://pastie.org/719546
Step 4:
Download and install the LAME encoder. This will install both the libmp3lame library and the lame command-line utility.
Step 5:
Now you can peruse LAME's API or just fork & exec a process to lame to convert your wav file to an mp3 file.
Step 6: Bring out the champagne and caviar!
If there is a better way (I'm sure there is) to do this, please let me know. I personally have never seen a step-by-step roadmap like this so I thought I'd put it out there.
For converting between various formats (except MP3) check libsndfile http://mega-nerd.com/libsndfile/
Libsndfile is a library designed to
allow the reading and writing of many
different sampled sound file formats
(such as MS Windows WAV and the
Apple/SGI AIFF format) through one
standard library interface.
During read and write operations,
formats are seamlessly converted
between the format the application
program has requested or supplied and
the file's data format. The
application programmer can remain
blissfully unaware of issues such as
file endian-ness and data format
It is also simple to use, with the API following the style of the Standard C library function names:
http://mega-nerd.com/libsndfile/api.html
And examples are included in the source distribution.
For actual audio output, another library will be needed, SDL as already mentioned might be a good place to start. While SDL can also read/write audio files, libsndfile is far superior.
If your curious about DSP and computers, take a look at the Synthesis Toolkit. It's sweet. It's designed for learning. The examples and tutorials they have on their website are straightforward and thorough. Keep in mind, the guys who wrote it, wrote it so they could create acoustic models of real instruments. As a result, they've included some instruments that are just plain wacky, but fun. It will give you a core understanding of processing PCM sound. And you'll probably be able to hack together some fun little noisemakers while your at it.
https://ccrma.stanford.edu/software/stk/
Check libmad http://mad.sourceforge.net " "M"peg "A"udio "D"ecoder library", should provide a good example.
Also for an easy cross-platform audio handling, check SDL http://www.libsdl.org/.
Hope that helps.

Can one decode real media file to other media formats using any free C++ library?

Please provide some pointers on how to convert real media formats to other popular media formats using some C++ sdk(I guess Helix provides one but don't know how to use it).
I am a total newbie in the above area, any help would be highly appreciated.
libavcodec (a library behind ffmpeg and other heavily-used programs) supports some common Real video formats. See this page; the tutorial itself is obsolete, but there are linked updates such as An ffmpeg and SDL Tutorial
Helix may be an option, but keep in mind the actual Real Video codecs are only available as binaries, while libavcode is fully open source.
Don't expect this to be trivial, whichever library you use.