Skip to content

Commit

Permalink
Voice option addded in VoiceRssProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipesh79 authored and duncan3dc committed Nov 19, 2024
1 parent 6eb81bd commit 70892ff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ Changelog

--------

## 1.4.1 - 2022-04-14
## 1.5.0 - 2024-01-19

### Added

* [Providers] Added support for a different voice in VoiceRSS. ([#25](https://github.com/duncan3dc/speaker/issues/25))

--------

## 1.4.1 - 2024-11-18

### Fixed

Expand Down
29 changes: 28 additions & 1 deletion src/Providers/VoiceRssProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ class VoiceRssProvider extends AbstractProvider
/** @var int $speed */
private $speed = 0;

/** @var string $voice */
private $voice = "Alice";

/**
* Create a new instance.
*
* @param string $apikey Your Voice RSS API key.
* @param string $language The language to use.
* @param int $speed The speech rate to use.
* @param string $voice The voice to use.
*/
public function __construct(string $apikey, string $language = null, int $speed = null)
public function __construct(string $apikey, string $language = null, int $speed = null, string $voice = null)
{
$this->apikey = $apikey;

Expand All @@ -43,6 +47,10 @@ public function __construct(string $apikey, string $language = null, int $speed
if ($speed !== null) {
$this->speed = $this->getSpeed($speed);
}

if ($voice !== null) {
$this->voice = $voice;
}
}


Expand Down Expand Up @@ -120,10 +128,28 @@ public function withSpeed(int $speed): self
}


/**
* Set the voice to use.
*
* @param string $voice The voice to use (this must be compatible with the language)
*
* @return $this
*/
public function withVoice(string $voice): self
{
$provider = clone $this;

$provider->voice = $voice;

return $provider;
}


public function getOptions(): array
{
return [
"language" => $this->language,
"voice" => $this->voice,
"speed" => $this->speed,
];
}
Expand All @@ -142,6 +168,7 @@ public function textToSpeech(string $text): string
"key" => $this->apikey,
"src" => $text,
"hl" => $this->language,
"v" => $this->voice,
"r" => (string) $this->speed,
"c" => "MP3",
"f" => "16khz_16bit_stereo",
Expand Down
36 changes: 31 additions & 5 deletions tests/Providers/VoiceRssProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testTextToSpeech(): void

$this->client->shouldReceive("request")
->once()
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&r=0&c=MP3&f=16khz_16bit_stereo")
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&v=Alice&r=0&c=MP3&f=16khz_16bit_stereo")
->andReturn($response);

$this->assertSame("mp3", $this->provider->textToSpeech("Hello"));
Expand All @@ -58,7 +58,7 @@ public function testTextToSpeechFailure(): void

$this->client->shouldReceive("request")
->once()
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&r=0&c=MP3&f=16khz_16bit_stereo")
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&v=Alice&r=0&c=MP3&f=16khz_16bit_stereo")
->andReturn($response);

$this->expectException(ProviderException::class);
Expand All @@ -81,7 +81,7 @@ public function testWithLanguage(): void

$this->client->shouldReceive("request")
->once()
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=fr-fr&r=0&c=MP3&f=16khz_16bit_stereo")
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=fr-fr&v=Alice&r=0&c=MP3&f=16khz_16bit_stereo")
->andReturn($response);

$this->assertSame("mp3", $provider->textToSpeech("Hello"));
Expand All @@ -96,6 +96,30 @@ public function testWithLanguageFailure(): void
}


/**
* Ensure we can set a different voice.
*/
public function testWithVoice1(): void
{
$provider = $this->provider->withVoice("Harry");

# Ensure immutability
$this->assertSame("Harry", $provider->getOptions()["voice"]);
$this->assertSame("Alice", $this->provider->getOptions()["voice"]);

$response = Mockery::mock(ResponseInterface::class);
$response->shouldReceive("getStatusCode")->once()->andReturn("200");
$response->shouldReceive("getBody")->once()->andReturn(Utils::streamFor("mp3"));

$this->client->shouldReceive("request")
->once()
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&v=Harry&r=0&c=MP3&f=16khz_16bit_stereo")
->andReturn($response);

$this->assertSame("mp3", $provider->textToSpeech("Hello"));
}


public function testWithSpeed(): void
{
$provider = $this->provider->withSpeed(-5);
Expand All @@ -110,7 +134,7 @@ public function testWithSpeed(): void

$this->client->shouldReceive("request")
->once()
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&r=-5&c=MP3&f=16khz_16bit_stereo")
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&v=Alice&r=-5&c=MP3&f=16khz_16bit_stereo")
->andReturn($response);

$this->assertSame("mp3", $provider->textToSpeech("Hello"));
Expand All @@ -129,6 +153,7 @@ public function testGetOptions(): void
{
$options = [
"language" => "en-gb",
"voice" => "Alice",
"speed" => 0,
];

Expand All @@ -138,10 +163,11 @@ public function testGetOptions(): void

public function testConstructorOptions1(): void
{
$provider = new VoiceRssProvider("APIKEY", "ab-cd", 10);
$provider = new VoiceRssProvider("APIKEY", "ab-cd", 10, "Harry");

$options = [
"language" => "ab-cd",
"voice" => "Harry",
"speed" => 10,
];

Expand Down

0 comments on commit 70892ff

Please sign in to comment.