Welcome, guest | Sign In | My Account | Store | Cart

It generates random sound effects using AM and FM.

Python, 36 lines
 ``` 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) 7 years, 9 months ago
``````# Random Sound FX Using WAV File (Multi-Wave)
# http://en.wikipedia.org/wiki/Amplitude_modulation
# http://en.wikipedia.org/wiki/Frequency_modulation
# FB36 - 20150815
import math, wave, array, random
duration = 5.0 # seconds
volume = 100 # percent
n = random.randint(1, 10) # of waves to add together
# frequency of the carrier wave (Hz)
freqCR = [random.randint(500, 3000) for j in range(n)]
# frequency of the AM wave (Hz)
freqAM = [random.randint(1, 10) for j in range(n)]
# frequency of the FM wave (Hz)
freqFM = [random.randint(1, 10) for j in range(n)]
# frequency deviation for FM (Hz)
freqFMDev = [random.randint(100, 400) for j in range(n)]
phaseCR = [random.random() * math.pi * 2 for j in range(n)]
phaseAM = [random.random() * math.pi * 2 for j in range(n)]
phaseFM = [random.random() * math.pi * 2 for j in range(n)]
# 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 = int(sampleRate * duration)
# nSPC: number of Samples Per Cycle
nSPCCR = [int(sampleRate / freqCR[j]) for j in range(n)]
nSPCAM = [int(sampleRate / freqAM[j]) for j in range(n)]
nSPCFM = [int(sampleRate / freqFM[j]) for j in range(n)]
for i in range(numSamples):
sampleSum = 0
for j in range(n):
sample = 32767 * float(volume) / 100
tCR = math.pi * 2 * (i % nSPCCR[j]) / nSPCCR[j] + phaseCR[j]
tFM = math.pi * 2 * (i % nSPCFM[j]) / nSPCFM[j] + phaseFM[j]
tAM = math.pi * 2 * (i % nSPCAM[j]) / nSPCAM[j] + phaseAM[j]
sample *= math.sin(tCR + math.sin(tFM) * freqFMDev[j] / freqFM[j])
sample *= (math.sin(tAM) + 1) / 2
sampleSum += sample
data.append(int(sampleSum / n))
f = wave.open('RndSoundFX.wav', 'w')
f.setparams((numChan, dataSize, sampleRate, numSamples, "NONE", "Uncompressed"))
f.writeframes(data.tostring())
f.close()
`````` Created by FB36 on Sun, 1 Jul 2012 (MIT)

### Required Modules

• (none specified)