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

Fix:The speed parameter type for the audioCreatSpeech request #206

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions Sources/OpenAI/Public/Models/AudioSpeechQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public struct AudioSpeechQuery: Codable {
case aac
case flac
}

/// The text to generate audio for. The maximum length is 4096 characters.
public let input: String
/// One of the available TTS models: tts-1 or tts-1-hd
Expand All @@ -50,7 +49,7 @@ public struct AudioSpeechQuery: Codable {
public let responseFormat: AudioSpeechResponseFormat?
/// The speed of the generated audio. Select a value from **0.25** to **4.0**. **1.0** is the default.
/// Defaults to 1
public let speed: String?
public let speed: Double?

public enum CodingKeys: String, CodingKey {
case model
Expand All @@ -60,7 +59,7 @@ public struct AudioSpeechQuery: Codable {
case speed
}

public init(model: Model, input: String, voice: AudioSpeechVoice, responseFormat: AudioSpeechResponseFormat = .mp3, speed: Double?) {
public init(model: Model, input: String, voice: AudioSpeechVoice, responseFormat: AudioSpeechResponseFormat = .mp3, speed: Double = 1.0) {
self.model = AudioSpeechQuery.validateSpeechModel(model)
self.speed = AudioSpeechQuery.normalizeSpeechSpeed(speed)
self.input = input
Expand Down Expand Up @@ -89,13 +88,13 @@ public extension AudioSpeechQuery {
case min = 0.25
}

static func normalizeSpeechSpeed(_ inputSpeed: Double?) -> String {
guard let inputSpeed else { return "\(Self.Speed.normal.rawValue)" }
static func normalizeSpeechSpeed(_ inputSpeed: Double?) -> Double {
guard let inputSpeed = inputSpeed else { return Self.Speed.normal.rawValue }
let isSpeedOutOfBounds = inputSpeed <= Self.Speed.min.rawValue || Self.Speed.max.rawValue <= inputSpeed
guard !isSpeedOutOfBounds else {
print("[AudioSpeech] Speed value must be between 0.25 and 4.0. Setting value to closest valid.")
return inputSpeed < Self.Speed.min.rawValue ? "\(Self.Speed.min.rawValue)" : "\(Self.Speed.max.rawValue)"
return inputSpeed < Self.Speed.min.rawValue ? Self.Speed.min.rawValue : Self.Speed.max.rawValue
}
return "\(inputSpeed)"
return inputSpeed
}
}