C++ audio/wav files

Hellp guys,
is there any library that I can use in order to open, close, manipulate and write a new wav file in c++?
Thanks in advance!
Last edited on
https://github.com/adamstark/AudioFile
https://github.com/audionamix/wave

A quick google gives you so many hits... is there some specific feature you need?
Last edited on
In fact I want to open two wav files, store some elements into an array and get the convolution them and create a third wav file. I've already implement my convolution function and now I need to figure it out how to do the other part. I also wanna do this with a simple way.
Last edited on
Well, using a pre-made library like the ones mentioned in this thread and the previous thread will probably be the simplest way you can do this. Header-only libraries don't even require separate building.
Well, how can I use them? I have no idea how to do it. I'm working on codeblocks. How can I add them?
Last edited on
See jonnin's first link. Read the description of the README.md

The README has example usage.
You download the header file (AudioFile.h), and then put it in the same folder as your .cpp file.

Then, in your .cpp file, you #include "AudioFile.h", like in the usage example.

You can open a wav file like:
1
2
AudioFile<double> audioFile;
audioFile.load ("/path/to/my/audiofile.wav");


You can access the samples directly like:
1
2
3
4
5
6
7
int channel = 0;
int numSamples = audioFile.getNumSamplesPerChannel();

for (int i = 0; i < numSamples; i++)
{
	double currentSample = audioFile.samples[channel][i];
}


After modifying samples or doing whatever processing you can save another wav file:
1
2
// Wave file (implicit)
audioFile.save ("path/to/desired/audioFile.wav");
Last edited on
Ok I got it. Thank you so much guys. What about the copyright thing? Am I allowed to use that .h file? Is is legal
Last edited on
Actually, now that you mention it, the AudioFile library is GLPv3, which is a pretty strong 'copyleft' license. Yes, you can still use it, but if you ever distribute your project or put it online, you must make your source code available for others to use, and have a copy of the license distributed as well - the same license that's in the LICENSE file on the GitHub page.

The audionamix/wave project that jonnin also linked is the MIT license, which is a lot more permissive.
A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

However, the audinamix/wave project is not a simple header-only file. You need to build it or manually integrate the files with your project.

We don't know what your history with coding is. Has your professor gone over the WAVE file audio format, or shown you how to read/write audio files?
Like I said before in https://cplusplus.com/forum/beginner/274267/, you can do all this yourself, but you need to read up on how to read/write binary files, and how a WAVE file is formatted, which might be a daunting task for a beginner.
http://www.cplusplus.com/articles/DzywvCM9/

The license of the project FurryGuy linked in the previous thread is also very permissive, and you can easily use it.
https://www.codeproject.com/articles/29676/cwave-a-simple-c-class-to-manipulate-wav-files
Last edited on
this is stupidly over simplified (and not everything i say is always true, but usually), but the concepts are here, the details are on the web.
- a library is just normal code, without a main program.
- you compile it with some compiler settings or IDE flags that tell it not to look for main, and that it is, in fact, a library.
- it will compile as normal into something appropriate for your operating system, eg .lib for windows. The binary compiled result is usually OS specific, same as an executable is.
- you can then tell the linker in your project that you want to use this binary file as if it were code. To do that, most often, you have the .h files for the library in your project and call the functions listed in that as if you had the source code for them there, too. But you don't: they are in the compiled file. The linker will resolve this when you compiled YOUR code, and the calls the those functions will work. In visual studio, it is literally as simple as adding a .lib file to the project and the .h file, and it all just works.

- some libraries are stupidly over complicated to build and link. Cough, boost, cough. It may take reading some documentation and fooling with it to get them working. Unix/linux etc are notorious for forcing the user to screw with the makefile before it will work.
- many more popular libraries, esp the popular ones that are complicated, have prebuilt compiled versions you can download instead. This can save hours or even days (rarely) off your efforts.

Hopefully that gets you the 2 second gist of what its all about.

As said, watch out for those darn license types. If its a home project for you, no one cares, but as soon as you give it out or sell it, these details become important.
Last edited on
I'm answering to Ganado

First of all, I've copied the code of that link https://github.com/adamstark/AudioFile and stored it into a .h file. Inside of the code it is mentioned the author and some other stuff. It is ok or I must include something else? Also, I don't think that my project wil published somewhere. I'll just send it to my professor to grade it.

Secondly, I would describe my self as a medium programmer (If I can called it like that, haha). I mean I have some experience with c, c++ and Java so far (Note that the project asked to implement in c/c++). But this is the first time that I'm called to implement something like that in any of these languages.

Thirdly, I've sent an email to the professor's assistant and she said that "There are a lot of libraries online that provide some tools and functions that will help you to read and create wav files". Also, he didn't show us how to do it. This is the first time that I will have to use a library that is not provided from codeblocks and I have no idea what am I supposed to do. So I guess I have to search all over the internet and find that library and then import it to my project.

To be honest I forgot that I've already ask something here. So I didn't check your answer. Sorry :D.
Last edited on
That's fine, it sounds like you're definitely expected (or at least are allowed) to use a third-party library, so you should be fine.

First of all, I've copied the code of that link https://github.com/adamstark/AudioFile and stored it into a .h file.
I assume you're talking about the AudioFile.h file, right? Yes, just download/copy that file verbatim.

Try to just get the simplest example possible to compile, first:
1
2
3
4
5
6
#include "AudioFile.h"

int main()
{
    AudioFile<double> audioFile;
}


Then, add more stuff onto it.
I'm answering to jonnin

Well, like I said to Ganado that is a project for the university. I'm not gonna sell it or publish it anywhere. I'll just give it to my professor just to grade my code for the winter semester.

I'm just need to import to my project two wav files then store some elements into two vectors, then give those vectors as a parameters into the convolution function that I've made.

I think the hard part is to find the correct library to use. I said before that is the first time that I have to use a library that is not provided from codeblocks and that's challenging.

Thank you for helping me!
Last edited on
Ganado

Yes, I opened the AudioFile.h and I've copied everything from it.

Okay, I'll definitely do it. I forgot to thank you before. So thank so much you helped me a lot. I hope you'll be fine! If I'll need anythinf else I will ask again here and hope you'll answer to me again.

Oh, Ganado one more thing. I should mention into my essay that I'm using that library and give the link, right?
Last edited on
In my college, we would send the TA or professor a zip file with all source code and a makefile within the zip. You'd have to ask your instructor how he or she wants you to hand in your project.
I've done a fair amount of low-level audio work, including classes, which might help make things clearer to you. You can find me at github.com/lbrandewie, you'll want the "C++ Audio Files" repo. HTH.
yes, give proper credit. you can probably google the current academic standard for software credit? Usually in the real world the link and name of the package and maybe authors would do but some school types want a formal format.
Last edited on
Topic archived. No new replies allowed.