Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. import java.lang.Math;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.RandomAccessFile;
  5.  
  6. public class CreateSine
  7. {
  8. static String fileNameString = "Sine.wav";
  9.  
  10. static File file = new File(fileNameString);
  11. static String filePath = file.getAbsolutePath();
  12.  
  13. static RandomAccessFile raw;
  14.  
  15. static int byteCount = 0;
  16. static double pow215 = Math.pow(2, 15);
  17.  
  18. static float freq = 440.0f;
  19. static int sRate = 44100;
  20. static int bitDepth = 16;
  21. static int nChannels = 1;
  22. static int dur = 1;
  23.  
  24. static float changeRate = (float)((2.0 * Math.PI * freq) / sRate);
  25.  
  26. public static void main(String[] args)
  27. {
  28. try
  29. {
  30. raw = new RandomAccessFile(filePath, "rw");
  31.  
  32. raw.setLength(0); // Set file length to 0, to prevent unexpected behavior in case the file already existed
  33. raw.writeBytes("RIFF");
  34. raw.writeInt(0); // Final file size not known yet, write 0. This is = sample count + 36 bytes from header.
  35. raw.writeBytes("WAVE");
  36. raw.writeBytes("fmt ");
  37. raw.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
  38. raw.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
  39. raw.writeShort(Short.reverseBytes((short)nChannels));// Number of channels, 1 for mono, 2 for stereo
  40. raw.writeInt(Integer.reverseBytes(sRate)); // Sample rate
  41. raw.writeInt(Integer.reverseBytes(sRate*bitDepth*nChannels/8)); // Byte rate, SampleRate*NumberOfChannels*bitDepth/8
  42. raw.writeShort(Short.reverseBytes((short)(nChannels*bitDepth/8))); // Block align, NumberOfChannels*bitDepth/8
  43. raw.writeShort(Short.reverseBytes((short)bitDepth)); // Bit Depth
  44. raw.writeBytes("data");
  45. raw.writeInt(0); // Data chunk size not known yet, write 0. This is = sample count.
  46. }
  47. catch(IOException e)
  48. {
  49. System.out.println("I/O exception occured while writing data");
  50. }
  51.  
  52. for (int i = 0; i < sRate*dur; i++)
  53. {
  54. writeSample( (float)Math.sin( i * changeRate ) );
  55. }
  56.  
  57. closeFile();
  58. System.out.print("Finished");
  59. }
  60.  
  61. static void writeSample(float floatValue)
  62. {
  63. try
  64. {
  65. short hexSample = (short)((floatValue * pow215));
  66. raw.writeShort(Short.reverseBytes(hexSample));
  67. byteCount += 2;
  68. }
  69. catch(IOException e)
  70. {
  71. System.out.println("I/O exception occured while writing data");
  72. }
  73. }
  74.  
  75. static void closeFile()
  76. {
  77. try
  78. {
  79. raw.seek(4); // Write size to RIFF header
  80. raw.writeInt(Integer.reverseBytes(byteCount + 36));
  81. raw.seek(40); // Write size to Subchunk2Size field
  82. raw.writeInt(Integer.reverseBytes(byteCount));
  83. raw.close();
  84. }
  85. catch(IOException e)
  86. {
  87. System.out.println("I/O exception occured while closing output file");
  88. }
  89. }
  90. }
  91.  
  92. import java.lang.Math;
  93. import java.io.File;
  94. import java.io.IOException;
  95. import java.io.RandomAccessFile;
  96.  
  97. public class CreateSine
  98. {
  99. static String fileNameString;
  100. static File file;
  101. static String filePath;
  102.  
  103. static RandomAccessFile raw;
  104.  
  105. static int byteCount = 0;
  106.  
  107. static float freq;
  108. static int sRate = 44100;
  109. static int bitDepth = 16;
  110. static int nChannels = 1;
  111. static int dur;
  112.  
  113. static float changeRate;
  114.  
  115. public static void main(String[] args)
  116. {
  117. freq = Float.parseFloat(args[0]);
  118. changeRate = (float)((2.0 * Math.PI * freq) / sRate);
  119.  
  120. dur = Integer.parseInt(args[1]);
  121.  
  122. fileNameString = (String)args[2] + ".wav";
  123. file = new File(fileNameString);
  124. filePath = file.getAbsolutePath();
  125.  
  126. try
  127. {
  128. raw = new RandomAccessFile(filePath, "rw");
  129.  
  130. raw.setLength(0); // Set file length to 0, to prevent unexpected behavior in case the file already existed
  131. raw.writeBytes("RIFF");
  132. raw.writeInt(0); // Final file size not known yet, write 0. This is = sample count + 36 bytes from header.
  133. raw.writeBytes("WAVE");
  134. raw.writeBytes("fmt ");
  135. raw.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
  136. raw.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
  137. raw.writeShort(Short.reverseBytes((short)nChannels));// Number of channels, 1 for mono, 2 for stereo
  138. raw.writeInt(Integer.reverseBytes(sRate)); // Sample rate
  139. raw.writeInt(Integer.reverseBytes(sRate*bitDepth*nChannels/8)); // Byte rate, SampleRate*NumberOfChannels*bitDepth/8
  140. raw.writeShort(Short.reverseBytes((short)(nChannels*bitDepth/8))); // Block align, NumberOfChannels*bitDepth/8
  141. raw.writeShort(Short.reverseBytes((short)bitDepth)); // Bit Depth
  142. raw.writeBytes("data");
  143. raw.writeInt(0); // Data chunk size not known yet, write 0. This is = sample count.
  144. }
  145. catch(IOException e)
  146. {
  147. System.out.println("I/O exception occured while writing data");
  148. }
  149.  
  150. for (int i = 0; i < sRate*dur; i++)
  151. {
  152. writeSample( (float)Math.sin( i * changeRate ) );
  153. }
  154.  
  155. closeFile();
  156. System.out.print("Finished");
  157. }
  158.  
  159. static void writeSample(float floatValue)
  160. {
  161. try
  162. {
  163. char shortSample = (char)( (floatValue)*0x7FFF );
  164. raw.writeShort(Character.reverseBytes(shortSample));
  165. byteCount += 2;
  166. }
  167. catch(IOException e)
  168. {
  169. System.out.println("I/O exception occured while writing data");
  170. }
  171. }
  172.  
  173. static void closeFile()
  174. {
  175. try
  176. {
  177. raw.seek(4); // Write size to RIFF header
  178. raw.writeInt(Integer.reverseBytes(byteCount + 36));
  179. raw.seek(40); // Write size to Subchunk2Size field
  180. raw.writeInt(Integer.reverseBytes(byteCount));
  181. raw.close();
  182. }
  183. catch(IOException e)
  184. {
  185. System.out.println("I/O exception occured while closing output file");
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement