-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavlength.py
92 lines (64 loc) · 2.89 KB
/
wavlength.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
""" Created by Christian Morte. 2-25-2015.
iOS Development Team - SIMHome - CalPlug
"""
import os.path
import contextlib
import wave
class PathDoesNotExistError(Exception): pass
class NotWavFileError(Exception): pass
def get_wav_path() -> str:
'''Gets name of wav file from text and returns full path of wav'''
path = input("Please enter the full path of the .wav file: ")
print("Finding .wav file...")
if os.path.exists(path):
ext = os.path.splitext(path)[1]
if ext != '.wav':
raise NotWavFileError
print ("File found! Opening {}...".format(os.path.basename(path)))
else:
raise PathDoesNotExistError
return path
def outputToFile() -> bool:
'''Returns true if user wants to output length of wav file into a txt file'''
while True:
outputToFile = input("Do you want to output the length onto a txt file? [Y/N] ")
if outputToFile.upper() in ["Y", "YES"]:
return True
elif outputToFile.upper() in ["N", "NO"]:
return False
else:
print("Invalid command! Please enter again...")
def output_length(path: str, length: float) -> None:
'''Outputs the duration of wave file into same directory as wav file'''
with open(path, 'w') as file:
file.write(str(length))
def get_wav_length() -> None:
'''Outputs length of wav file on the console and
in a text file if the user desires'''
try:
path = get_wav_path()
file_with_ext = os.path.basename(path)
filename = file_with_ext.rstrip('.wav')
with wave.open(path,'r') as file:
frame = file.getnframes()
rate = file.getframerate()
duration = frame / float(rate)
print("\n{} was {} seconds long.\n".format(file_with_ext,
duration))
if outputToFile():
output_path = path.rstrip(file_with_ext) + filename + '-length.txt'
output_length(output_path, duration)
print("Successfully written duration in file {} in path {}!\n\nClosing {}...".format(filename + 'length.txt',
os.path.dirname(output_path) ,
filename + 'length.txt'))
print("Closing {}...".format(file_with_ext))
except PathDoesNotExistError:
print("Path was not valid! File not found.")
except NotWavFileError:
print("Path was not a wav file! Exiting...")
print("Exiting wavlength.py script...")
def main() -> None:
'''Runs the script to get duration of wav file'''
get_wav_length()
if __name__ == '__main__':
main()