◆ Lego Mindstoms NXT - leJOS - (11) トーン
■ 音を鳴らす
トーンを使って、音を鳴らすことができます。これには、周波数、持続
時間、音量を指定する playTone メソッドを使います。このメソッドは、
playSample メソッドと違い、pause メソッドを使って音を鳴らす時間
を指定しなければなりません。
□ Sound.setVolume(40); // (1)
音量を 40 に設定します。
□ Sound.playTone(1976, 400); Sound.pause(500); // (2)
周波数 1976、持続時間 400 ミリ秒でトーンを鳴らします。
ただし、このメソッドも、すぐにつぎの命令を実行してしまうので、
pause メソッドを使ってスリープ状態を作ってあげなければなりません。
多少、持続時間に余裕を持たせてあげた方がいいでしょう。
□ 周波数はつぎの表によります。
ただし、この表は NXC のマニュアルから持ってきたもので、leJOS の
API には書かれていませんので、この値でいいかどうかはわかりません。
(★)
■ 実際の音楽っぽく
つぎは、ある曲を実装したものです。
楽譜を基に実装したのだが、まったく違うものらしい。
わたしの楽譜の読み方が違うのか、周波数のトーンが間違っているのか
?
(中学の音楽は 1 がついたこともあったくらいなので...)
まずは、音階関係を定数にしておいたほうが使いやすいので、これらを
ユーティリティクラスとして作っておきます。
それでは、これを基に音を鳴らしてみましょう。
以上
■ 音を鳴らす
トーンを使って、音を鳴らすことができます。これには、周波数、持続
時間、音量を指定する playTone メソッドを使います。このメソッドは、
playSample メソッドと違い、pause メソッドを使って音を鳴らす時間
を指定しなければなりません。
------------------------------------------------------ import lejos.nxt.Sound; /** * PlayingMusic01.nxc * 「トライボット」使用 * 音を鳴らす。 * @author maruno * @version 1.0, 2008-01-12 * @since 1.0 */ public class PlayingMusic01 { public static void main(String[] args) { Sound.setVolume(40); // (1) // 音階6 を B から C まで Sound.playTone(1976, 400); Sound.pause(500); // (2) Sound.playTone(1865, 400); Sound.pause(500); Sound.playTone(1760, 400); Sound.pause(500); Sound.playTone(1661, 400); Sound.pause(500); Sound.playTone(1568, 400); Sound.pause(500); Sound.playTone(1480, 400); Sound.pause(500); Sound.playTone(1397, 400); Sound.pause(500); Sound.playTone(1319, 400); Sound.pause(500); Sound.playTone(1245, 400); Sound.pause(500); Sound.playTone(1175, 400); Sound.pause(500); Sound.playTone(1109, 400); Sound.pause(500); Sound.playTone(1047, 400); Sound.pause(500); } } ------------------------------------------------------
□ Sound.setVolume(40); // (1)
音量を 40 に設定します。
□ Sound.playTone(1976, 400); Sound.pause(500); // (2)
周波数 1976、持続時間 400 ミリ秒でトーンを鳴らします。
ただし、このメソッドも、すぐにつぎの命令を実行してしまうので、
pause メソッドを使ってスリープ状態を作ってあげなければなりません。
多少、持続時間に余裕を持たせてあげた方がいいでしょう。
□ 周波数はつぎの表によります。
ただし、この表は NXC のマニュアルから持ってきたもので、leJOS の
API には書かれていませんので、この値でいいかどうかはわかりません。
(★)
--- Sound 3 4 5 6 7 8 9 B 247 494 988 1976 3951 7902 A# 233 466 932 1865 3729 7458 A 220 440 880 1760 3520 7040 14080 G# 415 831 1661 3322 6644 13288 G 392 784 1568 3136 6272 12544 F# 370 740 1480 2960 5920 11840 F 349 698 1397 2794 5588 11176 E 330 659 1319 2637 5274 10548 D# 311 622 1245 2489 4978 9956 D 294 587 1175 2349 4699 9398 C# 277 554 1109 2217 4435 8870 C 262 523 1047 2093 4186 8372 ---
■ 実際の音楽っぽく
つぎは、ある曲を実装したものです。
楽譜を基に実装したのだが、まったく違うものらしい。
わたしの楽譜の読み方が違うのか、周波数のトーンが間違っているのか
?
(中学の音楽は 1 がついたこともあったくらいなので...)
まずは、音階関係を定数にしておいたほうが使いやすいので、これらを
ユーティリティクラスとして作っておきます。
------------------------------------------------------ package u; import lejos.nxt.Sound; /** * 音関係のユーティリティのクラス * @author marunomaruno * @version 1.0, 2008/06/29 * @since 1.0 */ public class SoundUtil { /* * 周波数 */ public static int FR_B3 = 247; public static int FR_B4 = 494; public static int FR_B5 = 988; public static int FR_B6 = 1976; public static int FR_B7 = 3951; public static int FR_B8 = 7902; public static int FR_AS3 = 233; public static int FR_AS4 = 466; public static int FR_AS5 = 932; public static int FR_AS6 = 1865; public static int FR_AS7 = 3729; public static int FR_AS8 = 7458; public static int FR_A3 = 220; public static int FR_A4 = 440; public static int FR_A5 = 880; public static int FR_A6 = 1760; public static int FR_A7 = 3520; public static int FR_A8 = 7040; public static int FR_A9 = 14080; public static int FR_GS4 = 415; public static int FR_GS5 = 831; public static int FR_GS6 = 1661; public static int FR_GS7 = 3322; public static int FR_GS8 = 6644; public static int FR_GS9 = 13288; public static int FR_G4 = 392; public static int FR_G5 = 784; public static int FR_G6 = 1568; public static int FR_G7 = 3136; public static int FR_G8 = 6272; public static int FR_G9 = 12544; public static int FR_FS4 = 370; public static int FR_FS5 = 740; public static int FR_FS6 = 1480; public static int FR_FS7 = 2960; public static int FR_FS8 = 5920; public static int FR_FS9 = 11840; public static int FR_F4 = 349; public static int FR_F5 = 698; public static int FR_F6 = 1397; public static int FR_F7 = 2794; public static int FR_F8 = 5588; public static int FR_F9 = 11176; public static int FR_E4 = 330; public static int FR_E5 = 659; public static int FR_E6 = 1319; public static int FR_E7 = 2637; public static int FR_E8 = 5274; public static int FR_E9 = 10548; public static int FR_DS4 = 311; public static int FR_DS5 = 622; public static int FR_DS6 = 1245; public static int FR_DS7 = 2489; public static int FR_DS8 = 4978; public static int FR_DS9 = 9956; public static int FR_D4 = 294; public static int FR_D5 = 587; public static int FR_D6 = 1175; public static int FR_D7 = 2349; public static int FR_D8 = 4699; public static int FR_D9 = 9398; public static int FR_CS4 = 277; public static int FR_CS5 = 554; public static int FR_CS6 = 1109; public static int FR_CS7 = 2217; public static int FR_CS8 = 4435; public static int FR_CS9 = 8870; public static int FR_C4 = 262; public static int FR_C5 = 523; public static int FR_C6 = 1047; public static int FR_C7 = 2093; public static int FR_C8 = 4186; public static int FR_C9 = 8372; /** * 全音符 */ public static int NOTE_SEMIBREVE = 1200; /** * 二分音符 */ public static int NOTE_MINIM = (NOTE_SEMIBREVE / 2); /** * 四分音符 */ public static int NOTE_CROTCHET = (NOTE_MINIM / 2); /** * 八分音符 */ public static int NOTE_QUAVER = (NOTE_CROTCHET / 2); /** * 指定周波数、持続時間でトーンを鳴らします。このとき、指定された間隔時間を鳴らした後に取ります。 * @param frequency 周波数 * @param duration 持続時間 * @param interval 間隔時間 */ public static void playTone(int frequency, int duration, int interval) { Sound.playTone(frequency, duration); Sound.pause(duration + interval); } /** * 指定周波数、持続時間、音量でトーンを鳴らします。このとき、指定された間隔時間を鳴らした後に取ります。 * @param frequency 周波数 * @param duration 持続時間 * @param volume 音量 * @param interval 間隔時間 */ public static void playTone(int frequency, int duration, int volume, int interval) { Sound.playTone(frequency, duration, volume); Sound.pause(duration + interval); } } ------------------------------------------------------
それでは、これを基に音を鳴らしてみましょう。
------------------------------------------------------ import lejos.nxt.Sound; import u.SoundUtil; /** * 「トライボット」使用 * 音を鳴らす。 * @author maruno * @version 1.0, 2008-06-29 * @since 1.0 */ public class PlayingMusic03 { public static void main(String[] args) { final int INTERVAL = 80; Sound.setVolume(40); SoundUtil.playTone(SoundUtil.FR_C6, SoundUtil.NOTE_MINIM, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C5, SoundUtil.NOTE_QUAVER, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C4, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C5, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_AS6, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_AS6, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_AS6, SoundUtil.NOTE_CROTCHET, INTERVAL + SoundUtil.NOTE_CROTCHET); SoundUtil.playTone(SoundUtil.FR_DS5, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_DS5, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_DS5, SoundUtil.NOTE_CROTCHET, INTERVAL + SoundUtil.NOTE_CROTCHET); SoundUtil.playTone(SoundUtil.FR_G6, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_G8, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_G8, SoundUtil.NOTE_CROTCHET, INTERVAL + SoundUtil.NOTE_CROTCHET); SoundUtil.playTone(SoundUtil.FR_C6, SoundUtil.NOTE_MINIM, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C5, SoundUtil.NOTE_QUAVER, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C4, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C5, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_AS6, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_AS6, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_AS6, SoundUtil.NOTE_CROTCHET, INTERVAL + SoundUtil.NOTE_CROTCHET); SoundUtil.playTone(SoundUtil.FR_DS5, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_DS5, SoundUtil.NOTE_CROTCHET, INTERVAL); SoundUtil.playTone(SoundUtil.FR_DS6, SoundUtil.NOTE_MINIM, INTERVAL); SoundUtil.playTone(SoundUtil.FR_DS5, SoundUtil.NOTE_QUAVER, INTERVAL); SoundUtil.playTone(SoundUtil.FR_C4, SoundUtil.NOTE_SEMIBREVE, INTERVAL); } } ------------------------------------------------------
以上
※コメント投稿者のブログIDはブログ作成者のみに通知されます