Reputation: 67
I want to create engine rev sounds (similar to the RevHeadz app on android / apple) using python. Is there a library or a way to do this? I tried increasing frequency of a sample audio file as the revs increase, but I don't know how to change frequency of an audio sample on the fly (fast enough that it seems continuous). Any suggestions are greatly appreciated!
Code for what I already tried -
import sounddevice as sd
import soundfile as sf
import curses
import time
screen = curses.initscr() # initialize a terminal screen for you
curses.noecho() # don't show what input was entered
curses.cbreak() #character break -> don't wait for enter to accept input
screen.keypad(True)
screen.scrollok(True)
data, fs = sf.read('engine_start.wav', dtype='float32')
sd.play(data, fs)
sd.wait()
filename = 'engine_short.wav'
# Extract data and sampling rate from file
data, fs = sf.read(filename, dtype='float32')
sd.play(data, fs, loop = True)
#status = sd.wait() # Wait until file is done playing
class ecu:
def __init__(self, frequency):
self.fs = 1
def speedUp(self):
if (self.fs + 0.1 <= 2):
self.fs += 0.1
def slowDown(self):
if (self.fs - 0.1 >= 0.5):
self.fs -= 0.1
newECU = ecu(fs)
while True:
char = screen.getch()
if char == 113:
screen.addstr("Exiting...\n")
curses.echo()
curses.nocbreak()
screen.keypad(False)
curses.endwin() #close application
exit() # q
elif char == curses.KEY_UP:
if (newECU.fs + 0.005 <= 2.0):
newECU.fs += 0.005
value = "UP: " + str(newECU.fs) + "\n"
sd.play(data, fs * newECU.fs, loop = True)
screen.addstr(value)
time.sleep(0.100)
elif char == curses.KEY_DOWN:
if (newECU.fs - 0.005 >= 1.0):
newECU.fs -= 0.005
value = "DOWN: " + str(newECU.fs) + "\n"
sd.play(data, fs * newECU.fs, loop = True)
screen.addstr(value)
time.sleep(0.100)
elif char == 115:
value = "Current Speed: " + str(newECU.fs) + "\n"
screen.addstr(value)
Upvotes: 2
Views: 577