OpenAIWhisperEngine

class OpenAIWhisperEngine(apiKey: String, model: String = "whisper-1", httpClient: HttpClient = createDefaultWhisperHttpClient(), chunkDurationSeconds: Int = 10, endpointUrl: String = OPENAI_TRANSCRIPTIONS_URL, additionalHeaders: Headers = Headers.Empty) : TranscriptionEngine

OpenAI Whisper API transcription engine.

Note: This is NOT true real-time streaming. OpenAI's Whisper API processes complete audio files, so this engine buffers audio and sends it in chunks rather than doing true low-latency streaming.

This engine is best suited for:

  • Post-recording transcription

  • High accuracy requirements

  • Short audio clips

Example

val engine = OpenAIWhisperEngine(apiKey = "your-openai-api-key")

// Best used with complete recordings
val recording = recorder.getRecording()
recording?.asAudioFlow()?.transcribe(engine)?.collect { result ->
println("Transcription: ${result}")
}

Browsers (JS / WasmJS)

OpenAI's API does not send Access-Control-Allow-Origin, so direct calls from a browser are blocked by CORS. Stand up a thin backend that forwards the multipart upload to OpenAI and point endpointUrl at it. Your backend keeps the API key; pass any auth headers it requires via additionalHeaders (and leave apiKey blank if your backend doesn't proxy the bearer token).

val engine = OpenAIWhisperEngine(
apiKey = "",
endpointUrl = "https://my-app.example.com/api/transcribe",
additionalHeaders = headersOf("X-App-Token", "client-secret"),
)

Parameters

apiKey

Your OpenAI API key. Leave empty when proxying through your own backend (see endpointUrl).

model

The Whisper model to use (default: "whisper-1").

httpClient

HTTP client for API calls (default: platform createDefaultWhisperHttpClient).

chunkDurationSeconds

Duration in seconds for each chunk (for streaming mode).

endpointUrl

The URL to POST audio chunks to. Defaults to OpenAI's public endpoint; override to point at your own proxy when running in a browser (avoids CORS issues, see GitHub issue #16).

additionalHeaders

Extra headers appended to every request (useful for proxy authentication tokens, tracing, etc.).

Constructors

Link copied to clipboard
constructor(apiKey: String, model: String = "whisper-1", httpClient: HttpClient = createDefaultWhisperHttpClient(), chunkDurationSeconds: Int = 10, endpointUrl: String = OPENAI_TRANSCRIPTIONS_URL, additionalHeaders: Headers = Headers.Empty)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val isAvailable: Boolean

The engine is available when either an apiKey is configured or the caller has pointed endpointUrl at a custom proxy that supplies its own authentication.

Link copied to clipboard
open override val provider: TranscriptionProvider

The provider type for this engine.

Functions

Link copied to clipboard
open override fun release()

Releases any resources held by this engine. After calling this method, the engine should not be used for further transcription.

Link copied to clipboard
open override fun transcribe(audioFlow: AudioFlow, config: TranscriptionConfig = TranscriptionConfig.Default): Flow<TranscriptionResult>

Transcribes audio from the given AudioFlow in real-time.