-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio2.py
45 lines (35 loc) · 1.22 KB
/
audio2.py
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
import time, sys
import pymedia.audio.sound as sound
import pymedia.audio.acodec as acodec
def voiceRecorder( secs, name ):
f= open( name, 'wb' )
# Minimum set of parameters we need to create Encoder
cparams= { 'id': acodec.getCodecId( 'mp3' ),
'bitrate': 128000,
'sample_rate': 44100,
'channels': 2 }
ac= acodec.Encoder( cparams )
snd= sound.Input( 44100, 2, sound.AFMT_S16_LE)
snd.start()
# Loop until recorded position greater than the limit specified
while snd.getPosition()<= secs:
s= snd.getData()
if s and len( s ):
for fr in ac.encode( s ):
# We definitely should use mux first, but for
# simplicity reasons this way it'll work also
f.write( fr )
else:
time.sleep( .003 )
# Stop listening the incoming sound from the microphone or line in
snd.stop()
# -------------------------------------------------------------------
# Record stereo sound from the line in or microphone
# and save it as mp3 file
# Specify length and output file name
# http://pymedia.org/
if __name__ == "__main__":
if len( sys.argv )!= 3:
print 'Usage: voice_recorder '
else:
voiceRecorder( int( sys.argv[ 1 ] ), sys.argv[ 2 ] )