import struct


def listen_and_parse ( input_stream, cheetah ):
	"""
	Use a loop to listen and process speech in real time.  
	Once the loop is broken (by sending SIGINT; ctrl+C), transcribe.
	"""
	print("Listening... use SIGINT (ctrl+C) to stop listening and transcibe." )

	# unset the `interrupted` flag first; this flag will be checked with each loop
	global interrupted
	interrupted = False		# unset interrupted flag

	# listen until SIGINT (user should )
	while not interrupted:
		# reach in a "chunk"; each chunk contains the specified number of frames (i.e samples)
		pcm = input_stream.read ( cheetah.frame_length )
		# unpack the chunk to make it processable
		pcm = struct.unpack_from ("h" * cheetah.frame_length, pcm)
		# process with Cheetah
		cheetah.process(pcm)

	# once the loop is broken, transcribe
	transcript = cheetah.transcribe()
	print ( "\n", transcript )
	return transcript