1
1. How to save sound sources Last time in the xylophone app, only playing music was possible. This time let’s add the ability that records and playbacks. We are also going to learn about a program skill called “Recursion”. First, drag the “Sound” and “Clock ” components from the “Designer” screen, and then come back to the “Blocks” screen. Playback means that computer receives and saves the input of each key of the music and then outputs the saved keys in order. Therefore, you need to secure a space for saving the sound file.
Bring the “initialize global name to” block in ‘Variables’ and change the variable ‘name’ to ‘note’. To fill the socket, bring the “make a list” block in ‘Lists’. “List” is used when you secure a saving space, and physically it saves sound source of each key, which is saved in the smartphone’s memory and later played.
Bring in the ‘add items to list’ block to the subroutine ‘PlayNote’ so that the played sound sources are saved in the space already secured. In the “list” and “item” sockets, connect the “get global note” and “Sound1.Source” blocks respectively. So far, we have dealt with a program that makes xylophone sounds every time we touched the keys and saves sound sources in the ‘note’ list. Next we will learn about a program that playbacks the saved sound sources in order when you touch the smart phone’s ‘Play’ button. Remember that the name of the “Play” button’s component is “Blue_Button”.
2
2. Playback the saved sound sources by using “Recursion” The variable that points to the order of the played sound sources is called ‘count’, and we will start with the first sound source.
In the beginning of the “Subroutine” body, you have to connect the “select list item” block to the socket of the “set Sound1.Source to” block. Next, playback the first key of the music. We will create conditions to playback throughout the entire saving space of ‘note’ while increasing the value of “count”.
In the “Control” component, choose “if then”. Increase the value of ‘count’ by 1 until it reaches the end of the ‘note’ list.
3
This kind of program that playbacks sound sources is a subroutine that begins as the ‘Play’ button of the smartphone is touched.
4
So, drag the block ‘to procedure do’ to set the name of the subroutine as ‘PlaybackNote’. In order to playback until the last sound source of the ‘note’ list, this subroutine needs to continuously call itself. Theses skill is named ‘recursion’. It is a similar situation as when you are standing in the middle of two mirrors.
We will also learn about a program related to the touch of the ‘Play’ button. You need to playback the sound files only if you played the music. This means that there should be at least one sound source in the ‘note’ list. We implemented this with ‘length of list’ block. Next, when you call in ‘PlaybackNote’ subroutine, you can playback the sound sources saved in the ‘note’ list.
5
3. Steps in solving the problem: Debugging Start the app by using the Emulator, and you will encounter a ridiculous result. First, the playing time between notes is too short that you cannot playback the music as it is. Second, even if you have saved several files, only the last one is done playback. So let’s start with our second problem. When you see our program, you will find new music piling up in the ‘note’ list. The value of ‘count’ is always pointing to the sound source of the first key in the last music. If you want to listen to the whole music again from the beginning, set the value of “count” to 1 by adding ‘set global count to’ block in the ‘Blue_Botton’ program. If you want to listen to only the last one, press ‘reset’ button, play a new one, and lastly touch the ‘Play’ button.
So set the ‘note’ list used earlier in the Reset(Red_Button) program. Because there are no argument in the socket of the “make a list” block, this list should be empty. In other words, the original sound sources are all gone. If you know what this means, you will use Reset only when it is needed.
In order to solve the first problem mentioned earlier, we will use the “Clock” component.
6
4. How to complete an app using the “Timer” event Make a saving space named “time”. The ‘Click1.Now’ method in the ‘to PlayNote’ subroutine provides the current time information in the ‘instant’ format.
Every time the key is touched, the time is saved in ‘time’ list. The ‘time’ list should be emptied when the ‘Reset’ button is touched by adding ‘set global time to’ block. This means that all the records for each time will disappear. When the ‘Play’ button is pressed, the “PlaybackNote” subroutine is called. Things like “When Blue_Button.Click” are named “Even hander”. Every time a key is touched, we will save it in the list and secure enough time for the next sound sources to playback by adding ‘set Clock1.TimeEnabled’ block in the ‘PlaybackNote’. Then, ‘PlaybackNote’ uses the ‘when Click1.Timer‘ event handler and creates an implicit ‘Recursion’ situation.
You can enable and disable the “Timer” event using ‘true’ and ‘false,’ which are arguments of “Click1.TimerEnabled”. 7
Check this out with the Emulator or your smartphone. You can ignore the “Runtime Error” message. You will find that the playing time between notes is still too short.
To solve this problem, you can set the interval between sound sources by adding the ‘call Clock1.Duration’ block in the ‘PlaybackNote’ to playback the music as it is. However, there is one thing to improve. If you press the ‘play’ button between music, you cannot hear anything by the same amount of time the next time you playback it.
8