-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainProgram.py
106 lines (98 loc) · 4.11 KB
/
mainProgram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import argparse
import os
import sys
import pydub
import random
#Let's add random noise to audio files!
def main():
# parse all the arguments to the client
parser = argparse.ArgumentParser(description="Audio Hasher\nMinimum audio length is 3 seconds!")
parser.add_argument('-s','--sourceFile', help='Path to the audio file to modify', required=True)
parser.add_argument('-n','--noiseFile', help='Path to the audio file to interpolate into the source', required=True)
parser.add_argument('-f','--noiseFactor', help='Fraction of the source audio to replace with noise. (Number between 0 and 1 please!)', required=False)
parser.add_argument('-o','--outputFile', help='Specify the name of the output audio file', required=False)
# get the arguments into local variables
args = vars(parser.parse_args())
sourceFile = args['sourceFile']
noiseFile = args['noiseFile']
outputFile = args['outputFile']
noiseFactor = int(args['noiseFactor'])
source_audio = pydub.AudioSegment.empty()
noise_audio = pydub.AudioSegment.empty()
try:
fileFormat = sourceFile[-3:]
source_audio = pydub.AudioSegment.from_file(sourceFile, format=fileFormat)
if(len(source_audio) < 3000):
print("error! The source file is too short! It should be at least 3 seconds long")
exit(-2)
except:
print ( "error opening the source file: %s" % (sourceFile))
e = sys.exc_info()[0]
print( "\tError: %s" % e )
exit(-1)
try:
fileFormat = noiseFile[-3:]
noise_audio = pydub.AudioSegment.from_file(noiseFile, format=fileFormat)
if(len(noise_audio) < 3000):
print("error! The noise file is too short! It should be at least 3 seconds long")
exit(-2)
except:
print ( "error opening the noise file: %s" % (noiseFile))
e = sys.exc_info()[0]
print( "\tError: %s" % e )
exit(-1)
final_audio = pydub.AudioSegment.empty()
# source_audio[2000:7000] + noise_audio[3000:5000] + source_audio[9000:14000]
#save the length of the original audio for bound checking
original_length = len(source_audio)
# leave the first 1% of the audio unedited
currentIndex = original_length/100
final_audio += source_audio[0:currentIndex]
# Define the noise factor
if(noiseFactor):
if(noiseFactor < 0):
noiseFactor = random.random()
while(noiseFactor > 1):
noiseFactor = noiseFactor / 10
else:
noiseFactor = 0.6
# A boolean flag to safeguard against playing the noise too many
# times in a row
noiseJustUsed = False
while(currentIndex < original_length):
percentage = 100*currentIndex/original_length
print ("\tProgress: %d percent\r" % percentage),
# Randomly pick how many seconds of the audio we will work on in
# this step of the loop
nextSegment = random.randint(600,2350)
audioSnippet = pydub.AudioSegment.empty()
if(random.random() < noiseFactor and noiseJustUsed == False):
# Choose a random starting point in the noise audio
startIndex = random.randint(0,len(noise_audio)-nextSegment)
# Pull out a segment of the noise file
audioSnippet = noise_audio[startIndex:startIndex+nextSegment]
noiseJustUsed = True
else:
# Pull out the next segment of the source file
audioSnippet = source_audio[currentIndex:currentIndex+nextSegment]
noiseJustUsed = False
final_audio += audioSnippet
currentIndex += nextSegment
#
print("\nSaving the output..."),
if (outputFile):
try:
fileFormat = outputFile[-3:]
file_handle = final_audio.export(outputFile, format=fileFormat)
except:
print ( "error opening the output file: %s" % (outputFile))
exit(-1)
else:
outputFile = "./output%d.mp3" % random.randint(1,9999)
file_handle = final_audio.export(outputFile, format="mp3")
print "Done!"
exit(0)
# this gives a main function in Python
if __name__ == "__main__":
main()
#end