From 96322f31d57b218e53e165904fd9f6f10221b1e8 Mon Sep 17 00:00:00 2001
From: Marlon Schumacher <schumacher@hfm-karlsruhe.de>
Date: Tue, 05 Nov 2024 15:31:49 +0100
Subject: [PATCH] feat: adding original python script for audio recording

---
 PythonExample Recording.py |   77 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 77 insertions(+), 0 deletions(-)

diff --git a/PythonExample Recording.py b/PythonExample Recording.py
new file mode 100644
index 0000000..8027db5
--- /dev/null
+++ b/PythonExample Recording.py
@@ -0,0 +1,77 @@
+import pyaudio
+import wave
+
+# Initialize PyAudio
+p = pyaudio.PyAudio()
+
+# List available input devices
+print("Available audio input devices:")
+for i in range(p.get_device_count()):
+    device_info = p.get_device_info_by_index(i)
+    if device_info['maxInputChannels'] > 0:
+        print(f"Device ID {i}: {device_info['name']} (Max channels: {device_info['maxInputChannels']})")
+
+# Prompt user to select a device
+device_index = int(input("Please select the device ID to use for recording: "))
+
+# Get the chosen device's info to validate it can support 19 channels and 48kHz
+device_info = p.get_device_info_by_index(device_index)
+max_channels = device_info['maxInputChannels']
+if max_channels < 19:
+    print(f"Error: The selected device only supports {max_channels} channels, but 19 channels are required.")
+    p.terminate()
+    exit(1)
+
+# Parameters
+CHANNELS = 19
+SAMPLE_RATE = 48000
+CHUNK = 2048  # Number of frames per buffer
+FORMAT = pyaudio.paInt24  # 16-bit audio
+RECORD_SECONDS = 10  # Duration to record (in seconds)
+OUTPUT_FILENAME = "output.wav"
+
+# Open stream
+stream = p.open(format=FORMAT,
+                channels=CHANNELS,
+                rate=SAMPLE_RATE,
+                input=True,
+                input_device_index=device_index,
+                frames_per_buffer=CHUNK)
+
+print(f"Recording {RECORD_SECONDS} seconds of audio from device ID {device_index}...")
+
+frames = []
+
+print (f"Check status {stream.is_active()}")
+
+for _ in range(int(SAMPLE_RATE / CHUNK * RECORD_SECONDS)):
+
+    try:
+        # Record audio
+        data = stream.read(CHUNK)
+        frames.append(data)
+    except:
+        print ("Error")
+    
+
+print("Finished recording.")
+
+
+print (f"Check status {stream.is_active()}")
+# Stop and close the stream
+stream.stop_stream()
+stream.close()
+
+# Terminate PyAudio
+p.terminate()
+
+# Save the recorded audio to a file
+wf = wave.open(OUTPUT_FILENAME, 'wb')
+wf.setnchannels(CHANNELS)
+wf.setsampwidth(p.get_sample_size(FORMAT))
+wf.setframerate(SAMPLE_RATE)
+wf.writeframes(b''.join(frames))
+wf.close()
+
+print(f"Audio saved to {OUTPUT_FILENAME}.")
+

--
Gitblit v1.9.1