2

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)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Interesting question! Is there a reason why this has to be done in real-time, and say not by loading a separate pre-prepared audio file? – Kingsley Jun 21 '20 at 06:46
  • 1
    Thanks! I want to increase/decrease the revs using an input (arrow keys, for example) and would like to connect the back end to a front end. The program above loads an audio file and replays it at different frequencies as the revs change. That did not work because every time the file is replayed, there is a noticeable gap. – Speedracer1702 Jun 21 '20 at 23:05

0 Answers0