Advertisement
Guest User

Untitled

a guest
Apr 12th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. package script;
  2. import javazoom.jl.decoder.BitstreamException;
  3. import javazoom.jl.decoder.Decoder;
  4. import javazoom.jl.decoder.DecoderException;
  5. import javazoom.jl.decoder.Header;
  6. import javazoom.jl.decoder.JavaLayerException;
  7. import javazoom.jl.decoder.Obuffer;
  8. import javazoom.jl.decoder.SampleBuffer;
  9. import javazoom.jl.decoder.Bitstream;
  10. import javazoom.jl.player.Player;
  11.  
  12. import java.text.NumberFormat;
  13. import java.util.Arrays;
  14. import java.awt.AWTException;
  15. import java.awt.Robot;
  16. import java.awt.event.KeyEvent;
  17. import java.io.BufferedWriter;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.Map;
  24.  
  25. import javax.sound.sampled.AudioFileFormat;
  26. import javax.sound.sampled.AudioSystem;
  27. import javax.sound.sampled.UnsupportedAudioFileException;
  28.  
  29. import org.tritonus.share.sampled.file.TAudioFileFormat;
  30.  
  31. import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D;
  32.  
  33. public class MusicPlayer {
  34. static double getDurationWithMp3Spi(File file)
  35. throws UnsupportedAudioFileException, IOException {
  36.  
  37. AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(file);
  38. if (fileFormat instanceof TAudioFileFormat) {
  39. Map<?, ?> properties = ((TAudioFileFormat) fileFormat).properties();
  40. String key = "duration";
  41. Long microseconds = (Long) properties.get(key);
  42. int mili = (int) (microseconds / 1000);
  43. int sec = (mili / 1000);
  44. int min = (mili / 1000) / 60;
  45. return microseconds / 1000000.0;
  46. } else {
  47. throw new UnsupportedAudioFileException();}
  48. }
  49.  
  50.  
  51. public static void main(String[] args) throws IOException,
  52. JavaLayerException, InterruptedException, UnsupportedAudioFileException, AWTException {
  53. // TODO Auto-generated method stub
  54. //E:\\Program Files (x86)\\Steam\\SteamApps\\common\\team fortress 2\\tf\\cfg\\script.cfg
  55.  
  56. FileInputStream mp3 = new FileInputStream("30hz.mp3");
  57. FileInputStream mp3player = new FileInputStream("30hz.mp3");
  58. File song = new File("30hz.mp3");
  59. Decoder decoder = new Decoder();
  60. Bitstream bitstream = new Bitstream(mp3);
  61. Bitstream playerBitstream = new Bitstream(mp3player);
  62. SampleBuffer currentBuffer = (SampleBuffer) decoder.decodeFrame(
  63. bitstream.readFrame(), bitstream);
  64. Player player = new Player(mp3player);
  65. int bufferLength = currentBuffer.getBufferLength();
  66. DoubleFFT_1D transform = new DoubleFFT_1D(bufferLength);
  67.  
  68. double duration = getDurationWithMp3Spi(song);
  69. bitstream.unreadFrame();
  70. bitstream.closeFrame();
  71. String fov;
  72. int x =0;
  73. int placeholder=0;
  74. double currentMaximum=Double.MIN_VALUE;
  75. double maximum=50000;
  76. int freq=0;
  77. Robot robot = new Robot();
  78.  
  79. String text=" ";
  80. String[] textAmp = new String[200];
  81. for(int i=0;i<200;i++){
  82. textAmp[i]="O";
  83. for(int j=0;j<i;j++){
  84. textAmp[i]=textAmp[i]+"O";
  85. }
  86. }
  87. player.play(1);
  88. player.play(1);
  89. player.play(1);
  90. player.play(1);
  91. player.play(1);
  92. player.play(1);
  93. player.play(1);
  94. player.play(1);
  95. player.play(1);
  96. player.play(1);
  97. double intensity=0;
  98. while (((x <= (int) (((44100 * (duration - 1)) / 1152.0))))) {
  99. player.play(1);
  100.  
  101. currentBuffer = (SampleBuffer) decoder.decodeFrame(bitstream.readFrame(), bitstream);
  102. short[] originalBufferArray = new short[bufferLength];
  103. originalBufferArray = currentBuffer.getBuffer();
  104. double[] doubleBufferArray = new double[bufferLength*2];
  105. for(int i =0; i<bufferLength;i+=2){
  106. doubleBufferArray[i]=(double)originalBufferArray[i];
  107. doubleBufferArray[i+1]=0;
  108. //System.out.println(doubleBufferArray[i]);
  109. //System.out.println(doubleBufferArray[i+1]);
  110. }
  111. double temp=0;
  112. //BEGIN ARRAYLIST
  113. // double[] organizedArray = new double[2304];
  114. // for(int i=0;i<1152;i++){
  115. // organizedArray[i*2]=doubleBufferArray[i];
  116. // }
  117. // for(int i=1152;i<2304;i++){
  118. // organizedArray[((i-1152)*2)+1]=doubleBufferArray[i];
  119. // }
  120. //END ARRAYLIST
  121. transform.complexForward(doubleBufferArray);
  122. double[] finalArray = new double[bufferLength];
  123.  
  124. for(int i =0;i<10;i+=2){
  125. finalArray[i/2]= Math.sqrt(Math.pow(doubleBufferArray[i],2)+(Math.pow(doubleBufferArray[i+1], 2)));
  126. finalArray[i/2]= finalArray[i/2]/(double)(bufferLength/2.0);
  127. if(finalArray[i/2]>currentMaximum){
  128. currentMaximum=finalArray[i/2];
  129. freq=i/2;
  130. }
  131. }
  132. //System.out.println(currentMaximum);
  133. // if (freq==4){
  134. // currentMaximum/=4;
  135. // intensity=currentMaximum/maximum;
  136. //
  137. // }
  138. // else if (freq==3){
  139. // currentMaximum/=3;
  140. // intensity=currentMaximum/maximum;
  141. // }
  142. // else if (freq==2){
  143. // currentMaximum/=2;
  144. // intensity=currentMaximum/maximum;
  145. // }
  146. // else{
  147. intensity=currentMaximum/maximum;
  148. System.out.println(textAmp[(int) (intensity*100*2)]);
  149. int fovInt=(int)(90-(intensity*25));
  150.  
  151. // }
  152.  
  153.  
  154.  
  155. // text=String.valueOf(intensity);
  156. // text=text.substring(2,4);
  157. // placeholder = (int)Double.parseDouble(text);
  158. try{
  159. File script = new File("E:\\Program Files (x86)\\Steam\\SteamApps\\common\\team fortress 2\\tf\\cfg\\script.cfg");
  160. BufferedWriter writer = new BufferedWriter(new FileWriter(script));
  161. fov = "fov_desired " + fovInt +"\necho \"working\"";
  162. writer.write(fov, 0 ,fov.length());
  163. writer.flush();
  164. writer.close();
  165.  
  166. robot.keyPress(KeyEvent.VK_NUM_LOCK );
  167. robot.keyRelease(KeyEvent.VK_NUM_LOCK );
  168. } finally{}
  169. currentMaximum=Double.MIN_VALUE;
  170. bitstream.unreadFrame();
  171. bitstream.closeFrame();
  172. x++;
  173. }
  174.  
  175. /*
  176. * 0~0hz
  177. * 1~19.140625hz
  178. * 2~38.28125hz
  179. * 3~57.422145hz
  180. * 4~76.56277
  181. * 5~ 95.703395
  182. */
  183.  
  184.  
  185.  
  186.  
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement