Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of getting bytes instead of saving directly to file for speech #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dylandhall
Copy link

@dylandhall dylandhall commented May 29, 2024

This modifies the speech API to allow getting the bytes directly to play.

This will allow platforms like web which cannot write files to play the audio, and will allow other platforms to play the audio without adding file system permissions to write the temporary files.

tests as working with code:


    var audio = await OpenAI.instance.audio.createSpeechBytes(
        model: "tts-1",
        input: "message",
        voice: "fable",
        responseFormat: OpenAIAudioSpeechResponseFormat.mp3);

  var audioPlayer = AudioPlayer();

  await audioPlayer.play(BytesSource(audio, mimeType: 'audio/mpeg'));

@@ -159,7 +159,7 @@ packages:
path: ".."
relative: true
source: path
version: "4.1.4"
version: "5.1.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was automatic, please revert if incorrect

@@ -13,6 +14,14 @@ abstract class CreateInterface {
Directory? outputDirectory,
});

Future<Uint8List> createSpeechBytes({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't create an overload with the same param names, rename if unsuitable.

return response.bodyBytes;
}

static Future<http.Response> postAndGetResponse({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separated the basics of getting the response with bytes data so we still have the error handling/logging

}) async {

var response = await postAndGetResponse(to: to, body: body, client: client);
return response.bodyBytes;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case just get the bytes, should mean we can play with AudioPlayer BytesSource data source on platforms which don't support file operations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant