-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_percussion_samples.py
More file actions
executable file
·303 lines (238 loc) · 9.46 KB
/
Copy pathscrape_percussion_samples.py
File metadata and controls
executable file
·303 lines (238 loc) · 9.46 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
"""
Advanced percussion sample scraper using BeautifulSoup
Scrapes Freesound.org to find and download the best tabla/daf samples
"""
import os
import sys
import re
import time
import requests
from bs4 import BeautifulSoup
from pathlib import Path
from urllib.parse import urljoin, quote
# User agent to avoid being blocked
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
}
# Search queries for each stroke type
SEARCH_QUERIES = {
'tabla': {
'na.wav': ['tabla na stroke', 'tabla ta stroke'],
'tin.wav': ['tabla tin stroke', 'tabla closed stroke'],
'te.wav': ['tabla te stroke', 'tabla ti stroke'],
'ge.wav': ['tabla ge stroke', 'tabla bass open'],
'ka.wav': ['tabla ka stroke', 'tabla bass slap'],
'dha.wav': ['tabla dha stroke', 'tabla dha']
},
'daf': {
'dum.wav': ['daf dum', 'frame drum bass', 'doumbek dum'],
'tak.wav': ['daf tak', 'frame drum high', 'doumbek tak'],
'roll.wav': ['frame drum roll', 'daf roll', 'riq roll']
}
}
def search_freesound(query, max_results=10):
"""
Search Freesound.org and scrape results
Returns list of sound info dicts
"""
search_url = f"https://freesound.org/search/?q={quote(query)}&f=duration:[0 TO 3]&s=downloads+desc"
try:
print(f" Searching: {query}")
response = requests.get(search_url, headers=HEADERS, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# Find sound results
results = []
sound_items = soup.find_all('div', class_='sample_player_small')
for item in sound_items[:max_results]:
try:
# Extract sound ID from player
sound_id_match = re.search(r'sound_id:\s*(\d+)', str(item))
if not sound_id_match:
continue
sound_id = sound_id_match.group(1)
# Find the link to get more info
parent = item.find_parent('div', class_='sound_filename')
if not parent:
parent = item.find_parent()
title_elem = parent.find('a') if parent else None
title = title_elem.text.strip() if title_elem else f"sound_{sound_id}"
# Get username
user_elem = soup.find('a', href=re.compile(f'/people/[^/]+/sounds/{sound_id}/'))
username = 'unknown'
if user_elem:
user_match = re.search(r'/people/([^/]+)/', user_elem.get('href', ''))
if user_match:
username = user_match.group(1)
results.append({
'id': sound_id,
'title': title,
'username': username,
'url': f'https://freesound.org/people/{username}/sounds/{sound_id}/'
})
except Exception as e:
print(f" Error parsing result: {e}")
continue
print(f" Found {len(results)} results")
return results
except Exception as e:
print(f" Error searching: {e}")
return []
def get_preview_url(sound_url):
"""
Scrape the sound page to get the preview download URL
"""
try:
response = requests.get(sound_url, headers=HEADERS, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# Method 1: Find preview URLs in the page source
preview_patterns = [
r'https://freesound\.org/data/previews/\d+/\d+_\d+-hq\.mp3',
r'https://freesound\.org/data/previews/\d+/\d+_\d+-lq\.mp3',
]
for pattern in preview_patterns:
match = re.search(pattern, response.text)
if match:
return match.group(0)
# Method 2: Look for download/preview buttons
download_links = soup.find_all('a', href=re.compile(r'previews.*\.mp3'))
if download_links:
return urljoin(sound_url, download_links[0]['href'])
# Method 3: Construct preview URL from sound ID
sound_id_match = re.search(r'/sounds/(\d+)/', sound_url)
if sound_id_match:
sound_id = sound_id_match.group(1)
# Try to extract preview from embedded player
player_match = re.search(rf'{sound_id}_(\d+)-hq\.mp3', response.text)
if player_match:
file_id = player_match.group(1)
folder = sound_id[:3] # First 3 digits
return f'https://freesound.org/data/previews/{folder}/{sound_id}_{file_id}-hq.mp3'
return None
except Exception as e:
print(f" Error getting preview URL: {e}")
return None
def download_file(url, output_path):
"""Download a file from URL"""
try:
response = requests.get(url, headers=HEADERS, timeout=30, stream=True)
response.raise_for_status()
with open(output_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
file_size = os.path.getsize(output_path) / 1024 # KB
return True, file_size
except Exception as e:
return False, str(e)
def download_sample(filename, queries, output_dir):
"""
Try to download a sample by searching with multiple queries
"""
output_path = os.path.join(output_dir, filename)
# Skip if already exists
if os.path.exists(output_path):
print(f" ✓ {filename}: Already exists")
return True
print(f"\n [{filename}]")
# Try each search query
for query in queries:
results = search_freesound(query, max_results=5)
if not results:
continue
# Try to download the first good result
for i, result in enumerate(results, 1):
print(f" Trying result {i}: {result['title']} by {result['username']}")
# Get preview URL
preview_url = get_preview_url(result['url'])
if not preview_url:
print(f" ✗ Could not find preview URL")
continue
print(f" Downloading from: {preview_url}")
# Download to MP3 first
mp3_path = output_path.replace('.wav', '.mp3')
success, info = download_file(preview_url, mp3_path)
if success:
print(f" ✓ Downloaded: {info:.1f} KB")
print(f" Source: {result['url']}")
time.sleep(1) # Be nice to the server
return True
else:
print(f" ✗ Download failed: {info}")
# Wait between queries
time.sleep(2)
print(f" ✗ Could not download {filename}")
return False
def main():
print("="*70)
print(" FREESOUND PERCUSSION SCRAPER")
print("="*70)
print("\nThis script intelligently searches and downloads percussion samples")
print("from Freesound.org using web scraping (no API key needed).")
print()
# Check dependencies
try:
from bs4 import BeautifulSoup
except ImportError:
print("Error: BeautifulSoup not installed")
print("Install with: pip install beautifulsoup4")
sys.exit(1)
try:
import requests
except ImportError:
print("Error: requests library not installed")
print("Install with: pip install requests")
sys.exit(1)
# Create directories
os.makedirs('sounds/tabla', exist_ok=True)
os.makedirs('sounds/daf', exist_ok=True)
print("✓ Directories created\n")
# Download samples
stats = {'success': 0, 'failed': 0, 'skipped': 0}
for category, samples in SEARCH_QUERIES.items():
print(f"\n{'='*70}")
print(f" {category.upper()} SAMPLES")
print(f"{'='*70}")
output_dir = f'sounds/{category}'
for filename, queries in samples.items():
result = download_sample(filename, queries, output_dir)
if result:
if os.path.exists(os.path.join(output_dir, filename.replace('.wav', '.mp3'))):
stats['success'] += 1
else:
stats['skipped'] += 1
else:
stats['failed'] += 1
time.sleep(1) # Rate limiting
# Summary
print("\n" + "="*70)
print(" DOWNLOAD COMPLETE")
print("="*70)
print(f"\n ✓ Successfully downloaded: {stats['success']} files")
if stats['skipped'] > 0:
print(f" ⊙ Skipped (already exist): {stats['skipped']} files")
if stats['failed'] > 0:
print(f" ✗ Failed to download: {stats['failed']} files")
print("\n" + "-"*70)
print(" NEXT STEPS")
print("-"*70)
print("\n1. Files are saved as MP3 format (HQ previews)")
print("\n2. Update config to use MP3:")
print(" Edit configs/daf_tabla.yaml")
print(" Change: ./sounds/tabla/na.wav → ./sounds/tabla/na.mp3")
print("\n3. Or convert to WAV:")
print(" for f in sounds/tabla/*.mp3; do")
print(" ffmpeg -i \"$f\" \"${f%.mp3}.wav\"")
print(" done")
print("\n4. Run the demo:")
print(" python demos/daf_tabla.py")
print("\n5. License: All samples from Freesound.org (CC licensed)")
print(" Attribution may be required - check individual sound pages")
print("\n" + "="*70 + "\n")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\n\nScraping cancelled by user.")
sys.exit(0)