|
| 1 | +""" |
| 2 | +Play Audio Example |
| 3 | +
|
| 4 | +This example demonstrates how to play generated audio immediately: |
| 5 | +- Generate text-to-speech audio |
| 6 | +- Play audio using the play() utility |
| 7 | +- Different playback methods (ffmpeg, sounddevice) |
| 8 | +
|
| 9 | +Requirements: |
| 10 | + pip install fishaudio |
| 11 | +
|
| 12 | + # For audio playback (choose one): |
| 13 | + # Option 1 (recommended): Install ffmpeg |
| 14 | + # macOS: brew install ffmpeg |
| 15 | + # Ubuntu: sudo apt install ffmpeg |
| 16 | + # Windows: Download from ffmpeg.org |
| 17 | +
|
| 18 | + # Option 2: Use sounddevice (Python-only) |
| 19 | + # pip install sounddevice soundfile |
| 20 | +
|
| 21 | +Environment Setup: |
| 22 | + export FISH_AUDIO_API_KEY="your_api_key_here" |
| 23 | +
|
| 24 | +Expected Output: |
| 25 | + - Plays the generated audio through your speakers |
| 26 | + - No file is saved (unless you uncomment the save section) |
| 27 | +""" |
| 28 | + |
| 29 | +from fishaudio import FishAudio |
| 30 | +from fishaudio.utils import play |
| 31 | + |
| 32 | + |
| 33 | +def main(): |
| 34 | + # Initialize the client |
| 35 | + client = FishAudio() |
| 36 | + |
| 37 | + # Text to convert to speech |
| 38 | + text = "Welcome to Fish Audio! This audio will play immediately after generation." |
| 39 | + |
| 40 | + print(f"Generating speech: '{text}'") |
| 41 | + print("Please ensure your speakers are on...\n") |
| 42 | + |
| 43 | + # Generate the audio |
| 44 | + audio = client.tts.convert(text=text) |
| 45 | + |
| 46 | + # Play the audio immediately |
| 47 | + # By default, this uses ffplay (from ffmpeg) if available, |
| 48 | + # otherwise falls back to sounddevice |
| 49 | + print("▶ Playing audio...") |
| 50 | + play(audio) |
| 51 | + print("✓ Playback complete!") |
| 52 | + |
| 53 | + # Optional: Save the audio to a file as well |
| 54 | + # Uncomment the following lines if you want to save it: |
| 55 | + # print("\nSaving audio to file...") |
| 56 | + # audio = client.tts.convert(text=text) # Regenerate since audio was consumed |
| 57 | + # save(audio, "playback_example.mp3") |
| 58 | + # print("✓ Saved to playback_example.mp3") |
| 59 | + |
| 60 | + |
| 61 | +def demo_playback_methods(): |
| 62 | + """ |
| 63 | + Demonstrate different playback methods. |
| 64 | +
|
| 65 | + Note: The play() function automatically chooses the best available method, |
| 66 | + but you can force specific methods by modifying the code. |
| 67 | + """ |
| 68 | + client = FishAudio() |
| 69 | + text = "This is a demonstration of different playback methods." |
| 70 | + |
| 71 | + # Method 1: Default (uses ffmpeg if available) |
| 72 | + print("Method 1: Default playback") |
| 73 | + audio = client.tts.convert(text=text) |
| 74 | + play(audio, use_ffmpeg=True) |
| 75 | + |
| 76 | + # Method 2: Using sounddevice (requires pip install sounddevice soundfile) |
| 77 | + print("\nMethod 2: Sounddevice playback") |
| 78 | + audio = client.tts.convert(text=text) |
| 79 | + play(audio, use_ffmpeg=False) |
| 80 | + |
| 81 | + # Method 3: Jupyter notebook mode (for .ipynb files) |
| 82 | + # This returns an IPython.display.Audio object |
| 83 | + # Uncomment if running in a notebook: |
| 84 | + # audio = client.tts.convert(text=text) |
| 85 | + # play(audio, notebook=True) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + try: |
| 90 | + main() |
| 91 | + |
| 92 | + # Uncomment to try different playback methods: |
| 93 | + # print("\n" + "="*50) |
| 94 | + # print("Testing different playback methods...") |
| 95 | + # print("="*50 + "\n") |
| 96 | + # demo_playback_methods() |
| 97 | + |
| 98 | + except Exception as e: |
| 99 | + print(f"Error: {e}") |
| 100 | + print("\nTroubleshooting:") |
| 101 | + print("1. Make sure your API key is set: export FISH_AUDIO_API_KEY='your_key'") |
| 102 | + print("2. Install ffmpeg for audio playback:") |
| 103 | + print(" - macOS: brew install ffmpeg") |
| 104 | + print(" - Ubuntu: sudo apt install ffmpeg") |
| 105 | + print("3. Or install sounddevice: pip install sounddevice soundfile") |
0 commit comments