tags:

views:

2312

answers:

11

My degree was in audio engineering, but I'm fairly new to programming. I'd like to learn how to work with audio in a programming environment, partly so I can learn C++ better through interesting projects.

First off, is C++ the right language for this? Is there any reason I shouldn't be using it? I've heard of Soundfile and some other libraries - what would you recommend?

Finally, does anyone know of any good tutorials in this subject? I've learnt the basics of DSP - I just want to program it!

EDIT: I use Windows. I'd like to play about with real-time stuff, a bit like Max/MSP but with more control.

+1  A: 

On windows look up tutorials on DirectSound.

On Linux there are a variety of libraries and methods to access sound cards, the easiest of which is simply open a file to the device, and use ioctrl to set the speed, etc.

If you give more information on the environment, we might have better recommendations.

Adam Davis
+2  A: 

It really depends on what kind of audio work you want to do, If you want to implement audio for a game, C++ is sure the right language. There are many libraries around, OpenAL is great, free and multiplatform. I also used DirectSound and Fmod with great sucess. Check them out, it all depends on your needs.

MissT
The waveOutWrite win32 API is easier to work with than DirectSound in my experience, even when DirectSound is more powerful. (Also, DirectSound has been superseded by XACT, AFAIK).
Jasper Bekkers
+1  A: 

If you already learnt the basics of DSP, I would recommend Analog Devices Blackfin Processors. You can use both C and C++, and they provide a number of Audio examples to get you started. They supply enough code to read audio into the board, process it, and play it back out.

From this you can apply any types of filters or other processing you wish. I used the BF533 Ez-Kit Lite in combination with a serial LCD Panel to make an Audio Equalizer for a project once. Was a great learning experience and a lot of fun!

Sean
+2  A: 

Check out Audacity. It is a cross-platform (Linux, OS X, and Windows) open-source audio editor written in C++. I don't know what level your GUI skills are at, but the code that deals with audio work should be fairly easy to distinguish from the wxWidgets (GUI) code.

Chris Lutz
+1 for audacity, voted the best open source audio editing software.
gath
+6  A: 

Is C++ the right language? That depends entirely on the context. If you need to plug your code into another application, framework or whatever else, you probably have to use whatever language that framework is made for.

If you're making your own free-standing app? Probably not. There are several reasons why I'd suggest you go with another language:

  • C++ isn't well suited for beginners. If your goal is to learn programming through these projects, go with a language that is more amenable to "learning by doing". C++ is full of pitfalls and things that "seem to work", until one day they don't. Add to this a horribly complex syntax and some pretty complex idioms and techniques you have to master to work around quirks of the language that simply are non-issues in pretty much any other language, and you have a good source of bugs and frustrations, and not much more.
  • C++ has no concept of "audio". C++ doesn't come with any kind of support for recording, processing or playing audio, loading or saving audio files or anything else. Most modern languages have some form of audio support built into the standard language (at least the ability to load and play mp3 files and other such basics). In C++, all of this will have to be done through third-party libraries and OS-specific code.

If you have a real choice in language, I'd say go for something like Python or C#. They're easier to learn, come with a lot more functionality out of the box, and make it a lot harder to shoot yourself in the foot. I wouldn't recommend C++ to someone who isn't already an experienced programmer.

jalf
Just remember that audio-processing is a generally realtime process in which you have to preform complicated (FFT) calculations in an order of milliseconds because you need to queue up the audio. If you're too late you'll hear noticeable artifacts.
Jasper Bekkers
+1  A: 

I don't have all that much experience in creating audio apps for myself - but if I was in your position I'd play around with creating VSTi's first (virtual studio technology instrument). As far as I know, Steinberg have released a C++ SDK for this and I reckon it would be a good starting point if you'd like to get into sound design applications

Ciaran
+1  A: 

If you want to focus on the actual sound processing (as opposed to all the crud that GUI applications are saddled with), I'd check out CSound. That gives you a textfile based langugae that allows you to build almost any audio device virtually from what they call opcodes, but also you have the source code which you can delve into to see the actual algorithms that the opcodes represent.

U62
+2  A: 

Dont listen to me! I might be wrong!

I do not program C++, but agree with Jalf in it being difficult for beginners, there are lots of things that have to be done manually that other languages will do for you automatically, such as memory allocation, garbage collection etc. they are also right in saying that C++ does not know audio, and that other languages have audio capabilities built in from scratch. However, generally, most of these are fairly basic capabilties: opening and playing files, sending the playhead to different parts of the file, adjust panning and volume, maybe extracting eq data.

But...Im guessing that you want to get a little deeper and actually shape the sound, whether through synthesis or effects, and that you want to make these things instead of just add them. I would encourage you to look at VST and VSTi, but this is c++ area (JUCE library, awesome awesome awesome), and hard hard work, but if you want to make your own product, then it might be the way. otherwise, as others have suggested CSound, Max, Cycling, and maybe Processing might be of interest. software like Reaktor allow you to patch together modular componenets with a lot less (if any) coding.

go and have a look at some of the forums on KVRAudio, there are a few heroes there who developed old classic freeware like Hammerhead, and then other indie software like Fruity Loops, Rebirth, Tuareg, Audiomulch etc.

I think learning about VST and C++ is the best, but it will be a long road with pain and anguish, and unless you wish to create a stand alone piece of software or a plugin for another piece of software (apart from Csound etc), it will be dissapointing.

I think.

Assembler
+5  A: 

If you do decide to use C++, then The Synthesis Toolkit is worth noting. I use it for a schoolproject and it is very usable, developed at stanford university, crossplatform (win, mac and linux), free and opensource. An extract from the wikipedia page on it:

Versions of the STK instrument classes have been integrated into ChucK, Csound, Real-Time Cmix, Max/MSP (as part of PeRColate) and SuperCollider

They have a lot of testcode included + on the site are some tutorials to get started with their library. (But you do have to know some C++ of course)

STK has some classed to read/write audio files (and matlab files), realtime audio/midi io, some basic algorithms (usage based on similar functions out of matlab), ...


Another obvious option and probably THE most used environment to learn/test dsp stuff is Matlab (or free octave, but i havent used it before). I sometimes test stuff out in Matlab before implementing it in C++.

Emile Vrijdags
Matlab was my first experience of coding! I probably wouldn't be here now (on Stack Overflow that is!) if I hadn't been taught DSP in Matlab at Uni. I've still got an old copy - maybe I should play about with that first... good idea.
Skilldrick
A: 
sness
A: 

C might be a better choice than C++ for audio, particularly if you want to do any DSP.

  • Most practical audio and DSP reference books use C for code examples
  • Most audio/DSP libraries are written in C (or at least have a C API)
  • C++ greatly increases the learning curve compared to C but doesn't add a lot of benefit for audio/DSP work (OOP is not particularly relevant for low-level/real-time code)

There's nothing "wrong" with learning C++ of course and it won't stop you understanding and using C code (since C++ is mostly a superset of C) - it's just that you'll be up and running a lot quicker with C, and you can always move on to C++ later if you decide to.

Paul R