> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Audio APIs

> Chọn các route âm thanh của CometAPI cho quy trình text-to-speech, phiên âm và dịch âm thanh.

Sử dụng tài liệu về model âm thanh của CometAPI bằng cách chọn xem ứng dụng của bạn cần đầu ra giọng nói, phiên âm hay dịch thuật. Các endpoint âm thanh dùng cùng mẫu API key CometAPI như các endpoint tương thích OpenAI khác.

## Chọn một audio API

<CardGroup cols={3}>
  <Card title="Tạo giọng nói" icon="microphone" href="/api/audio/create-speech">
    Chuyển văn bản thành giọng nói.
  </Card>

  <Card title="Tạo bản phiên âm" icon="message" href="/api/audio/create-transcription">
    Phiên âm âm thanh thành văn bản.
  </Card>

  <Card title="Tạo bản dịch" icon="sparkles" href="/api/audio/create-translation">
    Dịch âm thanh sang văn bản tiếng Anh.
  </Card>
</CardGroup>

## Create speech

Sử dụng một model ID có khả năng xử lý âm thanh từ [trang Models](/vi/overview/models) hoặc [thư mục model](https://www.cometapi.com/models/). Các ví dụ bên dưới gọi endpoint speech.

<Note>
  Các ví dụ này sử dụng giá trị giữ chỗ `your-audio-model-id`. Hãy thay thế nó bằng một model ID âm thanh khả dụng từ [trang Models](/vi/overview/models) hoặc [thư mục model](https://www.cometapi.com/models/) trước khi bạn chạy request.
</Note>

<Tip>
  Mở [Create speech](/api/audio/create-speech) để sử dụng playground và schema của endpoint.
</Tip>

<CodeGroup>
  ```python Python theme={null}
  import os
  import requests

  response = requests.post(
      "https://api.cometapi.com/v1/audio/speech",
      headers={
          "Authorization": "Bearer " + os.environ["COMETAPI_KEY"],
          "Content-Type": "application/json",
      },
      json={
          "model": "your-audio-model-id",
          "input": "Welcome to CometAPI.",
          "voice": "alloy",
          "response_format": "mp3",
      },
      timeout=60,
  )

  response.raise_for_status()

  with open("speech.mp3", "wb") as audio_file:
      audio_file.write(response.content)
  ```

  ```javascript Node.js theme={null}
  import { writeFile } from "node:fs/promises";

  const response = await fetch("https://api.cometapi.com/v1/audio/speech", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "your-audio-model-id",
      input: "Welcome to CometAPI.",
      voice: "alloy",
      response_format: "mp3",
    }),
  });

  if (!response.ok) {
    throw new Error(await response.text());
  }

  const audio = Buffer.from(await response.arrayBuffer());
  await writeFile("speech.mp3", audio);
  ```

  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/audio/speech \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "your-audio-model-id",
      "input": "Welcome to CometAPI.",
      "voice": "alloy",
      "response_format": "mp3"
    }' \
    --output speech.mp3
  ```
</CodeGroup>

## Ví dụ phản hồi

Một phản hồi speech thành công là dữ liệu âm thanh nhị phân. Phản hồi HTTP có thể trông như sau:

```text theme={null}
HTTP/1.1 200 OK
Content-Type: audio/mpeg

speech.mp3
```

## Bản ghi model ví dụ

<Info>
  Phản hồi danh mục model ví dụ này hiển thị lớp bao `/api/models` và hình dạng của một bản ghi model âm thanh. Đây không phải là danh sách model đầy đủ.
</Info>

```bash cURL theme={null}
curl https://api.cometapi.com/api/models
```

```json theme={null}
{
  "success": true,
  "page": 1,
  "page_size": 20,
  "total": 302,
  "data": [
    {
      "created": 0,
      "id": "your-audio-model-id",
      "code": "your-audio-model-id",
      "provider": "ExampleProvider",
      "provider_code": "example",
      "name": "Example audio model",
      "model_type": "audio",
      "features": [
        "text-to-speech"
      ],
      "endpoints": [
        "openai"
      ],
      "pricing": {
        "currency": "USD / M Tokens",
        "input": 12,
        "output": 12,
        "per_request": null,
        "per_second": null
      }
    }
  ]
}
```

## Các lỗi thường gặp

<AccordionGroup>
  <Accordion title="Định dạng âm thanh không được hỗ trợ">
    Sử dụng các định dạng được ghi trong tài liệu trên trang endpoint.
  </Accordion>

  <Accordion title="Tải lên dung lượng lớn bị từ chối">
    Nén tệp âm thanh hoặc chia tác vụ thành các tệp nhỏ hơn.
  </Accordion>

  <Accordion title="Bản chép lời trống">
    Xác nhận rằng tệp có chứa giọng nói và tên trường khớp với tài liệu.
  </Accordion>

  <Accordion title="Base URL sai">
    Sử dụng `https://api.cometapi.com/v1`.
  </Accordion>
</AccordionGroup>

## Mã lỗi và chiến lược retry

<AccordionGroup>
  <Accordion title="400">
    Không retry cho đến khi văn bản, tệp, model ID, giọng nói hoặc định dạng được sửa đúng.
  </Accordion>

  <Accordion title="401">
    Không retry cho đến khi API key đã có và hợp lệ.
  </Accordion>

  <Accordion title="404">
    Kiểm tra base URL, đường dẫn và model ID trước khi retry.
  </Accordion>

  <Accordion title="413">
    Giảm kích thước tải lên trước khi retry.
  </Accordion>

  <Accordion title="429">
    Retry với exponential backoff và giảm mức độ đồng thời.
  </Accordion>

  <Accordion title="500 or 503">
    Retry với backoff cho các lỗi tạm thời từ provider hoặc dịch vụ.
  </Accordion>
</AccordionGroup>

<Tip>
  Để xem các mẫu triển khai, hãy xem [Mã lỗi và chiến lược retry](/vi/guides/error-codes-and-retry-strategy) và [Giới hạn tốc độ và mức độ đồng thời](/vi/guides/rate-limits-and-concurrency).
</Tip>

## Giá và danh mục model

<CardGroup cols={3}>
  <Card title="Trang models" icon="list" href="/overview/models">
    Đọc cách CometAPI hiển thị model ID trong tài liệu.
  </Card>

  <Card title="Danh mục model" icon="puzzle-piece" href="https://www.cometapi.com/models/">
    Xem tình trạng khả dụng và năng lực của model.
  </Card>

  <Card title="Giá" icon="tag" href="https://www.cometapi.com/pricing/">
    Kiểm tra giá trước khi bạn gọi model.
  </Card>
</CardGroup>
