My Code Works, but I don't understand. Help!

This is the place for queries that don't fit in any of the other categories.

My Code Works, but I don't understand. Help!

Postby hillbro » Fri Dec 20, 2013 2:29 pm

Hi all. I am new to Python, and I am trying to record my microphone once I begin speaking. I found some code online that allows me to record, and it works well. However, I need to make some adjustments and I wanted to know how the code I have is working...I have never seen anything like this, it's almost like it's appending data and then when I am done it compiles it? Any advice?

I do not understand the chunk, stream, stream.read

Code: Select all
CHUNK = 1024


Code: Select all
stream = p.open(format=FORMAT,
            channels=CHANNELS,
            rate=RATE,
            input=True,
            frames_per_buffer=CHUNK) #buffer


Code: Select all
data = stream.read(CHUNK)

Here is my entire main function:
Code: Select all
def main():

   import pyaudio
   import wave
   global record
   
   p = pyaudio.PyAudio()
   
   voice_timeout = 2
   start_time = 0
   elapsed_time = 0
   record = False
   
   while micVolume():
      print micVolume()
      if micVolume() > 450:
         record = True
         start_time = time.time()
         break
   
   if record == True:
      print("* recording")
      CHUNK = 1024
      FORMAT = pyaudio.paInt16 #paInt8
      CHANNELS = 2
      RATE = 44100 #sample rate
      
      WAVE_OUTPUT_FILENAME = "out.wav"

      frames = []
      
      stream = p.open(format=FORMAT,
                  channels=CHANNELS,
                  rate=RATE,
                  input=True,
                  frames_per_buffer=CHUNK) #buffer
   
      while elapsed_time < voice_timeout and record == True:
         # Handle time
         elapsed_time = time.time() - start_time
         data = stream.read(CHUNK)
         frames.append(data) # 2 bytes(16 bits) per channel
         
      if elapsed_time > voice_timeout:
         print("* done recording")

         stream.stop_stream()
         stream.close()
         p.terminate()

         wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
         wf.setnchannels(CHANNELS)
         wf.setsampwidth(p.get_sample_size(FORMAT))
         wf.setframerate(RATE)
         wf.writeframes(b''.join(frames))
         wf.close()
            
         os.system("flac out.wav -f")
Last edited by Mekire on Fri Dec 20, 2013 2:40 pm, edited 1 time in total.
Reason: First post lock.
hillbro
 
Posts: 2
Joined: Fri Dec 20, 2013 2:13 pm

Re: My Code Works, but I don't understand. Help!

Postby Somelauw » Sat Dec 21, 2013 12:03 am

You might want to start reading this
http://people.csail.mit.edu/hubert/pyaudio/docs/

Also read this to understand the python api for wave files.
http://docs.python.org/2/library/wave.html
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 69
Joined: Tue Feb 12, 2013 8:30 pm

Re: My Code Works, but I don't understand. Help!

Postby hillbro » Mon Dec 23, 2013 5:20 pm

Thank you so much!! This seems to be exactly what I wanted!
hillbro
 
Posts: 2
Joined: Fri Dec 20, 2013 2:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests