I am working on an interesting project relating solving mathematical problems and music. It is easy to generate a specific note (or tone):
ton = amplitude * sin(2pi * frequency * time / samplerate)
I'm working in python, and have code to do this much from http://code.google.com/p/scitools/.
def note(frequency, length, amplitude=1, sample_rate=44100):
time_points = numpy.linspace(0, length, length * sample_rate)
return numpy.sin(2 * numpy.pi * frequency * time_points) * amplitude
Of course, in real music, there are generally multiple tones being played during the same time step. I tried to do this by generating then summing two tones, i.e.:
twotone = note(440, 2)+note(261.63, 2)
but this just gives crap. How would I mathematically encode more than one simultaneous tone?
you should half the individual amplitudes when using two tones, or the total amplitude potentially doubles. If you use more than two, you should mix in a ratio that reflects their relative volume, with a total amplitude of one.