piper_tts 0.0.1 copy "piper_tts: ^0.0.1" to clipboard
piper_tts: ^0.0.1 copied to clipboard

piper_tts is a flutter text to speech package for using piper neural voice models cross platform.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:piper_tts/piper_tts.dart';
import 'package:audioplayers/audioplayers.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Piper TTS Example'),
        ),
        body: const TTSPage(),
      ),
    );
  }
}

class TTSPage extends StatefulWidget {
  const TTSPage({super.key});

  @override
  State<TTSPage> createState() => _TTSPageState();
}

class _TTSPageState extends State<TTSPage> {
  final TextEditingController _controller = TextEditingController();
  final AudioPlayer _audioPlayer = AudioPlayer();
  bool _isPlaying = false;

  void _generateAndPlaySpeech() async {
    if (_controller.text.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Please enter some text')));
      return;
    }

    try {
      // Generate speech
      final file = await Piper.generateSpeech(_controller.text);

      // Convert the file path to a DeviceFileSource for the play method
      final source = DeviceFileSource(file.path);

      // Play the generated audio
      await _audioPlayer.play(source);
      setState(() {
        _isPlaying = true;
      });

      // Listen to the state of the audio player to reset the play state when it finishes playing
      _audioPlayer.onPlayerComplete.listen((event) async {
        await Future.delayed(const Duration(milliseconds: 500));

        setState(() {
          _isPlaying = false;
        });
      });
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(e.toString())));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(20.0),
      child: Column(
        children: <Widget>[
          TextField(
            controller: _controller,
            decoration: const InputDecoration(
              hintText: 'Enter text to speak',
            ),
          ),
          const SizedBox(height: 20),
          ElevatedButton(
            onPressed: _isPlaying ? null : _generateAndPlaySpeech,
            child: Text(_isPlaying ? 'Playing...' : 'Generate Speech'),
          ),
        ],
      ),
    );
  }
}
2
likes
140
points
24
downloads

Publisher

unverified uploader

Weekly Downloads

piper_tts is a flutter text to speech package for using piper neural voice models cross platform.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

ffi, flutter, plugin_platform_interface

More

Packages that depend on piper_tts