From 544d81a1035b094a0103c02aba5dd201e0939512 Mon Sep 17 00:00:00 2001 From: Arjun Kumar Date: Sun, 2 Nov 2025 15:02:50 +0530 Subject: [PATCH] Add Simple interactive audio recorder script # Audio Recorder (crypto.py) Simple interactive audio recorder script that uses sounddevice and wavio to record microphone input and save as a WAV file. ## Features - List available audio input devices - Record audio for a user-specified duration - Configure device index, sample rate, channels and output filename - Saves recording as a .wav file --- .../Simple _interactive_audio_recorder.py | 58 +++++++++++++++++++ VOICE_RECORDER/voice_recorder.py | 14 ----- 2 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 VOICE_RECORDER/Simple _interactive_audio_recorder.py delete mode 100644 VOICE_RECORDER/voice_recorder.py diff --git a/VOICE_RECORDER/Simple _interactive_audio_recorder.py b/VOICE_RECORDER/Simple _interactive_audio_recorder.py new file mode 100644 index 00000000..caa61bcf --- /dev/null +++ b/VOICE_RECORDER/Simple _interactive_audio_recorder.py @@ -0,0 +1,58 @@ +import sounddevice as sd +import wavio +import os +import time + +def list_input_devices(): + try: + devices = sd.query_devices() + print("Available audio devices:") + for i, dev in enumerate(devices): + print(f"{i}: {dev['name']} (max input channels: {dev.get('max_input_channels', 0)})") + except Exception as e: + print("Could not query devices:", e) + +def record_audio(duration, filename, samplerate=44100, channels=2, dtype='int16', device=None): + if not filename.lower().endswith('.wav'): + filename += '.wav' + try: + frames = int(duration * samplerate) + print(f"Recording for {duration} seconds (samplerate={samplerate}, channels={channels})...") + start = time.time() + audio = sd.rec(frames, samplerate=samplerate, channels=channels, dtype=dtype, device=device) + sd.wait() + elapsed = time.time() - start + # Ensure output directory exists + out_dir = os.path.dirname(os.path.abspath(filename)) + if out_dir and not os.path.exists(out_dir): + os.makedirs(out_dir, exist_ok=True) + wavio.write(filename, audio, samplerate, sampwidth=2) + print(f"Recording saved as '{filename}' ({elapsed:.2f}s)") + except KeyboardInterrupt: + print("\nRecording cancelled by user.") + except Exception as e: + print("Recording failed:", e) + +if __name__ == "__main__": + try: + print("Press Enter to see available input devices or type 'skip' to continue with default device.") + if input().strip().lower() != 'skip': + list_input_devices() + + device_input = input("Enter device index to use (or press Enter for default): ").strip() + device = int(device_input) if device_input != '' else None + + record_duration = float(input("Enter duration in seconds: ").strip()) + file_name = input("Enter filename (with or without .wav extension) [default: recording.wav]: ").strip() or "recording.wav" + + sr_input = input("Enter sample rate (default 44100): ").strip() + samplerate = int(sr_input) if sr_input else 44100 + + ch_input = input("Enter number of channels (1=mono,2=stereo) (default 2): ").strip() + channels = int(ch_input) if ch_input else 2 + + record_audio(record_duration, file_name, samplerate=samplerate, channels=channels, device=device) + except ValueError: + print("Invalid numeric input. Exiting.") + except Exception as e: + print("Error:", e) diff --git a/VOICE_RECORDER/voice_recorder.py b/VOICE_RECORDER/voice_recorder.py deleted file mode 100644 index 774cf663..00000000 --- a/VOICE_RECORDER/voice_recorder.py +++ /dev/null @@ -1,14 +0,0 @@ -import sounddevice as sd -import wavio - -def record_audio(duration, filename): - print("Recording...") - audio = sd.rec(int(duration * 44100), samplerate=44100, channels=2, dtype='int16') - sd.wait() # Wait until the recording is finished - wavio.write(filename, audio, 44100, sampwidth=2) # Save as WAV file - print(f"Recording saved as '{filename}'") - -if __name__ == "__main__": - record_duration = float(input("Enter duration in seconds: ")) - file_name = input("Enter filename (with .wav extension): ") - record_audio(record_duration, file_name)