Modulate brown noise in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Modulate brown noise in python (/thread-9215.html) |
Modulate brown noise in python - pyxel - Mar-27-2018 Hi everyone, First of all thank you for taking time to read this. I assure you that I looked at all posts I could find, and other sources, but I can't find what I what so I post here my issue. 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. My aim is to create a wave file of brown noise with amplitude modulation at a given frequency. From what I saw on the internet I have two possibilities: (I) generate brown noise and modulate it, or (II) get a brown noise wave file and modulate it. My problems are: 1) One of the options should be to choose the duration (i.e. the number of periods of the modulation frequency generated). For 1 second it generates a wave file of 1s, but for 3 seconds it generates a 5 seconds file, and for 5 seconds it generates a 9 seconds file! I don't get it... 2) I want to have a modulation frequency defined by the user, unfortunately, I don't have any mean to make sure that the frequency defined is really the final idea. Is there something to plot the signal? 3) 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. 4) Maybe the idea (II) would be easier to use: import a brown noise file, cut to the duration I want and modulate with my frequency. Unfortunately I don't get how to modulate wave files frame by frame. Any idea? Thank you if you have any tips for this, I am really struggling on this one. Please find below my code. Pyxel ---------------------------- 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 frequencyModulation = 40 period = 1/frequencyModulation duration = 1 maxVolume = 23000.0 ########################################### # Ask the user about the modulation frequency wanted temp = "" temp = input("Modulation frequency wanted:") if temp != "": frequencyModulation = int(temp) period = 1/frequencyModulation # Ask the user about the duration wanted temp = "" temp = input("Duration wanted:") if temp != "": duration = int(temp) print("------------------------") ########################################### # Open the original wave file originalWaveFile = wave.open("brownNoise.wav", "r") newWaveFile = wave.open("waveXP.wav", "w") # Compute how many frame does one period take nPeriodFrames = int(period*originalWaveFile.getframerate()) print("Real modulation frequency is %s" % (originalWaveFile.getframerate()/nPeriodFrames)) print(nPeriodFrames) # Get one period of frames periodFrames = originalWaveFile.readframes(nPeriodFrames) print(len(periodFrames)) # Copy parameters but set to mono newWaveFile.setparams(originalWaveFile.getparams()) newWaveFile.setnchannels(1) # Calculate the number of repetitions of the period repetitions = 1 if duration/period > 1: repetitions = int(duration*originalWaveFile.getframerate()/nPeriodFrames) print(repetitions) # Loop for how many repetitions as requested for j in range(1, repetitions): for i in range(1, nPeriodFrames): # Calculate the sin function for amplitude modulation volume = maxVolume*(math.sin((2*math.pi*i/len(periodFrames)-math.pi/2))+1)/2 # Single tone #newWaveFile.writeframes(struct.pack('<h', int(volume*math.cos(440*math.pi*float(i)/float(originalWaveFile.getframerate()))))) # White noise newWaveFile.writeframes(struct.pack('<h', int(volume*random.random()))) # Brown noise #newWaveFile.writeframes(struct.pack('<h', int(volume*acoustics.generator.brown(1)))) ########################################### # Get a one period long segment of the wave file # Close wave files originalWaveFile.close() newWaveFile.close() RE: Modulate brown noise in python - Larz60+ - Mar-27-2018 did you see this in the documentation for python acoustics package: http://python-acoustics.github.io/python-acoustics/generator.html?highlight=brown%20noise I have to add, this is the first time I have seen this package. It looks to have some really awesome classes in it. RE: Modulate brown noise in python - pyxel - Mar-28-2018 Indeed I looked at the documentation, but I did not find what I wanted. I tried something else: newWaveFile.writeframes(struct.pack('h'*framerate*duration, int(maxVolume*0.7*acoustics.generator.brown(framerate*duration))))But python does not like it: Any idea?
|