vibha-mah commited on
Commit
3e60a6f
·
1 Parent(s): 1fc88fa

Upload WAVSplitting.ipynb

Browse files
Files changed (1) hide show
  1. WAVSplitting.ipynb +187 -0
WAVSplitting.ipynb ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 80,
6
+ "id": "ecebe95b",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import librosa\n",
11
+ "import os\n",
12
+ "import soundfile as sf"
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 89,
18
+ "id": "ca1d6ddb",
19
+ "metadata": {},
20
+ "outputs": [],
21
+ "source": [
22
+ "def splitWAVFile(file_path, short_name, step, folder_path):\n",
23
+ " # Load the audio file\n",
24
+ " audio, sr = librosa.load(file_path, sr=None)\n",
25
+ " \n",
26
+ " # Calculate the number of samples per segment\n",
27
+ " samples_per_segment = int(step * sr)\n",
28
+ " total_segments = len(audio) // samples_per_segment\n",
29
+ " \n",
30
+ " if not os.path.exists(folder_path):\n",
31
+ " os.makedirs(folder_path)\n",
32
+ " \n",
33
+ " if(len(audio)<samples_per_segment):\n",
34
+ " print(drop)\n",
35
+ " return\n",
36
+ " \n",
37
+ " for i in range(total_segments):\n",
38
+ " start_sample = i * samples_per_segment\n",
39
+ " end_sample = start_sample + samples_per_segment\n",
40
+ " \n",
41
+ " # Extract the segment\n",
42
+ " segment = audio[start_sample:end_sample]\n",
43
+ " \n",
44
+ " output_path = folder_path+\"/\"+short_name+\"_\"+str(i)+\".wav\"\n",
45
+ " sf.write(output_path, segment, sr)"
46
+ ]
47
+ },
48
+ {
49
+ "cell_type": "code",
50
+ "execution_count": 90,
51
+ "id": "ad236328",
52
+ "metadata": {
53
+ "scrolled": true
54
+ },
55
+ "outputs": [
56
+ {
57
+ "name": "stdout",
58
+ "output_type": "stream",
59
+ "text": [
60
+ "0\n",
61
+ "0\n",
62
+ "0\n",
63
+ "0\n",
64
+ "0\n",
65
+ "0\n",
66
+ "0\n",
67
+ "0\n",
68
+ "0\n",
69
+ "0\n",
70
+ "0\n",
71
+ "0\n",
72
+ "0\n",
73
+ "0\n",
74
+ "0\n",
75
+ "0\n",
76
+ "0\n",
77
+ "0\n",
78
+ "0\n",
79
+ "0\n",
80
+ "0\n",
81
+ "0\n",
82
+ "0\n",
83
+ "0\n",
84
+ "0\n",
85
+ "0\n",
86
+ "0\n",
87
+ "0\n",
88
+ "0\n",
89
+ "0\n",
90
+ "0\n",
91
+ "0\n",
92
+ "0\n",
93
+ "0\n",
94
+ "0\n",
95
+ "0\n",
96
+ "0\n",
97
+ "0\n",
98
+ "0\n",
99
+ "0\n",
100
+ "0\n",
101
+ "0\n",
102
+ "0\n",
103
+ "0\n",
104
+ "0\n",
105
+ "0\n",
106
+ "0\n",
107
+ "0\n",
108
+ "0\n",
109
+ "0\n",
110
+ "0\n",
111
+ "0\n",
112
+ "0\n",
113
+ "0\n",
114
+ "0\n",
115
+ "0\n",
116
+ "0\n",
117
+ "0\n",
118
+ "0\n",
119
+ "0\n",
120
+ "0\n",
121
+ "0\n",
122
+ "0\n",
123
+ "0\n",
124
+ "0\n"
125
+ ]
126
+ }
127
+ ],
128
+ "source": [
129
+ "nocNoise_folder = \"/Users/lainiecederholm/bat data/Noctula nyctalus with noise\"\n",
130
+ "nocNoNoise_folder = \"/Users/lainiecederholm/bat data/Noctula nyctalus with out social sound and noise\"\n",
131
+ "pipNoise_folder = \"/Users/lainiecederholm/bat data/Pipistrellus pygmaus with social sound\"\n",
132
+ "pipNoNoise_folder = \"/Users/lainiecederholm/bat data/Pipistrellus pygmaus without social sound\"\n",
133
+ "\n",
134
+ "\n",
135
+ "for file in os.listdir(nocNoise_folder):\n",
136
+ " if (file.endswith(\".wav\")):\n",
137
+ " file_path = \"/Users/lainiecederholm/bat data/Noctula nyctalus with noise/\"+file\n",
138
+ " splitWAVFile(file_path, file, 2, \"/Users/lainiecederholm/bat data/Noctula with Noise Crop\")\n",
139
+ " \n",
140
+ "for file in os.listdir(nocNoNoise_folder):\n",
141
+ " if (file.endswith(\".wav\")):\n",
142
+ " file_path = \"/Users/lainiecederholm/bat data/Noctula nyctalus with out social sound and noise/\"+file\n",
143
+ " splitWAVFile(file_path, file, 2, \"/Users/lainiecederholm/bat data/Noctula without Noise Crop\")\n",
144
+ "\n",
145
+ "for file in os.listdir(pipNoise_folder):\n",
146
+ " if (file.endswith(\".wav\")):\n",
147
+ " file_path = \"/Users/lainiecederholm/bat data/Pipistrellus pygmaus with social sound/\"+file\n",
148
+ " splitWAVFile(file_path, file, 2, \"/Users/lainiecederholm/bat data/Pipistrellus with Noise Crop\")\n",
149
+ " \n",
150
+ "for file in os.listdir(pipNoNoise_folder):\n",
151
+ " if (file.endswith(\".wav\")):\n",
152
+ " file_path = \"/Users/lainiecederholm/bat data/Pipistrellus pygmaus without social sound/\"+file\n",
153
+ " splitWAVFile(file_path, file, 2, \"/Users/lainiecederholm/bat data/Pipistrellus without Noise Crop\")\n",
154
+ "print(drop)"
155
+ ]
156
+ },
157
+ {
158
+ "cell_type": "code",
159
+ "execution_count": null,
160
+ "id": "3fc56932",
161
+ "metadata": {},
162
+ "outputs": [],
163
+ "source": []
164
+ }
165
+ ],
166
+ "metadata": {
167
+ "kernelspec": {
168
+ "display_name": "Python 3 (ipykernel)",
169
+ "language": "python",
170
+ "name": "python3"
171
+ },
172
+ "language_info": {
173
+ "codemirror_mode": {
174
+ "name": "ipython",
175
+ "version": 3
176
+ },
177
+ "file_extension": ".py",
178
+ "mimetype": "text/x-python",
179
+ "name": "python",
180
+ "nbconvert_exporter": "python",
181
+ "pygments_lexer": "ipython3",
182
+ "version": "3.11.4"
183
+ }
184
+ },
185
+ "nbformat": 4,
186
+ "nbformat_minor": 5
187
+ }