Advertisement
here2share

# wav_stereo.py

Feb 11th, 2021 (edited)
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. # wav_stereo.py
  2.  
  3. from struct import pack
  4. import math
  5. import wave
  6. import random
  7. import sys
  8. import array
  9.  
  10. dir='C://py//audio//'
  11.  
  12. RATE=44100
  13.  
  14. def makeSineWaveFile(freq=100, duration=3, sampleRate=44100):
  15.     freq = int(freq)
  16.     duration = int(duration)
  17.     sampleRate = int(sampleRate)
  18.     #duration = 3 # seconds
  19.     #freq = 100 # of cycles per second (Hz) (frequency of the sine waves)
  20.     volume = 100 # percent
  21.     data = array.array('h') # signed short integer (-32768 to 32767) data
  22.     #sampleRate = 44100 # of samples per second (standard)
  23.     numChan = 2 # of channels (1: mono, 2: stereo)
  24.     dataSize = 2 # 2 bytes because of using signed short integers => bit depth = 16
  25.     numSamplesPerCyc = int(sampleRate / freq)
  26.     numSamples = (sampleRate * duration) / 2
  27.     for i in range(numSamples):
  28.         for hz in (200,500):
  29.             sample = 32767 * float(volume) / hz
  30.             sample *= math.sin(math.pi * 2 * (i % numSamplesPerCyc) / numSamplesPerCyc)
  31.             data.append(int(sample))
  32.     f = wave.open(dir+'wav_stereo.wav', 'w') # .mp3 or .wav
  33.     f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed"))
  34.     f.writeframes(data.tostring())
  35.     f.close()
  36.  
  37. makeSineWaveFile()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement