PageRenderTime 117ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/sineWave.py

https://gitlab.com/jack/AVR-Programming
Python | 38 lines | 32 code | 4 blank | 2 comment | 0 complexity | c7c8d07a9ec40eb0a3de5c1e405e5c8c MD5 | raw file
  1. ## Demo python program to create sine wave
  2. from struct import pack, unpack
  3. from math import sin, pi
  4. import wave
  5. import os
  6. import random
  7. RATE = 44100
  8. ## GENERATE MONO FILE ##
  9. wv = wave.open('test_mono.wav', 'w')
  10. wv.setparams((1, 2, RATE, 0, 'NONE', 'not compressed'))
  11. maxVol=2**15-1.0 #maximum amplitude
  12. wvData=""
  13. for i in range(0, RATE*3):
  14. wvData+=pack('h', maxVol*sin(2*pi*i*440.0/RATE))
  15. wv.writeframes(wvData)
  16. wv.close()
  17. os.system("mplayer test_mono.wav")
  18. ## GENERATE STEREO FILE ##
  19. wv = wave.open('test_stereo.wav', 'w')
  20. wv.setparams((2, 2, RATE, 0, 'NONE', 'not compressed'))
  21. maxVol=2**15-1.0 #maximum amplitude
  22. waveData = ""
  23. for i in range(0, RATE*3):
  24. t = 2*pi*i/RATE # time-step in radians*sec
  25. waveData+=pack('h', maxVol*sin(t*440.0)) #440Hz left
  26. waveData+=pack('h', maxVol*sin(t*220.0)) #220Hz right
  27. wv.writeframes(waveData)
  28. wv.close()
  29. os.system("mplayer test_stereo.wav")