OSDN Git Service

bb832f0d51fcf0ee02e995aaa7d9f42ceddf9d86
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / lejos / nxt / Sound.java
1 package lejos.nxt;
2
3 import java.io.*;
4
5 /**
6  * NXT sound routines.
7  *
8  */
9 public class Sound
10 {
11   private Sound()
12   {
13   }
14
15   /**
16    * Play a system sound - not yet implemented.
17    * <TABLE BORDER=1>
18    * <TR><TH>aCode</TH><TH>Resulting Sound</TH></TR>
19    * <TR><TD>0</TD><TD>short beep</TD></TR>
20    * <TR><TD>1</TD><TD>double beep</TD></TR>
21    * <TR><TD>2</TD><TD>descending arpeggio</TD></TR>
22    * <TR><TD>3</TD><TD>ascending  arpeggio</TD></TR>
23    * <TR><TD>4</TD><TD>long, low beep</TD></TR>
24    * <TR><TD>5</TD><TD>quick ascending arpeggio</TD></TR>
25    * </TABLE>
26    */
27    
28   public static int C2 = 1056;
29   
30   public static void systemSound (boolean aQueued, int aCode)
31   {
32         if(aCode==0)
33         {
34                 playTone(1200,200);
35         }
36         else if(aCode == 1)
37         {
38                 playTone(1200,150);
39                 pause(200);
40                 playTone(1200,150);
41                 pause(150);
42         }
43         else if(aCode == 2)
44         {// C major arpeggio
45                 for(int i = 4; i<8; i++)
46                 {
47                         playTone(C2*i/4,100);
48                         pause(100);
49                 }
50         }
51         else if(aCode == 3)
52         {
53                 for(int i = 7; i>3; i--)
54                 {
55                         playTone(C2*i/4,100);
56                         pause(100);
57                 }
58         }
59         else if(aCode == 4 )
60         { 
61                 playTone(100,500);
62                 pause(500);
63         }
64   }
65
66   /**
67    * Beeps once - not yet implemented.
68    */
69   public static void beep()
70   {
71     systemSound (true, 0);
72   }
73
74   /**
75    * Beeps twice - not yet implemented.
76    */
77   public static void twoBeeps()
78   {
79     systemSound (true, 1);
80   }
81
82   /**
83    * Downward tones.
84    */
85   public static void beepSequence()
86   {
87     systemSound (true, 3);
88   }
89
90   /**
91    * Downward tones.
92    */
93  public static void beepSequenceUp()
94  {
95         systemSound (true,2);
96  }
97
98   /**
99    * Low buzz 
100    */
101   public static void buzz()
102   {
103     systemSound (true, 4);
104   }
105   
106   public static void pause(int t)
107         {
108                 try { Thread.sleep(t); }
109                 catch(InterruptedException e){}
110         }
111         
112   /**
113    * Plays a tone, given its frequency and duration. Frequency is audible from about 31 to 2100 Hertz. The
114    * duration argument is in hundreds of a seconds (centiseconds, not milliseconds) and is truncated
115    * at 256, so the maximum duration of a tone is 2.56 seconds.
116    * @param aFrequency The frequency of the tone in Hertz (Hz).
117    * @param aDuration The duration of the tone, in centiseconds. Value is truncated at 256 centiseconds.
118    */
119   public static native void playTone (int aFrequency, int aDuration);
120   
121   /**
122    * Internal method used to play sound sample from a file
123    * @param page the start page of the file
124    * @param len the length of the file
125    * @param freq the frequency
126    * @param vol the volume
127    */
128   public static native void playSample(int page, int len, int freq, int vol);
129   
130   /**
131    * 
132    * @param file the 8-bit PWM (WAV) sample file
133    * @param freq the average frequency to play the sample at
134    * @param vol the sound volume
135    */
136   public static void playSample(File file, int freq, int vol) {
137           playSample(file.getPage(), file.length(), freq, vol);
138   }
139   
140 }