├── README.md
├── VDSTextToSpeech.xcodeproj
├── project.pbxproj
├── project.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
└── xcuserdata
│ └── vimaldas.xcuserdatad
│ ├── xcdebugger
│ └── Breakpoints_v2.xcbkptlist
│ └── xcschemes
│ └── xcschememanagement.plist
└── VDSTextToSpeech
├── AppDelegate.swift
├── Assets.xcassets
├── AppIcon.appiconset
│ └── Contents.json
└── Contents.json
├── Base.lproj
├── LaunchScreen.storyboard
└── Main.storyboard
├── Info.plist
├── VDSTextToSpeech.swift
└── ViewController.swift
/README.md:
--------------------------------------------------------------------------------
1 | # VDSTextToSpeech
2 |
3 | To use VDSTextToSpeech, follow the simple steps
4 |
5 | 1. drag VDSTextToSpeech to your project.
6 |
7 | 2. create a variable for easy access to VDSTextToSpeech
8 |
9 | let vds = VDSTextToSpeech.shared
10 |
11 | 3. give the message to be read.
12 |
13 | vds.text = "welcome to VDS Text to speech module. Hope you enjoy"
14 |
15 | 4. speak() : to make VDSTextToSpeech read the message
16 |
17 | stopSpeech() : to stop reading
18 |
19 | pauseSpeech() : to pause reading (to resume call speak() again)
20 |
21 | 8. to get callbacks and progress, add VDSTextToSpeech delegate to class
22 | eg:
23 |
24 | class ViewController: UIViewController,VDSSpeechSynthesizerDelegate {
25 |
26 | vds.speechSynthesizerDelegate = self
27 |
28 |
29 | //MARK:- VDSSpeechSynthesizerDelegate
30 | func speechSynthesizerProgress(_ progress: Float, attributedText: NSMutableAttributedString) {
31 | messageLabel.attributedText = attributedText
32 | progressView.progress = progress
33 | }
34 |
35 | func speechSynthesizerDidStart() {
36 | print("speechSynthesizerDidStart")
37 | }
38 |
39 | func speechSynthesizerDidFinish() {
40 | print("speechSynthesizerDidFinish")
41 | }
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/VDSTextToSpeech.xcodeproj/project.pbxproj:
--------------------------------------------------------------------------------
1 | // !$*UTF8*$!
2 | {
3 | archiveVersion = 1;
4 | classes = {
5 | };
6 | objectVersion = 50;
7 | objects = {
8 |
9 | /* Begin PBXBuildFile section */
10 | 025C4ECF217DDCCF00BEEE05 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025C4ECE217DDCCF00BEEE05 /* AppDelegate.swift */; };
11 | 025C4ED1217DDCCF00BEEE05 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025C4ED0217DDCCF00BEEE05 /* ViewController.swift */; };
12 | 025C4ED4217DDCCF00BEEE05 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 025C4ED2217DDCCF00BEEE05 /* Main.storyboard */; };
13 | 025C4ED6217DDCD000BEEE05 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 025C4ED5217DDCD000BEEE05 /* Assets.xcassets */; };
14 | 025C4ED9217DDCD000BEEE05 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 025C4ED7217DDCD000BEEE05 /* LaunchScreen.storyboard */; };
15 | 025C4EE1217DDCED00BEEE05 /* VDSTextToSpeech.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025C4EE0217DDCED00BEEE05 /* VDSTextToSpeech.swift */; };
16 | /* End PBXBuildFile section */
17 |
18 | /* Begin PBXFileReference section */
19 | 025C4ECB217DDCCF00BEEE05 /* VDSTextToSpeech.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = VDSTextToSpeech.app; sourceTree = BUILT_PRODUCTS_DIR; };
20 | 025C4ECE217DDCCF00BEEE05 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; };
21 | 025C4ED0217DDCCF00BEEE05 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; };
22 | 025C4ED3217DDCCF00BEEE05 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; };
23 | 025C4ED5217DDCD000BEEE05 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; };
24 | 025C4ED8217DDCD000BEEE05 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; };
25 | 025C4EDA217DDCD000BEEE05 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; };
26 | 025C4EE0217DDCED00BEEE05 /* VDSTextToSpeech.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VDSTextToSpeech.swift; sourceTree = ""; };
27 | /* End PBXFileReference section */
28 |
29 | /* Begin PBXFrameworksBuildPhase section */
30 | 025C4EC8217DDCCF00BEEE05 /* Frameworks */ = {
31 | isa = PBXFrameworksBuildPhase;
32 | buildActionMask = 2147483647;
33 | files = (
34 | );
35 | runOnlyForDeploymentPostprocessing = 0;
36 | };
37 | /* End PBXFrameworksBuildPhase section */
38 |
39 | /* Begin PBXGroup section */
40 | 025C4EC2217DDCCF00BEEE05 = {
41 | isa = PBXGroup;
42 | children = (
43 | 025C4ECD217DDCCF00BEEE05 /* VDSTextToSpeech */,
44 | 025C4ECC217DDCCF00BEEE05 /* Products */,
45 | );
46 | sourceTree = "";
47 | };
48 | 025C4ECC217DDCCF00BEEE05 /* Products */ = {
49 | isa = PBXGroup;
50 | children = (
51 | 025C4ECB217DDCCF00BEEE05 /* VDSTextToSpeech.app */,
52 | );
53 | name = Products;
54 | sourceTree = "";
55 | };
56 | 025C4ECD217DDCCF00BEEE05 /* VDSTextToSpeech */ = {
57 | isa = PBXGroup;
58 | children = (
59 | 025C4ECE217DDCCF00BEEE05 /* AppDelegate.swift */,
60 | 025C4ED0217DDCCF00BEEE05 /* ViewController.swift */,
61 | 025C4EE0217DDCED00BEEE05 /* VDSTextToSpeech.swift */,
62 | 025C4ED2217DDCCF00BEEE05 /* Main.storyboard */,
63 | 025C4ED5217DDCD000BEEE05 /* Assets.xcassets */,
64 | 025C4ED7217DDCD000BEEE05 /* LaunchScreen.storyboard */,
65 | 025C4EDA217DDCD000BEEE05 /* Info.plist */,
66 | );
67 | path = VDSTextToSpeech;
68 | sourceTree = "";
69 | };
70 | /* End PBXGroup section */
71 |
72 | /* Begin PBXNativeTarget section */
73 | 025C4ECA217DDCCF00BEEE05 /* VDSTextToSpeech */ = {
74 | isa = PBXNativeTarget;
75 | buildConfigurationList = 025C4EDD217DDCD000BEEE05 /* Build configuration list for PBXNativeTarget "VDSTextToSpeech" */;
76 | buildPhases = (
77 | 025C4EC7217DDCCF00BEEE05 /* Sources */,
78 | 025C4EC8217DDCCF00BEEE05 /* Frameworks */,
79 | 025C4EC9217DDCCF00BEEE05 /* Resources */,
80 | );
81 | buildRules = (
82 | );
83 | dependencies = (
84 | );
85 | name = VDSTextToSpeech;
86 | productName = VDSTextToSpeech;
87 | productReference = 025C4ECB217DDCCF00BEEE05 /* VDSTextToSpeech.app */;
88 | productType = "com.apple.product-type.application";
89 | };
90 | /* End PBXNativeTarget section */
91 |
92 | /* Begin PBXProject section */
93 | 025C4EC3217DDCCF00BEEE05 /* Project object */ = {
94 | isa = PBXProject;
95 | attributes = {
96 | LastSwiftUpdateCheck = 1000;
97 | LastUpgradeCheck = 1000;
98 | ORGANIZATIONNAME = "Vimal Das";
99 | TargetAttributes = {
100 | 025C4ECA217DDCCF00BEEE05 = {
101 | CreatedOnToolsVersion = 10.0;
102 | };
103 | };
104 | };
105 | buildConfigurationList = 025C4EC6217DDCCF00BEEE05 /* Build configuration list for PBXProject "VDSTextToSpeech" */;
106 | compatibilityVersion = "Xcode 9.3";
107 | developmentRegion = en;
108 | hasScannedForEncodings = 0;
109 | knownRegions = (
110 | en,
111 | Base,
112 | );
113 | mainGroup = 025C4EC2217DDCCF00BEEE05;
114 | productRefGroup = 025C4ECC217DDCCF00BEEE05 /* Products */;
115 | projectDirPath = "";
116 | projectRoot = "";
117 | targets = (
118 | 025C4ECA217DDCCF00BEEE05 /* VDSTextToSpeech */,
119 | );
120 | };
121 | /* End PBXProject section */
122 |
123 | /* Begin PBXResourcesBuildPhase section */
124 | 025C4EC9217DDCCF00BEEE05 /* Resources */ = {
125 | isa = PBXResourcesBuildPhase;
126 | buildActionMask = 2147483647;
127 | files = (
128 | 025C4ED9217DDCD000BEEE05 /* LaunchScreen.storyboard in Resources */,
129 | 025C4ED6217DDCD000BEEE05 /* Assets.xcassets in Resources */,
130 | 025C4ED4217DDCCF00BEEE05 /* Main.storyboard in Resources */,
131 | );
132 | runOnlyForDeploymentPostprocessing = 0;
133 | };
134 | /* End PBXResourcesBuildPhase section */
135 |
136 | /* Begin PBXSourcesBuildPhase section */
137 | 025C4EC7217DDCCF00BEEE05 /* Sources */ = {
138 | isa = PBXSourcesBuildPhase;
139 | buildActionMask = 2147483647;
140 | files = (
141 | 025C4EE1217DDCED00BEEE05 /* VDSTextToSpeech.swift in Sources */,
142 | 025C4ED1217DDCCF00BEEE05 /* ViewController.swift in Sources */,
143 | 025C4ECF217DDCCF00BEEE05 /* AppDelegate.swift in Sources */,
144 | );
145 | runOnlyForDeploymentPostprocessing = 0;
146 | };
147 | /* End PBXSourcesBuildPhase section */
148 |
149 | /* Begin PBXVariantGroup section */
150 | 025C4ED2217DDCCF00BEEE05 /* Main.storyboard */ = {
151 | isa = PBXVariantGroup;
152 | children = (
153 | 025C4ED3217DDCCF00BEEE05 /* Base */,
154 | );
155 | name = Main.storyboard;
156 | sourceTree = "";
157 | };
158 | 025C4ED7217DDCD000BEEE05 /* LaunchScreen.storyboard */ = {
159 | isa = PBXVariantGroup;
160 | children = (
161 | 025C4ED8217DDCD000BEEE05 /* Base */,
162 | );
163 | name = LaunchScreen.storyboard;
164 | sourceTree = "";
165 | };
166 | /* End PBXVariantGroup section */
167 |
168 | /* Begin XCBuildConfiguration section */
169 | 025C4EDB217DDCD000BEEE05 /* Debug */ = {
170 | isa = XCBuildConfiguration;
171 | buildSettings = {
172 | ALWAYS_SEARCH_USER_PATHS = NO;
173 | CLANG_ANALYZER_NONNULL = YES;
174 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
175 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
176 | CLANG_CXX_LIBRARY = "libc++";
177 | CLANG_ENABLE_MODULES = YES;
178 | CLANG_ENABLE_OBJC_ARC = YES;
179 | CLANG_ENABLE_OBJC_WEAK = YES;
180 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
181 | CLANG_WARN_BOOL_CONVERSION = YES;
182 | CLANG_WARN_COMMA = YES;
183 | CLANG_WARN_CONSTANT_CONVERSION = YES;
184 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
186 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
187 | CLANG_WARN_EMPTY_BODY = YES;
188 | CLANG_WARN_ENUM_CONVERSION = YES;
189 | CLANG_WARN_INFINITE_RECURSION = YES;
190 | CLANG_WARN_INT_CONVERSION = YES;
191 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
192 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
193 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
194 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
195 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
196 | CLANG_WARN_STRICT_PROTOTYPES = YES;
197 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
198 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
199 | CLANG_WARN_UNREACHABLE_CODE = YES;
200 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
201 | CODE_SIGN_IDENTITY = "iPhone Developer";
202 | COPY_PHASE_STRIP = NO;
203 | DEBUG_INFORMATION_FORMAT = dwarf;
204 | ENABLE_STRICT_OBJC_MSGSEND = YES;
205 | ENABLE_TESTABILITY = YES;
206 | GCC_C_LANGUAGE_STANDARD = gnu11;
207 | GCC_DYNAMIC_NO_PIC = NO;
208 | GCC_NO_COMMON_BLOCKS = YES;
209 | GCC_OPTIMIZATION_LEVEL = 0;
210 | GCC_PREPROCESSOR_DEFINITIONS = (
211 | "DEBUG=1",
212 | "$(inherited)",
213 | );
214 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
215 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
216 | GCC_WARN_UNDECLARED_SELECTOR = YES;
217 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
218 | GCC_WARN_UNUSED_FUNCTION = YES;
219 | GCC_WARN_UNUSED_VARIABLE = YES;
220 | IPHONEOS_DEPLOYMENT_TARGET = 12.0;
221 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
222 | MTL_FAST_MATH = YES;
223 | ONLY_ACTIVE_ARCH = YES;
224 | SDKROOT = iphoneos;
225 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
226 | SWIFT_OPTIMIZATION_LEVEL = "-Onone";
227 | };
228 | name = Debug;
229 | };
230 | 025C4EDC217DDCD000BEEE05 /* Release */ = {
231 | isa = XCBuildConfiguration;
232 | buildSettings = {
233 | ALWAYS_SEARCH_USER_PATHS = NO;
234 | CLANG_ANALYZER_NONNULL = YES;
235 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
236 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
237 | CLANG_CXX_LIBRARY = "libc++";
238 | CLANG_ENABLE_MODULES = YES;
239 | CLANG_ENABLE_OBJC_ARC = YES;
240 | CLANG_ENABLE_OBJC_WEAK = YES;
241 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
242 | CLANG_WARN_BOOL_CONVERSION = YES;
243 | CLANG_WARN_COMMA = YES;
244 | CLANG_WARN_CONSTANT_CONVERSION = YES;
245 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
246 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
247 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
248 | CLANG_WARN_EMPTY_BODY = YES;
249 | CLANG_WARN_ENUM_CONVERSION = YES;
250 | CLANG_WARN_INFINITE_RECURSION = YES;
251 | CLANG_WARN_INT_CONVERSION = YES;
252 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
253 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
254 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
255 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
256 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
257 | CLANG_WARN_STRICT_PROTOTYPES = YES;
258 | CLANG_WARN_SUSPICIOUS_MOVE = YES;
259 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
260 | CLANG_WARN_UNREACHABLE_CODE = YES;
261 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
262 | CODE_SIGN_IDENTITY = "iPhone Developer";
263 | COPY_PHASE_STRIP = NO;
264 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
265 | ENABLE_NS_ASSERTIONS = NO;
266 | ENABLE_STRICT_OBJC_MSGSEND = YES;
267 | GCC_C_LANGUAGE_STANDARD = gnu11;
268 | GCC_NO_COMMON_BLOCKS = YES;
269 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
270 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
271 | GCC_WARN_UNDECLARED_SELECTOR = YES;
272 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
273 | GCC_WARN_UNUSED_FUNCTION = YES;
274 | GCC_WARN_UNUSED_VARIABLE = YES;
275 | IPHONEOS_DEPLOYMENT_TARGET = 12.0;
276 | MTL_ENABLE_DEBUG_INFO = NO;
277 | MTL_FAST_MATH = YES;
278 | SDKROOT = iphoneos;
279 | SWIFT_COMPILATION_MODE = wholemodule;
280 | SWIFT_OPTIMIZATION_LEVEL = "-O";
281 | VALIDATE_PRODUCT = YES;
282 | };
283 | name = Release;
284 | };
285 | 025C4EDE217DDCD000BEEE05 /* Debug */ = {
286 | isa = XCBuildConfiguration;
287 | buildSettings = {
288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
289 | CODE_SIGN_STYLE = Automatic;
290 | DEVELOPMENT_TEAM = R5G5Y6XAFM;
291 | INFOPLIST_FILE = VDSTextToSpeech/Info.plist;
292 | LD_RUNPATH_SEARCH_PATHS = (
293 | "$(inherited)",
294 | "@executable_path/Frameworks",
295 | );
296 | PRODUCT_BUNDLE_IDENTIFIER = com.vimal.VDSTextToSpeech;
297 | PRODUCT_NAME = "$(TARGET_NAME)";
298 | SWIFT_VERSION = 4.2;
299 | TARGETED_DEVICE_FAMILY = "1,2";
300 | };
301 | name = Debug;
302 | };
303 | 025C4EDF217DDCD000BEEE05 /* Release */ = {
304 | isa = XCBuildConfiguration;
305 | buildSettings = {
306 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
307 | CODE_SIGN_STYLE = Automatic;
308 | DEVELOPMENT_TEAM = R5G5Y6XAFM;
309 | INFOPLIST_FILE = VDSTextToSpeech/Info.plist;
310 | LD_RUNPATH_SEARCH_PATHS = (
311 | "$(inherited)",
312 | "@executable_path/Frameworks",
313 | );
314 | PRODUCT_BUNDLE_IDENTIFIER = com.vimal.VDSTextToSpeech;
315 | PRODUCT_NAME = "$(TARGET_NAME)";
316 | SWIFT_VERSION = 4.2;
317 | TARGETED_DEVICE_FAMILY = "1,2";
318 | };
319 | name = Release;
320 | };
321 | /* End XCBuildConfiguration section */
322 |
323 | /* Begin XCConfigurationList section */
324 | 025C4EC6217DDCCF00BEEE05 /* Build configuration list for PBXProject "VDSTextToSpeech" */ = {
325 | isa = XCConfigurationList;
326 | buildConfigurations = (
327 | 025C4EDB217DDCD000BEEE05 /* Debug */,
328 | 025C4EDC217DDCD000BEEE05 /* Release */,
329 | );
330 | defaultConfigurationIsVisible = 0;
331 | defaultConfigurationName = Release;
332 | };
333 | 025C4EDD217DDCD000BEEE05 /* Build configuration list for PBXNativeTarget "VDSTextToSpeech" */ = {
334 | isa = XCConfigurationList;
335 | buildConfigurations = (
336 | 025C4EDE217DDCD000BEEE05 /* Debug */,
337 | 025C4EDF217DDCD000BEEE05 /* Release */,
338 | );
339 | defaultConfigurationIsVisible = 0;
340 | defaultConfigurationName = Release;
341 | };
342 | /* End XCConfigurationList section */
343 | };
344 | rootObject = 025C4EC3217DDCCF00BEEE05 /* Project object */;
345 | }
346 |
--------------------------------------------------------------------------------
/VDSTextToSpeech.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/VDSTextToSpeech.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/VDSTextToSpeech.xcodeproj/xcuserdata/vimaldas.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/VDSTextToSpeech.xcodeproj/xcuserdata/vimaldas.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | VDSTextToSpeech.xcscheme
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/VDSTextToSpeech/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // VDSTextToSpeech
4 | //
5 | // Created by Vimal Das on 22/10/18.
6 | // Copyright © 2018 Vimal Das. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/VDSTextToSpeech/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/VDSTextToSpeech/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/VDSTextToSpeech/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/VDSTextToSpeech/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/VDSTextToSpeech/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/VDSTextToSpeech/VDSTextToSpeech.swift:
--------------------------------------------------------------------------------
1 | //
2 | // VDSTextToSpeech.swift
3 | // VDSTextToSpeech
4 | //
5 | // Created by Vimal Das on 22/10/18.
6 | // Copyright © 2018 Vimal Das. All rights reserved.
7 | //
8 |
9 | import UIKit
10 | import Speech
11 |
12 | protocol VDSSpeechSynthesizerDelegate: class {
13 | func speechSynthesizerDidStart()
14 | func speechSynthesizerDidFinish()
15 | func speechSynthesizerProgress(_ progress:Float, attributedText:NSMutableAttributedString)
16 | }
17 | class VDSTextToSpeech: NSObject, AVSpeechSynthesizerDelegate {
18 |
19 | static let shared = VDSTextToSpeech()
20 |
21 | weak var speechSynthesizerDelegate:VDSSpeechSynthesizerDelegate?
22 |
23 | var fontColor:UIColor = UIColor.orange
24 |
25 | //MARK:- Speech Synthesizer Variables
26 | private var speechSynthesizer: AVSpeechSynthesizer!
27 | var rate: Float = 0.0
28 | var pitch: Float = 0.0
29 | var volume: Float = 0.0
30 | var totalUtterances: Int! = 0
31 | var currentUtterance: Int! = 0
32 | var totalTextLength: Int = 0
33 | var spokenTextLengths: Int = 0
34 | var preferredVoiceLanguageCode: String!
35 | private var previousSelectedRange: NSRange!
36 | var text:String = "" {
37 | didSet{
38 | attributedText = NSMutableAttributedString(string: text)
39 | }
40 | }
41 | private var attributedText:NSMutableAttributedString = NSMutableAttributedString(string: "")
42 |
43 | //MARK:- Main Funcs
44 | private override init() {
45 | super.init()
46 |
47 | speechSynthesizer = AVSpeechSynthesizer()
48 |
49 | setupTextToSpeech()
50 |
51 | }
52 |
53 |
54 | // MARK:- AVSpeechSynthesizer
55 | private func setupTextToSpeech() {
56 |
57 | if !loadSettings() {
58 | registerDefaultSettings()
59 | }
60 |
61 | speechSynthesizer.delegate = self
62 |
63 | setInitialFontAttribute()
64 | }
65 |
66 | private func registerDefaultSettings() {
67 | rate = AVSpeechUtteranceDefaultSpeechRate
68 | pitch = 1.0
69 | volume = 1.0
70 |
71 | let defaultSpeechSettings = ["rate": rate, "pitch": pitch, "volume": volume]
72 |
73 | UserDefaults.standard.register(defaults: defaultSpeechSettings)
74 | }
75 |
76 |
77 | private func loadSettings() -> Bool {
78 | let userDefaults = UserDefaults.standard as UserDefaults
79 |
80 | if let theRate: Float = userDefaults.value(forKey: "rate") as? Float {
81 | rate = theRate
82 | pitch = userDefaults.value(forKey:"pitch") as! Float
83 | volume = userDefaults.value(forKey:"volume") as! Float
84 |
85 | return true
86 | }
87 |
88 | return false
89 | }
90 |
91 | func setInitialFontAttribute() {
92 | let rangeOfWholeText = NSMakeRange(0, text.utf16.count)
93 | attributedText = NSMutableAttributedString(string: text)
94 | attributedText.addAttribute(.font, value: UIFont(name: "Arial", size: 18.0)!, range: rangeOfWholeText)
95 | }
96 |
97 | private func unselectLastWord() {
98 | if let selectedRange = previousSelectedRange {
99 |
100 | let currentAttributes = attributedText.attributes(at: selectedRange.location, effectiveRange: nil)
101 |
102 | let fontAttribute = currentAttributes[NSAttributedString.Key.font] ?? UIFont.boldSystemFont(ofSize: 17)
103 |
104 |
105 | let attributedWord = NSMutableAttributedString(string: attributedText.attributedSubstring(from: selectedRange).string)
106 |
107 |
108 | attributedWord.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: NSMakeRange(0, attributedWord.length))
109 | attributedWord.addAttribute(.font, value: fontAttribute, range: NSMakeRange(0, attributedWord.length))
110 |
111 | // Update the text storage property and replace the last selected word with the new attributed string.
112 |
113 | attributedText.replaceCharacters(in: selectedRange, with: attributedWord)
114 |
115 | }
116 | }
117 |
118 |
119 | func speak() {
120 | if !speechSynthesizer.isSpeaking {
121 | /**
122 | let speechUtterance = AVSpeechUtterance(string: tvEditor.text)
123 | speechUtterance.rate = rate
124 | speechUtterance.pitchMultiplier = pitch
125 | speechUtterance.volume = volume
126 | speechSynthesizer.speakUtterance(speechUtterance)
127 | */
128 |
129 | let textParagraphs = text.components(separatedBy: "\n")
130 |
131 | totalUtterances = textParagraphs.count
132 | currentUtterance = 0
133 | totalTextLength = 0
134 | spokenTextLengths = 0
135 |
136 | for pieceOfText in textParagraphs {
137 | let speechUtterance = AVSpeechUtterance(string: pieceOfText)
138 | speechUtterance.rate = rate
139 | speechUtterance.pitchMultiplier = pitch
140 | speechUtterance.volume = volume
141 | speechUtterance.postUtteranceDelay = 0.005
142 |
143 | if let voiceLanguageCode = preferredVoiceLanguageCode {
144 | let voice = AVSpeechSynthesisVoice(language: voiceLanguageCode)
145 | speechUtterance.voice = voice
146 | }
147 |
148 | totalTextLength = totalTextLength + pieceOfText.utf16.count
149 |
150 | speechSynthesizer.speak(speechUtterance)
151 | }
152 |
153 |
154 | }
155 | else{
156 | speechSynthesizer.continueSpeaking()
157 | }
158 |
159 | }
160 |
161 |
162 | func pauseSpeech() {
163 | speechSynthesizer.pauseSpeaking(at: AVSpeechBoundary.word)
164 | }
165 |
166 |
167 | func stopSpeech() {
168 | speechSynthesizer.stopSpeaking(at: AVSpeechBoundary.immediate)
169 | }
170 |
171 |
172 |
173 | // MARK: AVSpeechSynthesizerDelegate method implementation
174 |
175 | func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) {
176 | spokenTextLengths = spokenTextLengths + utterance.speechString.utf16.count + 1
177 |
178 | let progress: Float = Float(spokenTextLengths * 100 / totalTextLength)
179 | speechSynthesizerDelegate?.speechSynthesizerProgress(progress/100, attributedText: attributedText)
180 |
181 | if currentUtterance == totalUtterances {
182 | unselectLastWord()
183 | previousSelectedRange = nil
184 | }
185 | speechSynthesizerDelegate?.speechSynthesizerDidFinish()
186 | }
187 |
188 |
189 | func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didStart utterance: AVSpeechUtterance) {
190 | currentUtterance = currentUtterance + 1
191 | speechSynthesizerDelegate?.speechSynthesizerDidStart()
192 | }
193 |
194 |
195 | func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) {
196 | let progress: Float = Float(spokenTextLengths + characterRange.location) * 100 / Float(totalTextLength)
197 | speechSynthesizerDelegate?.speechSynthesizerProgress(progress/100, attributedText: attributedText)
198 |
199 | // Determine the current range in the whole text (all utterances), not just the current one.
200 | let rangeInTotalText = NSMakeRange(spokenTextLengths + characterRange.location, characterRange.length)
201 |
202 | // Select the specified range in the textfield.
203 |
204 | // Store temporarily the current font attribute of the selected text.
205 | let currentAttributes = attributedText.attributes(at: rangeInTotalText.location, effectiveRange: nil)
206 | let fontAttribute = currentAttributes[NSAttributedString.Key.font] ?? UIFont.boldSystemFont(ofSize: 17)
207 |
208 | // Assign the selected text to a mutable attributed string.
209 | let attributedString = NSMutableAttributedString(string: attributedText.attributedSubstring(from: rangeInTotalText).string)
210 |
211 | // Make the text of the selected area orange by specifying a new attribute.
212 | attributedString.addAttribute(NSAttributedString.Key.foregroundColor, value: fontColor, range: NSMakeRange(0, attributedString.length))
213 |
214 | // Make sure that the text will keep the original font by setting it as an attribute.
215 | attributedString.addAttribute(NSAttributedString.Key.font, value: fontAttribute, range: NSMakeRange(0, attributedString.string.utf16.count))
216 |
217 | attributedText.replaceCharacters(in: rangeInTotalText, with: attributedString)
218 |
219 | // If there was another highlighted word previously (orange text color), then do exactly the same things as above and change the foreground color to black.
220 | if let previousRange = previousSelectedRange {
221 | let previousAttributedText = NSMutableAttributedString(string: attributedText.attributedSubstring(from: previousRange).string)
222 | previousAttributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: NSMakeRange(0, previousAttributedText.length))
223 | previousAttributedText.addAttribute(NSAttributedString.Key.font, value: fontAttribute, range: NSMakeRange(0, previousAttributedText.length))
224 |
225 | attributedText.replaceCharacters(in: previousRange, with: previousAttributedText)
226 | }
227 |
228 | // Keep the currently selected range so as to remove the orange text color next.
229 | previousSelectedRange = rangeInTotalText
230 | }
231 | }
232 |
--------------------------------------------------------------------------------
/VDSTextToSpeech/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // VDSTextToSpeech
4 | //
5 | // Created by Vimal Das on 22/10/18.
6 | // Copyright © 2018 Vimal Das. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ViewController: UIViewController,VDSSpeechSynthesizerDelegate {
12 |
13 | @IBOutlet weak var message: UILabel!
14 | @IBOutlet weak var progressView: UIProgressView! {
15 | didSet{
16 | progressView.progressTintColor = UIColor.orange
17 | progressView.progress = 0
18 | }
19 | }
20 |
21 | override func viewDidLoad() {
22 | super.viewDidLoad()
23 |
24 | message.text = "welcome to VDS Text to speech module. Hope you enjoy"
25 |
26 | let vds = VDSTextToSpeech.shared
27 | vds.speechSynthesizerDelegate = self
28 |
29 | vds.text = message.text ?? "no text found."
30 |
31 | vds.speak()
32 | }
33 |
34 | //MARK:- VDSSpeechSynthesizerDelegate
35 | func speechSynthesizerProgress(_ progress: Float, attributedText: NSMutableAttributedString) {
36 | message.attributedText = attributedText
37 | progressView.progress = progress
38 | }
39 |
40 | func speechSynthesizerDidStart() {
41 | print("speechSynthesizerDidStart")
42 | }
43 |
44 | func speechSynthesizerDidFinish() {
45 | print("speechSynthesizerDidFinish")
46 | }
47 |
48 |
49 | }
50 |
51 |
--------------------------------------------------------------------------------