Sine wave?

EC vitamonkey at home.com
Sun Dec 22 13:42:19 EST 2002


Ive been banging my head against the wall for the past few days trying to
generate a sine wave... My math skills arent the best, I'll admit, and I
think I'm going crosseyed trying to understand this... I *think* the
following code should work but it outputs some really weird stuff.  I'm
sorta new so please help if you can! Thank you

Eric

#-------------------------------------------
import math
import wave
import string

pi = math.pi
sin = math.sin
cos = math.cos

#init output and related vars
sr = 44100
Outfile = wave.open("outfile.wav", "w")
Outfile.setnchannels(1)
Outfile.setsampwidth(4)
Outfile.setframerate(sr)
Outfile.setcomptype("NONE", "Uncompressed")
Outtemp = []
Outstr = ""

def get_phase(curphs, freq):
    phinc = (freq*1.0000/sr*1.0000)*360
    phase = curphs + phinc
    if phase >= 360:
        phase -= 360
    elif phase < 0:
        phase += 360
    return phase

def gen_wav(sec, freq, ampcent):
    numsamp = sec*sr
    amp = (ampcent*1.0000/100)*32767
    i = 0
    temp = []
    t = 0
    curphase = 0
    while i < numsamp:
        curphase = get_phase(curphase, freq)
        t = int(round(amp*sin(2*pi*curphase)))
        temp.append(t)
        i+=1
    return temp

Outtemp = gen_wav(.01, 440, 75)

Outstr = string.join(map(str, Outtemp), ",")
print Outstr
Outfile.writeframes(Outstr)
Outfile.close()





More information about the Python-list mailing list