Программирование LEGO NXT роботов на языке NXC - Создаём музыку

Материал из roboforum.ru Wiki
Версия от 16:24, 18 мая 2009; =DeaD= (обсуждение | вклад) (Проигрываем музыку)
Перейти к: навигация, поиск

Автор: Daniele Benedettelli

Перевод: © Ботов Антон aka =DeaD=, 2009

Эксклюзивно для www.roboforum.ru
копирование на другие ресурсы и публикация перевода
без разрешения его автора запрещены

Создаём музыку

The NXT has a built-in speaker that can play tones and even sound files. This is in particular useful when you want to make the NXT tell you that something is happening. But it can also be funny to have the robot make music or talk while it runs around.

Проигрываем звуковые файлы

BricxCC has a built-in utility to convert .wav files into .rso files accessible via menu Tools  Sound conversion. Then you can store .rso sound files on NXT flash memory using another utility, the NXT memory browser (Tools  NXT explorer) and play them with the command

PlayFileEx(filename, volume, loop?)

Its arguments are sound filename, volume (a number from 0 to 4), and loop: this last argument is set to 1 (TRUE) if you want the file to be looped or 0 (FALSE) if you want to play it only once.

#define TIME 200
#define MAXVOL 7
#define MINVOL 1
#define MIDVOL 3
#define pause_4th Wait(TIME)
#define pause_8th Wait(TIME/2)
#define note_4th \
PlayFileEx("! Click.rso",MIDVOL,FALSE); pause_4th
#define note_8th \
PlayFileEx("! Click.rso",MAXVOL,FALSE); pause_8th
task main()
{
  PlayFileEx("! Startup.rso",MINVOL,FALSE);
  Wait(2000);
  note_4th;
  note_8th;
  note_8th;
  note_4th;
  note_4th;
  pause_4th;
  note_4th;
  note_4th;
  Wait(100);
}

This nice program first plays the startup tune you might know already; then uses the other standard click sound to play “Shave and a haircut” jingle that made Roger Rabbit go crazy! The macros are really useful in this case to simplify notation in the main task: try modifying the volume settings to add accents in the tune.

Играем музыку

To play a tone, you can use the command PlayToneEx(frequency, duration, volume, loop?) It has four arguments. The first is the frequency in Hertz, the second the duration (in 1/1000 of a second, like in the wait command), and the last are volume a loop as before. PlayTone(frequency, duration) can also be used; in this case the volume is the one set by NXT menu, and loop is disabled.

Here is a table of useful frequencies:

As with the case of PlayFileEx, the NXT does not wait for the note to finish. So if you use multiple tones in a row then you had better add (slightly longer) wait commands in between. Here is an example:

#define VOL 3
task main()
{
PlayToneEx(262,400,VOL,FALSE); Wait(500);
PlayToneEx(294,400,VOL,FALSE); Wait(500);
PlayToneEx(330,400,VOL,FALSE); Wait(500);
PlayToneEx(294,400,VOL,FALSE); Wait(500);
PlayToneEx(262,1600,VOL,FALSE); Wait(2000);
}

You can create pieces of music very easily using the Brick Piano that is part of the BricxCC.

If you want to have the NXT play music while driving around, better use a separate task for it. Here you have an example of a rather stupid program where the NXT drives back and forth, constantly making music.

task music()
{
  while (true)
  {
    PlayTone(262,400); Wait(500);
    PlayTone(294,400); Wait(500);
    PlayTone(330,400); Wait(500);
    PlayTone(294,400); Wait(500);
  }
}

task movement()
{
  while(true)
  {
    OnFwd(OUT_AC, 75); Wait(3000);
    OnRev(OUT_AC, 75); Wait(3000);
  }
}

task main()
{
  Precedes(music, movement);
}

Подводим итоги

In this chapter you learned how to let the NXT play sounds and music. Also you saw how to use a separate task for music.