Skip to content

Commit 334d40f

Browse files
author
github-actions
committed
Update documentation with code samples and descriptions
1 parent ed41161 commit 334d40f

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

documentation/swift-readme.md

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
## Speech-to-Text Conversion Using Deepgram API with URLSession in Swift
2+
3+
**Title:** Converting Pre-recorded Speech to Text Using URLSession in Swift
4+
5+
**Code Sample:** speech-to-text/prerecorded/remote/urlsession/main.swift
6+
7+
**Description:** This Swift code uses URLSession to send a POST request to the Deepgram API. It uploads a pre-recorded audio file (spacewalk.wav) and converts the speech in the audio file to text. The API response is then parsed and printed to the console.
8+
9+
### speech-to-text/prerecorded/remote/urlsession/main.swift
10+
11+
```swift
12+
import Foundation
13+
14+
// Define the URL for the Deepgram API endpoint
15+
let url = URL(string: "https://api.deepgram.com/v1/listen")!
16+
17+
// Define the request body
18+
let requestBody = ["url": "https://dpgr.am/spacewalk.wav"]
19+
guard let httpBody = try? JSONSerialization.data(withJSONObject: requestBody) else {
20+
print("Error: Unable to serialize request body")
21+
exit(1)
22+
}
23+
24+
// Define the request headers
25+
var request = URLRequest(url: url)
26+
request.httpMethod = "POST"
27+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
28+
request.setValue("application/json", forHTTPHeaderField: "Accept")
29+
request.setValue("Token DEEPGRAM_API_KEY", forHTTPHeaderField: "Authorization") // Replace YOUR_DEEPGRAM_API_KEY with your actual API key
30+
request.httpBody = httpBody
31+
32+
// Perform the HTTP request
33+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
34+
guard let data = data, let httpResponse = response as? HTTPURLResponse, error == nil else {
35+
print("Error: \(error?.localizedDescription ?? "Unknown error")")
36+
return
37+
}
38+
39+
// Check if the HTTP request was successful (status code 200)
40+
guard httpResponse.statusCode == 200 else {
41+
print("HTTP request failed with status code \(httpResponse.statusCode)")
42+
return
43+
}
44+
45+
// Parse and print the response body
46+
if let responseBody = String(data: data, encoding: .utf8) {
47+
print("Response: \(responseBody)")
48+
} else {
49+
print("Error: Unable to parse response body")
50+
}
51+
}
52+
53+
task.resume()
54+
55+
// Keep the program running until the HTTP request completes
56+
RunLoop.main.run()
57+
58+
```
59+
60+
## Speech-to-Text Conversion using Deepgram API with Local File
61+
62+
**Title:** Converting Pre-recorded Audio to Text using Deepgram API
63+
64+
**Code Sample:** speech-to-text/prerecorded/local/urlsession/main.swift
65+
66+
**Description:** This Swift code uses the Deepgram API to convert a pre-recorded audio file into text. It reads an audio file as binary data, creates a POST request with the audio data as the body, and sends it to the Deepgram API. The API response is then printed to the console.
67+
68+
### speech-to-text/prerecorded/local/urlsession/main.swift
69+
70+
```swift
71+
import Foundation
72+
73+
// Specify the URL for the Deepgram API endpoint
74+
let url = URL(string: "https://api.deepgram.com/v1/listen")!
75+
76+
// Specify the path to the audio file
77+
let audioFilePath = "/path/to/youraudio.wav"
78+
79+
// Read the audio file as binary data
80+
guard let audioData = FileManager.default.contents(atPath: audioFilePath) else {
81+
print("Error: Unable to read audio file")
82+
exit(1)
83+
}
84+
85+
// Create the URLRequest object
86+
var request = URLRequest(url: url)
87+
request.httpMethod = "POST"
88+
89+
// Set request headers
90+
request.setValue("Token DEEPGRAM_API_KEY", forHTTPHeaderField: "Authorization") // Replace YOUR_DEEPGRAM_API_KEY with your actual API key
91+
request.setValue("audio/wav", forHTTPHeaderField: "Content-Type")
92+
93+
// Set request body with audio data
94+
request.httpBody = audioData
95+
96+
// Create URLSession task to perform the request
97+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
98+
if let error = error {
99+
print("Error: \(error)")
100+
return
101+
}
102+
103+
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
104+
print("Error: Invalid response")
105+
return
106+
}
107+
108+
if let data = data {
109+
if let responseBody = String(data: data, encoding: .utf8) {
110+
print("Response: \(responseBody)")
111+
} else {
112+
print("Error: Unable to parse response body")
113+
}
114+
} else {
115+
print("Error: No response data")
116+
}
117+
}
118+
119+
// Start the URLSession task
120+
task.resume()
121+
122+
// Keep the program running until the URLSession task completes
123+
RunLoop.main.run()
124+
125+
```
126+
127+
## Text-to-Speech Conversion Using Deepgram API
128+
129+
**Title:** Converting Text to Speech using URLSession in Swift
130+
131+
**Code Sample:** text-to-speech/urlsession/main.swift
132+
133+
**Description:** This Swift code uses the Deepgram API to convert text to speech. It sends a POST request with the text to be spoken to the Deepgram API. The response, an audio file, is then saved locally. The code uses URLSession for networking tasks.
134+
135+
### text-to-speech/urlsession/main.swift
136+
137+
```swift
138+
import Foundation
139+
140+
// Specify the URL for the Deepgram API endpoint
141+
let url = URL(string: "https://api.deepgram.com/v1/speak?model=aura-asteria-en")!
142+
143+
// Replace DEEPGRAM_API_KEY with your actual API key
144+
let apiKey = "DEEPGRAM_API_KEY"
145+
146+
// Text to be converted to speech
147+
let textToSpeak = "Hello, how can I help you today?"
148+
149+
// Create the URLRequest object
150+
var request = URLRequest(url: url)
151+
request.httpMethod = "POST"
152+
153+
// Set request headers
154+
request.setValue("Token \(apiKey)", forHTTPHeaderField: "Authorization")
155+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
156+
157+
// Create request body with text data
158+
let textData = ["text": textToSpeak]
159+
let jsonData = try! JSONSerialization.data(withJSONObject: textData)
160+
request.httpBody = jsonData
161+
162+
// Create URLSession task to perform the request
163+
let task = URLSession.shared.dataTask(with: request) { data, response, error in
164+
if let error = error {
165+
print("Error: \(error)")
166+
return
167+
}
168+
169+
guard let httpResponse = response as? HTTPURLResponse, (200...299).contains(httpResponse.statusCode) else {
170+
print("Error: Invalid response")
171+
return
172+
}
173+
174+
if let audioData = data {
175+
do {
176+
// Specify the path to save the output MP3 file
177+
let outputPath = "your_output_file.mp3"
178+
try audioData.write(to: URL(fileURLWithPath: outputPath))
179+
print("MP3 file saved at: \(outputPath)")
180+
} catch {
181+
print("Error saving MP3 file: \(error)")
182+
}
183+
} else {
184+
print("Error: No response data")
185+
}
186+
}
187+
188+
// Start the URLSession task
189+
task.resume()
190+
191+
// Keep the program running until the URLSession task completes
192+
RunLoop.main.run()
193+
194+
```
195+

documentation/update_languages.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ languages:
99
# - "python"
1010
# - "ruby"
1111
# - "rust"
12-
- "swift"
12+
# - "swift"

0 commit comments

Comments
 (0)