diff --git a/examples/ggwave-to-file/ggwave-to-file.py b/examples/ggwave-to-file/ggwave-to-file.py index 8819ca2..bfa0dd7 100644 --- a/examples/ggwave-to-file/ggwave-to-file.py +++ b/examples/ggwave-to-file/ggwave-to-file.py @@ -1,7 +1,9 @@ import sys import requests +import wave -def ggwave(message: str, protocolId: int = 1, sampleRate: float = 48000, volume: int = 50, payloadLength: int = -1): + +def ggwave(message: str, file: str, protocolId: int = 1, sampleRate: float = 48000, volume: int = 50, payloadLength: int = -1) -> None: url = 'https://ggwave-to-file.ggerganov.com/' @@ -15,11 +17,15 @@ def ggwave(message: str, protocolId: int = 1, sampleRate: float = 48000, volume: response = requests.get(url, params=params) - if response == '': + if response == '' or b'Usage: ggwave-to-file' in response.content: raise SyntaxError('Request failed') - return response + with wave.open(file, 'wb') as f: + f.setnchannels(1) + f.setframerate(sampleRate) + f.setsampwidth(2) + f.writeframes(response.content) -result = ggwave("Hello world!") -sys.stdout.buffer.write(result.content) +if __name__ == "__main__": + ggwave("Hello world!", "hello_world.wav")