Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Modulate brown noise in python
#1
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()
Quote
#2
did you see this in the documentation for python acoustics package: http://python-acoustics.github.io/python...wn%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.
Quote
#3
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:
Error:
TypeError: only size-1 arrays can be converted to Python scalars
Any idea?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  creating terrain without noise or seed? hsunteik 3 1,906 Jan-25-2017, 10:27 PM
Last Post: ichabod801
  voronoi vs perlin noise? hsunteik 1 1,563 Jan-22-2017, 04:34 AM
Last Post: micseydel
  how to draw terrain like minecraft in python using opengl with perlin noise value? hsunteik 0 2,876 Jan-21-2017, 11:45 AM
Last Post: hsunteik
  what should i do after generating the perlin noise value? hsunteik 8 2,665 Jan-21-2017, 10:02 AM
Last Post: hsunteik

Forum Jump:


Users browsing this thread: 1 Guest(s)