'Amplitude modulation of a brown noise generated with acoustics package in python
I am totally new to python, so I tried to read and learn what I could but I cannot seem to do what I want, and I haven't found a solution on Stack Overflow or other sources. My aim is to create a wave file of brown noise with amplitude modulation at a given frequency. I want to generate brown noise and modulate it.
I intended to use the python acoustics package, unfortunately I don't understand how to use the functions to create colored noise. I looked at the examples, but I don't see examples on colored noises functions use.
Anyone can help me solving this issue? Thanks.
Here is my code:
""" This file proposes to modulate a given wav file"""
import wave
import struct
import time
import math
import random
import acoustics
###########################################
# General variables
outputName = "waveXP.wav"
frequencyModulation = 40
period = 1/frequencyModulation
duration = 1
maxVolume = 23000.0
framerate = 44100
###########################################
# Ask the user about the output file name
temp = ""
temp = input("Name of the output wave file to import (with extension):")
if temp != "":
outputName = str(temp)
# Ask the user about the modulation frequency wanted
temp = ""
temp = input("Modulation frequency wanted (in hertz):")
if temp != "":
frequencyModulation = int(temp)
period = 1/frequencyModulation
# Ask the user about the duration wanted
temp = ""
temp = input("Duration wanted (in seconds):")
if temp != "":
duration = int(temp)
print("------------------------")
###########################################
# Create the output wave file
newWaveFile = wave.open(outputName, "w")
# Define parameters of the wave file
# nchannels = 1 for mono; sampwidth = 2 for 2 bytes per sample; framerate = 44100 for wave file;
# comptype = "NONE" for no compression support; compname = 'not compressed' for no compression support
newWaveFile.setparams([1, 2, framerate, duration, 'NONE', 'not compressed'])
# Generate noise
newWaveFile.writeframes(struct.pack('h'*framerate*duration, *int(maxVolume*0.7*acoustics.generator.brown(framerate*duration))))
# Close wave files
originalWaveFile.close()
newWaveFile.close()
Solution 1:[1]
Looks like your proposed library already has a brown noise generator:
http://python-acoustics.github.io/python-acoustics/generator.html
Assuming you want to just apply a single modulation as an amplitude gain to the whole signal, then you would want to sample your modulation at the sample rate of the signal, and add the modulation (times some scale factor) to the brown noise signal. Alternatively, you could multiply the signal by the modulation, but that would be a much more extreme effect.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ruan Caiman |