Objective:  Explore classes and instances with a fun activity.

Goal:  Students will be challenged to use the String class to construct a custom string given input from the user.   When students are happy with the resulting string, the lesson can be extended to introduce voice synthesis on the iPad and have the string spoken (sung) back to the student.

Introduction Activity:  Listen to the “Name Game” on youtube:

https://www.youtube.com/watch?v=-7NEYSKRJzA

Using partners and small groups, have student practice singing the song using different partners names.  Bring the whole class together and talk about a possible algorithm to customize the song for each name.  Try to avoid code at this point.  Encourage big-picture discussion like: “the last part of the name stays the same in many of the rhyming words”.  There are filler phrases that are the same for each version of the song like: “banana-fana-fo” and “fee-fi-fo”.  Avoid detailed algorithms at this point.  Just get the students to the point where they are excited to generate the details of the algorithm and the code themselves.

Lesson Activity:

Part 1:  Getting input from the user

A.  Have students explore the Answers template – first page

Run the code and interact with the application.  What does this code do?  (Encourage the use of words “input” from user and “output” to screen)

What variable stores the name of the user?

Part 2:  Constructing the Lyrics

Students should work in small teams to construct an algorithm that creates a single string with the lyrics of the song using the name provided as input by the user.  Have the students work backwards from the original goal to compose this string out of parts.  After a few minutes of discussion, suggest students compose the lyrics out of individual lines.  Maybe 3 or 4 lines concatenated together.  Then the students should work on each line individually, identifying the parts of the line that are always the same and the parts of the song that are different (or depend on the input name).

How many different rhymes for the name are there?  Billy?  Filly?  Milly?  others?

How will you construct each of the different rhymes?  (Have the student come to agreement that only the first character of the input name needs to be replaced to construct each rhyming word)

Explore the Swift String class to find methods (functions on String instances) that:

a.  identify the first character of a String instance (e.g., var firstCharacter = name.first )

b.  create a string that contains all the characters of the original name, except the first. (e.g., var allButFirst = String(name.dropFirst()). )

c.  create all the words needed that rhyme with the original name.  e.g. if name = “Billy”, mName = “Milly” below :

let mCharacter:Character = "M"
var mName = allButFirst
mName.insert(mCharacter, at: allButFirst.startIndex)

Use String interpolation or concatenation to construct each line of the song.  Print (‘show()’ command) each line to debug your results.

Once each line is correct, us concatenation (‘+’ operator) on all the lines to create one String instance that has all the lyrics to the song.  Print (‘show()’ command) the lyric variable to debug your results.

Part 3:  Sing Siri, Sing!

A.  Listen to the first 6 minutes of this NPR feature story “How do you construct a voice“.  (Note: not the TED talk, but the radio program (audio only by Rupal Patel)

If you want, stop a few minutes into the story and lead a discussion asking students “How would you solve the problem of people born without the ability to speak getting a voice of their own?”  Then, listen to how this researcher solved the problem in the rest of the 6 minute section of the story.

If you want, lead a discussion about the responsibility of computer scientists to solve problems that humans have.  Ask what problems have been solved already?  What problems might computer scientists be able to solve in your lifetime?

B.  Add some code to your playground to 1) create an instance of a SpeechSynthesizer, 2) set the voice properties of the synthesizer, 3) set the volume, rate and voice dialect for the utterance that will speak (sing) the lyrics you created for the Name Game.  (See solution below to create and initialize the SpeechSynthesizer and the Utterance the synthesizer will speak.)

let synthesizer = AVSpeechSynthesizer() 
// let voice = AVSpeechSynthesisVoice(language: "ja_JP") 
// let voice = AVSpeechSynthesisVoice(language: "en-US") 
let voice = AVSpeechSynthesisVoice(language: "en-GB") 
//  let voice = AVSpeechSynthesisVoice(language: "ru-RU") 
// let voice = AVSpeechSynthesisVoice(language: "es-ES") 
// let voice = AVSpeechSynthesisVoice(language: "ru-RU") 
// let voice = AVSpeechSynthesisVoice(language: "en-AU") 
// let voice = AVSpeechSynthesisVoice(language: "en-IE") 
let utterance = AVSpeechUtterance(string: wholeSong) 
// utterance.rate = 1.0 utterance.volume = 1.0 
utterance.voice = voice 
synthesizer.speak(utterance)

C.  Students should experiment with different rates, voices (Great Britain, US, Russian, Australian, etc.  See commented voice code above)

D.  Students should create a new string variable containing their impression of what they learned this day and any questions they have on the material they worked on.  They should have the synthesizer speak their feedback to the class and/or to the instructor.

Summary Activity:

A.  Ask the students what methods they used on the instance of String (name) to help them create the lines of their lyrics?

B.  Ask the students how they found these methods and descriptions of what they do?  (documentation?  trial and error using the shortcut menu suggestions)

C.  Have the students look up three other interesting methods that can be called on a String instance that they didn’t use today and describe their name and behavior to the class.

One Solution:

//#-code-completion(identifier, hide, setupLiveView())

//#-hidden-code

import Foundation

import UIKit

import AVFoundation

import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

//#-end-hidden-code

//#-editable-code Tap to enter code

//: Playground - noun: a place where people can play

show("What is your name?")

let name = ask("Name")

var firstCharacter = name.first

var allButFirst = String(name.dropFirst())

// var nameWithAnM = name.first! + allButFirst

let mCharacter:Character = "M"

let bCharacter:Character = "B"

let fCharacter:Character = "F"

var mName = allButFirst

var fName = allButFirst

var bName = allButFirst

mName.insert(mCharacter, at: allButFirst.startIndex)

//print(mName)

fName.insert(fCharacter, at: allButFirst.startIndex)

//print(fName)

bName.insert(bCharacter, at: allButFirst.startIndex)

//print(bName)

var line1 = "\(name) \(name) bo-b\(allButFirst),"

var line2 = "Bonana-fanna fo-f\(allButFirst),"

var line3 = "Fee fy mo-m\(allButFirst),"

var line4 = "\(name)!"

let wholeSong = line1 + line2 + line3 + line4

show(wholeSong)

let synthesizer = AVSpeechSynthesizer()

// let voice = AVSpeechSynthesisVoice(language: "ja_JP")

// let voice = AVSpeechSynthesisVoice(language: "en-US")

 let voice = AVSpeechSynthesisVoice(language: "en-GB")

//  let voice = AVSpeechSynthesisVoice(language: "ru-RU")

// let voice = AVSpeechSynthesisVoice(language: "es-ES")

// let voice = AVSpeechSynthesisVoice(language: "ru-RU")

// let voice = AVSpeechSynthesisVoice(language: "en-AU")

// let voice = AVSpeechSynthesisVoice(language: "en-IE")

let utterance = AVSpeechUtterance(string: wholeSong)

// utterance.rate = 1.0

utterance.volume = 1.0

utterance.voice = voice

synthesizer.speak(utterance)

// print(AVSpeechSynthesisVoice.speechVoices())

//#-end-editable-code

Concluding Activity: Have the student try out different voice dialects (e.g., British, Australian, German, French, etc) from the list below.

Swift Playgrounds Voice Synthesizer Dialects