Advertisement
Guest User

Generate sound

a guest
May 19th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1.  
  2.  
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6.  
  7. public class SndGen {
  8.     public static void main(String[] args) throws IOException {
  9.         double freqs[]=new double[]{440, 660, 880};
  10.         double amplitudes[]=new double[]{1,1,1};
  11.         double sampleFreq=44000;
  12.         double lengthInSecs=5;
  13.         int nSample=(int)(lengthInSecs*sampleFreq);
  14.         ByteBuffer bb=ByteBuffer.allocate(8);
  15.         bb.order(ByteOrder.LITTLE_ENDIAN);
  16.         byte[] buffer=new byte[8];
  17.         for(int i=0;i<nSample;++i)
  18.         {
  19.             double t=i*1.0/sampleFreq;
  20.             double sample=0;
  21.             for(int j=0;j<freqs.length;++j)
  22.             {
  23.                 double omega=freqs[j]*2.0*Math.PI;
  24.                 sample+=amplitudes[j]*Math.sin(omega*t);
  25.             }
  26.             bb.clear();
  27.             bb.putDouble(sample);
  28.             bb.flip();
  29.             bb.get(buffer);
  30.             System.out.write(buffer);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement