A simple-to-use sound file writer

Alf P. Steinbach alfps at start.no
Thu Jan 14 22:04:13 EST 2010


* Lie Ryan:
> On 01/15/10 05:42, Alf P. Steinbach wrote:
>> I'm beginning to believe that you maybe didn't grok that simple procedure.
>>
>> It's very very very trivial, so maybe you were looking for something
>> more intricate  --  they used to say, in the old days, "hold on, this
>> proof goes by so fast you may not notice it!"
> 
> Since you said it's trivial, then...
> 
>>> Nothing about you there. Just the information you are promoting. I don't
>>> normally deal in innuendo and personal attacks. Though I do occasionally
>>> get irritated by what I perceive to be hogwash. People who know me will
>>> tell you, if I am wrong I will happily admit it.
>> There's a difference between an algorithm that you can implement, and
>> hogwash.
> 
> please prove your claim by writing that algorithm in code and post it in
> this list. The program must accept a .wav file (or sound format of your
> choice) and process it according to your algorithm and the output
> another .wav file (or sound format of your choice) that sounds roughly
> similar to the input file.

In addition to my earlier reply (that the presented algorithm itself doesn't 
generate arbitrary wave forms but only sines, i.e. that you're totally off the 
mark),

your request is meaningless in another way, namely:

the algorithm does synthesis, which is easy (like computer speech), while you're 
asking for analysis (like computer speech recognition), which is hard.

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 & 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
     def first_square_wave( t, i = i, amp = amp ):
         phase = i/n
         return amp*simple_sound.sample_square( 1, t - phase )
     def second_square_wave( t, i = i, amp = amp ):
         phase = (i + 1)/n
         return -amp*simple_sound.sample_square( 1, 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 a [.aiff] file so as to possibly satisfy Lie Rian:
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 )
         writer.write( sample )
     writer.close()
</code>


Cheers & hth.,

- Alf



More information about the Python-list mailing list