Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.09 KB | None | 0 0
  1. package recorderCoba1;
  2.  
  3. import java.io.File;
  4.  
  5. import javax.swing.*;
  6.  
  7. import java.awt.Font;
  8. import java.awt.event.*;
  9.  
  10. public class Starter extends JFrame implements MouseListener{
  11. /**
  12. *
  13. */
  14. private static final long serialVersionUID = 1L;
  15. JButton btnStart;
  16. JButton btnBass;
  17. JButton btnSnare;
  18. JButton btnHihat;
  19. JButton btnTom;
  20. JButton btnFloorTom;
  21. JButton btnCrash;
  22. //JTextField txtInputFile;
  23. JLabel lblTitle;
  24.  
  25. static String path = System.getProperty("user.dir");
  26. static boolean stop = true;
  27.  
  28. static double[] bufferSampleBass = new double[2000]; //buffer
  29. static double[] bufferSampleSnare = new double[2000];
  30. static double[] bufferSampleHihat = new double[2000];
  31. static double[] bufferSampleTom = new double[2000];
  32. static double[] bufferSampleFloorTom = new double[2000];
  33. static double[] bufferSampleCrash = new double[2000];
  34. static double[] bufferRead2 = new double[2000];
  35.  
  36. static Complex[] fftSampleBass; //complex hasil fft
  37. static Complex[] fftSampleSnare;
  38. static Complex[] fftSampleHihat;
  39. static Complex[] fftSampleTom;
  40. static Complex[] fftSampleFloorTom;
  41. static Complex[] fftSampleCrash;
  42.  
  43. public static void main(String args[]){
  44.  
  45. Read read = new Read();
  46.  
  47. //FFT SAMPLE
  48. //FFT Sample Bass
  49. try{
  50. WavFile sampleBass = WavFile.openWavFile(new File(path+"/RecordAudio2.wav"));
  51. bufferSampleBass = read.ReadWav(sampleBass);
  52. }catch(Exception ex){
  53.  
  54. }
  55. Complex[] dataBass = new Complex[2048];
  56. for (int i = 0; i < 2048; i++) {
  57. dataBass[i] = new Complex(0.0, 0.0);
  58. }
  59. for (int i = 0; i < bufferSampleBass.length; i++) {
  60. dataBass[i] = new Complex(bufferSampleBass[i], 0.0);
  61. }
  62.  
  63. fftSampleBass = FFT.fft(dataBass);
  64.  
  65. //FFT Sample Snare
  66. try{
  67. WavFile sampleSnare = WavFile.openWavFile(new File(path+"/RecordAudio2.wav"));
  68. bufferSampleSnare = read.ReadWav(sampleSnare);
  69. }catch(Exception ex){
  70.  
  71. }
  72. Complex[] dataSnare = new Complex[2048];
  73. for (int i = 0; i < 2048; i++) {
  74. dataSnare[i] = new Complex(0.0, 0.0);
  75. }
  76. for (int i = 0; i < bufferSampleSnare.length; i++) {
  77. dataSnare[i] = new Complex(bufferSampleSnare[i], 0.0);
  78. }
  79.  
  80. fftSampleSnare = FFT.fft(dataSnare);
  81.  
  82. //FFT Sample Hihat
  83. try{
  84. WavFile sampleHihat = WavFile.openWavFile(new File(path+"/RecordAudio2.wav"));
  85. bufferSampleHihat = read.ReadWav(sampleHihat);
  86. }catch(Exception ex){
  87.  
  88. }
  89. Complex[] dataHihat = new Complex[2048];
  90. for (int i = 0; i < 2048; i++) {
  91. dataHihat[i] = new Complex(0.0, 0.0);
  92. }
  93. for (int i = 0; i < bufferSampleHihat.length; i++) {
  94. dataHihat[i] = new Complex(bufferSampleHihat[i], 0.0);
  95. }
  96.  
  97. fftSampleSnare = FFT.fft(dataHihat);
  98.  
  99. //FFT Sample Tom
  100. try{
  101. WavFile sampleTom = WavFile.openWavFile(new File(path+"/RecordAudio2.wav"));
  102. bufferSampleTom = read.ReadWav(sampleTom);
  103. }catch(Exception ex){
  104.  
  105. }
  106. Complex[] dataTom = new Complex[2048];
  107. for (int i = 0; i < 2048; i++) {
  108. dataTom[i] = new Complex(0.0, 0.0);
  109. }
  110. for (int i = 0; i < bufferSampleTom.length; i++) {
  111. dataTom[i] = new Complex(bufferSampleTom[i], 0.0);
  112. }
  113.  
  114. fftSampleTom = FFT.fft(dataTom);
  115.  
  116. //FFT Sample Floor Tom
  117. try{
  118. WavFile sampleFloorTom = WavFile.openWavFile(new File(path+"/RecordAudio2.wav"));
  119. bufferSampleFloorTom = read.ReadWav(sampleFloorTom);
  120. }catch(Exception ex){
  121.  
  122. }
  123. Complex[] dataFloorTom = new Complex[2048];
  124. for (int i = 0; i < 2048; i++) {
  125. dataFloorTom[i] = new Complex(0.0, 0.0);
  126. }
  127. for (int i = 0; i < bufferSampleFloorTom.length; i++) {
  128. dataFloorTom[i] = new Complex(bufferSampleFloorTom[i], 0.0);
  129. }
  130.  
  131. fftSampleFloorTom = FFT.fft(dataFloorTom);
  132.  
  133. //FFT Sample Crash
  134. try{
  135. WavFile sampleCrash = WavFile.openWavFile(new File(path+"/RecordAudio2.wav"));
  136. bufferSampleCrash = read.ReadWav(sampleCrash);
  137. }catch(Exception ex){
  138.  
  139. }
  140. Complex[] dataCrash = new Complex[2048];
  141. for (int i = 0; i < 2048; i++) {
  142. dataCrash[i] = new Complex(0.0, 0.0);
  143. }
  144. for (int i = 0; i < bufferSampleCrash.length; i++) {
  145. dataCrash[i] = new Complex(bufferSampleCrash[i], 0.0);
  146. }
  147.  
  148. fftSampleCrash = FFT.fft(dataCrash);
  149.  
  150. //=================================================================
  151.  
  152. new Starter(); //GUI
  153.  
  154. run();
  155.  
  156. }
  157.  
  158. public static void run(){
  159. Read read = new Read();
  160. final JavaSoundRecorder recorder = new JavaSoundRecorder();
  161. // creates a new thread that waits for a specified
  162. // of time before stopping
  163.  
  164. //long startTime = System.currentTimeMillis();
  165. //do{
  166. Thread stopper = new Thread(new Runnable() {
  167. public void run() {
  168. try {
  169. Thread.sleep(300);
  170. } catch (InterruptedException ex) {
  171. ex.printStackTrace();
  172. }
  173. recorder.finish();
  174. }
  175. });
  176.  
  177. stopper.start();
  178.  
  179. // start recording
  180. recorder.start("input");
  181.  
  182. try{
  183.  
  184. WavFile wavFile2 = WavFile.openWavFile(new File(path+"/input.wav"));
  185. bufferRead2 = read.ReadWav(wavFile2);
  186.  
  187. Complex[] data2 = new Complex[2048];
  188. for (int i = 0; i < 2048; i++) {
  189. data2[i] = new Complex(0.0, 0.0);
  190. }
  191. for (int i = 0; i < bufferRead2.length; i++) {
  192. data2[i] = new Complex(bufferRead2[i], 0.0);
  193. }
  194.  
  195. Complex[] fftResult2 = FFT.fft(data2);
  196.  
  197. boolean compareResult = Compare.compare(fftSampleBass, fftResult2);
  198. compareResult = Compare.compare(fftSampleSnare, fftResult2);
  199. compareResult = Compare.compare(fftSampleHihat, fftResult2);
  200. compareResult = Compare.compare(fftSampleTom, fftResult2);
  201. compareResult = Compare.compare(fftSampleFloorTom, fftResult2);
  202. compareResult = Compare.compare(fftSampleCrash, fftResult2);
  203. //boolean compareResult = Compare.compare2(bufferRead, bufferRead2);
  204.  
  205. //AudioPlayerExample2 ape = new AudioPlayerExample2();
  206.  
  207. if(compareResult) System.out.println("True"); //ape.play(path+"/bass.wav");
  208. else System.out.println("False");
  209.  
  210. //FFT.show(fftResult, "Hasil FFT");
  211. }
  212. catch(Exception ex){
  213. System.err.println(ex);
  214. }
  215. //}while(!stop);
  216. }
  217.  
  218. public void mousePressed(MouseEvent e)
  219. {
  220. // when the user clicks the Play button
  221. if( e.getSource() == btnStart )
  222. {
  223. if(stop==true){
  224. stop=false;
  225. }
  226. else
  227. stop=true;
  228.  
  229. System.out.println(stop);
  230.  
  231.  
  232. //long endTime = System.currentTimeMillis();
  233. //long totalTime = endTime - startTime;
  234. //System.out.println(totalTime);
  235.  
  236. /*wavIO wavFile = new wavIO(txtInputFile.getText());
  237. if( wavFile.read() == true )
  238. {
  239. lblOutput.setText(wavFile.getSummary());
  240.  
  241. // now save the file
  242. wavFile.setPath("testout.wav");
  243. wavFile.save();
  244. }
  245. else
  246. {
  247. lblOutput.setText("Error!");
  248. }*/
  249. }
  250. }
  251. public void mouseReleased(MouseEvent e) {}
  252. public void mouseClicked(MouseEvent e) {}
  253. public void mouseEntered(MouseEvent e) {}
  254. public void mouseExited(MouseEvent e) {}
  255.  
  256. public Starter()
  257. {
  258. setLayout(null); // tell java not to organize our controls automatically
  259.  
  260. /*txtInputFile = new JTextField("test.wav");
  261. txtInputFile.setBounds(0, 0, 200,20);
  262. getContentPane().add(txtInputFile);*/
  263.  
  264. lblTitle = new JLabel("Table Drum");
  265. lblTitle.setFont(new Font("Courier New", Font.BOLD, 32));
  266. lblTitle.setBounds(325, 10, 300, 100);
  267. getContentPane().add(lblTitle);
  268.  
  269. btnStart = new JButton("Start");
  270. btnStart.setBounds(270, 100, 300, 100);
  271. btnStart.addMouseListener(this);
  272. getContentPane().add(btnStart);
  273.  
  274. btnBass = new JButton("Bass");
  275. btnBass.setBounds(100, 250, 200, 200);
  276. btnBass.addMouseListener(this);
  277. getContentPane().add(btnBass);
  278.  
  279. btnSnare = new JButton("Snare");
  280. btnSnare.setBounds(320, 250, 200, 200);
  281. btnSnare.addMouseListener(this);
  282. getContentPane().add(btnSnare);
  283.  
  284. btnHihat = new JButton("Hi Hat");
  285. btnHihat.setBounds(540, 250, 200, 200);
  286. btnHihat.addMouseListener(this);
  287. getContentPane().add(btnHihat);
  288.  
  289. btnTom = new JButton("Tom");
  290. btnTom.setBounds(100, 470, 200, 200);
  291. btnTom.addMouseListener(this);
  292. getContentPane().add(btnTom);
  293.  
  294. btnFloorTom = new JButton("Floor Tom");
  295. btnFloorTom.setBounds(320, 470, 200, 200);
  296. btnFloorTom.addMouseListener(this);
  297. getContentPane().add(btnFloorTom);
  298.  
  299. btnCrash = new JButton("Crash");
  300. btnCrash.setBounds(540, 470, 200, 200);
  301. btnCrash.addMouseListener(this);
  302. getContentPane().add(btnCrash);
  303.  
  304. // show the application window
  305. setTitle("Table Drum");
  306. setDefaultCloseOperation(EXIT_ON_CLOSE);
  307. setSize(840,750);
  308. setVisible(true);
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement