One of my friends wants to write lower-level audio tools, and a real-time audio engine, like FMOD, for games. He went to art school for sound effects, and he's learning how to program.

We both agreed he'll need to get a solid understanding on how programming works, so he's learning C++. What I'm suggesting for the next step is to learn how to load and uncompress raw audio samples from a file format (most-likely wav or ogg to start with a library), and use OpenAL to send loaded audio samples to the audio hardware to play it.

The thing is, he wants to write custom sound effect algorithms to manipulate sounds in a 3D environment similar to FMOD as mentioned above. This means he needs direct access to modify these raw audio samples somehow in real-time --which, of course, gets copied into an OpenAL buffer like an OpenGL texture would. Is there a way to flexibly manipulate samples already loaded into an OpenAL buffer?

What he'll start off doing is writing things like a sound interpolation, a way to scale volume to specific sounds, etc --basic stuff. Then, he will move into more complex stuff (I have no clue what he's planning). Whatever he ends up doing, it will involve tons of calculations on a massive amount of samples, which will be very CPU-intensive. Are there any APIs out there where he can write code that'll run directly on the audio hardware similar to shaders on the GPU? It doesn't look like OpenAL offers functionality like that.

No there will be no shader like manipulation languages for audio. All the audio calculations will probably have to be run through the CPU. As far as manipulating the audio he will probably need to write algorithms to manipulate the raw audio bits that are stored in the buffer. This is rather low level programming but there may be api's out there that glue well with OpenAL to get the effect you want I am not sure. The best thing is to take a step back and do some research on audio manipulation. Some tools like audacity allow you to apply filters to the audio to get different effect and it may be worth looking into the source code of the project to see how they implement the filters. From a general perspective audio is nothing more then voltages converted to audible tone frequencies so without a doubt he is going to have to get good experience with the lower level details of development because at some point he will have to bit twiddle.

He hasn't gotten to any audio processing stuff, but I suspect it'll go that direction.

As far as accessing hardware for audio processing, I'm not aware of any APIs. If the requirements are not realtime, you could use GPU programming APIs like C++ AMP, DirectCompute, OpenCL or CUDA, but for real-time DSP/audio mixing, the latency of communication back and forth across the PCIe bus is probably too high for now, given the typically small chunks of audio you will process in one go. We might see this change soon, as hardware and APIs evolve to support a shared address space (and especially if your CPU has an integrated GPU that can be used), but its not here yet.

Its not so bad as it seems though -- Since Vista the entire Windows audio stack has been processed on the CPU, and fancy audio cards are becoming less and less necessary. Some of the less expensive of the fancy audio cards are little more than high-quality DACs and analogue components. With multi-core PCs commonplace, its entirely possible to throw even an entire hardware thread at audio.

Its not so bad as it seems though -- Since Vista the entire Windows audio stack has been processed on the CPU, and fancy audio cards are becoming less and less necessary.

I think this is an upside-down way to look at it. Due to underinvestment (arguably caused by Creative's near-monopoly over PC audio for a decade or so), audio hardware and APIs haven't progressed at even a tenth as much as graphics have since the 90s. In theory audio is a much better candidate for parallelization and moving to dedicated hardware than graphics is:

Audio is almost always write-only, unlike graphics where several advanced applications require reads.

Separate streams of audio data typically do not interact at all until the final mixing stage, which is purely additive. This simplifies the hardware and software design.

Audio rarely needs any conditional values that can stall or complicate the pipeline.

Audio data is homogeneous throughout the pipeline; no need for several different types of shaders (or indeed different types of processor, if that still applies to anything on the market) and the instruction set would be simple.

Expensive audio operations such as convolution are still too expensive to perform well on CPUs but are trivial to implement in hardware. You could 'throw even an entire hardware thread' at it and still have nowhere near enough power to handle good reverb for 10 to 20 sound sources.

As well as the commercial applications in games, it could be useful for pro audio as well.

Ideally what we'd have is more and better audio hardware, not a plan to push it back onto the CPU - especially not at a time when CPUs are ceasing to improve much year on year.

I perform real-time DSP processing using CUDA for signal processing applicatiions. My application are in 5-10 mega sample range, much high than audio sampling without much problem. Depending on what algorithms you want to run it really should not be a problem. Using CUDA you can directly write from the PCIe audio capture device to the CUDA GPU without going thourgh CPU memory as of CUDA 4.x. This cut down on the latency. The best latency you will see is around 5ms.

Well, I never meant to suggest that the lack of audio hardware was a great state of affairs -- just that the OP's buddy isn't any worse of than anyone else, including Microsoft.

There were actually a couple of guys proposing programmable sound hardware, and they had gone as far as to implement a library that provided "sound shaders" running on the CPU.

Today, I don't think anyone's going to make the investment, frankly. It'll probably be very soon when we can just dedicate some idle GPU resources with low enough latency to be effective -- sooner than it would take to reintroduce and popularize audio processors, anyhow. The next-gen consoles don't appear to include that kind of audio hardware either, but likely will support low-letency offload to GPU, so it looks like the next 6-10 years or so are already settled.

It *would* be nice, but alas it looks like a problem who's opportunity has passed.

I know very little about audio programming other than "makin' it work" so take this with a pinch of salt ;)

I have been interested in the idea of audio shaders but since sound from a file is always going to be the same, does it have as much need for a shader to alter it at runtime? It can be manipulated offline on the development machine, then stored and ran at runtime. I always assumed this was why sound shaders wern't used.Similar to why you might not pass a video through a shader during your game. (Novelty video effects in VLC aside ;)

Graphics however is generated on the fly (i.e depending on how the player moves and interacts) so I guess this is why each frame needs to be shaded.

EDIT: Nope, I have thought about this further and decided that the same .wav file (i.e shooting) might want to be used whether you are underwater or not. In which case a shader to make it sound muffled would be great.

since sound from a file is always going to be the same, does it have as much need for a shader to alter it at runtime?

That's the exactly the problem - because we don't have much technical advancement in audio, people find it hard to even imagine that you'd want to do anything other than just play back a sound. It's like we're back in the days of 2D graphics when graphics was literally "copy this picture offscreen to a position on-screen" - but there was always the capability for more. Examples:

Muffling sounds underwater, as you said

Muffling sounds that are occluded by other surfaces (eg. listening outside a door)

Reverberation based on the room's physical characteristics (this already exists on some hardware, though only on a crude level)

Echoes based on the room's physical characteristics (which is distinct from reverberation in terms of implementation)

Pitch and amplitude variations to allow re-use of one sample without it sounding too repetitive (eg. footsteps)

Dynamic compression of all playing sounds so that music automatically gets quieter so that speech can be heard (basically HDR for audio)

It'll probably be very soon when we can just dedicate some idle GPU resources with low enough latency to be effective

Unfortunately I don't think the latency will be low enough. GPUs are considered to be doing well if they can generate a frame in 16ms and that assumes that there are no frames being buffered. 16ms is a long time in audio, and 33ms is an especially long time. Admittedly I don't know much about how GPUs will handle non graphical data and whether it will be affected by frame rates, frame buffering, and alternate frame rendering, but I would want to see an actual definition of "low latency" rather than just assume it will be sufficiently low.

Do you really need low latency? The time that would be critical is when a button press generates a sound that you want to process. Even in this case, a small delay may not be noticeable. For everything else, you can process the sound in advance, and buffer it to keep the audio steady.<br /><br />So to the OP, your friend should look into learning CUDA and OpenGL in addition to C++.

It'll probably be very soon when we can just dedicate some idle GPU resources with low enough latency to be effective

Unfortunately I don't think the latency will be low enough. GPUs are considered to be doing well if they can generate a frame in 16ms and that assumes that there are no frames being buffered. 16ms is a long time in audio, and 33ms is an especially long time. Admittedly I don't know much about how GPUs will handle non graphical data and whether it will be affected by frame rates, frame buffering, and alternate frame rendering, but I would want to see an actual definition of "low latency" rather than just assume it will be sufficiently low.

Phil126 above said he's getting 5ms latency, best case, on current hardware, going between graphics and audio devices directly with no CPU intervention, but across PCIe bus still. I agree latency is *the* issue, but I think we already can see low-enough latency, and that it'll be commonplace soon enough. I suspect that the PS4 and XboxNext will be able to support sufficiently-low latencies to make it possible, which will mean that it'll have 5-10 years of incubation if true, plenty of time for the PC to catch up. At AMD's Fusion Developer conference last year they started pushing their HSA aliance, and began talking about how the PCIe bottleneck is a huge issue for certain classes of problems (namely, latency-sensitive, smallish-data problems--like audio), and what they're doing to fix it.

Do you really need low latency? The time that would be critical is when a button press generates a sound that you want to process. Even in this case, a small delay may not be noticeable. For everything else, you can process the sound in advance, and buffer it to keep the audio steady.<br /><br />So to the OP, your friend should look into learning CUDA and OpenGL in addition to C++.

Latency is super important in audio because we're far more sensitive to audio latency and audio anomolies than visual ones. Our vision systems do a lot of work that we're not even aware of to reconstruct the image we percieve, and it fills in and smoothes over incomplete information. In particular, latency is essential for making audio match the visual info that we percieve in time, and to ensure gapless playback with dynamic mixing of sound elements. Having written audio playback code on an embedded system, I can attest that playback gaps of even a few CPU cycles is quite noticable. In rendering you can (and in fact, regularly do) miss entire frames of information, but with audio you'll notice even when a 'frame' arrives late.

Ravyne, on 08 Mar 2013 - 14:25, said:
Latency is super important in audio because we're far more sensitive to audio latency and audio anomolies than visual ones. Our vision systems do a lot of work that we're not even aware of to reconstruct the image we percieve, and it fills in and smoothes over incomplete information. In particular, latency is essential for making audio match the visual info that we percieve in time, and to ensure gapless playback with dynamic mixing of sound elements. Having written audio playback code on an embedded system, I can attest that playback gaps of even a few CPU cycles is quite noticable. In rendering you can (and in fact, regularly do) miss entire frames of information, but with audio you'll notice even when a 'frame' arrives late.

Yes, but playback gaps can be avoided by having a buffer. Audio response to user input needs to be quick, but that's still different from noticing a change in sound; it ties in to how precisely we perceive our own motion.

In order to have quick dynamic response, the buffer is typically very small, maybe a few hundred samples wide. And you've got to have the next one ready before the previous one runs out. You can't just repeat the last frame and catch up later like you can with video. Increase the size of the buffer, and you either introduce more latency, or you throw it out and recompute everything whenever a new sound comes in, the environment dynamics change, etc.

Buffers never get very big -- even at 30fps video, and 96ksps audio (which is over 2x CD quality) you've got just over 3k samples per video frame. At 60fps video and 48ksps audio, you've got just 800 samples per frame.