Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- /**
- * Commands to generate and play sound file:
- *
- * $ avconv -f f64le -ar 44100 -ac 1 -i /tmp/snd.raw /tmp/snd.wav
- * $ mplayer /tmp/snd.wav
- *
- */
- public class SndGen {
- public static void main(String[] args) throws IOException {
- double freqs[]=new double[]{440, 660, 880};
- double amplitudes[]=new double[]{.5,.3,.2};
- double sampleFreq=44100;
- double lengthInSecs=5;
- int nSample=(int)(lengthInSecs*sampleFreq);
- ByteBuffer bb=ByteBuffer.allocate(8);
- bb.order(ByteOrder.LITTLE_ENDIAN);
- byte[] buffer=new byte[8];
- try(FileOutputStream fos=new FileOutputStream("/tmp/snd.raw"))
- {
- for(int i=0;i<nSample;++i)
- {
- double t=i*1.0/sampleFreq;
- double sample=0;
- for(int j=0;j<freqs.length;++j)
- {
- double omega=freqs[j]*2.0*Math.PI;
- sample+=amplitudes[j]*Math.sin(omega*t);
- }
- bb.clear();
- bb.putDouble(sample);
- bb.flip();
- bb.get(buffer);
- fos.write(buffer);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment