0

Same as in topic, i want to create timer while the recording function would run. I have tried a lot of ideas but all of them resulted as an error or just broke up program, maybe you have some ideas?

class rec(object):  
  def __init__(self):
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    CHUNK = 1024
    WAVE_OUTPUT_FILENAME = str(e2.get() + '.wav')       

    try:        
      RECORD_SECONDS = int(e1.get())
    except ValueError:
      tkMessageBox.showinfo("Error", "Please Enter number of seconds")

    i = 0
    while os.path.exists(WAVE_OUTPUT_FILENAME):     
      WAVE_OUTPUT_FILENAME = str(e2.get() + '%d.wav'%i)
      i += 1

    audio = pyaudio.PyAudio()
    stream = audio.open(format=FORMAT, channels=CHANNELS,
                        rate=RATE, input=True,
                        frames_per_buffer=CHUNK)

    frames = []

    print "recording...\n\n"
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
      sys.stdout.write('\r%i' % i)
      data = stream.read(CHUNK)
      frames.append(data)
    print "\nfinished recording\n"

    # stop Recording
    stream.stop_stream()
    stream.close()
    audio.terminate()

    waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    waveFile.setnchannels(CHANNELS)
    waveFile.setsampwidth(audio.get_sample_size(FORMAT))
    waveFile.setframerate(RATE)
    waveFile.writeframes(b''.join(frames))

    # waveFile.Wave_read.getnframes()

    waveFile.close()
    tkMessageBox.showinfo("Recorded", "Track:%s"%WAVE_OUTPUT_FILENAME)

I tried to make other function with timer and initiate that function, when i press button, but if i pressed button firstly program ran rec function, and after gone timer function, also i tried to initiate those function by pipe but it does the same, do i have to make that with threads?

Jorgusss
  • 7
  • 1
  • 3
  • I would suggest that you update the time on every for() loop iteration, assuming you want a timer that shows the elapsed time to do this (you don't have any functions in the program, let alone a recording function, so there is no way to tell what you mean by "recording function"). Also note that *WAVE_OUTPUT_FILENAME = str(e2.get() + '.wav')* could error because e2 has not been declared yet, or passed to the class, so it's scope is unknown to us. –  Jan 14 '16 at 00:35
  • e2 was declared as a global variable in other class, which is used in entry box all those points in rec class are working, also str(e2.get()+ '.wav'), but the main point is to create timer which would be shown while the record loop is working, and i even don' t have any idea how to make that – Jorgusss Jan 14 '16 at 14:06

0 Answers0