Generate music & sound in code?

Started by
6 comments, last by Wooh 14 years, 1 month ago
Hello! I want to create music and sound effects for my games by using code. I don't want to record anything. I know how to create simple beep sound but I want something more than that. I want sound that sounds different like drums, flute or piano. It doesn't have to sound exactly like a real instrument as long as it sounds good enough. I think what I need to know is the graphs of the sound waves and how to describe them in code. Does anyone know anything about this or know any tutorials?
Advertisement
Look up Pulse-code Modulation. (PCM). It is the type of data that you actually send to be processed. Besides that, I have no other tips. I would say likely recording is your best bet, even if you record a single 'drum beat' and then play it back using code inside your games. Creating such data from scratch would be necessarily hard - and may not come close enough to the sound you are aiming for.
I believe sound card support for MIDI is alive and well. It'll take just a bit of effort programming the interface but MIDI lets you select from a large assortment of intstruments (several types of drums, types of strings, brass instruments, etc) played simultaneously.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

there is also the fmod i think, that lest you take audio patches and play them in a simular manner as midi
0))))))>|FritzMar>
Charles Petzold's book "Programming Windows" devotes a chapter to waveform audio that includes sample code for generating synthesized audio. It's very basic code. Iirc, it generates a single frequency sound (e.g. 440 Hz or A on the keyboard). It might disappoint you, however, because most of the sounds you hear in life are composed of many frequencies and are subject to dampening and other effects and if you're not familiar with differential equations you probably don't want to attack the problem at such a low level. MIDI is a better route to pursue.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Here's an example of the waveform of a snare drum:

http://deeperunderstanding.org/student_waveforms/wf_darryl.html

Some instruments produce very complicated sounds. Others are simpler. You "represent this in code" as data - you'll establish some 'sampling rate' and you'll have an amplitude for each sample.
If you're really interested in how sounds can be generated, check out the "Synth Secrets" series, available for free online.

Be warned, however, this is something which could take years to learn, especially if you want to simulate complex, natural sounds like strings.

Various techniques are applied - often sounds are created by combining basic waveforms (sine, triangle/saw, square) at various frequencies and amplitudes. Other sounds, like drums, are often created from short bursts of noise/static. Filters, amplitude envelopes and modulation (chorus, phaser, reverb etc) are all required to make a convincing set of instruments.
Thanks for all answers. Yes making something that sounds realistic might be hard. And slow ..

I think I can go for something much more simple. I just wanted different kinds of instrument sound to give more variation and be able to give a feeling. Maybe it's enough to just have small variations like vibration, fading and such.. well, I don't know much about creating music so I guess it will be hard to compose something good despite what sound I have. But trying won't hurt ;)

About sound effects like footsteps, ringing phone, success sound and so on.. I think this is simpler because they do not involve any composing and creative stuff. Maybe the sound effects are more complicated than than tones.. I don't think all are. Like flowing water, it sounds almost like white noise but there is something more to it than that. Maybe white noise will do but it is such easy things I like to get some input on. I have noticed that it's easy to find effects that would fit in an Alien/Space game. You just get these kind of effects by doing something wrong :p Too bad I don't want to make space games.

I'm not very picky. I think I can satisfy with how it sounded over 20 years ago. But pure square waves can be a little irritating on the ears.. I'm sorry if it sounds like I don't know what I want. I don't think I do exactly.

This topic is closed to new replies.

Advertisement