1
+ import requests
1
2
import soundcard as sc
2
3
import soundfile as sf
4
+ import io
3
5
4
6
5
7
def speaker (filename : str , seconds ):
6
8
"""
7
9
Records the device's speaker.
8
10
9
- :param filename: Name of audio file written.
11
+ :param filename: Name and directory of the audio file written.
10
12
:param seconds: Duration to record (seconds).
11
13
"""
12
14
with sc .get_microphone (
@@ -23,7 +25,7 @@ def microphone(filename: str, seconds):
23
25
"""
24
26
Records the device's device.
25
27
26
- :param filename: Name of audio file written.
28
+ :param filename: Name and directory of the audio file written.
27
29
:param seconds: Duration to record (seconds).
28
30
"""
29
31
with sc .get_microphone (
@@ -33,3 +35,31 @@ def microphone(filename: str, seconds):
33
35
data = mic .record (numframes = 44100 * seconds )
34
36
sf .write (file = filename , data = data , samplerate = 44100 )
35
37
return filename
38
+
39
+
40
+ def url (url : str , filename : str ):
41
+ """
42
+ Downloads audio from the provided URL.
43
+
44
+ :param url: URL of the audio file.
45
+ :param filename: Name and directory of the audio file written.
46
+ :param seconds: Duration to record (seconds).
47
+ """
48
+ print (f"Downloading audio from URL..." )
49
+ headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246' }
50
+
51
+ try :
52
+ response = requests .get (url , headers = headers )
53
+ response .raise_for_status () # Raises an HTTPError if code is 4XX/5XX
54
+
55
+ except requests .exceptions .HTTPError as http_err :
56
+ print (f'HTTP error occurred: { http_err } ' )
57
+ return None
58
+ except Exception as err :
59
+ print (f'An error occurred: { err } ' )
60
+ return None
61
+
62
+ # Ensure the response content is in a suitable format for soundfile
63
+ data , samplerate = sf .read (io .BytesIO (response .content ))
64
+ sf .write (file = filename , data = data , samplerate = samplerate )
65
+ return filename
0 commit comments