2

I'm very new to Python,so I have small projet to get use to know more about Python, my project is simple, just recording audio use sounddevice but problem is , in documentary, it only record if have static seconds like code sample code below,and i have no idea how to stop or pause recording,in my case is press a key, like "enter to stop, shift to pause"

import sounddevice as sd
import soundfile as sf
import time
def sync_record(filename, duration, fs, channels):
   print('recording')
   myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=channels)
   sd.wait()
   sf.write(filename, myrecording, fs)
   print('done recording')
# playback file
sync_record('sync_record.wav', 10, 16000, 1)

so my question is, how can i pause and record audio with no duration required, and how to stop and pause that use sounddevice, thank you guys a lot

khánh lê
  • 21
  • 3
  • 1
    Please be more specific. Stack Overflow is not a free code writing service, nor is it meant to provide personalized guides and tutorials. See: [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Mar 16 '20 at 19:08

1 Answers1

0
print "* record until keypress"
from openexp.keyboard import keyboard
# Short timeout so get_key() doesn't block
my_keyboard = keyboard(exp, timeout=2)
all = []
while my_keyboard.get_key()[0] == None: # Loop until a key has been pressed
    data = stream.read(chunk) # Record data
    all.append(data) # Add the data to a buffer (a list of chunks)
print "* done recording"

This illustrates how you could stream data till a key is pressed.

import keyboard
k = keyboard.read_key()  # in my python interpreter, this captures "enter up"
k = keyboard.read_key()  # so repeat the line if in the python interpreter

Use the same logic with your code. So that the recording stops once the user presses a key.

Roast Biter
  • 651
  • 1
  • 6
  • 22