piper1-rs 0.1.0

Safe Rust bindings to Piper, the TTS library
Documentation
use std::os::raw::c_int;

use piper1_rs_sys::piper_synthesize_options;

/// Options to pass to the Piper model.
#[derive(Copy, Clone)]
pub struct PiperSynthesisOptions {
	/// ID of speaker to use (multi-speaker models only).
	///
	/// ID 0 is the first speaker.
	pub speaker_id: c_int,

	/// How fast the text is spoken.
	///
	/// A length scale of 0.5 means to speak twice as fast.
	/// A length scale of 2.0 means to speak twice as slow.
	/// The default is 1.0.
	pub length_scale: f32,

	/// Controls how much noise is added during synthesis.
	///
	/// The best value depends on the voice.
	/// For single speaker models, a value of 0.667 is usually good.
	/// For multi-speaker models, a value of 0.333 is usually good.
	pub noise_scale: f32,

	/// Controls how much phonemes vary in length during synthesis.
	///
	/// The best value depends on the voice.
	/// For single speaker models, a value of 0.8 is usually good.
	/// For multi-speaker models, a value of 0.333 is usually good.
	pub noise_w_scale: f32,
}

impl PiperSynthesisOptions {
	/// See [`Self.speaker_id`](PiperSynthesisOptions::speaker_id) for more info.
	pub fn set_speaker_id(&mut self, speaker_id: c_int) -> &mut Self {
		self.speaker_id = speaker_id;
		self
	}

	/// See [`Self.length_scale`](PiperSynthesisOptions::length_scale) for more info.
	pub fn set_length_scale(&mut self, length_scale: f32) -> &mut Self {
		self.length_scale = length_scale;
		self
	}

	/// See [`Self.noise_scale`](PiperSynthesisOptions::noise_scale) for more info.
	pub fn set_noise_scale(&mut self, noise_scale: f32) -> &mut Self {
		self.noise_scale = noise_scale;
		self
	}

	/// See [`Self.noise_w_scale`](PiperSynthesisOptions::noise_w_scale) for more info.
	pub fn set_noise_w_scale(&mut self, noise_w_scale: f32) -> &mut Self {
		self.noise_w_scale = noise_w_scale;
		self
	}
}

impl From<piper_synthesize_options> for PiperSynthesisOptions {
	fn from(
		piper_synthesize_options {
			speaker_id,
			length_scale,
			noise_scale,
			noise_w_scale,
		}: piper_synthesize_options,
	) -> Self {
		Self {
			speaker_id,
			length_scale,
			noise_scale,
			noise_w_scale,
		}
	}
}

impl From<&PiperSynthesisOptions> for piper_synthesize_options {
	fn from(val: &PiperSynthesisOptions) -> Self {
		piper_synthesize_options {
			speaker_id:    val.speaker_id,
			length_scale:  val.length_scale,
			noise_scale:   val.noise_scale,
			noise_w_scale: val.noise_w_scale,
		}
	}
}