How to automate linearly or exponentially? #111
Answered
by
DBraun
samuelbraun04
asked this question in
Q&A
-
How could I automate the frequency of my filter_processor to go from 300.0 Hz to 20000.0 Hz over the duration of the length of a wav file? My current code automates using a sine wave, but I'd like the automation to follow a linear or exponential line. My current code: from librosa import load
from moviepy.editor import AudioFileClip
from scipy.io.wavfile import write
from pydub import AudioSegment
import numpy as np
import dawdreamer as daw
SAMPLE_RATE = 44100
BUFFER = 512
#Function to properly format song
def loadAudioFile(song):
sig, rate = load(song, duration=None, mono=False, sr=SAMPLE_RATE )
assert(rate == SAMPLE_RATE )
return sig
#Makes a sine wave for automation
def make_sine(freq: float, duration: float, sr=SAMPLE_RATE ):
N = int(duration * sr)
return np.sin(np.pi*2.*freq*np.arange(N)/sr)
#Turns graph into wav file
def exportGraphAsWav(graph, file, name):
engine.load_graph(graph)
durationOfClip = AudioFileClip(file)
engine.render(durationOfClip.duration)
durationOfClip.close()
audio = engine.get_audio()
write(r'C:\Users\samlb\Downloads'+'\\'+name+'.wav', SAMPLE_RATE , audio.transpose())
#The wav I'm using
file = r'C:\Users\samlb\Documents\Minor\Cm - 128 BPM.wav'
#Set the engine
engine = daw.RenderEngine(SAMPLE_RATE , BUFFER)
#Set playback processor and load the song
playback_processor = engine.make_playback_processor("song", loadAudioFile(file))
#Set filter processor and automate the frequency from 300 Hz to 20000 Hz (currently doesn't do this)
filter_processor = engine.make_filter_processor("the_filter", "low", 300.0)
filter_processor.mode = 'low'
filter_processor.frequency = 300.0
freq_automation = make_sine(.5, AudioSegment.from_wav(file).duration_seconds)*5000.0 + 7000.0
filter_processor.set_automation("freq", freq_automation)
#Set graph
graph = [
(playback_processor, []),
(filter_processor, ["song"])
]
#Load graph and export as wav
exportGraphAsWav(graph, file, 'newfile') Any help would be greatly appreciated. Thank you. |
Beta Was this translation helpful? Give feedback.
Answered by
DBraun
Jul 20, 2022
Replies: 1 comment 1 reply
-
There are many subjective ways to do this. import numpy as np
num_samples = AudioSegment.from_wav(file).duration_seconds * SAMPLE_RATE
a = np.linspace(0, 1, num=num_samples, endpoint=True)
# now a starts at 0 and ends at 1
# choose a base, either e or another number greater than 1
base = np.e
# base = 2
b = np.power(base, a)
# now b starts at 1 and ends at base
# remap from [1,base] to [0, 1]
c = (b-1)/(base-1)
# remap from [0, 1] to [300, 20000]
freq = 300. + (20000-300.)*c
filter_processor.set_automation("freq", freq) For linear, just set |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
samuelbraun04
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
There are many subjective ways to do this.
For linear, just set
c = a
before creatingfreq
. Also experiment with matplotlib, wolfram alpha etc to your liking.