-
Notifications
You must be signed in to change notification settings - Fork 17
/
MixSpeechNoise.praat
92 lines (76 loc) · 2.59 KB
/
MixSpeechNoise.praat
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
# PRAAT SCRIPT "MIX SPEECH WITH NOISE"
# This Praat script takes a directory of sound files, and a single noise file, mixes the stimuli with the noise at a specified SNR, and writes the resultant sounds as WAV files in a specified directory. Two methods of (optional) intensity scaling of the resultant sounds is available.
# AUTHOR: Daniel McCloy ([email protected])
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE v3.0: http://www.gnu.org/licenses/gpl.html
form Mix speech with noise
comment Make sure to put trailing slashes in the folder paths
sentence Stimuli_folder /home/dan/Desktop/stimuli/
sentence Noise_file /home/dan/Desktop/SpeechShapedNoise.wav
sentence Output_folder /home/dan/Desktop/stimuliWithNoise/
real Desired_SNR 0
optionmenu finalIntensity: 1
option match final intensity to stimulus intensity
option maximize (scale peaks to plus/minus 1)
option just add noise to signal (don't scale result)
endform
echo Mixing at SNR 'desired_SNR'
# if output_folder already exists, this does nothing:
createDirectory("'output_folder$'")
# NOISE
noise = Read from file... 'noise_file$'
Rename... noise
noiseDur = Get total duration
noiseRMS = Get root-mean-square... 0 0
# STIMULI
stimList = Create Strings as file list... stimList 'stimuli_folder$'*.wav
n = Get number of strings
for i from 1 to n
# READ IN EACH STIMULUS
select stimList
curFile$ = Get string... 'i'
curSound = Read from file... 'stimuli_folder$''curFile$'
curDur = Get total duration
curRMS = Get root-mean-square... 0 0
curInten = Get intensity (dB)
while curDur > noiseDur
# duplicate noise and concatenate it to itself
select noise
temp = Concatenate
plus noise
noise = Concatenate
select temp
Remove
select noise
noiseDur = Get total duration
endwhile
# CALCULATE NOISE COEFFICIENT THAT YIELD DESIRED SNR
# SNR = 20*log10(SignalAmpl/NoiseAmpl)
# NoiseAmpl = SignalAmpl / (10^(SNR/20))
noiseAdjustCoef = (curRMS / (10^(desired_SNR/20))) / noiseRMS
# MIX SIGNAL AND NOISE AT SPECIFIED SNR
select curSound
Formula... self[col] + 'noiseAdjustCoef' * Sound_noise[col]
# SCALE RESULT IF NECESSARY
if finalIntensity = 1
# scale to match original
Scale intensity... curInten
elsif finalIntensity = 2
# scale to +/- 1
Scale peak... 0.99
endif
# WRITE OUT FINAL FILE
select curSound
Save as WAV file... 'output_folder$''curFile$'
Remove
endfor
if finalIntensity = 1
printline Scaling to match original intensities
elsif finalIntensity = 2
printline Scaling peaks to +/- 0.99
else
printline No scaling requested, watch out for clipping
endif
select noise
plus stimList
Remove
printline Done!