A simple-to-use sound file writer

Steve Holden steve at holdenweb.com
Thu Jan 14 09:51:02 EST 2010


Alf P. Steinbach wrote:
> * Steve Holden:
>> Alf P. Steinbach wrote:
>>> Just as a contribution, since someone hinted that I haven't really
>>> contributed much to the Python community.
>>>
>>> The [simple_sound] code will probably go into my ch 3 at <url:
>>> http://tinyurl.com/programmingbookP3>, but sans sine wave generation
>>> since I haven't yet discussed trig functions, and maybe /with/ changes
>>> suggested by you?
>>>
>> I wouldn't hold back on the sine wave just because it would represent a
>> "forward reference". That's OK sometimes. Why not just put a comment in
>> to the effect that "The sine wave is created using a function from the
>> math module, which we'll be looking at in ..."?
>>
>> Since the sine is the basis for all other waveforms its omission would
>> seem more than a little strange to anyone knowledgeable about audio, for
>> example.
> 
> I don't know very much if anything about audio. For example, in the code
> what I called "sawtooth" wave is really "triangle" wave. The sawtooth is
> simpler, what I called "linear" in the code.
> 
> And if you wonder, I was just checking the terminology now before
> starting to write it up... Perhaps should have done that before posting
> code. But once I got the idea of posting it I just posted it.
> 
> Anyway, as I recall any wave can be decomposed into sine waves, or
> square waves, or almost whatever kind of waves. Think about a square
> wave of frequency f and one of frequency 3f and perhaps third the
> amplitude (not sure), combined that's already a good start on a sine
> wave. With some ugly staircasing but hey. And as a matter of programming
> practicality, a triangle wave sounds almost like a sine wave. It's just
> a little more edgy or "hairy", a slight buzz.
> 
It's not clear to me that you can approximate any waveform with a
suitable combination of square waves, though I admit the idea has
intuitive appeal. But I know beyond a shadow of a doubt from my
education that any periodic function with a fundamental frequency f can
be approximated to whatever desired accuracy by the sum of sine and
cosine waves of frequencies Nf (N = 0, 1, 2, 3, ...) of appropriate
amplitudes (the frequency 0 component allows you to insert a "DC shift"
for waveforms that aren't symmetrical about zero). I seem to remember
the Fourier's theorem was the fundamental proof.

There is a very pretty discussion of all this, if you are mathematically
inclined, in

  http://press.princeton.edu/books/maor/chapter_15.pdf

with a specific example for the sawtooth waveform.

I would definitely recommend renaming the waveforms you *do* use to
conform with accepted terminology. This will reduce reader confusion.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list