Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
SyncTTS features: specify format, retrieve URL (#101)
Browse files Browse the repository at this point in the history
* Deprecate _post_request_raw, add option to specify format in syncTTS

* Add tests

* Bump version

* Update changelog

* Update CHANGELOG.md
  • Loading branch information
martinezpl authored Jul 19, 2022
1 parent 0972dd0 commit 0220dcf
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ response = apiaudio.SyncTTS.create(text="Hello", voice="shelly", metadata=True)

* **Breaking Change** Since 4th of July, when using certain sound templates in conjunction with mastering section properties, resulted in sections that were in-fact to long. We've fixed these errors and this is already shipped. This will improve your experience, but you may notice some differences with behaviour of our mastering engine.

## Wednesday 13th July 2022
* We added two features to `syncTTS` resource:
* Retrieve URL to audio instead of raw data (Note: size limits don't apply in this case!):
```python
dictionary = apiaudio.SyncTTS.create(
text="Hello, how are you?",
voice="joanna",
url=True
)
url = dictionary["url"]
```
* Specify format of audio (currently supported formats are: "mp3", "wav", "pcm"):
```python
mp3_bytes = apiaudio.SyncTTS.create(
text="Hello, how are you?",
voice="joanna",
format="mp3"
)
```

## Friday 8th July 2022

### New Voices
Expand Down
2 changes: 1 addition & 1 deletion apiaudio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Configuration variables

sdk_version = "0.16.4"
sdk_version = "0.16.5"

api_key = None
assume_org_id = None
Expand Down
20 changes: 5 additions & 15 deletions apiaudio/api_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ def _build_header(cls):
}

@classmethod
def _post_request(cls, json, url=None):
def _post_request(cls, json, url=None, headers=None):
headers = headers or {}
loop_status_code = cls.__dict__.get("loop_status_code")
url = url or f"{apiaudio.api_base}{cls.resource_path}"
headers = cls._build_header()
headers.update(cls._build_header())
r = requests.post(url=url, headers=headers, json=json)

if loop_status_code:
Expand All @@ -58,21 +59,10 @@ def _post_request(cls, json, url=None):

cls._expanded_raise_for_status(r)

if r.headers["Content-Type"] != "application/json":
return r.content
return r.json()

@classmethod
def _post_request_raw(cls, json, url=None, istype="wav"):
url = url or cls.resource_path
url = f"{apiaudio.api_base}{url}"
headers = cls._build_header()
if istype == "wav":
headers["Accept"] = "audio/wav"
r = requests.post(url=url, headers=headers, json=json)

cls._expanded_raise_for_status(r)

return r.content

@classmethod
def _delete_request(cls, url=None, path_param=None, request_params=None):
url = url or f"{apiaudio.api_base}{cls.resource_path}"
Expand Down
7 changes: 4 additions & 3 deletions apiaudio/api_resources/syncTTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SyncTTS(CreatableResource):

@classmethod
def create(cls, **params):
return cls._post_request_raw(
json=params, url=f"{cls.resource_path}", istype="wav"
)
headers = {"Accept": "audio/wav"}
if params.get("format"):
headers = {"Accept": f"audio/{params.pop('format')}"}
return cls._post_request(json=params, headers=headers)
26 changes: 25 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from time import time

apiaudio.api_key = os.environ["AFLR_API_KEY"]
apiaudio.api_base = "https://staging-v1.api.audio"
#apiaudio.api_base = "https://staging-v1.api.audio"


def test_level_setting():
Expand Down Expand Up @@ -74,6 +74,30 @@ def test_script_versions():
assert speech[key]["status_code"] == "201"


def test_synctts():
audio = apiaudio.SyncTTS.create(
text="Hello, test 123!",
voice="joanna"
)
assert isinstance(audio, bytes)
assert b"RIFF" in audio

d = apiaudio.SyncTTS.create(
text="Hello, test 123.",
voice="joanna",
url=True
)
assert isinstance(d, dict)
assert "api.audio" in d.get("url", [])

mp3_audio = apiaudio.SyncTTS.create(
text="Hello, test 123.",
voice="joanna",
format="mp3"
)
assert isinstance(mp3_audio, bytes)
assert not b"RIFF" in mp3_audio

def test_processing_loop_speech():
t0 = time()
speech = apiaudio.Speech.create(scriptId="longProcessing", voice="Dieter")
Expand Down

0 comments on commit 0220dcf

Please sign in to comment.