Advertisement
here2share

# audio_frames.py

Feb 11th, 2021
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. # audio_frames.py
  2.  
  3. import math, wave, array
  4.  
  5. dir='C://py//audio//'
  6.  
  7. sin = math.sin
  8. pi = math.pi
  9. twopi = 2*pi
  10.  
  11. frameRate = 25 #frames per second
  12. frameTime = 1/float(frameRate)
  13.  
  14. sampleRate = 44100 #44100 Samples per Second
  15.  
  16. length = 0.2 #0.2 seconds or you can define it with frame times, i.e. for 3 frames it'd be 3 * frameTime
  17. numSamples = int(length * sampleRate)
  18.  
  19. freq = 440
  20. cyclesPerSample = float(freq)/sampleRate
  21.  
  22. volumeScale = (.99)*32767
  23.  
  24. sdata = array.array('h') #Beep Sound Array
  25. for samp in xrange(numSamples):
  26.     phi = samp * cyclesPerSample
  27.     phi -= int(phi)
  28.    
  29.     sdata.append(int(round(volumeScale * sin(twopi * phi))))
  30.    
  31. totalFrames = 250
  32. totalSamps = totalFrames * frameTime * sampleRate
  33. targetFrames = [21, 47, 65, 80, 120, 140, 170, 200, 220]
  34.  
  35. # Builds frame triggers in terms of samples
  36. trigger = [int(frm * frameTime * sampleRate) for frm in targetFrames]
  37.  
  38. endData = array.array('h')
  39. curSamp = 0
  40. while curSamp < totalSamps:
  41.     if curSamp in trigger:
  42.         for smp in sdata:
  43.             endData.append(smp)
  44.         curSamp += numSamples
  45.        
  46.     else:
  47.         endData.append(0)
  48.         curSamp += 1
  49.  
  50.  
  51. f = wave.open(dir+'frames.wav', 'w')
  52. f.setparams((1, 2, sampleRate, 0, "NONE", "Uncompressed"))
  53. f.writeframes(endData.tostring())
  54. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement