Thanks，I deleted the other post. I tried pyaudio many many many many times but it didn't work. then I tried the arecord through subprocess. : press joystick button to start recording and press button again to stop recording, It works randomly: when I press the button, the background picture changes successfully but sometimes it couldn't record. really strange to me.
Code: Select all
#detect button press
if joystick.get_numbuttons() >= 1 and joystick.get_button( 0 ) == 0 and button_pressed:
button_pressed = False
is_recording = not is_recording
dotting = dotting + 1
filename = "%s.wav" % datetime.now().strftime("%Y%m%d_%H%M%S")
p=subprocess.Popen(['arecord', '--device=hw:1,0', '--format', 'S16_LE', '--rate', '44100-c1', filename], shell=False)
background = pygame.image.load(background_image2).convert()
stopFlag = Event()
timer = MyTimer(stopFlag,time.clock())
text2 = font.render('.', True, BLACK)
#record, remaining here
#stop recording, save record
#back to previous question
timer.do_run = False
background = pygame.image.load(background_image).convert()
paddyg wrote: ↑
Thu Nov 02, 2017 4:17 pm
Well that sounds like progress. How many times did print(index) run?
By the way, you did include the lines that I didn't bother to re-write (i.e. reading from the stream and appending to the frames list variable? (I will edit my previous post to make it clearer, also fix the line that got the wrong side of the closing code tag!)
If the process still isn't fast enough or you want to record long sounds and memory becomes an issue then it would probably be quite feasible to write directly to file.
PS I see you've started another thread on essentially this subject which may cause general confusion for people trying to help. If the example code you started from works ok then I would suggest you move from that in small stages, making sure that your code runs as expected before adding more complexity. i.e. stick with the same CHUNK size and just get the recording to stop when you press the joystick button. Then figure out the logic to keep track of the number of times the button has been pressed so you can start and then stop the recording. etc. etc.