Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement