* 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
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to