Skip to content

Commit 70892ff

Browse files
Dipesh79duncan3dc
authored andcommitted
Voice option addded in VoiceRssProvider
1 parent 6eb81bd commit 70892ff

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ Changelog
55

66
--------
77

8-
## 1.4.1 - 2022-04-14
8+
## 1.5.0 - 2024-01-19
9+
10+
### Added
11+
12+
* [Providers] Added support for a different voice in VoiceRSS. ([#25](https://github.com/duncan3dc/speaker/issues/25))
13+
14+
--------
15+
16+
## 1.4.1 - 2024-11-18
917

1018
### Fixed
1119

src/Providers/VoiceRssProvider.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ class VoiceRssProvider extends AbstractProvider
2525
/** @var int $speed */
2626
private $speed = 0;
2727

28+
/** @var string $voice */
29+
private $voice = "Alice";
30+
2831
/**
2932
* Create a new instance.
3033
*
3134
* @param string $apikey Your Voice RSS API key.
3235
* @param string $language The language to use.
3336
* @param int $speed The speech rate to use.
37+
* @param string $voice The voice to use.
3438
*/
35-
public function __construct(string $apikey, string $language = null, int $speed = null)
39+
public function __construct(string $apikey, string $language = null, int $speed = null, string $voice = null)
3640
{
3741
$this->apikey = $apikey;
3842

@@ -43,6 +47,10 @@ public function __construct(string $apikey, string $language = null, int $speed
4347
if ($speed !== null) {
4448
$this->speed = $this->getSpeed($speed);
4549
}
50+
51+
if ($voice !== null) {
52+
$this->voice = $voice;
53+
}
4654
}
4755

4856

@@ -120,10 +128,28 @@ public function withSpeed(int $speed): self
120128
}
121129

122130

131+
/**
132+
* Set the voice to use.
133+
*
134+
* @param string $voice The voice to use (this must be compatible with the language)
135+
*
136+
* @return $this
137+
*/
138+
public function withVoice(string $voice): self
139+
{
140+
$provider = clone $this;
141+
142+
$provider->voice = $voice;
143+
144+
return $provider;
145+
}
146+
147+
123148
public function getOptions(): array
124149
{
125150
return [
126151
"language" => $this->language,
152+
"voice" => $this->voice,
127153
"speed" => $this->speed,
128154
];
129155
}
@@ -142,6 +168,7 @@ public function textToSpeech(string $text): string
142168
"key" => $this->apikey,
143169
"src" => $text,
144170
"hl" => $this->language,
171+
"v" => $this->voice,
145172
"r" => (string) $this->speed,
146173
"c" => "MP3",
147174
"f" => "16khz_16bit_stereo",

tests/Providers/VoiceRssProviderTest.php

+31-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testTextToSpeech(): void
4343

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

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

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

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

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

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

9898

99+
/**
100+
* Ensure we can set a different voice.
101+
*/
102+
public function testWithVoice1(): void
103+
{
104+
$provider = $this->provider->withVoice("Harry");
105+
106+
# Ensure immutability
107+
$this->assertSame("Harry", $provider->getOptions()["voice"]);
108+
$this->assertSame("Alice", $this->provider->getOptions()["voice"]);
109+
110+
$response = Mockery::mock(ResponseInterface::class);
111+
$response->shouldReceive("getStatusCode")->once()->andReturn("200");
112+
$response->shouldReceive("getBody")->once()->andReturn(Utils::streamFor("mp3"));
113+
114+
$this->client->shouldReceive("request")
115+
->once()
116+
->with("GET", "https://api.voicerss.org/?key=APIKEY&src=Hello&hl=en-gb&v=Harry&r=0&c=MP3&f=16khz_16bit_stereo")
117+
->andReturn($response);
118+
119+
$this->assertSame("mp3", $provider->textToSpeech("Hello"));
120+
}
121+
122+
99123
public function testWithSpeed(): void
100124
{
101125
$provider = $this->provider->withSpeed(-5);
@@ -110,7 +134,7 @@ public function testWithSpeed(): void
110134

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

116140
$this->assertSame("mp3", $provider->textToSpeech("Hello"));
@@ -129,6 +153,7 @@ public function testGetOptions(): void
129153
{
130154
$options = [
131155
"language" => "en-gb",
156+
"voice" => "Alice",
132157
"speed" => 0,
133158
];
134159

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

139164
public function testConstructorOptions1(): void
140165
{
141-
$provider = new VoiceRssProvider("APIKEY", "ab-cd", 10);
166+
$provider = new VoiceRssProvider("APIKEY", "ab-cd", 10, "Harry");
142167

143168
$options = [
144169
"language" => "ab-cd",
170+
"voice" => "Harry",
145171
"speed" => 10,
146172
];
147173

0 commit comments

Comments
 (0)