import pyaudio import wave import sys Your existing code … def…
- User
import pyaudio import wave import sys
Your existing code
…
def record_from_microphone(duration, output_file): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 RECORD_SECONDS = duration
codep = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print(“* recording from microphone”) frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print(“* done recording”) stream.stop_stream() stream.close() p.terminate() with wave.open(output_file, ‘wb’) as wf: wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b’'.join(frames))def recognize(internal_audio=False, duration=10): # You need to provide a way to either listen to the microphone or internal audio if internal_audio: # Implement the logic to capture internal audio # This might involve platform-specific details and might not be # straightforward print(“Capturing internal audio is platform-specific and requires additional setup.”) return else: # Record from microphone audio_file_path = ‘recording.wav’ record_from_microphone(duration, audio_file_path)
code# The rest of your existing code to identify and process the song # …if name == “main”: print(“Choose an option:”) print(“1. Listen through microphone”) print(“2. Detect internal audio”) choice = input("Enter option (1 or 2): ")
codeif choice == ‘1’: # Record from microphone and recognize recognize(internal_audio=False) elif choice == ‘2’: # Use OS-specific logic to capture and recognize internal audio recognize(internal_audio=True) else: print(“Invalid choice”)dont record audio from microphone instead capture audio from microphone and identify the audio captured instead of saving and recording. I mean capture a song and recognise the song and dont save and record the song through microphone
- Assistant