Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. import javax.sound.sampled.AudioFormat;
  4. import javax.sound.sampled.AudioSystem;
  5. import javax.sound.sampled.LineUnavailableException;
  6. import javax.sound.sampled.SourceDataLine;
  7.  
  8. public class Main
  9. {
  10. private static void playStuff()
  11. {
  12. melody.play(NOTE_A3);
  13. harmony.play(NOTE_CS4);
  14. delay(140);
  15.  
  16. melody.stop();
  17. harmony.stop();
  18. delay(10);
  19.  
  20. melody.play(NOTE_A3);
  21. harmony.play(NOTE_CS4);
  22. delay(290);
  23.  
  24. melody.stop();
  25. harmony.stop();
  26. delay(10);
  27.  
  28. melody.play(NOTE_A3);
  29. harmony.play(NOTE_CS4);
  30. delay(290);
  31.  
  32. melody.stop();
  33. harmony.stop();
  34. delay(10);
  35.  
  36. melody.play(NOTE_A3);
  37. harmony.play(NOTE_CS4);
  38. delay(290);
  39.  
  40. melody.stop();
  41. harmony.stop();
  42. delay(10);
  43.  
  44. melody.play(NOTE_A3);
  45. harmony.play(NOTE_CS4);
  46. delay(140);
  47.  
  48. melody.stop();
  49. harmony.stop();
  50. delay(10);
  51.  
  52. melody.play(NOTE_A3);
  53. harmony.play(NOTE_CS4);
  54. delay(290);
  55.  
  56. melody.stop();
  57. harmony.stop();
  58. delay(10);
  59.  
  60. melody.play(NOTE_GS3);
  61. harmony.play(NOTE_B3);
  62. delay(290);
  63.  
  64. melody.stop();
  65. harmony.stop();
  66. delay(10);
  67.  
  68. melody.play(NOTE_CS4);
  69. harmony.play(NOTE_E4);
  70. delay(290);
  71.  
  72. harmony.stop();
  73. delay(10);
  74.  
  75. harmony.play(NOTE_FS4);
  76. delay(140);
  77.  
  78. harmony.stop();
  79. delay(10);
  80.  
  81. harmony.play(NOTE_E4);
  82. delay(140);
  83.  
  84. harmony.stop();
  85. delay(10);
  86.  
  87. harmony.play(NOTE_CS4);
  88. delay(140);
  89.  
  90. harmony.stop();
  91. delay(10);
  92.  
  93. harmony.play(NOTE_B3);
  94. delay(140);
  95.  
  96. harmony.stop();
  97. delay(10);
  98.  
  99. harmony.play(NOTE_CS4);
  100. delay(140);
  101.  
  102. harmony.stop();
  103. delay(10);
  104.  
  105. harmony.play(NOTE_FS4);
  106. delay(290);
  107.  
  108. harmony.stop();
  109. delay(10);
  110.  
  111. harmony.play(NOTE_E4);
  112. delay(140);
  113.  
  114. harmony.stop();
  115. delay(10);
  116.  
  117. harmony.play(NOTE_B3);
  118. delay(140);
  119.  
  120. harmony.stop();
  121. delay(10);
  122.  
  123. harmony.play(NOTE_CS4);
  124. delay(140);
  125.  
  126. harmony.stop();
  127. delay(10);
  128.  
  129. harmony.play(NOTE_FS4);
  130. delay(140);
  131.  
  132. harmony.stop();
  133. delay(10);
  134.  
  135. harmony.play(NOTE_E4);
  136. delay(140);
  137.  
  138. harmony.stop();
  139. delay(10);
  140.  
  141. harmony.play(NOTE_CS4);
  142. delay(140);
  143.  
  144. harmony.stop();
  145. delay(10);
  146.  
  147. harmony.play(NOTE_B3);
  148. delay(140);
  149.  
  150. harmony.stop();
  151. delay(10);
  152.  
  153. harmony.play(NOTE_CS4);
  154. delay(140);
  155.  
  156. harmony.stop();
  157. delay(10);
  158.  
  159. harmony.play(NOTE_FS4);
  160. delay(140);
  161.  
  162. harmony.stop();
  163. delay(10);
  164.  
  165. harmony.play(NOTE_E4);
  166. delay(290);
  167.  
  168. harmony.stop();
  169. delay(10);
  170.  
  171. melody.stop();
  172. harmony.stop();
  173. }
  174.  
  175. public static Music melody;
  176. public static Music harmony;
  177.  
  178. public static void main(String[] args) throws LineUnavailableException
  179. {
  180. System.out.println("Compiling...");
  181. melody = new Music(0);
  182. harmony = new Music(1);
  183. playStuff();
  184.  
  185. System.out.println("Playing...");
  186. melody.close();
  187. harmony.close();
  188. }
  189.  
  190. public static void delay(int ms)
  191. {
  192. harmony.delay(ms);
  193. melody.delay(ms);
  194. }
  195.  
  196. static int NOTE_B0 = 31;
  197. static int NOTE_C1 = 33;
  198. static int NOTE_CS1 = 35;
  199. static int NOTE_D1 = 37;
  200. static int NOTE_DS1 = 39;
  201. static int NOTE_E1 = 41;
  202. static int NOTE_F1 = 44;
  203. static int NOTE_FS1 = 46;
  204. static int NOTE_G1 = 49;
  205. static int NOTE_GS1 = 52;
  206. static int NOTE_A1 = 55;
  207. static int NOTE_AS1 = 58;
  208. static int NOTE_B1 = 62;
  209. static int NOTE_C2 = 65;
  210. static int NOTE_CS2 = 69;
  211. static int NOTE_D2 = 73;
  212. static int NOTE_DS2 = 78;
  213. static int NOTE_E2 = 82;
  214. static int NOTE_F2 = 87;
  215. static int NOTE_FS2 = 93;
  216. static int NOTE_G2 = 98;
  217. static int NOTE_GS2 = 104;
  218. static int NOTE_A2 = 110;
  219. static int NOTE_AS2 = 117;
  220. static int NOTE_B2 = 123;
  221. static int NOTE_C3 = 131;
  222. static int NOTE_CS3 = 139;
  223. static int NOTE_D3 = 147;
  224. static int NOTE_DS3 = 156;
  225. static int NOTE_E3 = 165;
  226. static int NOTE_F3 = 175;
  227. static int NOTE_FS3 = 185;
  228. static int NOTE_G3 = 196;
  229. static int NOTE_GS3 = 208;
  230. static int NOTE_A3 = 220;
  231. static int NOTE_AS3 = 233;
  232. static int NOTE_B3 = 247;
  233. static int NOTE_C4 = 262;
  234. static int NOTE_CS4 = 277;
  235. static int NOTE_D4 = 294;
  236. static int NOTE_DS4 = 311;
  237. static int NOTE_E4 = 330;
  238. static int NOTE_F4 = 349;
  239. static int NOTE_FS4 = 370;
  240. static int NOTE_G4 = 392;
  241. static int NOTE_GS4 = 415;
  242. static int NOTE_A4 = 440;
  243. static int NOTE_AS4 = 466;
  244. static int NOTE_B4 = 494;
  245. static int NOTE_C5 = 523;
  246. static int NOTE_CS5 = 554;
  247. static int NOTE_D5 = 587;
  248. static int NOTE_DS5 = 622;
  249. static int NOTE_E5 = 659;
  250. static int NOTE_F5 = 698;
  251. static int NOTE_FS5 = 740;
  252. static int NOTE_G5 = 784;
  253. static int NOTE_GS5 = 831;
  254. static int NOTE_A5 = 880;
  255. static int NOTE_AS5 = 932;
  256. static int NOTE_B5 = 988;
  257. static int NOTE_C6 = 1047;
  258. static int NOTE_CS6 = 1109;
  259. static int NOTE_D6 = 1175;
  260. static int NOTE_DS6 = 1245;
  261. static int NOTE_E6 = 1319;
  262. static int NOTE_F6 = 1397;
  263. static int NOTE_FS6 = 1480;
  264. static int NOTE_G6 = 1568;
  265. static int NOTE_GS6 = 1661;
  266. static int NOTE_A6 = 1760;
  267. static int NOTE_AS6 = 1865;
  268. static int NOTE_B6 = 1976;
  269. static int NOTE_C7 = 2093;
  270. static int NOTE_CS7 = 2217;
  271. static int NOTE_D7 = 2349;
  272. static int NOTE_DS7 = 2489;
  273. static int NOTE_E7 = 2637;
  274. static int NOTE_F7 = 2794;
  275. static int NOTE_FS7 = 2960;
  276. static int NOTE_G7 = 3136;
  277. static int NOTE_GS7 = 3322;
  278. static int NOTE_A7 = 3520;
  279. static int NOTE_AS7 = 3729;
  280. static int NOTE_B7 = 3951;
  281. static int NOTE_C8 = 4186;
  282. static int NOTE_CS8 = 4435;
  283. static int NOTE_D8 = 4699;
  284. static int NOTE_DS8 = 4978;
  285. }
  286.  
  287. class Music
  288. {
  289. final AudioFormat af = new AudioFormat(Note.SAMPLE_RATE, 8, 2, true, true);
  290. SourceDataLine line = null;
  291. Note currentNote = new Note();
  292. byte[] song = new byte[0];
  293.  
  294. int side;
  295.  
  296. public Music(int side)
  297. {
  298. this.side = side;
  299. try {
  300. line = AudioSystem.getSourceDataLine(af);
  301. line.open(af, Note.SAMPLE_RATE);
  302. } catch (LineUnavailableException e) {
  303. e.printStackTrace();
  304. }
  305. line.start();
  306. }
  307.  
  308. public void play(final int freq)
  309. {
  310. currentNote = new Note(freq);
  311. }
  312.  
  313. public void delay(int ms)
  314. {
  315. int length = Note.SAMPLE_RATE * ms / 1000;
  316.  
  317. byte[] newSong = Arrays.copyOf(song, song.length + length * 2);
  318. byte[] newData = currentNote.data();
  319.  
  320. for (int i = 0; i < length * 2; ++i) {
  321. int pos = i / 2;
  322. if (i % 2 == side) {
  323. newSong[song.length + i] = newData[pos];
  324. } else {
  325. newSong[song.length + i] = 0;
  326. }
  327.  
  328. }
  329. song = newSong;
  330. }
  331.  
  332. public void stop()
  333. {
  334. // line.flush();
  335. currentNote = new Note();
  336. }
  337.  
  338. public void close()
  339. {
  340. new Thread()
  341. {
  342. public void run()
  343. {
  344. line.write(song, 0, song.length);
  345. line.drain();
  346. line.stop();
  347. line.flush();
  348. line.close();
  349. }
  350. }.start();
  351. }
  352. }
  353.  
  354. class Note
  355. {
  356. public static final int SAMPLE_RATE = 16 * 1024; // ~16KHz
  357. public static final int MAX_SECONDS = 15;
  358. private byte[] wave = new byte[MAX_SECONDS * SAMPLE_RATE];
  359.  
  360. Note()
  361. {
  362. }
  363.  
  364. Note(double f)
  365. {
  366. for (int i = 0; i < wave.length; i++) {
  367. double period = (double) SAMPLE_RATE / f;
  368. double angle = 2.0 * Math.PI * i / period;
  369.  
  370. // Sin
  371. // wave[i] = (byte) (Math.sin(angle) * 127f);
  372.  
  373. // Square
  374. wave[i] = (byte) ((Math.sin(angle) > 0 ? 1 : 0) * 127f);
  375. }
  376. }
  377.  
  378. public byte[] data()
  379. {
  380. return wave;
  381. }
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement