-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsynthesiser_functions.py
More file actions
87 lines (60 loc) · 2.22 KB
/
synthesiser_functions.py
File metadata and controls
87 lines (60 loc) · 2.22 KB
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
# leg5@nyu.edu
import os
import scipy.io.wavfile as wave
import numpy as np
def save_synthesised_speech(speaker, speech_rate, save_dest, text_file=None,
utterance=None):
"""
speaker: OSX built-in speaker name. e.g., Samantha, Alex
speech_rate: number of words per minute
save_dest: where to save the file. needs aiff extension.
text_file: path to file containing text to be synthesised
utterance: can provide a string to say rather than load txt
"""
if text_file:
os.system("say -v %s -r %s -f %s -o %s" %
(speaker, speech_rate, text_file, save_dest))
elif utterance:
os.system("say -v %s %s -r %s -o %s" %
(speaker, utterance, speech_rate, save_dest))
pass
def concatenate_aud_files(file_list, result_dest):
"""
Concatenate auditory files together, and save result.
Uses SoX -- format is inferred from file extension.
"""
# make string which is a list of the files to be joined
file_command = ' '.join(file_list)
# execute command
os.system("sox %s %s" % (file_command, result_dest))
pass
def aiff_to_wav(aiff_fname, wav_fname):
"""
Convert aiff file to wav file.
Uses SoX -- format is inferred from file extension.
"""
# execute command
os.system("sox %s %s" % (aiff_fname, wav_fname))
pass
def remove_silence(wav_file, remove_final, save_dest):
"""
When synthesising single words, OSX Samantha adds some ending silence.
This function removes a user-defined amount of silence.
wav_file: location of wave file
remove_final: how much of the file to remove (in seconds)
"""
# load file
fs, stim = wave.read(wav_file)
# get indices based on sample rate
stim_length = stim.shape[0]/float(fs)
n_samples_remove = stim_length-remove_final*float(fs)
# save the cleaned file
cleaned_stim = stim[0:n_samples_remove]
wave.write(save_dest, fs, cleaned_stim)
pass
def save_empty_wav(wav_length, fs, save_dest):
# make zero matrix
empty_wav = np.zeros(wav_length*float(fs))
# save as wav file
wave.write(save_dest, fs, empty_wav)
pass