Small Basic Curriculum: Lesson 2.4: Sound, Program, and Text Objects

Small Basic Curriculum: Lesson 2.4: Sound, Program, and Text Objects

Small Basic > Curriculum > Online > Lesson 2.4: Sound, Program, and Text Objects



Estimated time to complete this lesson: 1 hours

GSound, Program, and Text Objects

In this lesson, you will learn how to:

  • Use the Program object to manage how Small Basic programs run.
  • Enable playback of sounds by using the Sound object.
  • Perform text-related functions by using the Text object.

Introduction to the Program Object

When you create programs and work with various objects and operations that Small Basic offers, you can manipulate how your program runs if you use the Program object.

To better understand the Program object, let’s look at an example.

TextWindow.WriteLine("Program Directory: " + Program.Directory)
TextWindow.WriteLine("This program will run for 5 seconds")
Program.Delay(5000)
Program.End()

 

This is the output you will see:

Program object

In this example, you use the Delay operation to set a fixed time delay for the program to run, and you cause the program to stop running by using the End operation.

You can control how a program runs by using the Program object in Small Basic.

  1. First, you use the Directory operation to display the current directory of the program that is running.
    • To display the path of the directory, you use the TextWindow object with the WriteLine operation.
  2. Next, you delay the start of the program by five seconds by using the Delay operation and specifying the parameter for time in milliseconds.
  3. You use the Endoperation to force the program to stop running.

To verify the output of your program, click Run on the toolbar, or press F5 on the keyboard. The text window displays the program directory and then ends after five seconds.

Exploring the Program Object

By using the Program object, you can also retrieve information about the arguments that are passed to your program.

Let’s look at an example to understand how you can use the Program object to determine the count and value of arguments that are passed to a program as it runs.

If Program.ArgumentCount = 2 Then
TextWindow.WriteLine("First Number = " + Program.GetArgument(1) )
TextWindow.WriteLine("Second Number = " + Program.GetArgument(2) )
Multiplication=Program.GetArgument(1) * Program.GetArgument(2)
TextWindow.WriteLine("The product of the two number is: " + Multiplication)
Else
TextWindow.WriteLine("PLease enter two numbers to be multiplied.")
EndIf

 

This is the output you will see:

Information about the arguments

You can also use operations in a program to determine information about the arguments that are passed to that program. For example, you can use the ArgumentCount operation to find out how many command-line arguments are passed to the program. Similarly, you can use the GetArgument operation that returns the specified argument. You must provide the index value of the required argument as a parameter to the operation.

Introduction to the Sound Object

After we work with arguments and use the Program object, let’s explore some other interesting aspects of Small Basic.

You can work with sounds in your program by using the Sound object with operations such as Play, Pause, and Stop.

Did you know that you could include sounds in your Small Basic programs? In fact, you can choose from sample sounds provided in the Small Basic library.

GraphicsWindow.Show()
filepath = Program.Directory+"\Windows XP Startup.wav"
Sound.Play(filepath)
Program.Delay(2000)
Sound.Pause(filepath)
Program.Delay(1000)
Sound.Play(filepath)
Program.Delay(500)
Sound.Stop(filepath)

 

The output of this example plays, pauses, and stops the specified sound file at regular intervals.

You can play a sound in your program by using the Play operation of the Sound object. You specify the path of the local file or the URL of the file on the network as a parameter to the Play operation. Similarly, you use the Pause operation with the path of the file as the parameter. You stop playing the sound by using the Stop operation.

In addition, observe the use of the Delay operation of the Program object in the example. The Delay operation helps you delay the execution of commands.

You can use sound files in various formats (such as .mp3, .wav, or .wma) in your Small Basic programs.

Exploring the Sound Object

Let’s look at an example to understand how you can play specific sounds (such as a bell ring, a chime, or a click) by using certain operations of the Sound object in your Small Basic program.

GraphicsWindow.Show()
up:
Program.Delay(1000)
Sound.PlayBellRing()
Program.Delay(1000)
Sound.PlayChime()
Program.Delay(1000)
Sound.PlayClick()
Sound.PlayAndWait()
Goto up

 

In this example, the PlayAndWait operation plays an audio file and then waits until it finishes playing.

This program will play the sound of a bell ringing, a chime, and a click, at regular intervals.

In the previous example, you saw how to play a sound in your program by specifying the path of a sound file. The example here explains how to include built-in sounds such as a ringing bell, a chime, and a click.

After an interval of one second, the program runs the operation PlayBellRing and plays the bell sound. After another second, the PlayChime operation runs, and the chime sound plays. After one more second, the PlayClick operation runs and plays the click sound.

The PlayAndWait operation plays an audio file and waits until it finishes playing. If the particular file is paused, the operation resumes from the position where the audio playback was paused.

Introduction to the Text Object

Small Basic also offers useful operations that you can use to work with text.

For example, you may want to convert all names to uppercase letters, or you might want to search for specific information within some text.

You can use the Text object and its various functions and methods to perform operations on text in Small Basic.

For example, you can determine the length of a text string by using the GetLength operation.

Let’s learn about more operations of the Text object by reviewing an example...

TextWindow.Write("Enter you first name: ")
FirstName = TextWindow.Read()
TextWindow.Write("Enter your last name: ")
LastName = TextWindow.Read()
CompleteName = Text.Append(FirstName, "" + LastName)
NameInUpperCase = Text.ConvertToUpperCase(CompleteName)
TextWindow.WriteLine("Hello " + NameInUpperCase + "!")

 

This is the output you will see:

Text Object

When you work in Small Basic, you can perform operations on text by typing a statement that contains the Text object.

In this example:

  1. First, you ask for the user’s first name and last name by using the Write and Read operations of the TextWindow object.
  2. You then append the last name to the first name by using the Append operation of the Text object.
  3. Next, you convert the appended text into uppercase letters by using the ConvertToUpperCase operation of the Text object.
    • You must specify the name of the variable that is holding the assigned text as a parameter. Similarly, you can also use the ConvertToLowerCase operation to convert the text to lowercase letters.

To verify the output of your program, click Run on the toolbar, or press F5 on the keyboard.

More on the Text Object

Let’s look at another example to understand more operations of the Text object.

TextWindow.Write("Enter a valid e-mail address: ")
EmailID = TextWindow.Read()
m = Text.IsSubText(EmailID, ".")
n = Text.IsSubText(EmailID, "@")
If m = "True" And n = "True" Then
TextWindow.WriteLine("You have entered a valid e-mail address.")
Else
TextWindow.WriteLine("Please enter a valid e-mail address.")
EndIf

 

This is the output you will see:

More on the Text Object

In this example, you ask the user to specify an e-mail address. Then you use the IsSubText operation of the Text object to determine whether the address is valid.

You can use operations of the Text object to verify whether specific text or a character is a subset of the larger text. To demonstrate this technique, we use the Write and Read operations of the TextWindow object to obtain an e-mail address from the user. Then we use the IsSubText operation of the Text object to determine whether the user has specified a valid address.

Operations of the Text Object

Another operation of the Text object is the GetSubText operation. This operation takes three parameters¾the text from which you want to derive the subtext, the location from where you want to derive the subtext, and the length up to which you want to derive the subtext.

Here are some more uses of the Text object…

To determine whether given text starts with specified subtext, you can use the StartWith or EndWith operations.

Text.StartsWith(text1, "a")
Text.EndsWith(text1, "z")

 

To get the character code for a specific Unicode character, you can use the GetCharacterCode operation.

Text.GetCharacterCode("@")

To determine the index position of specific subtext, you can use the GetIndexOf operation.

Text.GetIndexOf(text1, "@")

Let’s Summarize…

Congratulations!

Now you know how to:

  • Manage how your program runs by using the Program object.
  • Include sounds by using the various operations of the Sound object.
  • Operate on text by using the Text object.

Show What You Know

Write a program that displays a text window and then performs the following steps:

  • Asks the user to type an answer to a simple question.
  • If the answer is correct, display an appropriate message along with the sound of a chime.
  • If the answer is incorrect, display an appropriate message along with the sound of a bell.

To see the answers to these questions, go to the Answer Key page.

Next Lesson


PowerPoint Downloads

  

Sort by: Published Date | Most Recent | Most Useful
Comments
  • Hi

    I'm looking around for more information on the Sound.PlayMusic operation.

    In particular the code and syntax for notes i.e cdefgab, how to raise and lower a semi tone, the length of time the note is played and what octive.

    This would be great if it was included in the cuuriculum.

    I think you are trying to keep the curriculum "small" and simple which is great and i think very important, but you could include links and references for students to goto for more detailed information.

    Thanks

  • Jibba Jabba, one option is to ask your specific question in the Forums. The reference documentation also doesn't include much on the topic: smallbasic.com/doc.aspx

  • Hi!

    I would like to ask you to check the "Exploring the program object", as the provided code does not produce the expected results. And I can`t figure out the missing part on my own.

Page 1 of 1 (3 items)