Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # audio_frames.py
- import math, wave, array
- dir='C://py//audio//'
- sin = math.sin
- pi = math.pi
- twopi = 2*pi
- frameRate = 25 #frames per second
- frameTime = 1/float(frameRate)
- sampleRate = 44100 #44100 Samples per Second
- length = 0.2 #0.2 seconds or you can define it with frame times, i.e. for 3 frames it'd be 3 * frameTime
- numSamples = int(length * sampleRate)
- freq = 440
- cyclesPerSample = float(freq)/sampleRate
- volumeScale = (.99)*32767
- sdata = array.array('h') #Beep Sound Array
- for samp in xrange(numSamples):
- phi = samp * cyclesPerSample
- phi -= int(phi)
- sdata.append(int(round(volumeScale * sin(twopi * phi))))
- totalFrames = 250
- totalSamps = totalFrames * frameTime * sampleRate
- targetFrames = [21, 47, 65, 80, 120, 140, 170, 200, 220]
- # Builds frame triggers in terms of samples
- trigger = [int(frm * frameTime * sampleRate) for frm in targetFrames]
- endData = array.array('h')
- curSamp = 0
- while curSamp < totalSamps:
- if curSamp in trigger:
- for smp in sdata:
- endData.append(smp)
- curSamp += numSamples
- else:
- endData.append(0)
- curSamp += 1
- f = wave.open(dir+'frames.wav', 'w')
- f.setparams((1, 2, sampleRate, 0, "NONE", "Uncompressed"))
- f.writeframes(endData.tostring())
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement