text
stringlengths
0
897
```
**Challenge:** modify the above program to use a digitized horn sound (`/sys/sounds/hornC4.wav`). You will need to remove the `snd.init` call, and change the `snd.play` line to calculate the correct pitch for each note as shown above. Finally, the `wait` command will need changing to use `n[0]` as the wait time.
It's worth noting that computers and music have a long and beautiful history together. In recent years the practice of "live coding" has become an increasingly popular form of performance art, where a composer/programmer/performer writes code to create music in real-time, in front of an audience. Who knows, perhaps y...
## Exploring the Demos
{i:"`/sys/demo`,`drumMachine`"}
The poster child for sound and music is certainly `drumMachine.ms` (in the `/sys/demo` folder). It uses a display type (tile) that you haven't learned about yet, but you should be able to understand how it is loading and playing sounds. It's also a fun program to play with. Feel free to load a different set...
{i:"`sounds` module;Mini Micro, `sounds` module;`/sys/lib`,`listUtil`"}
Most of the other game demos use sounds of either sort, but for synthesized sounds, the best place to look might actually be the import module `/sys/lib/sounds.ms`. This defines a handful of generally useful sounds. To use these in your own program, you can `import "sounds"` and then simply call (for example) `sounds...
A> **Chapter Review**
A> - You learned how to load and play digitized sounds from WAV and OGG files.
A> - You synthesized sounds from scratch, using nothing but code and ingenuity.
A> - You created music with the help of the `noteFreq` function.
{gap:50}
{width:"28%"}
![](chinchilla-06-note.svg)
{chapterHead: "Day 21: Fireworks Demo", startingPageNum:249}
{width: "50%"}
![](Chapter21.svg)
Q> After a long time of practicing, our work will become natural, skillful, swift, and steady.
Q>— Bruce Lee (martial artist, actor, instructor, and philosopher)
A> **Chapter Objectives**
A> - Rest and recouperate.
A> - Enter and play with a fun Mini Micro demo that includes display layers, sound, and mouse input.
A> - Review & synthesize what you've learned so far.
Congratulations! You have come a long way since Day 1. You've learned all about the MiniScript language, and how to use it in three different environments: the Try-It! page, command-line MiniScript, and Mini Micro. You've also learned many of the custom classes and functions available in MiniScript to work with the...
It's time to take a break, and instead of trying to pile on any new knowledge, let your existing knowledge settle a bit. You deserve a rest! In fact, your achievements so far are worthy of celebration! And what better way to celebrate than... fireworks?
![Fireworks!](Fireworks.png)
This program will generate pretty, physics-based fireworks, launched from wherever you click the mouse, complete with sounds on launch and burst. To create all the visual effects, we will use several display layers:
- Display 7 will be the background, but we'll switch it to pixel mode so we can draw a nice deep gradient.
- Display 5, the standard `gfx` layer, is where most of the drawing will take place.
- Display 2 will be used to draw a brief "flash" (white circle) when a rocket bursts.
We will leave the standard `text` layer (display 3) alone, so you can see your typing when the program is not running. The graphics displays will be set to a scale of 3, making the pixels three times bigger than normal, so that the firework particles are easier to see.
Also, to help us with some list operations like removing an element by value, or selecting a random value, we will import the *listUtil* module. So, as you might expect, our program begins with the `import` statement, followed by setting up the displays. Use `reset` (or freshly launch Mini Micro) to clear any program...
{caption:"Listing 1 (Fireworks set-up code)."}
```miniscript
// set up
import "listUtil"
clear
gfx.clear color.clear, 960/3, 640/3
gfx.scale = 3
display(2).mode = displayMode.pixel
flashGfx = display(2)
flashGfx.clear color.clear, gfx.width, gfx.height
flashGfx.scale = gfx.scale
flashGfx.color = color.white
display(7).mode = displayMode.pixel
bkgnd = display(7)
bkgnd.clear color.black, gfx.width, gfx.height
bkgnd.scale = gfx.scale
for y in range(0, bkgnd.height-1)
c = color.lerp("#550055", "#000022", y/bkgnd.height)
bkgnd.line 0, y, bkgnd.width, y, c
end for
```
Save this program as `fireworks.ms`. Then run, and you should see a purple gradient representing the evening sky. (This also makes a nice background; someday you may want to put something like this into `/usr/startup.ms` to make it your standard work background!)
Here's the next portion of the fireworks program:
{caption: "Listing 2 (more Fireworks set-up).", number-from: 22}
```miniscript
burstColors = [color.yellow, color.red, color.blue,
color.aqua, color.pink, color.lime, color.fuchsia]
launchSnd = new Sound
launchSnd.init 0.3, 5, [0,1,0], Sound.noiseWave
burstSnd = new Sound
burstSnd.init 1, 2, [1,0.1,0.01,0], Sound.noiseWave
panForX = function(x)
return 2 * (x / gfx.width - 0.5)
end function
for x in range(0, gfx.width, 100)
launchSnd.play