I've written a program to detect my keyboard presses and generate an audio file with pydub from this recording. When I inspect my two audio files (generated by python and recorded by smartphone) in audicity it's quiet obvious that the recording from my smartphone has spikes (higher amplitude) and some sort of fade in and fade out on every longer "typing sequence", but my generated file has not. Is it possible to detect and adjust the amplitude of a "typing sequence" in pydub, that it sounds more natural?
Here is my code to detect my key strokes and to generate my audio track:
def getTimestamp():
return round(time.time()*1000)
if __name__ == "__main__":
recording = {}
start, stop = (0, 0)
running = False
while True:
if keyboard.is_pressed("f10"):
start = getTimestamp()
print("Recording...")
running = True
if keyboard.is_pressed("f12"):
stop = getTimestamp()
print("Stopped recording!")
break
if running:
current_time = getTimestamp() - start
recording[current_time] = keyboard.read_key()
# create audio file
createSound("./test.wav", start, stop, recording)
print("Done!")
The createSound()
function looks like this:
from pydub import AudioSegment
def createSound(filename, start, stop, recording):
# create empty track
track = AudioSegment.silent(duration=(stop-start))
for timestamp, key in recording.items():
keypress = AudioSegment.from_file("./click.wav")
# add keypress to track
track = track.overlay(keypress, position=int(timestamp))
# export track
track.export(filename, format="wav")