A simple-to-use sound file writer

S

Steve Holden

Alf said:
* Lie Ryan:

You can't get it more trivial.



First, the (very very trivial) algorithm I posted does *not* do that: by
itself it represents a sine wave, not an arbitrary wave form.

And second I'm not about to write Fourier transform code to satisfy
someone who refuses to do a milligram of thinking.

The Fourier part stuff needed to do what you're requesting is
non-trivial, or at least it's some work to write the code.



I don't believe any of what you write here.
Well, it seems quite reasonable to me, but then I'm not the one being
challenged to write a trivial algorithm.

I will, however, observe that your definition of a square wave is what I
would have to call a "'square' wave" (and would prefer to call a "pulse
train"), as I envisage a square wave as a waveform having a 50% duty
cycle, as in

___ ___
| | | |
| | | |
| | | |
+---+---+---+---+ and so on ad infinitum, (though I might allow you
| | | | to adjust the position
| | | | of y=0 if you want)
|___| |___|

as opposed to your

_
| |
| |
______| |______ ______
| |
| |
|_|

So I can see how we might be at cross purposes. I could cite authorities
for differentiating between a square wave and a symmetric pulse train,
but will content myself with observing only that my impression is the
common definition of an ideal square wave (one with a zero transition
time) admits of only two instantaneous values, eschewing the zero you
use. If that is the case, we could perhaps agree that we differ merely
on terminology.

Or, best of all, you could show me how to synthesize any waveform by
adding square waves with a 50% duty cycle. Then I *will* be impressed.

not-wriggling-real-ly y'rs - steve
 
A

Alf P. Steinbach

* Steve Holden:
Well, it seems quite reasonable to me, but then I'm not the one being
challenged to write a trivial algorithm.

You're again into innuendo, misleading statements and so forth. Lie Ryan's
challenge is nothing but trivial, because it's about implementing very much more
than the algorithm. I did implement the algorithm for him, in Python, and posted
that in this thread.

I will, however, observe that your definition of a square wave is what I
would have to call a "'square' wave" (and would prefer to call a "pulse
train"), as I envisage a square wave as a waveform having a 50% duty
cycle, as in

___ ___
| | | |
| | | |
| | | |
+---+---+---+---+ and so on ad infinitum, (though I might allow you
| | | | to adjust the position
| | | | of y=0 if you want)
|___| |___|

as opposed to your

_
| |
| |
______| |______ ______
| |
| |
|_|

Try to read again, a sufficient number of times, how to generate the latter by
summing *two instances of the former*.

I'm sorry to say this but I find it hard to explain things simple enough for
you, because at the level of 2+2 any explanation is far more complex than the
concept itself.

That is, of course, a challenge to me! :)

So, thanks for challenging my pedagogical abilities.

I know they're not as good as they should be, and need some exercise!

So I can see how we might be at cross purposes. I could cite authorities
for differentiating between a square wave and a symmetric pulse train,
but will content myself with observing only that my impression is the
common definition of an ideal square wave (one with a zero transition
time) admits of only two instantaneous values, eschewing the zero you
use. If that is the case, we could perhaps agree that we differ merely
on terminology.

No, we don't differ on terminology: we seem to differ in that one of us has
severe difficulties understanding the very simplest things, such as what graph
one gets by summing two squares waves of same frequency but out of phase.

The one of us who has trouble understanding that is also apparently too lazy to
try out the earlier advice given him of graphing this on a piece of paper, and
instead prefers to spout innuendu, personal attacks and misleading statements.

That's a real challenge to the other person.

Or, best of all, you could show me how to synthesize any waveform by
adding square waves with a 50% duty cycle. Then I *will* be impressed.

You would, yes?

Perhaps you'd also admit to being wrong, and retract your innuoendo etc.?

Well, just epress the waveform as a sum of sine waves (Fourier transform);
synthesize each sine wave by a set of 50% duty cycle square waves (I've earlier
posted Python code for that in this thread); add all the square waves. The hard
part of this is implementing the Fourier transform, I leave that to you. ;-)


Cheers & htfh.,

- Alf
 
S

Steve Holden

Alf said:
* Steve Holden:

You're again into innuendo, misleading statements and so forth. Lie
Ryan's challenge is nothing but trivial, because it's about implementing
very much more than the algorithm. I did implement the algorithm for
him, in Python, and posted that in this thread.



Try to read again, a sufficient number of times, how to generate the
latter by summing *two instances of the former*.

I'm sorry to say this but I find it hard to explain things simple enough
for you, because at the level of 2+2 any explanation is far more complex
than the concept itself.

That is, of course, a challenge to me! :)

So, thanks for challenging my pedagogical abilities.

I know they're not as good as they should be, and need some exercise!



No, we don't differ on terminology: we seem to differ in that one of us
has severe difficulties understanding the very simplest things, such as
what graph one gets by summing two squares waves of same frequency but
out of phase.

The one of us who has trouble understanding that is also apparently too
lazy to try out the earlier advice given him of graphing this on a piece
of paper, and instead prefers to spout innuendu, personal attacks and
misleading statements.

That's a real challenge to the other person.



You would, yes?

Perhaps you'd also admit to being wrong, and retract your innuoendo etc.?

Well, just epress the waveform as a sum of sine waves (Fourier
transform); synthesize each sine wave by a set of 50% duty cycle square
waves (I've earlier posted Python code for that in this thread); add all
the square waves. The hard part of this is implementing the Fourier
transform, I leave that to you. ;-)
Finally, I think, the penny is beginning to drop. Give me a little more
time to think about it and I can see I might be willing to write the
words "I was wring". Shame about my typing, isn't it? ;-)

regards
Steve
 
S

Steve Holden

Alf P. Steinbach wrote:
[...]
Perhaps you'd also admit to being wrong, and retract your innuoendo etc.?
Disregarding any matters of right or wrong (for this post, at least), I
herebe retract anything I have said about you that you consider
innuendo. Feel free to remind me what that was.

regards
Steve
 
S

Steve Holden

Alf P. Steinbach wrote:
[...]
Perhaps you'd also admit to being wrong, and retract your innuoendo etc.?
Disregarding any matters of right or wrong (for this post, at least), I
herebe retract anything I have said about you that you consider
innuendo. Feel free to remind me what that was.

regards
Steve
 
A

Alf P. Steinbach

* Steve Holden:
Alf P. Steinbach wrote:
[...]
Perhaps you'd also admit to being wrong, and retract your innuoendo etc.?
Disregarding any matters of right or wrong (for this post, at least), I
herebe retract anything I have said about you that you consider
innuendo.
OK.


Feel free to remind me what that was.

It would IMHO serve no good purpose to reiterate that. At this point, let's just
start fresh.


Cheers & hth.,

- Alf
 
S

Steven D'Aprano

You're again into innuendo, misleading statements and so forth. [...]
[Steve Holden] prefers to spout innuendu, personal attacks and
misleading statements.

Your constant and repeated accusations that any questioning of you is
deliberate lying and innuendo (innuendo of what, I wonder?) is getting
tiresome. You did this to me, accusing me of deliberate lying, and now
you're doing the same to Steve.

Just because people question the (alleged) facts as you report it doesn't
mean they're deliberately trying to mislead others. Hasn't it occurred to
you that perhaps we're not deliberately lying to make you look bad but
actually think you are mistaken? A difference of opinion is not evidence
that people are lying.

For somebody who fancies himself a writer teaching beginners to
programming, your communication skills leave something to be desired. I
hope your book is better than your posts, because from what I have seen
your writing in this newsgroup is frequently sloppy and hard to
understand, you tend to use terms in non-standard ways and then take any
misunderstandings as a personal affront, and if anyone questions what you
say, you're likely bristle and start looking for a fight.

I don't know what your problem is, but I wish you'd deal with it in
private and stop dumping it on the rest of us. You're a smart fellow who
brings much of value to the community, but dealing with your posts is
becoming more trouble than they're worth.
 
A

Alf P. Steinbach

* Steven D'Aprano:
You're again into innuendo, misleading statements and so forth. [...]
[Steve Holden] prefers to spout innuendu, personal attacks and
misleading statements.

Your constant and repeated accusations that any questioning of you is
deliberate lying and innuendo (innuendo of what, I wonder?) is getting
tiresome. You did this to me, accusing me of deliberate lying, and now
you're doing the same to Steve.

You did lie, that's established. In addition as I recall in the same post you
went on about my motivations for doing the Terrible Deed that you invented. And
you named one poor person who'd made a silly mistake, stating that he didn't do
what the logs clearly showed he did do, and I think that was to force me to
argue a case against him (but happily he did a superb job after that initial
mistake, it would have been much harder for me to respond to your egging if not
for that very good job he did).

And now you're trying to get me to argue a case against another person.

That's not very nice.

Just because people question the (alleged) facts as you report it doesn't
mean they're deliberately trying to mislead others. Hasn't it occurred to
you that perhaps we're not deliberately lying to make you look bad but
actually think you are mistaken? A difference of opinion is not evidence
that people are lying.

Mostly I assume that people who disagree with me think I'm mistaken, yes.

But when someone writes an article that's mainly or only about me, as has
happened once earlier in this thread and that now you're doing, then I of course
assume that it's personal, that the person writing has transferred some
grievances from the technical domain to the personal domain.

For somebody who fancies himself a writer teaching beginners to
programming, your communication skills leave something to be desired. I
hope your book is better than your posts, because from what I have seen
your writing in this newsgroup is frequently sloppy and hard to
understand, you tend to use terms in non-standard ways and then take any
misunderstandings as a personal affront, and if anyone questions what you
say, you're likely bristle and start looking for a fight.

I'm probably a bad person, he he. :)

I don't know what your problem is, but I wish you'd deal with it in
private and stop dumping it on the rest of us. You're a smart fellow who
brings much of value to the community, but dealing with your posts is
becoming more trouble than they're worth.

Well please stop posting these silly articles about me, and *please* stop
dragging other people into it.


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

* Ben Finney:
None of that matches my (largely disinterested) observations. This is
pure fantasy, as best I can tell.

It's decidedly not kind of you to try to re-open those issues.

Anybody can have a bad day and write something that's untrue or actively
misleading, whatever.

When it's been dealt with, and it has, it should not be reiterated.

I've tried in private to talk you out of this persecution fantasy;
others have tried in public. It all seems to be in vain.

It's quite normal for people to use ad hominem arguments, especially when they
have chosen an indefensible position; that's why the technique has a name.

There's no persecution involved in that.

And people are not necessarily bad even when they do that from time to time, as
you do here: it's human.

You've now passed my twit threshold, so welcome to my kill file.

Goodbye.


Cheers & hth.,

- Alf
 
S

Steve Holden

Alf said:
* Ben Finney:

It's decidedly not kind of you to try to re-open those issues.

Anybody can have a bad day and write something that's untrue or actively
misleading, whatever.

When it's been dealt with, and it has, it should not be reiterated.



It's quite normal for people to use ad hominem arguments, especially
when they have chosen an indefensible position; that's why the technique
has a name.

There's no persecution involved in that.

And people are not necessarily bad even when they do that from time to
time, as you do here: it's human.



Goodbye.


Cheers & hth.,

- Alf

How you can possibly hope that helps anyone except you I can't possibly
imagine. It seems that any request to question your own behavior, any
remark about how it might usefully change, is taken to be an ad homime,
attack.

As my wife (who has a colorful Scottish turn of phrase) might say, if
you were chocolate you would eat yourself.

Please, get over this obsession with being "right".

regards
Steve
 
A

Alf P. Steinbach

* Steve Holden:
How you can possibly hope that helps anyone except you I can't possibly
imagine. It seems that any request to question your own behavior, any
remark about how it might usefully change, is taken to be an ad homime,
attack.

As my wife (who has a colorful Scottish turn of phrase) might say, if
you were chocolate you would eat yourself.

Please, get over this obsession with being "right".

You have (so far) refused to outright admit that you were wrong here, going to
the extreme half-way measure of using the word "wring" or something, perhaps
hoping that I'd interpret it one way and most everyone else another way.

I do respond to articles which are technically wrong, especially follow-ups to
my own articles, as your (at least initial) POV was in this thread: wrong.

That's not an obsession, nor is it a desire to be right, it's the *usual* Usenet
culture: one is expected to respond and to correct technical issues, and to not
collect social points at the expense of technical correctness.

And since it's you who brings this up again, and since earlier you wrote ...

"I herebe retract anything I have said about you that you consider
innuendo. Feel free to remind me what that was."

.... I now feel free to remind you about some of it.

Instead of saying OK or thanks or whatever normal for a simple explanation, you
pretended that my explanation was some kind of thesis from me and "this is
merely hand-waving. It looks appealing, but there's no rigor there".

And with our last discussion before this one fresh in mind I told you that that
was bullshit, using just that single word. But to expand on that: the
insinuation that the explanation was some kind of thesis from me was bullshit,
that it was merely "hand-waiving" was bullshit (while informal it was an exact
algorithm, and later in this thread I posted Python code implementing it), and
that it had "no rigor" was bullshit since it was an exact algorithm; moreover it
was a *trivial* algorithm, and as demonstrated, it works.

In short, the insinuation that I was some kind of crank posting a thesis that
lacked "rigor" and was "hand-waiving" was utter bullshit: it was a trivial and
exact algorithm, an explanation in response to your own question, and it
demonstrably works.

In response to someone else you then posted this:


Of course he can't. And it isn't the basis of analog quantization. And I
suspect Alf has never hear of Shannon's theorem.

But don't listen to me, apparently I'm full of it.

regards
Steve
</quote>


* The "of course he can't [provide a reference]"

is the same insinuation you made earlier repeated, that what I posted was
drivel, when it was a trivial and exact algorithm, one that does work.


* "Isn't the basis of analog quantization"

makes the reader believe I'd written that nonsense "basis of analog
quantization". I did not write such nonsense but readers get the impression that
I did. The quote above shows what I wrote instead of the words you put in my mouth.


* "And I suspect Alf has never hear of Shannon's theorem."

is solely about me, an insinuation about my education or competence.


* "But don't listen to me, apparently I'm full of it."

gives the reader the impression that I've written earlier that you're "full of it".

I haven't ever written that.

And /nothing/ in that posting was about any technical issue, each and every of
the four sentences just an ad hominem attack.

I very seldom do ad hominem (it has happened, yes, I'm human too). But this is
the second or third time you've responded to me with that last sentence, and it
implies that I've written that about you, that I'd earlier done unto you what
you were doing to me. I haven't, although in my follow-up, annoyed by all this
stuff from you, I then agreed with your self-evaluation, for which I apologize.



Cheers & hth.,

- Alf
 
R

r0g

Alf said:
But maybe you're simply not able to understand the algorithm, trivial as
it is.

So, a Python implementation (note, this program takes some time to run!):


<code filename="sinewave_from_squares.py">
# Generating a sine wave as a sum of square waves of various amplitudes


Pwned! Good one Alf :)

I think those guys owe you an apology really, but I wouldn't hold your
breath!

Roger.
 
S

Steve Holden

r0g said:
Pwned! Good one Alf :)

I think those guys owe you an apology really, but I wouldn't hold your
breath!
Well as you can now see at least one of "those guys" doesn't mind
admitting (and apologizing) when he is wrong.

regards
Steve
 
G

Grant Edwards

I will, however, observe that your definition of a square wave is what I
would have to call a "'square' wave" (and would prefer to call a "pulse
train"), as I envisage a square wave as a waveform having a 50% duty
cycle, as in

___ ___
| | | |
| | | |
| | | |
+---+---+---+---+ and so on ad infinitum, (though I might allow you
| | | | to adjust the position
| | | | of y=0 if you want)
|___| |___|

That is a square wave.
as opposed to your

_
| |
| |
______| |______ ______
| |
| |
|_|

That isn't.

Arguing to the contrary is just being Humpty Dumpty...
Or, best of all, you could show me how to synthesize any
waveform by adding square waves with a 50% duty cycle. Then I
*will* be impressed.

Isn't that what he claimed? He said that his algorithm for
summing square waves demonstrated the converse of the ability
to construct a periodic function (like a square wave) from a
sine-cosine summation.
 
R

r0g

Steve said:
r0g wrote:
Well as you can now see at least one of "those guys" doesn't mind
admitting (and apologizing) when he is wrong.

regards
Steve


I stand corrected, fair play sir :)

Roger.
 
A

Alf P. Steinbach

* Steve Holden:
For the record, yes, summing any waveforms that can be represented as
Fourier Series will necessarily result in another Fourier series, since
any linear combination of Fourier series must itself, be a Fourier
series, and therefore the representation of the sum of the summed waveforms.

As it is I do not know whether the above represents what I've written, or might
perhaps be /misprepresenting/ the conclusions of this thread.

If I knew a lot more about Fourier series (it's been a long time since college!
lots forgotten) I might agree or disagree with the above as it applies to
constructing a sine wave from square waves.

I just note that representing a sine wave as square waves, forming it from
square waves, in the practical sense works (demonstrated here), even though it's
totally impractical :), but just in answer to your original question.

And I note that in the mathematematical sense when n goes to infinity and
vanishingly thin pulses result from sums of square waves, like *impulse* waves,
then one is over in some regime where it is not at all clear to me that it is
valid to talk about Fourier series any more. Perhaps it's valid if the term
"Fourier series" does not necessarily imply sum of sine waves. I don't know.

I'm guessing that applying the Fourier series view for that is like actually
dividing by zero to maintain the running product of a collection of numbers when
an instance of 0 is removed from the collection.

It's no practical problem to maintain a running product (for programmers it's
interesting to note that polar representation complex numbers can do the job),
and it's well-defined also mathematically, with any /reasonable/ approach. But
the simple-minded direct way, just dividing the current product by the number
removed from the collection, is then invalid. And some people might take that
limited applicability of the direct simple way as evidence that it's impossible
to remove numbers, failing to see any of the trivial practical solutions. ;-)


Cheers,

- Alf
 
A

Alf P. Steinbach

* Grant Edwards:
That is a square wave.


That isn't.

Arguing to the contrary is just being Humpty Dumpty...

Neither I nor Steve has called that latter wave a square wave.

Steve, quoted above, has written that I defined a square wave that way. I have
not. So Steve's statement is a misrepresentation (I described it as a sum of two
square waves, which it is), whatever the reason for that misrepresentation.

Isn't that what he claimed? He said that his algorithm for
summing square waves demonstrated the converse of the ability
to construct a periodic function (like a square wave) from a
sine-cosine summation.

Not by itself, no: it just synthesizes a sine.

For the more general case read e.g. the PS in my reply to your earlier (single)
article in this thread.

For information about what the algorithm does, what you refer to as a "claim"
(but note that a Python implementation has been posted to this thread, and that
it works, and that besides the algorithm is trivial so that "claim" is a rather
meaningless word here), read the article that you then responded to.


Cheers & hth.,

- Alf
 
S

Steve Holden

Alf said:
* Grant Edwards:

Neither I nor Steve has called that latter wave a square wave.

Steve, quoted above, has written that I defined a square wave that way.
I have not. So Steve's statement is a misrepresentation (I described it
as a sum of two square waves, which it is), whatever the reason for that
misrepresentation.



Not by itself, no: it just synthesizes a sine.

For the more general case read e.g. the PS in my reply to your earlier
(single) article in this thread.

For information about what the algorithm does, what you refer to as a
"claim" (but note that a Python implementation has been posted to this
thread, and that it works, and that besides the algorithm is trivial so
that "claim" is a rather meaningless word here), read the article that
you then responded to.
Though for what it's worth I wasn't impressed by the results of running
the posted program, since it yielded an AIFF file of mostly zeroes that
produced no audible sound.

$ od -bc sinewave.aiff
0000000 106 117 122 115 000 002 261 076 101 111 106 106 103 117 115 115
F O R M \0 002 261 > A I F F C O M M
0000020 000 000 000 022 000 001 000 001 130 210 000 020 100 016 254 104
\0 \0 \0 022 \0 001 \0 001 X 210 \0 020 @ 016 254 D
0000040 000 000 000 000 000 000 123 123 116 104 000 002 261 030 000 000
\0 \0 \0 \0 \0 \0 S S N D \0 002 261 030 \0 \0
0000060 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0530500 000 000 000 000 000 000
\0 \0 \0 \0 \0 \0
0530506

Any idea what I did wrong?

regards
Steve
 
A

Alf P. Steinbach

* Steve Holden:
Though for what it's worth I wasn't impressed by the results of running
the posted program, since it yielded an AIFF file of mostly zeroes that
produced no audible sound.

$ od -bc sinewave.aiff
0000000 106 117 122 115 000 002 261 076 101 111 106 106 103 117 115 115
F O R M \0 002 261 > A I F F C O M M
0000020 000 000 000 022 000 001 000 001 130 210 000 020 100 016 254 104
\0 \0 \0 022 \0 001 \0 001 X 210 \0 020 @ 016 254 D
0000040 000 000 000 000 000 000 123 123 116 104 000 002 261 030 000 000
\0 \0 \0 \0 \0 \0 S S N D \0 002 261 030 \0 \0
0000060 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0530500 000 000 000 000 000 000
\0 \0 \0 \0 \0 \0
0530506

Any idea what I did wrong?

That sounds like something I did wrong, not like something you did wrong:

It sounds like a ... BUG! ... in my simple_writer code. :)

Or, that's perhaps not funny, but it occurred to me that it might, to some at
least, appear to be sort of incongruous in the context of the earlier thread. Heh.

Checking first 20 sample values generated:

<code>
if True:
f = 440
sample_rate = 44100
total_time = 2
n_samples = sample_rate*total_time

writer = simple_sound.Writer( "sinewave.aiff" )
for i in range( n_samples ):
t = 1*i/sample_rate
sample = sample_squares( f, t )
if i < 20: print( sample ) # Check 'em
writer.write( sample )
writer.close()
</code>

<output>
-0.0314107590781
-0.0314107590781
-0.0941083133185
-0.15643446504
-0.218143241397
-0.278991106039
-0.338737920245
-0.397147890635
-0.45399049974
-0.50904141575
-0.562083377852
-0.612907053653
-0.661311865324
-0.707106781187
-0.75011106963
-0.790155012376
-0.827080574275
-0.860742027004
-0.891006524188
-0.917754625684
</output>

Checking generated file:

<dump>
$ od -bc sinewave.aiff | head
0000000 106 117 122 115 000 001 130 266 101 111 106 106 103 117 115 115
F O R M \0 001 X 266 A I F F C O M M
0000020 000 000 000 022 000 001 000 000 254 104 000 020 100 016 254 104
\0 \0 \0 022 \0 001 \0 \0 254 D \0 020 @ 016 254 D
0000040 000 000 000 000 000 000 123 123 116 104 000 001 130 220 000 000
\0 \0 \0 \0 \0 \0 S S N D \0 001 X 220 \0 \0
0000060 000 000 000 000 000 000 373 373 373 373 363 364 353 372 344 024
\0 \0 \0 \0 \0 \0 373 373 373 373 363 364 353 372 344 024
0000100 334 112 324 245 315 053 305 344 276 330 270 016 261 215 253 133
334 J 324 245 315 + 305 344 276 330 270 016 261 215 253 [
</dump>


Hm, I'm inclined to think that you used Python 2.x instead of my 3.1.1!

I no longer have Python 2.x installed, I think, so no time to test that now.

But would that be the case?

If so, perhaps changing "t = 1*i/sample_rate" to "t = (1.0*i)/sample_rate" will
help?


Cheers,

- Alf
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* Steve Holden:
Though for what it's worth I wasn't impressed by the results of running
the posted program, since it yielded an AIFF file of mostly zeroes that
produced no audible sound.

$ od -bc sinewave.aiff
0000000 106 117 122 115 000 002 261 076 101 111 106 106 103 117 115 115
F O R M \0 002 261 > A I F F C O M M
0000020 000 000 000 022 000 001 000 001 130 210 000 020 100 016 254 104
\0 \0 \0 022 \0 001 \0 001 X 210 \0 020 @ 016 254 D
0000040 000 000 000 000 000 000 123 123 116 104 000 002 261 030 000 000
\0 \0 \0 \0 \0 \0 S S N D \0 002 261 030 \0 \0
0000060 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0530500 000 000 000 000 000 000
\0 \0 \0 \0 \0 \0
0530506

Any idea what I did wrong?
[snip]


Hm, I'm inclined to think that you used Python 2.x instead of my 3.1.1!

I no longer have Python 2.x installed, I think, so no time to test that
now.

But would that be the case?

If so, perhaps changing "t = 1*i/sample_rate" to "t =
(1.0*i)/sample_rate" will help?

I fixed up two more divisions. Not sure if this is 2.x-compatible, but perhaps:



<code>
# Python 3.1.1 -- *probably* works also with 2.x?
# Generating a sine wave as a sum of square waves of various amplitudes & phases.
import simple_sound


# Step 1 "Divide a full cycle of the sine wave into n intervals."
n = 100


# Step 2 -- Just an explanation of the rest


# Step 3 "In the first half of the cycle, for each bar create that bar as
# a square wave of frequency f, amplitude half the bar's height, and phase
# starting at the bar's left, plus same square wave with negative sign
# (inverted amplitude) and phase starting at the bar's right."

square_waves = []
for i in range( n//2 ):
middle_of_interval = (i + 0.5)/n
amp = simple_sound.sample_sine( 1, middle_of_interval ) / 2.0
def first_square_wave( t, i = i, amp = amp ):
phase = 1.0*i/n
return amp*simple_sound.sample_square( 1.0, t - phase )
def second_square_wave( t, i = i, amp = amp ):
phase = 1.0*(i + 1)/n
return -amp*simple_sound.sample_square( 1.0, t - phase )
square_waves.append( first_square_wave )
square_waves.append( second_square_wave )


# Step 4 "Sum all the square waves from step 3."

def sample_squares( f, t ):
samples = []
o_time = f*t
for func in square_waves:
sq_sample = func( o_time )
samples.append( sq_sample )
return sum( samples )


# Finally, generate this is in an [.aiff] file:
if True:
f = 440
sample_rate = 44100
total_time = 2
n_samples = sample_rate*total_time

writer = simple_sound.Writer( "sinewave.aiff" )
for i in range( n_samples ):
t = 1.0*i/sample_rate
sample = sample_squares( f, t )
writer.write( sample )
writer.close()
</code>


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,073
Messages
2,570,537
Members
47,195
Latest member
RedaMahuri

Latest Threads

Top