yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Mac Programming Lesson 5


4m read
·Nov 3, 2024

Hey guys, this is Mac Kids, no one with Mac programming lesson five.

In this lesson, I'm going to be showing you how to use a speech synthesizer, which is the way that you make a computer say stuff through the actual speakers using a physical human voice. So the class for this is just NS Speech Synthesizer, and it's pretty easy to use.

I'll show you how to do this by creating a new project in Xcode and making a simple app that uses it. So I'll create a new project. I'll make it a Cocoa application, and I'll call it "S". Now under classes, I'll create a new class, Cocoa Objective-C class, and I'll call it "AppController".

So in "AppController," this is where we're going to be typing all our code. First of all, in "AppController," we're going to declare a new NS Speech Synthesizer. I'll declare it right here, and I'll call it "SS" for speech synthesizer.

Now up here, I'm going to have two IB Outlets: one for an NS Text Field, and I'll just call it "textField1." I'll have one for an NS Button, and I'll call it "button1." So that's easy. Now I'll add one IB Action called "startSpeaking," and I'll do an ID sender here.

So I'll just copy this, paste it into the implementation, and I'll get right started typing the code. But before we do that, you have to understand one thing: a speech synthesizer is like an NS String in the way that it has to be allocated and initialized in order to actually work.

And what better time to do that than when the application first opens? The thing is that awake from nib, which we learned in the last lesson, really isn't the best way to initialize things and start up your application when you're doing things like setting properties on user interface controls.

Awake from nib is perfect, but when you're doing more things like allocating stuff and setting the delegate of stuff and stuff like that, you really want to use another way of running code when the application initializes, and this is ID init.

So ID init has to return an ID, of course. We'll return self, and you have to run super init at the beginning to initialize and run the original thing. So you can kind of ignore that for now. Right in this ID init, between the super init and return self, all we're going to do is "SS = NSSpeechSynthesizer alloc] initWithVoice:nil."

Of course, that nil is not a string. So the way it works is if you pass it nil as the voice, then it just makes the voice be the default computer voice that you can set through system preferences.

Now, I'm going to call speech synthesizer, or SS in this case, set delegate self. The reason we're doing this is basically a delegate means that now SS knows who to tell when something happens, in that case itself, or this current class.

The way it tells when something happens, it'll run functions on its delegate. So for instance, now if I add a little function right at the bottom of here, this code right here, believe it or not, will run when the speech synthesizer is done because this is a magic method that's declared in the delegate of any speech synthesizer.

So it'll call stuff on the delegate. So I'm setting the delegate itself; that means I know when it's done, and I know all this whole stuff, so just keep that in mind. We do need this code right here, and we're going to put stuff in here when it's done speaking.

So now under this IB Action, we're going to say button1 setEnabled:no, and we're going to start speaking. So we're going to say "SS startSpeaking:textField1.stringValue." In this way, we're going to start speaking the text of textField1, and textField1 is where they're going to type some text.

And button1, in this case, is going to be this speak button that we're going to disable while it's talking so that way you can't say two things at once. And then when it's done talking, we're going to reenable the button by doing button1 setEnabled:yes.

And by the way, if you don't really want to type this in right from the video, I will have a link in the description to download the Xcode project for this, so that way you can use it instead.

So anyway, here we go. Now all we need to do is the interface. So I'll go into Interface Builder by double-clicking on the MainMenu.xib, and I will drag on two necessary things: a button and a text field. So the text field is going to go here, and the button here.

Now resize this window. So now I'm going to grab a little blue NS object and drag it to my document. You can get by going to Window > Document. On this object, I'm going to set the class to be AppController.

And now I'm going to click control from AppController to this first text field and link it up. Same thing with the button, back again. And just to ensure that everything's working, we're going to call an NSLog here, and we're going to log textField1.stringValue so that way we know what we're speaking.

So now if I build and go into the console, we'll see the application appear. I'll type "foo," and my computer just said "f" in a very, I guess it doesn't really know how to pronounce it. And there we go.

So as you can see, the button disables, and it comes up in the console. So we can get rid of this NSLog right here, but it's great! It works. I'll have the source code downloadable in the description.

So thanks for watching Mac kids in one. You can try making this application; you can mess around with the code a little bit. You have full permissions to do whatever you want with this code.

So anyway, thanks for watching, mades in one. Post any comments or questions you have in the description of the video. Subscribe, and goodbye!

More Articles

View All
Q&A With Grey: Millenia of Human Attention Edition
Hello Internet! I asked for questions, and boy, did you deliver with more than 7,000, so, let’s get started. Question: Immortality! Aging is a degenerative fatal disease of the genes in the same way Parkinson’s is. We’re trying to cure Parkinson’s with g…
John Gotti Sr.'s Rise to Power | Narco Wars: The Mob
[music playing] - It’s snowing out, a little snowing, white Christmas. - I know it’s going to happen any day now. So the plan that they came up with was rather ingenious. They decided to take Paul Castellano out by luring him to one of his favorite resta…
Position vector valued functions | Multivariable Calculus | Khan Academy
Let’s say I have some curve C and it’s described; it can be parameterized. I can’t say that word as, let’s say, x is equal to X of t, y is equal to some function y of T, and let’s say that this is valid for T between A and B, so T is greater than or equal…
Interpreting expressions with multiple variables: Cylinder | Modeling | Algebra 2 | Khan Academy
We’re told that given the height h and volume v of a certain cylinder, Jill uses the formula ( r ) is equal to the square root of ( \frac{v}{\pi h} ) to compute its radius to be 20 meters. If a second cylinder has the same volume as the first but is 100 t…
Estimating decimal subtraction (thousandths) | Grade 5 (TX TEKS) | Khan Academy
In this video, we’re going to get some practice estimating the difference of numbers with decimals in them. So, for example, if I wanted you to estimate what 16.39 minus 5.84 is, what do you think this is approximately equal to? This little squiggly equal…
Guided visualization to calm your mind
Welcome and thanks for taking out the time for yourself for what will hopefully be a nice inward journey. So just start off sitting upright, feet planted on the ground, if you’re ideally on some type of a firm chair. Start to soften your gaze. If your ey…