import os import pyaudio import numpy as np import wave import threading import msvcrt # Constants FORMAT = pyaudio.paInt16 CHANNELS = 1 SAMPLE_RATE = 44100 CHUNK_SIZE = (SAMPLE_RATE * 2) * 2 # Initialize PyAudio audio = pyaudio.PyAudio() # Open the microphone stream stream = audio.open(format=FORMAT, channels=CHANNELS, rate=SAMPLE_RATE, input=True, frames_per_buffer=CHUNK_SIZE) # Flag to indicate if the program should stop stop_program = False # Function to process audio data def process_audio_data(audio_data): # Here, you can include your own processing code return audio_data # Function to save audio data as a .wav file def save_audio_to_wav(audio_data, filename): # Ensure the directory exists if not os.path.exists("recorded_data"): os.makedirs("recorded_data") # Open a new .wav file in write mode with wave.open(f"recorded_data/{filename}.wav", "wb") as wav_file: # Set the parameters wav_file.setnchannels(CHANNELS) wav_file.setsampwidth(audio.get_sample_size(FORMAT)) wav_file.setframerate(SAMPLE_RATE) # Write the audio data wav_file.writeframes(audio_data.tobytes()) # Function to check for keypress events def check_keypress(): global stop_program while True: if msvcrt.kbhit(): key = msvcrt.getch() stop_program = True break # Start the keypress checking thread keypress_thread = threading.Thread(target=check_keypress) keypress_thread.start() # Continuously process audio data from the microphone i = 0 while not stop_program: try: # Read audio data from the stream audio_data = np.frombuffer(stream.read(CHUNK_SIZE), dtype=np.int16) # Process the audio data and get the converted output converted_audio = process_audio_data(audio_data) # Save the audio data as a .wav file save_audio_to_wav(converted_audio, f"chunk_{i}") i += 1 except KeyboardInterrupt: break # Stop and close the microphone stream stream.stop_stream() stream.close() audio.terminate()