[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Sine Waves



David Tweet wrote:
I would also suggest looking at more of the Numeric built-ins, such as "fromfunction".
For example, instead of looping to construct a native python list and then converting to an array, you can do:


arr_float = fromfunction(lambda x, y:(sin(x*freq*(6.28318/sample_rate)*length)*volume), (sample_rate * length, 2))

arr_int = arr_float.astype(Int16)
return arr_int

This converts using sndarray just fine, although not if you are using NumArray for some reason, you have to be using Numeric. The "sin" function here is from importing Numeric. The type conversion is because something complained about using the "int" type coercion inside the lambda.

Thanks. I got the sine wave maker working by limiting the volume (yeah, it must've been chopping off the upper parts of the sine waves) and eliminating the "*length" part, which didn't belong there. Looks like I could construct a standard Western musical scale by setting standard note A4 to 440 Hz, and calculating each higher note as 1.05946 times the previous note's frequency. (<http://www.physlink.com/Education/AskExperts/ae165.cfm>)


The reason I started messing with sound generation was an experiment with speech synthesis -- I had thought the array data was _frequency_ instead of volume. That explains why my attempt to do a modern speech-synth trick didn't work:

Apparently good speech synth systems don't just string together English phonemes. They alter the pitch of the head and tail of each, so that there's a smooth variation in frequency from one to the next, imitating the continuous stream of sound produced by a human speaking even multiple words. I thought I was being clever by writing a function to tweak the array data to make the values there closer together at the head/tail of phoneme pairs, but noticed no effect.

It looks like since the array values represent volume (pressure, really), I'd have to calculate what the frequency is on either end and do some sort of Numeric function to compress/stretch the data, maybe even changing the resulting array's length. This sounds hard; any leads?

Kris