Sound

LeJos allows you to play simple sounds.

The playTone() method requires a frequency and duration for each tone you want to play. The higher the frequency the higher the note. This program will play a series of 3 tones:

import lejos.nxt.*;
public class SoundTest{
   public static void main(String[] args) throws Exception{
      Sound.playTone(300,500); // frequency 300, duration 1/2 second
      Thread.sleep(500);        // wait half of a second while the sound plays

      Sound.playTone(400,500);  // frequency 400, duration 1/2 second
      Thread.sleep(1000);        // let the tone play half a second and wait half of a second

      Sound.playTone(500,1000);  // frequency 500, duration 1 second
      Thread.sleep(1000);        // wait a second
}}

Write a program to play this song:

       B   A   G,     B   A   G,     G G G G    A A A A,    B   A   G
Note Frequency Difference Type of step
C
262
   
D
287
25 whole
E
320
33 whole
F
349
29 half
G
392
43 whole
A
440
48 whole
B
494
54 whole
C ?   half
Notice the difference is greater as the frequency is higher

The whole note whole note lasts twice as long as a half note half note which is twice as long as a quarter note quarter note which (of course) is twice as long as an eighth note eighth note.

Questions:

  1. Can the robot play 2 tones at the same time? How can you prove or disprove it? Create a program that demonstrates the answer.
  2. What is the lowest or highest sound you can hear the robot play? Create a program that demonstrates your answer.

Sound files:

You have to use iCommand to upload and play prerecorded sound files. You can get .rso sound files that can be played by leJOS online.

You can also record sounds (Programs >Accessories > Entertainment > Sound recorder), save them as .wav files and convert them to rso using the Wav2Rso utility program from sourceforge