diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a3aba2..017c378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/apiaudio/__init__.py b/apiaudio/__init__.py index 15221e5..164d1e7 100644 --- a/apiaudio/__init__.py +++ b/apiaudio/__init__.py @@ -3,7 +3,7 @@ # Configuration variables -sdk_version = "0.16.4" +sdk_version = "0.16.5" api_key = None assume_org_id = None diff --git a/apiaudio/api_request.py b/apiaudio/api_request.py index 5ffe538..c3869d9 100644 --- a/apiaudio/api_request.py +++ b/apiaudio/api_request.py @@ -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: @@ -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}" diff --git a/apiaudio/api_resources/syncTTS.py b/apiaudio/api_resources/syncTTS.py index 97097af..0953fec 100644 --- a/apiaudio/api_resources/syncTTS.py +++ b/apiaudio/api_resources/syncTTS.py @@ -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) diff --git a/tests/test_config.py b/tests/test_config.py index 86d6354..7dd07ba 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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(): @@ -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")