It generates random sound effects using AM and FM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # Random Sound FX Using WAV File
# http://en.wikipedia.org/wiki/Amplitude_modulation
# http://en.wikipedia.org/wiki/Frequency_modulation
# FB36 - 20120701
import math, wave, array, random
duration = 5 # seconds
volume = 100 # percent
freqCR = random.randint(500, 3000) # frequency of the carrier wave (Hz)
freqAM = random.randint(1, 10) # frequency of the AM wave (Hz)
freqFM = random.randint(1, 10) # frequency of the FM wave (Hz)
freqFMDev = random.randint(100, 400) # frequency deviation for FM (Hz)
phaseCR = random.random() * math.pi * 2
phaseAM = random.random() * math.pi * 2
phaseFM = random.random() * math.pi * 2
# Assumed: ampCR = ampAM = ampFM = 1
data = array.array('h') # signed short integer (-32768 to 32767) data
dataSize = 2 # 2 bytes because of using signed short integers => bit depth = 16
numChan = 1 # of channels (1: mono, 2: stereo)
sampleRate = 44100 # of samples per second (standard)
numSamples = sampleRate * duration
# nSPC: number of Samples Per Cycle
nSPCCR = int(sampleRate / freqCR)
nSPCAM = int(sampleRate / freqAM)
nSPCFM = int(sampleRate / freqFM)
for i in range(numSamples):
sample = 32767 * float(volume) / 100
tCR = math.pi * 2 * (i % nSPCCR) / nSPCCR + phaseCR
tFM = math.pi * 2 * (i % nSPCFM) / nSPCFM + phaseFM
tAM = math.pi * 2 * (i % nSPCAM) / nSPCAM + phaseAM
sample *= math.sin(tCR + math.sin(tFM) * freqFMDev / freqFM)
sample *= (math.sin(tAM) + 1) / 2
data.append(int(sample))
f = wave.open('RndSoundFX.wav', 'w')
f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed"))
f.writeframes(data.tostring())
f.close()
|
One way to generate even more complex sound effects maybe to generate multiple AM and/or FM waves and add them together.
1 comment
FB36 (author)
8 years, 8 months ago
#
|
flag