-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioSplicer.py
More file actions
114 lines (81 loc) · 4.74 KB
/
audioSplicer.py
File metadata and controls
114 lines (81 loc) · 4.74 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import wave
import os
import glob
def split_wav_with_labels(input_file, output_dir, labels, times, initials, run_number, prepost):
"""
Split a .wav file according to given labels and start-times.
Parameters:
input_file (str): Path to the input wav file.
output_dir (str): Directory to save the chunks.
labels (list): List of labels for each segment.
times (list): List of start times (in seconds) for each segment.
The last segment will run until EOF.
"""
os.makedirs(output_dir, exist_ok=True)
with wave.open(input_file, 'rb') as wav:
frame_rate = wav.getframerate()
n_channels = wav.getnchannels()
samp_width = wav.getsampwidth()
total_frames = wav.getnframes()
# Convert start times to frame positions
start_frames = [int(t * frame_rate) for t in times]
start_frames.append(total_frames) # add end-of-file marker
for i, label in enumerate(labels):
try:
start_frame = start_frames[i]
end_frame = start_frames[i + 1]
num_frames = end_frame - start_frame
wav.setpos(start_frame)
frames = wav.readframes(num_frames)
output_file = os.path.join(output_dir, f"{initials}_run{run_number}_{prepost}_{i:03d}_{label}.wav")
with wave.open(output_file, 'wb') as out_wav:
out_wav.setnchannels(n_channels)
out_wav.setsampwidth(samp_width)
out_wav.setframerate(frame_rate)
out_wav.writeframes(frames)
print(f"Saved {output_file}")
except Exception as e:
print(f"Audio was likely cropped too short for final trial. Failed at label: {label}. Error message: {e}")
if __name__ == "__main__":
labels146 = [
"sing","cue-sing","sing","cue-sing","sing","Fixation1","Fixation1","cue-nosing",
"nosing","cue-nosing","nosing","cue-nosing","nosing","Fixation2","Fixation2",
"cue-sing","sing","cue-sing","sing","cue-sing","sing","Fixation1","Fixation1",
"cue-nosing","nosing","cue-nosing","nosing","cue-nosing","nosing","Fixation2",
"Fixation2","cue-sing","sing","cue-sing","sing","cue-sing","sing","Fixation1",
"Fixation1","cue-nosing","nosing","cue-nosing","nosing","cue-nosing","nosing",
"Fixation2","Fixation2","cue-sing","sing","cue-sing","sing","cue-sing","sing",
"Fixation1","Fixation1","cue-nosing","nosing","cue-nosing","nosing","cue-nosing",
"nosing","Fixation2","Fixation2","Fixation2"
]
labels235 = [
"nosing","cue-nosing","nosing","cue-nosing","nosing","Fixation1","Fixation1","cue-sing",
"sing","cue-sing","sing","cue-sing","sing","Fixation2","Fixation2",
"cue-nosing","nosing","cue-nosing","nosing","cue-nosing","nosing","Fixation1","Fixation1",
"cue-sing","sing","cue-sing","sing","cue-sing","sing","Fixation2",
"Fixation2","cue-nosing","nosing","cue-nosing","nosing","cue-nosing","nosing","Fixation1",
"Fixation1","cue-sing","sing","cue-sing","sing","cue-sing","sing",
"Fixation2","Fixation2","cue-nosing","nosing","cue-nosing","nosing","cue-nosing","nosing",
"Fixation1","Fixation1","cue-sing","sing","cue-sing","sing","cue-sing",
"sing","Fixation2","Fixation2","Fixation2"
]
times = [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 248, 252]
directory_input = input("Enter the full directory of the folder with your .wav files.")
files = glob.glob(directory_input + "/*/*.wav", recursive=True)
output_dir = "labeled_splices"
for file in files:
if "labeled_splices" in file:
continue
subject_initials = file.split("/")[-1].split("_")[0]
run_number = file.lower().split("run")[-1][0]
prepost_letter = file.split("_")[-1].split(".wav")[0]
if "run1" in file.lower() or "run4" in file.lower() or "run6" in file.lower():
split_wav_with_labels(file, output_dir, labels146, times, subject_initials, run_number, prepost_letter)
elif "run2" in file.lower() or "run3" in file.lower() or "run5" in file.lower():
split_wav_with_labels(file, output_dir, labels235, times, subject_initials, run_number, prepost_letter)
else:
print(f"Run not recognized in {file}. Confirm that 'runx' is included in the file name denoting the run number. (i.e., 'run2')")