|
| 1 | +import torchaudio as ta |
| 2 | +import torch |
| 3 | +from transformers import WhisperProcessor, WhisperForConditionalGeneration |
| 4 | +from chatterbox.tts import ChatterboxTTS |
| 5 | + |
| 6 | +def voice_to_text_to_voice_pipeline(input_audio_path, audio_prompt_path, output_path): |
| 7 | + """ |
| 8 | + Complete pipeline: voice -> text -> voice (transformed) |
| 9 | + |
| 10 | + Args: |
| 11 | + input_audio_path: Path to input audio file to transcribe |
| 12 | + audio_prompt_path: Path to audio prompt for voice transformation |
| 13 | + output_path: Path to save the transformed output audio |
| 14 | + """ |
| 15 | + |
| 16 | + # Step 1: Voice -> Text using Whisper from transformers |
| 17 | + print("Loading Whisper model...") |
| 18 | + processor = WhisperProcessor.from_pretrained("openai/whisper-base") |
| 19 | + model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base") |
| 20 | + |
| 21 | + print(f"Transcribing audio from {input_audio_path}...") |
| 22 | + # Load audio file |
| 23 | + audio_input, sample_rate = ta.load(input_audio_path) |
| 24 | + |
| 25 | + # Resample to 16kHz if needed (Whisper expects 16kHz) |
| 26 | + if sample_rate != 16000: |
| 27 | + resampler = ta.transforms.Resample(sample_rate, 16000) |
| 28 | + audio_input = resampler(audio_input) |
| 29 | + |
| 30 | + # Convert to mono if stereo |
| 31 | + if audio_input.shape[0] > 1: |
| 32 | + audio_input = torch.mean(audio_input, dim=0, keepdim=True) |
| 33 | + |
| 34 | + # Process audio |
| 35 | + input_features = processor(audio_input.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features |
| 36 | + |
| 37 | + # Generate transcription |
| 38 | + with torch.no_grad(): |
| 39 | + predicted_ids = model.generate(input_features) |
| 40 | + |
| 41 | + transcribed_text = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0] |
| 42 | + print(f"Transcribed text: {transcribed_text}") |
| 43 | + |
| 44 | + # Step 2: Text -> Voice using ChatterboxTTS with audio prompt |
| 45 | + print("Loading ChatterboxTTS model...") |
| 46 | + assert torch.cuda.is_available(), "CUDA is not available. Please install CUDA and PyTorch with GPU support." |
| 47 | + tts_model = ChatterboxTTS.from_pretrained(device="cuda") |
| 48 | + |
| 49 | + print(f"Generating transformed voice using prompt from {audio_prompt_path}...") |
| 50 | + wav = tts_model.generate(transcribed_text, audio_prompt_path=audio_prompt_path) |
| 51 | + |
| 52 | + # Step 3: Save the transformed audio |
| 53 | + ta.save(output_path, wav, tts_model.sr) |
| 54 | + print(f"Transformed audio saved to {output_path}") |
| 55 | + |
| 56 | + return transcribed_text, output_path |
| 57 | + |
| 58 | +# Example usage with your existing setup |
| 59 | +if __name__ == "__main__": |
| 60 | + # Original text-to-speech example (keeping for reference) |
| 61 | + print("=== Original Text-to-Speech Example ===") |
| 62 | + text = "Ezreal and Jinx teamed up with Ahri, Yasuo, and Teemo to take down the enemy's Nexus in an epic late-game pentakill." |
| 63 | + model = ChatterboxTTS.from_pretrained(device="cuda" if torch.cuda.is_available() else "cpu") |
| 64 | + AUDIO_PROMPT_PATH = "male_petergriffin.wav" |
| 65 | + wav = model.generate(text, audio_prompt_path=AUDIO_PROMPT_PATH) |
| 66 | + ta.save("output_original.wav", wav, model.sr) |
| 67 | + print("Original output saved to output_original.wav") |
| 68 | + |
| 69 | + # New voice-to-text-to-voice pipeline with file input |
| 70 | + print("\n=== Voice-to-Text-to-Voice Pipeline (File Input) ===") |
| 71 | + # You'll need to provide an input audio file to transcribe |
| 72 | + # For demo purposes, let's use the audio prompt as input (you can change this) |
| 73 | + INPUT_AUDIO_PATH = "male_petergriffin.wav" # Change this to your input audio file |
| 74 | + AUDIO_PROMPT_PATH = "male_petergriffin.wav" # This transforms the voice style |
| 75 | + OUTPUT_PATH = "output_transformed.wav" |
| 76 | + |
| 77 | + try: |
| 78 | + transcribed_text, output_file = voice_to_text_to_voice_pipeline( |
| 79 | + INPUT_AUDIO_PATH, |
| 80 | + AUDIO_PROMPT_PATH, |
| 81 | + OUTPUT_PATH |
| 82 | + ) |
| 83 | + print(f"\nFile pipeline completed successfully!") |
| 84 | + print(f"Transcribed: '{transcribed_text}'") |
| 85 | + print(f"Transformed audio saved to: {output_file}") |
| 86 | + except Exception as e: |
| 87 | + print(f"Error in file pipeline: {e}") |
| 88 | + print("Make sure you have an input audio file and the required models are available.") |
| 89 | + |
| 90 | + # Live microphone recording demo |
| 91 | + print("\n=== Live Microphone Recording Demo ===") |
| 92 | + try: |
| 93 | + from microphone_recorder import MicrophoneRecorder |
| 94 | + |
| 95 | + response = input("Would you like to try live microphone recording? (y/n): ").lower().strip() |
| 96 | + if response in ['y', 'yes']: |
| 97 | + recorder = MicrophoneRecorder() |
| 98 | + |
| 99 | + print("\nπ€ Available audio devices:") |
| 100 | + recorder.list_audio_devices() |
| 101 | + |
| 102 | + print(f"\nπ΄ Ready to record! Speak into your microphone...") |
| 103 | + print("Press ENTER when you're done speaking.") |
| 104 | + |
| 105 | + temp_recording_path = "temp_recording.wav" |
| 106 | + success = recorder.record_and_save(temp_recording_path) |
| 107 | + |
| 108 | + if success: |
| 109 | + print(f"\nπ― Processing your recorded audio...") |
| 110 | + transcribed_text, output_file = voice_to_text_to_voice_pipeline( |
| 111 | + temp_recording_path, |
| 112 | + AUDIO_PROMPT_PATH, |
| 113 | + "output_live_recording.wav" |
| 114 | + ) |
| 115 | + |
| 116 | + print(f"\nβ
Live recording pipeline completed!") |
| 117 | + print(f"π You said: '{transcribed_text}'") |
| 118 | + print(f"π Your voice transformed and saved to: output_live_recording.wav") |
| 119 | + |
| 120 | + # Clean up temporary file |
| 121 | + import os |
| 122 | + os.remove(temp_recording_path) |
| 123 | + print("ποΈ Temporary recording file cleaned up.") |
| 124 | + else: |
| 125 | + print("β Recording failed.") |
| 126 | + else: |
| 127 | + print("Skipping live recording demo.") |
| 128 | + |
| 129 | + except ImportError: |
| 130 | + print("Microphone recording not available. Install sounddevice and soundfile packages.") |
| 131 | + except Exception as e: |
| 132 | + print(f"Error in live recording demo: {e}") |
0 commit comments